Skip to content

Commit

Permalink
Support SwiftUI publishing new value while rendering warning (#462)
Browse files Browse the repository at this point in the history
  • Loading branch information
muukii authored Feb 22, 2024
1 parent 2e42823 commit 790f1ef
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 95 deletions.
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

0 comments on commit 790f1ef

Please sign in to comment.