UNPKG

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