-
-
Notifications
You must be signed in to change notification settings - Fork 89
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
Unsubscribe #38
Comments
I don't know of a way to unsubscribe right now. I'll look into it. |
@linonetwo How do other react libraries handle this? If you could call something like proxiedObject['[[target]]'] to get the original target, would that be sufficient? Or do you need a way to stop all further callbacks on the proxied object? |
I think unsubscribe can be something like: watchedObject[SymbolUnsubscribe] // [Function] And react hook user can use it like: function SomeReactComponent(props) {
const obj = useRef();
useEffect(() => {
const watchedObject = onChange(object, () => {});
obj.current = watchedObject;
return watchedObject[SymbolUnsubscribe];
}, [props.obj])
return <><ChildComponent obj={obj} /></>
}
Do you think it is reasonable? |
Would this do the same thing? function SomeReactComponent(props) {
const obj = useRef();
useEffect(() => {
const original = {};
obj.current = onChange(original, () => {});
return original;
}, [props.obj])
return <><ChildComponent obj={obj} /></>
} either way, you're still creating a new onChange proxy every time What do you want to happen when the object changes? |
I know what you mean. Maybe just replace function SomeReactComponent(props) {
const obj = useRef();
useEffect(() => {
const original = {};
obj.current = onChange(original, () => {});
}, [props.obj])
return <><ChildComponent obj={obj} /></>
} Normally subscription won't be easily GC, for example, socket subscription, so we need to return an unsubscription function. But in this case, we might not need to. Thank you for answering. |
@linonetwo unsubscribe is now supported: onChange.unsubscribe(watchedObject); |
Cool, thank you for that. |
Hope I can unsubscribe to change, so this can be used in React hook, for example, useEffct, which ask you to return an unsubscription function.
The text was updated successfully, but these errors were encountered: