Skip to content

Commit 6060619

Browse files
OguzYuukselOguz Yuksel
andauthored
Don't send value to subject when asserted. (#120)
* don't publish value when asserted from test * add unit test --------- Co-authored-by: Oguz Yuksel <[email protected]>
1 parent c9fd8da commit 6060619

File tree

2 files changed

+32
-2
lines changed

2 files changed

+32
-2
lines changed

Sources/Sharing/Internal/Reference.swift

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,10 @@ final class _BoxReference<Value>: MutableReference, Observable, Perceptible, @un
4747
#if canImport(Combine)
4848
private var value: Value {
4949
willSet {
50-
subject.send(newValue)
50+
@Dependency(\.snapshots) var snapshots
51+
if !snapshots.isAsserting {
52+
subject.send(newValue)
53+
}
5154
}
5255
}
5356
let subject = PassthroughRelay<Value>()
@@ -173,7 +176,10 @@ final class _PersistentReference<Key: SharedReaderKey>:
173176
#if canImport(Combine)
174177
private var value: Key.Value {
175178
willSet {
176-
subject.send(newValue)
179+
@Dependency(\.snapshots) var snapshots
180+
if !snapshots.isAsserting {
181+
subject.send(newValue)
182+
}
177183
}
178184
}
179185
private let subject = PassthroughRelay<Value>()

Tests/SharingTests/SharedChangeTrackerTests.swift

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,30 @@ import Testing
7878
}
7979
}
8080

81+
@Test func assertedChanges() {
82+
@Shared(value: 0) var count
83+
84+
let counts = Mutex<[Int]>([])
85+
let cancellable = $count.publisher.sink { @Sendable value in
86+
counts.withLock { $0.append(value) }
87+
}
88+
defer { _ = cancellable }
89+
90+
let tracker = SharedChangeTracker()
91+
tracker.track {
92+
$count.withLock { $0 += 1 }
93+
}
94+
95+
tracker.assert {
96+
#expect($count != $count)
97+
98+
$count.withLock { $0 = 1 }
99+
100+
#expect($count == $count)
101+
#expect(counts.withLock(\.self) == [0 ,1])
102+
}
103+
}
104+
81105
@Test func unassertedChanges() {
82106
@Shared(value: 0) var count
83107

0 commit comments

Comments
 (0)