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 27, 2024
1 parent b8c1e7c commit dfdddca
Show file tree
Hide file tree
Showing 5 changed files with 106 additions and 223 deletions.
10 changes: 5 additions & 5 deletions Sources/Defaults/Defaults+iCloud.swift
Original file line number Diff line number Diff line change
Expand Up @@ -249,11 +249,11 @@ 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.CompositeDefaultsObservation = .init { [weak self] pair, _ in
guard
let self,
let suite = observable.suite,
let key = keys.first(where: { $0.name == observable.key && $0.suite == suite }),
let suite = pair.suite,
let key = keys.first(where: { $0.name == pair.key && $0.suite == suite }),
// Prevent triggering local observation when syncing from remote.
!remoteSyncingKeys.contains(key)
else {
Expand All @@ -273,7 +273,7 @@ final class iCloudSynchronizer {
self.keys.formUnion(keys)
syncWithoutWaiting(keys)
for key in keys {
localKeysMonitor.addObserver(key)
localKeysMonitor.add(key: key)
}
}

Expand All @@ -283,7 +283,7 @@ final class iCloudSynchronizer {
func remove(_ keys: [Defaults.Keys]) {
self.keys.subtract(keys)
for key in keys {
localKeysMonitor.removeObserver(key)
localKeysMonitor.remove(key: key)
}
}

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 = DefaultsObservation(object: key.suite, key: key.name) { _, change in
// 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 = DefaultsObservation(object: key.suite, key: key.name) { _, _ in
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: DefaultsObservationWithLifeTime?
private let options: ObservationOptions

init(subscriber: SubscriberType, suite: UserDefaults, key: String, options: ObservationOptions) {
self.subscriber = subscriber
self.options = options
self.observation = UserDefaultsKeyObservation(
self.observation = DefaultsObservationWithLifeTime(
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 dfdddca

Please sign in to comment.