Skip to content
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

Closed
linonetwo opened this issue Jun 21, 2019 · 7 comments · Fixed by #42
Closed

Unsubscribe #38

linonetwo opened this issue Jun 21, 2019 · 7 comments · Fixed by #42
Assignees

Comments

@linonetwo
Copy link

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.

@DarrenPaulWright
Copy link
Collaborator

I don't know of a way to unsubscribe right now. I'll look into it.

@DarrenPaulWright
Copy link
Collaborator

@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?

@linonetwo
Copy link
Author

linonetwo commented Jul 11, 2019

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} /></>
}

useEffect will be called many time during rendering, so Unsubscribe can prevent memory leak I think.

Do you think it is reasonable?

@DarrenPaulWright
Copy link
Collaborator

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 useEffect is called.

What do you want to happen when the object changes?

@linonetwo
Copy link
Author

I know what you mean. Maybe just replace ref.current with a new proxyed object, and letting old one to be GC.

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.

@DarrenPaulWright
Copy link
Collaborator

@linonetwo unsubscribe is now supported:

onChange.unsubscribe(watchedObject);

@linonetwo
Copy link
Author

Cool, thank you for that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants