Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
80529 views
1
/**
2
* Copyright 2013-2015, 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 CallbackQueue
10
*/
11
12
'use strict';
13
14
var PooledClass = require("./PooledClass");
15
16
var assign = require("./Object.assign");
17
var invariant = require("./invariant");
18
19
/**
20
* A specialized pseudo-event module to help keep track of components waiting to
21
* be notified when their DOM representations are available for use.
22
*
23
* This implements `PooledClass`, so you should never need to instantiate this.
24
* Instead, use `CallbackQueue.getPooled()`.
25
*
26
* @class ReactMountReady
27
* @implements PooledClass
28
* @internal
29
*/
30
function CallbackQueue() {
31
this._callbacks = null;
32
this._contexts = null;
33
}
34
35
assign(CallbackQueue.prototype, {
36
37
/**
38
* Enqueues a callback to be invoked when `notifyAll` is invoked.
39
*
40
* @param {function} callback Invoked when `notifyAll` is invoked.
41
* @param {?object} context Context to call `callback` with.
42
* @internal
43
*/
44
enqueue: function(callback, context) {
45
this._callbacks = this._callbacks || [];
46
this._contexts = this._contexts || [];
47
this._callbacks.push(callback);
48
this._contexts.push(context);
49
},
50
51
/**
52
* Invokes all enqueued callbacks and clears the queue. This is invoked after
53
* the DOM representation of a component has been created or updated.
54
*
55
* @internal
56
*/
57
notifyAll: function() {
58
var callbacks = this._callbacks;
59
var contexts = this._contexts;
60
if (callbacks) {
61
("production" !== process.env.NODE_ENV ? invariant(
62
callbacks.length === contexts.length,
63
'Mismatched list of contexts in callback queue'
64
) : invariant(callbacks.length === contexts.length));
65
this._callbacks = null;
66
this._contexts = null;
67
for (var i = 0, l = callbacks.length; i < l; i++) {
68
callbacks[i].call(contexts[i]);
69
}
70
callbacks.length = 0;
71
contexts.length = 0;
72
}
73
},
74
75
/**
76
* Resets the internal queue.
77
*
78
* @internal
79
*/
80
reset: function() {
81
this._callbacks = null;
82
this._contexts = null;
83
},
84
85
/**
86
* `PooledClass` looks for this.
87
*/
88
destructor: function() {
89
this.reset();
90
}
91
92
});
93
94
PooledClass.addPoolingTo(CallbackQueue);
95
96
module.exports = CallbackQueue;
97
98