Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support SwiftUI publishing new value while rendering warning #462

Merged
merged 7 commits into from
Feb 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion .github/workflows/CommitChecks.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
name: CommitChecks

on: [push, pull_request]
on:
push:
branches:
- "**"

jobs:
test:
Expand Down
9 changes: 3 additions & 6 deletions Sources/Verge/Store/Store.swift
Original file line number Diff line number Diff line change
Expand Up @@ -209,12 +209,9 @@ open class Store<State: Equatable, Activity>: EventEmitter<_StoreEvent<State, Ac
case .state(let stateEvent):
switch stateEvent {
case .willUpdate:
if Thread.isMainThread {
objectWillChange.send()
} else {
DispatchQueue.main.async { [weak self] in
self?.objectWillChange.send()
}
DispatchQueue.main.async { [weak self] in
// For: `Publishing changes from within view updates is not allowed, this will cause undefined behavior.`
self?.objectWillChange.send()
}
case .didUpdate(let state):
_valueSubject.send(state)
Expand Down
5 changes: 4 additions & 1 deletion Sources/Verge/SwiftUI/StoreReader.swift
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,10 @@ public enum StoreReaderComponents<StateType: Equatable> {
}()

if shouldUpdate {
self._publisher.send()
DispatchQueue.main.async {
// For: Publishing changes from within view updates is not allowed, this will cause undefined behavior.
self._publisher.send()
}
}
}

Expand Down
178 changes: 91 additions & 87 deletions Tests/VergeTests/StoreReaderTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -52,30 +52,30 @@ final class StoreReaderTests: XCTestCase {

XCTAssertEqual(count, 1)

try await Task.sleep(nanoseconds: 1)
try await Task.sleep(nanoseconds: 5_000_000)

store.commit {
$0.count_1 += 1
}

try await Task.sleep(nanoseconds: 1)
XCTAssertEqual(count, 2)
try await Task.sleep(nanoseconds: 5_000_000)

XCTAssertEqual(count, 3)

store.commit {
$0.count_1 += 1
}

try await Task.sleep(nanoseconds: 1)
XCTAssertEqual(count, 3)
try await Task.sleep(nanoseconds: 5_000_000)

XCTAssertEqual(count, 4)

store.commit {
$0.count_1 += 1
}

try await Task.sleep(nanoseconds: 1)
XCTAssertEqual(count, 4)
try await Task.sleep(nanoseconds: 5_000_000)
XCTAssertEqual(count, 5)
}

@MainActor
Expand All @@ -93,34 +93,38 @@ final class StoreReaderTests: XCTestCase {

XCTAssertEqual(count, 1)

try await Task.sleep(nanoseconds: 1)
try await Task.sleep(nanoseconds: 1_000_000_000)

store.commit {
$0.count_1 += 1
}

try await Task.sleep(nanoseconds: 1)

XCTAssertEqual(count, 2)

store.commit {
$0.count_2 += 1
}

try await Task.sleep(nanoseconds: 1)

// not change because count_2 never read anyone.
XCTAssertEqual(count, 2)

store.commit {
$0.count_2 += 1
try await Task.sleep(nanoseconds: 1_000_000_000)

XCTAssertEqual(count, 3)

do {

store.commit {
$0.count_2 += 1
}

try await Task.sleep(nanoseconds: 1_000_000_000)


// not change because count_2 never read anyone.
XCTAssertEqual(count, 3)

store.commit {
$0.count_2 += 1
}

try await Task.sleep(nanoseconds: 1_000_000_000)

// not change because count_2 never read anyone.
XCTAssertEqual(count, 3)
}

try await Task.sleep(nanoseconds: 1)

// not change because count_2 never read anyone.
XCTAssertEqual(count, 2)


}

@MainActor
Expand All @@ -138,31 +142,31 @@ final class StoreReaderTests: XCTestCase {

XCTAssertEqual(count, 1)

try await Task.sleep(nanoseconds: 1)
try await Task.sleep(nanoseconds: 5_000_000)

store.commit {
$0.count_1 += 1
}

try await Task.sleep(nanoseconds: 1)
XCTAssertEqual(count, 2)
try await Task.sleep(nanoseconds: 5_000_000)

XCTAssertEqual(count, 3)

store.commit {
$0.count_1 += 1
}

try await Task.sleep(nanoseconds: 1)
XCTAssertEqual(count, 3)
try await Task.sleep(nanoseconds: 5_000_000)

XCTAssertEqual(count, 4)

store.commit {
$0.count_1 += 1
}

try await Task.sleep(nanoseconds: 1)
XCTAssertEqual(count, 4)
try await Task.sleep(nanoseconds: 5_000_000)
XCTAssertEqual(count, 5)

}

@MainActor
Expand Down Expand Up @@ -208,31 +212,31 @@ final class StoreReaderTests: XCTestCase {

XCTAssertEqual(count, 1)

try await Task.sleep(nanoseconds: 1)
try await Task.sleep(nanoseconds: 1_000_000)

store.commit {
$0.wrapped += 1
}

try await Task.sleep(nanoseconds: 1)
XCTAssertEqual(count, 2)
try await Task.sleep(nanoseconds: 1_000_000)

XCTAssertEqual(count, 3)

store.commit {
$0.wrapped += 1
}

try await Task.sleep(nanoseconds: 1)
XCTAssertEqual(count, 3)
try await Task.sleep(nanoseconds: 1_000_000)

XCTAssertEqual(count, 4)

store.commit {
$0.wrapped += 1
}

try await Task.sleep(nanoseconds: 1)
XCTAssertEqual(count, 4)
try await Task.sleep(nanoseconds: 1_000_000)
XCTAssertEqual(count, 5)

}

@MainActor
Expand Down Expand Up @@ -278,31 +282,31 @@ final class StoreReaderTests: XCTestCase {

XCTAssertEqual(count, 1)

try await Task.sleep(nanoseconds: 1)
try await Task.sleep(nanoseconds: 1_000_000)

store.commit {
$0.count_1 += 1
}

try await Task.sleep(nanoseconds: 1)
XCTAssertEqual(count, 2)
try await Task.sleep(nanoseconds: 1_000_000)

XCTAssertEqual(count, 3)

store.commit {
$0.count_1 += 1
}

try await Task.sleep(nanoseconds: 1)
XCTAssertEqual(count, 3)
try await Task.sleep(nanoseconds: 1_000_000)

XCTAssertEqual(count, 4)

store.commit {
$0.count_1 += 1
}

try await Task.sleep(nanoseconds: 1)
XCTAssertEqual(count, 4)
try await Task.sleep(nanoseconds: 1_000_000)
XCTAssertEqual(count, 5)

}


Expand All @@ -321,31 +325,31 @@ final class StoreReaderTests: XCTestCase {

XCTAssertEqual(count, 1)

try await Task.sleep(nanoseconds: 1)
try await Task.sleep(nanoseconds: 1_000_000)

store.commit {
$0.count_1 += 1
}

try await Task.sleep(nanoseconds: 1)
XCTAssertEqual(count, 2)
try await Task.sleep(nanoseconds: 1_000_000)

XCTAssertEqual(count, 3)

store.commit {
$0.count_1 += 1
}

try await Task.sleep(nanoseconds: 1)
XCTAssertEqual(count, 3)
try await Task.sleep(nanoseconds: 1_000_000)

XCTAssertEqual(count, 4)

store.commit {
$0.count_1 += 1
}

try await Task.sleep(nanoseconds: 1)
XCTAssertEqual(count, 4)
try await Task.sleep(nanoseconds: 1_000_000)
XCTAssertEqual(count, 5)

}

private struct NonEquatableBox<Value> {
Expand Down
Loading