Skip to content

Commit 8141fd7

Browse files
authored
Add yieldLoading to SharedSubscriber (#125)
1 parent 4a08300 commit 8141fd7

File tree

3 files changed

+60
-2
lines changed

3 files changed

+60
-2
lines changed

Sources/Sharing/Internal/Reference.swift

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,10 @@ final class _PersistentReference<Key: SharedReaderKey>:
224224
: .initialValue(initialValue)
225225
self.subscription = key.subscribe(
226226
context: context,
227-
subscriber: SharedSubscriber(callback: callback)
227+
subscriber: SharedSubscriber(
228+
callback: callback,
229+
onLoading: { [weak self] in self?.isLoading = $0 }
230+
)
228231
)
229232
}
230233

Sources/Sharing/SharedContinuations.swift

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,14 @@ public struct LoadContinuation<Value>: Sendable {
6767
/// an external system can be shared.
6868
public struct SharedSubscriber<Value>: Sendable {
6969
let callback: @Sendable (Result<Value?, any Error>) -> Void
70+
let onLoading: (@Sendable (Bool) -> Void)?
7071

71-
public init(callback: @escaping @Sendable (Result<Value?, any Error>) -> Void) {
72+
public init(
73+
callback: @escaping @Sendable (Result<Value?, any Error>) -> Void,
74+
onLoading: (@Sendable (Bool) -> Void)? = nil
75+
) {
7276
self.callback = callback
77+
self.onLoading = onLoading
7378
}
7479

7580
/// Yield an updated value from an external source.
@@ -79,6 +84,13 @@ public struct SharedSubscriber<Value>: Sendable {
7984
yield(with: .success(value))
8085
}
8186

87+
/// Yield a loading state from an external source.
88+
///
89+
/// - Parameter isLoading: Whether the external source is loading.
90+
public func yieldLoading(_ isLoading: Bool = true) {
91+
onLoading?(isLoading)
92+
}
93+
8294
/// Yield the initial value provided to the property wrapper when none exists in the external
8395
/// source.
8496
///

Tests/SharingTests/IsLoadingTests.swift

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,49 @@ import Testing
6464
#expect($value.isLoading == false)
6565
#expect(value == 42)
6666
}
67+
68+
@Test func isLoadingSubcriberYieldValue() {
69+
struct Key: SharedReaderKey {
70+
let id = UUID()
71+
let testScheduler: TestSchedulerOf<DispatchQueue>
72+
func load(context: LoadContext<Int>, continuation: LoadContinuation<Int>) {
73+
continuation.resumeReturningInitialValue()
74+
}
75+
func subscribe(
76+
context: LoadContext<Int>, subscriber: SharedSubscriber<Int>
77+
) -> SharedSubscription {
78+
subscriber.yieldLoading()
79+
testScheduler.schedule { subscriber.yield(42) }
80+
return SharedSubscription {}
81+
}
82+
}
83+
@SharedReader(Key(testScheduler: testScheduler)) var value = 0
84+
#expect($value.isLoading == true)
85+
testScheduler.advance()
86+
#expect($value.isLoading == false)
87+
#expect(value == 42)
88+
}
89+
90+
@Test func isLoadingSubcriberYieldIsLoading() {
91+
struct Key: SharedReaderKey {
92+
let id = UUID()
93+
let testScheduler: TestSchedulerOf<DispatchQueue>
94+
func load(context: LoadContext<Int>, continuation: LoadContinuation<Int>) {
95+
continuation.resumeReturningInitialValue()
96+
}
97+
func subscribe(
98+
context: LoadContext<Int>, subscriber: SharedSubscriber<Int>
99+
) -> SharedSubscription {
100+
subscriber.yieldLoading()
101+
testScheduler.schedule { subscriber.yieldLoading(false) }
102+
return SharedSubscription {}
103+
}
104+
}
105+
@SharedReader(Key(testScheduler: testScheduler)) var value = 0
106+
#expect($value.isLoading == true)
107+
testScheduler.advance()
108+
#expect($value.isLoading == false)
109+
}
67110
}
68111

69112
private struct Key: SharedReaderKey {

0 commit comments

Comments
 (0)