fix(batching): improve batching behavior to work automatically in all cases. #226
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.
Resolves #186
Proposed changes:
The basic theory of operation is captured in view.js. Whenever a component re-render is needed record that fact on a per-component-instance basis. When the current task finishes executing then perform all of the re-renders at once. It uses a microTask to detect the end of the current task (rather than using
setTimeout
) so that the re-renders happen before any other event handlers - so that render is not unduly delayed if the event queue is full, e.g. during a drag operation.Overall this means that changes are appropriately batched together regardless of the execution context. I think that in practice it also means that the
batch
method is probably not necessary, but I did not remove it or change its behavior since there may be some subtle aspects to existing code that depend on it.I had to add some helpers to the tests, and modify all of the tests, to handle microTasks and to run the timers before checking the test output.
The tests pass. I have also tested this against our real-world use of react-easy-state without finding any problems.