Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
81152 views
1
/**
2
* Copyright 2013-2014, 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
* @providesModule ReactDefaultBatchingStrategy
10
*/
11
12
"use strict";
13
14
var ReactUpdates = require('ReactUpdates');
15
var Transaction = require('Transaction');
16
17
var assign = require('Object.assign');
18
var emptyFunction = require('emptyFunction');
19
20
var RESET_BATCHED_UPDATES = {
21
initialize: emptyFunction,
22
close: function() {
23
ReactDefaultBatchingStrategy.isBatchingUpdates = false;
24
}
25
};
26
27
var FLUSH_BATCHED_UPDATES = {
28
initialize: emptyFunction,
29
close: ReactUpdates.flushBatchedUpdates.bind(ReactUpdates)
30
};
31
32
var TRANSACTION_WRAPPERS = [FLUSH_BATCHED_UPDATES, RESET_BATCHED_UPDATES];
33
34
function ReactDefaultBatchingStrategyTransaction() {
35
this.reinitializeTransaction();
36
}
37
38
assign(
39
ReactDefaultBatchingStrategyTransaction.prototype,
40
Transaction.Mixin,
41
{
42
getTransactionWrappers: function() {
43
return TRANSACTION_WRAPPERS;
44
}
45
}
46
);
47
48
var transaction = new ReactDefaultBatchingStrategyTransaction();
49
50
var ReactDefaultBatchingStrategy = {
51
isBatchingUpdates: false,
52
53
/**
54
* Call the provided function in a context within which calls to `setState`
55
* and friends are batched such that components aren't updated unnecessarily.
56
*/
57
batchedUpdates: function(callback, a, b) {
58
var alreadyBatchingUpdates = ReactDefaultBatchingStrategy.isBatchingUpdates;
59
60
ReactDefaultBatchingStrategy.isBatchingUpdates = true;
61
62
// The code is written this way to avoid extra allocations
63
if (alreadyBatchingUpdates) {
64
callback(a, b);
65
} else {
66
transaction.perform(callback, null, a, b);
67
}
68
}
69
};
70
71
module.exports = ReactDefaultBatchingStrategy;
72
73