react-easy-state
react-easy-state copied to clipboard
fix(batching): improve batching behavior to work automatically in all cases.
Resolves #186
Proposed changes:
- automatically include all component renders within a batch
- removes all monkey patching to do batching of component renders
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.