Skip to content

Commit 5cce327

Browse files
committed
Observe changes to UserDefaults instance also for events +++ Other minor readability improvements
1 parent 6b4bfc2 commit 5cce327

File tree

1 file changed

+14
-7
lines changed

1 file changed

+14
-7
lines changed

Sources/KeyboardShortcuts/KeyboardShortcuts.swift

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -206,8 +206,8 @@ public enum KeyboardShortcuts {
206206
legacyKeyUpHandlers = [:]
207207

208208
// invalidate and remove all elements of userDefaultsObservers
209-
for observation in userDefaultsObservers {
210-
observation.invalidate()
209+
for observer in userDefaultsObservers {
210+
observer.invalidate()
211211
}
212212
userDefaultsObservers.removeAll()
213213
}
@@ -441,8 +441,10 @@ public enum KeyboardShortcuts {
441441
*/
442442
public static func onKeyDown(for name: Name, action: @escaping () -> Void) {
443443
legacyKeyDownHandlers[name, default: []].append(action)
444-
startObservingShortcutIfNeeded(for: name)
445444
registerShortcutIfNeeded(for: name)
445+
446+
// observe changes to the UserDefaults instance for the given shortcut name
447+
startObservingShortcutIfNeeded(for: name)
446448
}
447449

448450
/**
@@ -468,8 +470,10 @@ public enum KeyboardShortcuts {
468470
*/
469471
public static func onKeyUp(for name: Name, action: @escaping () -> Void) {
470472
legacyKeyUpHandlers[name, default: []].append(action)
471-
startObservingShortcutIfNeeded(for: name)
472473
registerShortcutIfNeeded(for: name)
474+
475+
// observe changes to the UserDefaults instance for the given shortcut name
476+
startObservingShortcutIfNeeded(for: name)
473477
}
474478

475479
private static let userDefaultsPrefix = "KeyboardShortcuts_"
@@ -479,9 +483,9 @@ public enum KeyboardShortcuts {
479483
}
480484

481485
/**
482-
Start observing UserDefaults changes for a specific shortcut name.
483-
484-
This function manages the lifecycle of observations for keyboard shortcuts in the given suite (e.g. UserDefaults):
486+
Start observing changes to the `UserDefaults` instance for a specific shortcut name.
487+
488+
This function manages the lifecycle of observations for keyboard shortcuts in the given `UserDefaults` instance (set by `userDefaults` property):
485489
- Checks if the shortcut is already being observed
486490
- If already observed, restarts the observation
487491
- If not observed, creates a new observation and adds it to the observers list
@@ -610,6 +614,9 @@ extension KeyboardShortcuts {
610614
}
611615

612616
registerShortcutIfNeeded(for: name)
617+
618+
// observe changes to the UserDefaults instance for the given shortcut name
619+
startObservingShortcutIfNeeded(for: name)
613620
}
614621

615622
continuation.onTermination = { _ in

0 commit comments

Comments
 (0)