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
+27-9
Original file line number
Diff line number
Diff line change
@@ -105,6 +105,8 @@ extension Defaults {
105
105
Create a key.
106
106
107
107
- Parameter name: The name must be ASCII, not start with `@`, and cannot contain a dot (`.`).
108
+
- Parameter defaultValue: The default value.
109
+
- Parameter suite: The `UserDefaults` suite to store the value in.
108
110
- Parameter iCloud: Automatically synchronize the value with ``Defaults/iCloud``.
109
111
110
112
The `default` parameter should not be used if the `Value` type is an optional.
@@ -150,16 +152,18 @@ extension Defaults {
150
152
```
151
153
152
154
- Parameter name: The name must be ASCII, not start with `@`, and cannot contain a dot (`.`).
155
+
- Parameter suite: The `UserDefaults` suite to store the value in.
153
156
- Parameter iCloud: Automatically synchronize the value with ``Defaults/iCloud``.
157
+
- Parameter defaultValueGetter: The dynamic default value.
154
158
155
159
- Note: This initializer will not set the default value in the actual `UserDefaults`. This should not matter much though. It's only really useful if you use legacy KVO bindings.
156
160
*/
157
161
@_alwaysEmitIntoClient
158
162
publicinit(
159
163
_ name:String,
160
164
suite:UserDefaults=.standard,
161
-
default defaultValueGetter:@escaping()->Value,
162
-
iCloud:Bool=false
165
+
iCloud:Bool=false,
166
+
default defaultValueGetter:@escaping()->Value
163
167
){
164
168
self.defaultValueGetter = defaultValueGetter
165
169
@@ -178,14 +182,20 @@ extension Defaults.Key {
178
182
Create a key with an optional value.
179
183
180
184
- Parameter name: The name must be ASCII, not start with `@`, and cannot contain a dot (`.`).
185
+
- Parameter suite: The `UserDefaults` suite to store the value in.
181
186
- Parameter iCloud: Automatically synchronize the value with ``Defaults/iCloud``.
// `invalidate()` should be thread-safe, but it is not in practice.
288
+
DispatchQueue.main.async{
289
+
observation.invalidate()
290
+
}
277
291
}
278
292
}
279
293
}
280
294
281
-
// TODO: Make this include a tuple with the values when Swift supports variadic generics. I can then simply use `merge()` with the first `updates()` method.
295
+
// We still keep this as it can be useful to pass a dynamic array of keys.
282
296
/**
283
297
Observe updates to multiple stored values.
284
298
299
+
- Parameter keys: The keys to observe updates from.
285
300
- Parameter initial: Trigger an initial event on creation. This can be useful for setting default values on controls.
286
301
287
302
```swift
@@ -297,7 +312,7 @@ extension Defaults {
297
312
publicstaticfunc updates(
298
313
_ keys:[_AnyKey],
299
314
initial:Bool=true
300
-
)->AsyncStream<Void>{ // TODO: Make this `some AsyncSequence<Value>` when Swift 6 is out.
315
+
)->AsyncStream<Void>{ // TODO: Make this `some AsyncSequence<Void>` when targeting macOS 15.
301
316
.init { continuation in
302
317
letobservations= keys.indexed().map{ index, key in
303
318
letobservation=DefaultsObservation(object: key.suite, key: key.name){ _, _ in
@@ -311,8 +326,11 @@ extension Defaults {
311
326
}
312
327
313
328
continuation.onTermination ={ _ in
314
-
forobservationin observations {
315
-
observation.invalidate()
329
+
// `invalidate()` should be thread-safe, but it is not in practice.
0 commit comments