cycle-onionify
cycle-onionify copied to clipboard
Onion state resets when there are no current state subscribers
cycle-onionify (with RxJS) unsubscribes from the sink when there are no subscribers to the state$ and then restarts when a component in the tree uses onionify again. This restarts the state$. Shouldn't the onion state never restart, or is this the proper behavior?
I think this is a bug which shouldn't happen with xstream (because it does sync start and async stop) but might happen with RxJS.
It's not RxJS specific as per https://gitter.im/cyclejs/cyclejs?at=593b5ba06549436c7d32a4ce
It doesn't make sense that it would be RxJS specific. The use case is that only one component in a larger application uses onionify to start and that component gets removed from the component tree (map + flatten
) and then later gets re-instantiated. So for a time there are no subscribers to the onion state in the app and then later a subscriber comes back online.
The gitter link above is to me, I think. The problem is not RxJS specific. You can see it in action in my example project by commenting out this (ugly) workaround:
https://github.com/kylecordes/cycle-example-1/blob/2cb274c5f5a5f8baa7bb5f3256e061d9e92f0054/src/app.tsx#L36
I wonder if this resetting happens because of the initial reducer being
const initReducer$ = xs.of<Reducer>(() => ({ active: false }));
Try using a default reducer instead. Which means, check what the previous state is, and set the initial state only if the previous is undefined. Maybe we should make the README.md more clear in that basic initial reducers shouldn't be used, but default reducers are preferred.