UNPKG

1.76 kBJavaScriptView Raw
1/**
2 * Copyright (c) 2013-present, Facebook, Inc.
3 *
4 * This source code is licensed under the MIT license found in the
5 * LICENSE file in the root directory of this source tree.
6 *
7 */
8
9'use strict';
10
11var _assign = require('object-assign');
12
13var ReactUpdates = require('./ReactUpdates');
14var Transaction = require('./Transaction');
15
16var emptyFunction = require('fbjs/lib/emptyFunction');
17
18var RESET_BATCHED_UPDATES = {
19 initialize: emptyFunction,
20 close: function () {
21 ReactDefaultBatchingStrategy.isBatchingUpdates = false;
22 }
23};
24
25var FLUSH_BATCHED_UPDATES = {
26 initialize: emptyFunction,
27 close: ReactUpdates.flushBatchedUpdates.bind(ReactUpdates)
28};
29
30var TRANSACTION_WRAPPERS = [FLUSH_BATCHED_UPDATES, RESET_BATCHED_UPDATES];
31
32function ReactDefaultBatchingStrategyTransaction() {
33 this.reinitializeTransaction();
34}
35
36_assign(ReactDefaultBatchingStrategyTransaction.prototype, Transaction, {
37 getTransactionWrappers: function () {
38 return TRANSACTION_WRAPPERS;
39 }
40});
41
42var transaction = new ReactDefaultBatchingStrategyTransaction();
43
44var ReactDefaultBatchingStrategy = {
45 isBatchingUpdates: false,
46
47 /**
48 * Call the provided function in a context within which calls to `setState`
49 * and friends are batched such that components aren't updated unnecessarily.
50 */
51 batchedUpdates: function (callback, a, b, c, d, e) {
52 var alreadyBatchingUpdates = ReactDefaultBatchingStrategy.isBatchingUpdates;
53
54 ReactDefaultBatchingStrategy.isBatchingUpdates = true;
55
56 // The code is written this way to avoid extra allocations
57 if (alreadyBatchingUpdates) {
58 return callback(a, b, c, d, e);
59 } else {
60 return transaction.perform(callback, null, a, b, c, d, e);
61 }
62 }
63};
64
65module.exports = ReactDefaultBatchingStrategy;
\No newline at end of file