NOTE: this repo was intended as an example and is kept here for historical purposes. Please don't use it in a real project!
A batching strategy for React that's more like a game. This may make your apps more performant but harder to test.
A game-like batching strategy for React
As far as I can tell it works with v0.10.0, just need to update the package.json
to avoid npm complaints.
I actually like that this strategy enforces async setState
because it's more deterministic than default strategy in this aspect. Just like Promises resolve on next tick so there's just one code path and not two.
We've been using this strategy in production with some success but I'm filing this to warn about a few caveats I found:
<NativeAudio isPlaying>
that wraps HTML5 <audio>
and toggle isPlaying
in response to a click
, RAF batching will delay your call until next frame, and iOS Safari will block it. The solution (if you don't want to give up on RAF batching) is to simply call ReactUpdates.flushBatchedUpdates()
after setState
in click
handler.// Based on https://github.com/petehunt/react-raf-batching
// but also triggers `tick` regularly if tab is inactive.
'use strict';
var ReactUpdates = require('react/lib/ReactUpdates'),
now = require('react/lib/performanceNow');
var FORCE_TICK_INTERVAL = 1000,
FORCE_TICK_THRESHOLD = 100,
lastTick;
function tick() {
ReactUpdates.flushBatchedUpdates();
window.requestAnimationFrame(tick);
lastTick = now();
}
function forceTick() {
if (now() - lastTick > FORCE_TICK_THRESHOLD) {
tick();
}
}
var ReactRAFBatchingStrategy = {
isBatchingUpdates: true,
/**
* Call the provided function in a context within which calls to `setState`
* and friends are batched such that components aren't updated unnecessarily.
*/
batchedUpdates: function(callback, a, b) {
callback(a, b);
}
};
tick();
setInterval(forceTick, FORCE_TICK_INTERVAL);
module.exports = ReactRAFBatchingStrategy;
Otherwise it works well for us and we never had problems with controlled inputs that some people were reporting.
npm install is failing.
In its current state, this strategy doesn't work out of the box.
If the root component has a state, and you manage to update it X times before the next frame, you might lose updates:
Frame 1
this.setState({ todos: this.state.todos.concat(newTodo) });
this.setState({ todos: this.state.todos.concat(newTodo) });
this.setState({ todos: this.state.todos.concat(newTodo) });
Frame 2 -> The todos only has a length of 1.
The state object is not updated synchronously.
is this a bug with batched updates?
I understand you require this somewhere and override the core batching strategy... but how?
When we run this package in a Node environment, where requestAnimationFrame
is not defined, we fall back to the setTimeout
-based strategy. Is that the best option? What about something like process.nextTick
?
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.