UNPKG

3.04 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
10'use strict';
11
12var _prodInvariant = require('./reactProdInvariant');
13
14function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
15
16var PooledClass = require('./PooledClass');
17
18var invariant = require('fbjs/lib/invariant');
19
20/**
21 * A specialized pseudo-event module to help keep track of components waiting to
22 * be notified when their DOM representations are available for use.
23 *
24 * This implements `PooledClass`, so you should never need to instantiate this.
25 * Instead, use `CallbackQueue.getPooled()`.
26 *
27 * @class ReactMountReady
28 * @implements PooledClass
29 * @internal
30 */
31
32var CallbackQueue = function () {
33 function CallbackQueue(arg) {
34 _classCallCheck(this, CallbackQueue);
35
36 this._callbacks = null;
37 this._contexts = null;
38 this._arg = arg;
39 }
40
41 /**
42 * Enqueues a callback to be invoked when `notifyAll` is invoked.
43 *
44 * @param {function} callback Invoked when `notifyAll` is invoked.
45 * @param {?object} context Context to call `callback` with.
46 * @internal
47 */
48
49
50 CallbackQueue.prototype.enqueue = function enqueue(callback, context) {
51 this._callbacks = this._callbacks || [];
52 this._callbacks.push(callback);
53 this._contexts = this._contexts || [];
54 this._contexts.push(context);
55 };
56
57 /**
58 * Invokes all enqueued callbacks and clears the queue. This is invoked after
59 * the DOM representation of a component has been created or updated.
60 *
61 * @internal
62 */
63
64
65 CallbackQueue.prototype.notifyAll = function notifyAll() {
66 var callbacks = this._callbacks;
67 var contexts = this._contexts;
68 var arg = this._arg;
69 if (callbacks && contexts) {
70 !(callbacks.length === contexts.length) ? process.env.NODE_ENV !== 'production' ? invariant(false, 'Mismatched list of contexts in callback queue') : _prodInvariant('24') : void 0;
71 this._callbacks = null;
72 this._contexts = null;
73 for (var i = 0; i < callbacks.length; i++) {
74 callbacks[i].call(contexts[i], arg);
75 }
76 callbacks.length = 0;
77 contexts.length = 0;
78 }
79 };
80
81 CallbackQueue.prototype.checkpoint = function checkpoint() {
82 return this._callbacks ? this._callbacks.length : 0;
83 };
84
85 CallbackQueue.prototype.rollback = function rollback(len) {
86 if (this._callbacks && this._contexts) {
87 this._callbacks.length = len;
88 this._contexts.length = len;
89 }
90 };
91
92 /**
93 * Resets the internal queue.
94 *
95 * @internal
96 */
97
98
99 CallbackQueue.prototype.reset = function reset() {
100 this._callbacks = null;
101 this._contexts = null;
102 };
103
104 /**
105 * `PooledClass` looks for this.
106 */
107
108
109 CallbackQueue.prototype.destructor = function destructor() {
110 this.reset();
111 };
112
113 return CallbackQueue;
114}();
115
116module.exports = PooledClass.addPoolingTo(CallbackQueue);
\No newline at end of file