Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Summarize internal UserDefaults observation class #181

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@
@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 @@
self.keys.formUnion(keys)
syncWithoutWaiting(keys)
for key in keys {
localKeysMonitor.addObserver(key)
localKeysMonitor.add(key: key)
}
}

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

Expand Down Expand Up @@ -539,7 +539,7 @@
if
let localTimestamp = self.timestamp(forKey: key, source: .local),
localTimestamp >= remoteTimestamp
{

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

View workflow job for this annotation

GitHub Actions / lint

Opening Brace Spacing Violation: Opening braces should be preceded by a single space and on the same line as the declaration (opening_brace)
continue
}

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
Loading