Hello.
Do we need to unsubscribe/unwatch?
Example:
class MyComponent extends React.Component {
componentDidMount() {
this.calls = horizon('calls').watch().subscribe(this.setState.bind(this))
}
componentWillUnmount() {
this.calls.unsubscribe()
}
}
@hnordt This issue tracker is for bugs. Please ask on the slack channel or here https://discuss.horizon.io/
This is ok, we use it for feature suggestions too.
@hnordt I definitely recommend unsubscribing as a good practice. It's not technically necessary if your use case for cleaning up is "the user navigates away from the webpage", but if you're doing a multi-page app, cleaning up unused feeds is really important
@deontologician the correct way to unsubscribe is the way I written above?
For fetch() there is no need to unsubscribe, right?
@hnordt Yeah, what you have is correct. If you use forEach instead of subscribe there's no way to unsubscribe. And yes, fetch() leaves no resources on the server, so there's no real reason to unsubscribe.
Thank you @deontologician, you are a master!
Is this documented? I can't see it referenced anywhere in the Collections docs
I don't think that I have seen this in the documentation. Thanks for pointing this out though because it is very important to unsubscribe in SPAs.
I haven't looked at the source code, but basically if you keep the subscription open in a single page application that is only needed in a given component, your horizon instance will keep getting data on changes, even when that component is destructed.
I think this should actually not only be mentioned but highlighted in the documentation. While somewhat common sense for anyone used to working with rxjs subjects, it is definitely a potential source of massive memory leaks and wasted network resources.