UNPKG

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