Skip to content

Commit

Permalink
Summarize internal UserDefaults observation class
Browse files Browse the repository at this point in the history
  • Loading branch information
hank121314 committed Jul 20, 2024
1 parent 5c6d5bf commit f131fa5
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 232 deletions.
2 changes: 1 addition & 1 deletion Sources/Defaults/Defaults+iCloud.swift
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ final class iCloudSynchronizer {
@Atomic(value: []) private var remoteSyncingKeys: Set<Defaults.Keys>

// TODO: Replace it with async stream when Swift supports custom executors.
private lazy var localKeysMonitor: Defaults.CompositeUserDefaultsAnyKeyObservation = .init { [weak self] observable in
private lazy var localKeysMonitor: Defaults.UserDefaultsAnyKeysObservation = .init { [weak self] (observable, _) in

Check warning on line 252 in Sources/Defaults/Defaults+iCloud.swift

View workflow job for this annotation

GitHub Actions / lint

Unneeded Parentheses in Closure Argument Violation: Parentheses are not needed when declaring closure arguments (unneeded_parentheses_in_closure_argument)
guard
let self,
let suite = observable.suite,
Expand Down
4 changes: 2 additions & 2 deletions Sources/Defaults/Defaults.swift
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ extension Defaults {
initial: Bool = true
) -> AsyncStream<Value> { // TODO: Make this `some AsyncSequence<Value>` when Swift 6 is out.
.init { continuation in
let observation = UserDefaultsKeyObservation2(object: key.suite, key: key.name) { change in
let observation = UserDefaultsAnyKeysObservation(object: key.suite, key: key.name) { (_, change) in

Check warning on line 242 in Sources/Defaults/Defaults.swift

View workflow job for this annotation

GitHub Actions / lint

Unneeded Parentheses in Closure Argument Violation: Parentheses are not needed when declaring closure arguments (unneeded_parentheses_in_closure_argument)
// TODO: Use the `.deserialize` method directly.
let value = KeyChange(change: change, defaultValue: key.defaultValue).newValue
continuation.yield(value)
Expand Down Expand Up @@ -275,7 +275,7 @@ extension Defaults {
) -> AsyncStream<Void> { // TODO: Make this `some AsyncSequence<Value>` when Swift 6 is out.
.init { continuation in
let observations = keys.indexed().map { index, key in
let observation = UserDefaultsKeyObservation2(object: key.suite, key: key.name) { _ in
let observation = UserDefaultsAnyKeysObservation(object: key.suite, key: key.name) { (_, _) in

Check warning on line 278 in Sources/Defaults/Defaults.swift

View workflow job for this annotation

GitHub Actions / lint

Unneeded Parentheses in Closure Argument Violation: Parentheses are not needed when declaring closure arguments (unneeded_parentheses_in_closure_argument)
continuation.yield()
}

Expand Down
8 changes: 4 additions & 4 deletions Sources/Defaults/Observation+Combine.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,16 @@ extension Defaults {
*/
final class DefaultsSubscription<SubscriberType: Subscriber>: Subscription where SubscriberType.Input == BaseChange {
private var subscriber: SubscriberType?
private var observation: UserDefaultsKeyObservation?
private var observation: UserDefaultsAnyKeysObservation?
private let options: ObservationOptions

init(subscriber: SubscriberType, suite: UserDefaults, key: String, options: ObservationOptions) {
self.subscriber = subscriber
self.options = options
self.observation = UserDefaultsKeyObservation(
self.observation = UserDefaultsAnyKeysObservation(
object: suite,
key: key,
callback: observationCallback(_:)
observationCallback
)
}

Expand All @@ -33,7 +33,7 @@ extension Defaults {
observation?.start(options: options)
}

private func observationCallback(_ change: BaseChange) {
private func observationCallback(_: SuiteKeyPair, change: BaseChange) {
_ = subscriber?.receive(change)
}
}
Expand Down
Loading

0 comments on commit f131fa5

Please sign in to comment.