Immutable state on SSR (toString) #649
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR solves state leaking between calls to
toStringand race conditions that occur during double render pass (bankai et al.).The issue is that each call to
toStringwould extendapp.state, adding on more and more data for each render. If one was to expose the resultingapp.stateaswindow.initalStateone would get the combined state from all prior calls totoString.It is especially troublesome during double render pass (calling
toStringtwice, once to allow events to trigger data being fetched and another to render the view with fetched data). If two attempts at a double render pass are issued in quick succession (e.g. server responding to several requests), both calls will mutateapp.stateand the first to finish will get the intermediate state of both render calls.This PR ensures that the state passed into
toStringis only modified during that one pass through and thatapp.stateremains untouched.This would also allow us to proceed with choojs/bankai#446
Edit: Yet another issue solved by this PR is that every call to
toStringwould initialize all stores and hence, add all listeners to the event bus over and over again for every render. This means that after a couple of renders any event listeners that e.g. fetches data will be fetching the same data several times over on every call totoString.