-
Notifications
You must be signed in to change notification settings - Fork 11
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
Utilities to Implement #15
Comments
It would be nice to have a utility similar to MobX's
Something a bit like this: export const reaction = <T>(
data: () => T,
effect: (value: T, previousValue: T) => void,
equality = Object.is,
) => {
const computed = new Signal.Computed(data);
let previousValue = computed.get();
let isReacting = false;
const watcher = new Signal.subtle.Watcher(async () => {
if (isReacting === true) {
return;
}
isReacting = true;
await 0;
isReacting = false;
const value = computed.get();
if (!equality(previousValue, value)) {
effect(value, previousValue);
previousValue = value;
}
});
watcher.watch(computed);
}; |
oo nice! that does seem useful -- I know quite a few folks that would like such a utility for caching or memoizing complex calculations (tho I don't know an exact use case, but I know it's desired!). Would you be willing to submit a PR adding this utility along with a bunch of tests? |
Reminds me of Vue's |
@signal
(wrapper aroundnew Signal.State
)@localCopy
@keepLatest
@dedupe
@cached
(computed, but on a getter)localStorage
/@localStorage
Blocked on framework-specific semantics and timing (cleanup)
The text was updated successfully, but these errors were encountered: