Skip to content

Commit

Permalink
Update Observation.swift
Browse files Browse the repository at this point in the history
  • Loading branch information
sindresorhus authored and hank121314 committed Aug 2, 2024
1 parent dfdddca commit fbf740b
Showing 1 changed file with 22 additions and 15 deletions.
37 changes: 22 additions & 15 deletions Sources/Defaults/Observation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -120,10 +120,10 @@ extension Defaults {
}

final class SuiteKeyPair: Hashable {
weak var suite: NSObject?
weak var suite: UserDefaults?
let key: String

init(suite: NSObject, key: String) {
init(suite: UserDefaults, key: String) {
self.suite = suite
self.key = key
}
Expand All @@ -147,10 +147,11 @@ extension Defaults {
typealias Callback = (SuiteKeyPair, BaseChange) -> Void

static var observationContext = 0
private weak var suite: NSObject?
private weak var suite: UserDefaults?
private let name: String
private let callback: Callback
private var isObserving = false
private let lock: Lock = .make()

init(object: UserDefaults, key: String, _ callback: @escaping Callback) {
self.suite = object
Expand All @@ -169,20 +170,24 @@ extension Defaults {
}

func start(options: ObservationOptions) {
guard !isObserving else {
return
lock.with {
guard !isObserving else {
return
}
suite?.addObserver(self, forKeyPath: name, options: options.toNSKeyValueObservingOptions, context: &Self.observationContext)
isObserving = true
}
suite?.addObserver(self, forKeyPath: name, options: options.toNSKeyValueObservingOptions, context: &Self.observationContext)
isObserving = true
}

func invalidate() {
guard isObserving else {
return
lock.with {
guard isObserving else {
return
}
suite?.removeObserver(self, forKeyPath: name)
isObserving = false
suite = nil
}
suite?.removeObserver(self, forKeyPath: name)
isObserving = false
suite = nil
}

// swiftlint:disable:next block_based_kvo
Expand All @@ -205,7 +210,7 @@ extension Defaults {
}

guard
selfSuite == (object as? NSObject),
selfSuite == (object as? UserDefaults),
let change
else {
return
Expand Down Expand Up @@ -376,9 +381,11 @@ extension Defaults {
let compositeObservation = CompositeDefaultsObservation { _, _ in
handler()
}
keys.forEach {
compositeObservation.add(key: $0)

for key in keys {
compositeObservation.add(key: key)
}

compositeObservation.start(options: options)

return compositeObservation
Expand Down

0 comments on commit fbf740b

Please sign in to comment.