mirror of
https://github.com/facebook/react.git
synced 2025-11-01 09:12:30 +00:00
e779c39dfe
* Flatten everything * Fix ReactDOMServerNode build * Fix native builds
64 lines
2.0 KiB
JavaScript
64 lines
2.0 KiB
JavaScript
/**
|
|
* Copyright (c) 2013-present, Facebook, Inc.
|
|
*
|
|
* This source code is licensed under the MIT license found in the
|
|
* LICENSE file in the root directory of this source tree.
|
|
*
|
|
* @providesModule ReactGenericBatching
|
|
*/
|
|
|
|
'use strict';
|
|
|
|
var ReactControlledComponent = require('ReactControlledComponent');
|
|
|
|
// Used as a way to call batchedUpdates when we don't have a reference to
|
|
// the renderer. Such as when we're dispatching events or if third party
|
|
// libraries need to call batchedUpdates. Eventually, this API will go away when
|
|
// everything is batched by default. We'll then have a similar API to opt-out of
|
|
// scheduled work and instead do synchronous work.
|
|
|
|
// Defaults
|
|
var fiberBatchedUpdates = function(fn, bookkeeping) {
|
|
return fn(bookkeeping);
|
|
};
|
|
|
|
function batchedUpdates(fn, bookkeeping) {
|
|
// If we have Fiber loaded, we need to wrap this in a batching call so that
|
|
// Fiber can apply its default priority for this call.
|
|
return fiberBatchedUpdates(fn, bookkeeping);
|
|
}
|
|
|
|
var isNestingBatched = false;
|
|
function batchedUpdatesWithControlledComponents(fn, bookkeeping) {
|
|
if (isNestingBatched) {
|
|
// If we are currently inside another batch, we need to wait until it
|
|
// fully completes before restoring state. Therefore, we add the target to
|
|
// a queue of work.
|
|
return batchedUpdates(fn, bookkeeping);
|
|
}
|
|
isNestingBatched = true;
|
|
try {
|
|
return batchedUpdates(fn, bookkeeping);
|
|
} finally {
|
|
// Here we wait until all updates have propagated, which is important
|
|
// when using controlled components within layers:
|
|
// https://github.com/facebook/react/issues/1698
|
|
// Then we restore state of any controlled component.
|
|
isNestingBatched = false;
|
|
ReactControlledComponent.restoreStateIfNeeded();
|
|
}
|
|
}
|
|
|
|
var ReactGenericBatchingInjection = {
|
|
injectFiberBatchedUpdates: function(_batchedUpdates) {
|
|
fiberBatchedUpdates = _batchedUpdates;
|
|
},
|
|
};
|
|
|
|
var ReactGenericBatching = {
|
|
batchedUpdates: batchedUpdatesWithControlledComponents,
|
|
injection: ReactGenericBatchingInjection,
|
|
};
|
|
|
|
module.exports = ReactGenericBatching;
|