You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardexpand all lines: Sources/Defaults/Defaults.swift
+53-2
Original file line number
Diff line number
Diff line change
@@ -292,13 +292,64 @@ extension Defaults {
292
292
}
293
293
}
294
294
295
-
// We still keep this as it can be useful to pass a dynamic array of keys.
296
295
/**
297
296
Observe updates to multiple stored values.
298
297
299
298
- Parameter keys: The keys to observe updates from.
300
299
- Parameter initial: Trigger an initial event on creation. This can be useful for setting default values on controls.
301
300
301
+
```swift
302
+
Task {
303
+
for await (foo, bar) in Defaults.updates([.foo, .bar]) {
304
+
print("Values changed:", foo, bar)
305
+
}
306
+
}
307
+
```
308
+
*/
309
+
publicstaticfunc updates<eachValue:Serializable>(
310
+
_ keys:repeatKey<eachValue>,
311
+
initial:Bool=true
312
+
)->AsyncStream<(repeateachValue)>{
313
+
.init { continuation in
314
+
func getCurrentValues()->(repeateachValue){
315
+
(repeatself[each keys])
316
+
}
317
+
318
+
varobservations=[DefaultsObservation]()
319
+
320
+
if initial {
321
+
continuation.yield(getCurrentValues())
322
+
}
323
+
324
+
forkeyinrepeat(each keys){
325
+
letobservation=DefaultsObservation(object: key.suite, key: key.name){ _, _ in
326
+
continuation.yield(getCurrentValues())
327
+
}
328
+
329
+
observation.start(options:[])
330
+
observations.append(observation)
331
+
}
332
+
333
+
letimmutableObservations= observations
334
+
335
+
continuation.onTermination ={ _ in
336
+
// `invalidate()` should be thread-safe, but it is not in practice.
337
+
DispatchQueue.main.async{
338
+
forobservationin immutableObservations {
339
+
observation.invalidate()
340
+
}
341
+
}
342
+
}
343
+
}
344
+
}
345
+
346
+
// We still keep this as it can be useful to pass a dynamic array of keys.
347
+
/**
348
+
Observe updates to multiple stored values without receiving the values.
349
+
350
+
- Parameter keys: The keys to observe updates from.
351
+
- Parameter initial: Trigger an initial event on creation. This can be useful for setting default values on controls.
352
+
302
353
```swift
303
354
Task {
304
355
for await _ in Defaults.updates([.foo, .bar]) {
@@ -307,7 +358,7 @@ extension Defaults {
307
358
}
308
359
```
309
360
310
-
- Note: This does not include which of the values changed. Use ``Defaults/updates(_:initial:)-88orv`` if you need that. You could use [`merge`](https://github.com/apple/swift-async-algorithms/blob/main/Sources/AsyncAlgorithms/AsyncAlgorithms.docc/Guides/Merge.md) to merge them into a single sequence.
361
+
- Note: This does not include which of the values changed. Use ``Defaults/updates(_:initial:)-l03o`` if you need that.
0 commit comments