Skip to content

Instantly share code, notes, and snippets.

@glenjamin
Last active August 17, 2021 14:33
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save glenjamin/3e8d65944d4f4761e521 to your computer and use it in GitHub Desktop.
Save glenjamin/3e8d65944d4f4761e521 to your computer and use it in GitHub Desktop.
requestAnimationFrame batching
/*eslint-env browser*/
/**
* Cribbed from:
* github.com/facebook/react/blob/master/src/addons/ReactRAFBatchingStrategy.js
* github.com/petehunt/react-raf-batching/blob/master/ReactRAFBatching.js
*/
var ReactUpdates = require('react/lib/ReactUpdates');
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, param) {
callback(param);
}
};
ReactUpdates.injection.injectBatchingStrategy(ReactRAFBatchingStrategy);
function tick() {
ReactUpdates.flushBatchedUpdates();
requestAnimationFrame(tick);
}
if (window && typeof window.requestAnimationFrame == 'function') {
window.requestAnimationFrame(tick);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment