Join GitHub today
GitHub is home to over 31 million developers working together to host and review code, manage projects, and build software together.Sign up
Resuscribe after error in Query component #1580
Pinging @wdimiceli since I wrote the same method in
Btw that looks like a "hacky" way to fix it, we should definitely find a way to fix this in apollo client (especially due to the lack of loading state in the fetch of the result after the error. See the test for more details).
Jan 25, 2018
@leoasis I would also agree it's somewhat of a workaround rather than a full-fledged solution.
So I structured my code carefully to avoid the teardown logic. Basically, if a component is the last observer when it unsubscribes, the observable goes through the whole teardown process, and resubscribing then triggers the startup logic which has further side effects. My fix clears the handle and then resubscribes so we briefly have two subscribers before removing the original, sidestepping that whole process.
I'm sure the direct approach works too, but I was hoping to avoid unnecessary re-renders or other unexpected things that happen as a result of an observable restarting. Perhaps I was overthinking it.
Hmm looking quickly at the code in apollo-client I see what you say, there are some extra things being done when setting up/tearing down the observable. At the same time though, the test didn't change in the amount of renders in the component, which is why I'll see if I can understand a bit more how that impacts. But thanks for the insight, that's very helpful.
Ok looked a bit at the code, and it's a bit complicated, but I think I understand how it works.
It seems that zen-observable closes and cleans up the subscription whenever there's an error. This is written in its documentation: https://github.com/zenparsing/zen-observable#new-observable--subscribe- (check the
Trying to unsubscribe from within the subscription error handler doesn't do anything, since the subscription by that time is already closed (not yet cleaned up), so any calls to
I think this is the underlying issue in apollo client, since by definition zen observable terminates the subscription on error, apollo client somehow needs to be able to avoid this. This also makes me wonder if that behavior is part of the standard specification for observables
cc @jbaxleyiii which may be interested in this (unless are already aware of this