-
-
Notifications
You must be signed in to change notification settings - Fork 121
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
Add ability to subscribe to multiple keys and to prevent propagation #49
Conversation
As for |
This is looking promising. Thanks for working on it. 🙌 |
Too bad. "static" always lags behind in functionality. We could actually solve this without mirror if we switch to using Key paths instead of the dot syntax (It would then be |
Is there any way we could add propagation prevention to |
We could do that, by tweaking |
I’ve came up with a different approach, to make this feasible on both classic and combine implementations. A block based approach inspired by SwiftUIs func withNoPropagation(block: () -> Void) {
// tweak threadDictionary and call block
} Every change made in the block, would not be propagated at all. Usage example: Defaults.observeAll(.key1, .key2) {
// …
withNoPropagation {
// update some value at .key1
// this will not be propagated
Defaults[.key1] = 11
}
// this will be propagated
Defaults[.someKey] = true
} A big advantage is that, this may be used not only in observation callback, but also in Combine callbacks (regardless of publisher modifiers) or any other place. I’ve working implementation, let me know what you think 👍 |
@fredyshox That's a good approach! I like it. Should it be |
Also replied to #49 (comment). |
Sorry for the late reply. Summer and COVID made it hard to keep up with all the PRs. |
Can you document |
Keys conformance to BaseKey
I've added docstrings to new public API and updated readme. Also added short comment explaining inner workings of |
This is looking great. Nice work as always 🙌🏻 |
@fredyshox Would you be interested in joining the project as a maintainer? There's no commitment needed. Just want to highlight your contributions with your name in the readme. No worries if not though. |
Yes I do! Feel free to mention me :) |
Attempt to fix #30
Added
CompositeUserDefaultsKeyObservation
for observing multiple keys, withpreventPropagation
option to prevent infinite recursion when change is made within handler.I store flag in threadDictionary of the current thread to determine when changes should be propagated. This enables to still support changes made on other threads, while initial one is still processing handler (more in
testObservePreventPropagationMultiThread
test case).Code is still a little messy, but let me know what you think :)
IssueHunt Summary
Referenced issues
This pull request has been submitted to: