1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 | 'use strict';
|
12 |
|
13 | var _prodInvariant = require('./reactProdInvariant');
|
14 |
|
15 | var ReactCurrentOwner = require('react/lib/ReactCurrentOwner');
|
16 | var ReactInstanceMap = require('./ReactInstanceMap');
|
17 | var ReactInstrumentation = require('./ReactInstrumentation');
|
18 | var ReactUpdates = require('./ReactUpdates');
|
19 |
|
20 | var invariant = require('fbjs/lib/invariant');
|
21 | var warning = require('fbjs/lib/warning');
|
22 |
|
23 | function enqueueUpdate(internalInstance) {
|
24 | ReactUpdates.enqueueUpdate(internalInstance);
|
25 | }
|
26 |
|
27 | function formatUnexpectedArgument(arg) {
|
28 | var type = typeof arg;
|
29 | if (type !== 'object') {
|
30 | return type;
|
31 | }
|
32 | var displayName = arg.constructor && arg.constructor.name || type;
|
33 | var keys = Object.keys(arg);
|
34 | if (keys.length > 0 && keys.length < 20) {
|
35 | return displayName + ' (keys: ' + keys.join(', ') + ')';
|
36 | }
|
37 | return displayName;
|
38 | }
|
39 |
|
40 | function getInternalInstanceReadyForUpdate(publicInstance, callerName) {
|
41 | var internalInstance = ReactInstanceMap.get(publicInstance);
|
42 | if (!internalInstance) {
|
43 | if (process.env.NODE_ENV !== 'production') {
|
44 | var ctor = publicInstance.constructor;
|
45 |
|
46 |
|
47 |
|
48 | process.env.NODE_ENV !== 'production' ? warning(!callerName, '%s(...): Can only update a mounted or mounting component. ' + 'This usually means you called %s() on an unmounted component. ' + 'This is a no-op. Please check the code for the %s component.', callerName, callerName, ctor && (ctor.displayName || ctor.name) || 'ReactClass') : void 0;
|
49 | }
|
50 | return null;
|
51 | }
|
52 |
|
53 | if (process.env.NODE_ENV !== 'production') {
|
54 | process.env.NODE_ENV !== 'production' ? warning(ReactCurrentOwner.current == null, '%s(...): Cannot update during an existing state transition (such as ' + "within `render` or another component's constructor). Render methods " + 'should be a pure function of props and state; constructor ' + 'side-effects are an anti-pattern, but can be moved to ' + '`componentWillMount`.', callerName) : void 0;
|
55 | }
|
56 |
|
57 | return internalInstance;
|
58 | }
|
59 |
|
60 |
|
61 |
|
62 |
|
63 |
|
64 | var ReactUpdateQueue = {
|
65 | |
66 |
|
67 |
|
68 |
|
69 |
|
70 |
|
71 |
|
72 | isMounted: function (publicInstance) {
|
73 | if (process.env.NODE_ENV !== 'production') {
|
74 | var owner = ReactCurrentOwner.current;
|
75 | if (owner !== null) {
|
76 | process.env.NODE_ENV !== 'production' ? warning(owner._warnedAboutRefsInRender, '%s is accessing isMounted inside its render() function. ' + 'render() should be a pure function of props and state. It should ' + 'never access something that requires stale data from the previous ' + 'render, such as refs. Move this logic to componentDidMount and ' + 'componentDidUpdate instead.', owner.getName() || 'A component') : void 0;
|
77 | owner._warnedAboutRefsInRender = true;
|
78 | }
|
79 | }
|
80 | var internalInstance = ReactInstanceMap.get(publicInstance);
|
81 | if (internalInstance) {
|
82 |
|
83 |
|
84 |
|
85 | return !!internalInstance._renderedComponent;
|
86 | } else {
|
87 | return false;
|
88 | }
|
89 | },
|
90 |
|
91 | |
92 |
|
93 |
|
94 |
|
95 |
|
96 |
|
97 |
|
98 |
|
99 |
|
100 | enqueueCallback: function (publicInstance, callback, callerName) {
|
101 | ReactUpdateQueue.validateCallback(callback, callerName);
|
102 | var internalInstance = getInternalInstanceReadyForUpdate(publicInstance);
|
103 |
|
104 |
|
105 |
|
106 |
|
107 |
|
108 |
|
109 | if (!internalInstance) {
|
110 | return null;
|
111 | }
|
112 |
|
113 | if (internalInstance._pendingCallbacks) {
|
114 | internalInstance._pendingCallbacks.push(callback);
|
115 | } else {
|
116 | internalInstance._pendingCallbacks = [callback];
|
117 | }
|
118 |
|
119 |
|
120 |
|
121 |
|
122 | enqueueUpdate(internalInstance);
|
123 | },
|
124 |
|
125 | enqueueCallbackInternal: function (internalInstance, callback) {
|
126 | if (internalInstance._pendingCallbacks) {
|
127 | internalInstance._pendingCallbacks.push(callback);
|
128 | } else {
|
129 | internalInstance._pendingCallbacks = [callback];
|
130 | }
|
131 | enqueueUpdate(internalInstance);
|
132 | },
|
133 |
|
134 | |
135 |
|
136 |
|
137 |
|
138 |
|
139 |
|
140 |
|
141 |
|
142 |
|
143 |
|
144 |
|
145 |
|
146 |
|
147 | enqueueForceUpdate: function (publicInstance) {
|
148 | var internalInstance = getInternalInstanceReadyForUpdate(publicInstance, 'forceUpdate');
|
149 |
|
150 | if (!internalInstance) {
|
151 | return;
|
152 | }
|
153 |
|
154 | internalInstance._pendingForceUpdate = true;
|
155 |
|
156 | enqueueUpdate(internalInstance);
|
157 | },
|
158 |
|
159 | |
160 |
|
161 |
|
162 |
|
163 |
|
164 |
|
165 |
|
166 |
|
167 |
|
168 |
|
169 |
|
170 | enqueueReplaceState: function (publicInstance, completeState, callback) {
|
171 | var internalInstance = getInternalInstanceReadyForUpdate(publicInstance, 'replaceState');
|
172 |
|
173 | if (!internalInstance) {
|
174 | return;
|
175 | }
|
176 |
|
177 | internalInstance._pendingStateQueue = [completeState];
|
178 | internalInstance._pendingReplaceState = true;
|
179 |
|
180 |
|
181 | if (callback !== undefined && callback !== null) {
|
182 | ReactUpdateQueue.validateCallback(callback, 'replaceState');
|
183 | if (internalInstance._pendingCallbacks) {
|
184 | internalInstance._pendingCallbacks.push(callback);
|
185 | } else {
|
186 | internalInstance._pendingCallbacks = [callback];
|
187 | }
|
188 | }
|
189 |
|
190 | enqueueUpdate(internalInstance);
|
191 | },
|
192 |
|
193 | |
194 |
|
195 |
|
196 |
|
197 |
|
198 |
|
199 |
|
200 |
|
201 |
|
202 |
|
203 | enqueueSetState: function (publicInstance, partialState) {
|
204 | if (process.env.NODE_ENV !== 'production') {
|
205 | ReactInstrumentation.debugTool.onSetState();
|
206 | process.env.NODE_ENV !== 'production' ? warning(partialState != null, 'setState(...): You passed an undefined or null state object; ' + 'instead, use forceUpdate().') : void 0;
|
207 | }
|
208 |
|
209 | var internalInstance = getInternalInstanceReadyForUpdate(publicInstance, 'setState');
|
210 |
|
211 | if (!internalInstance) {
|
212 | return;
|
213 | }
|
214 |
|
215 | var queue = internalInstance._pendingStateQueue || (internalInstance._pendingStateQueue = []);
|
216 | queue.push(partialState);
|
217 |
|
218 | enqueueUpdate(internalInstance);
|
219 | },
|
220 |
|
221 | enqueueElementInternal: function (internalInstance, nextElement, nextContext) {
|
222 | internalInstance._pendingElement = nextElement;
|
223 |
|
224 | internalInstance._context = nextContext;
|
225 | enqueueUpdate(internalInstance);
|
226 | },
|
227 |
|
228 | validateCallback: function (callback, callerName) {
|
229 | !(!callback || typeof callback === 'function') ? process.env.NODE_ENV !== 'production' ? invariant(false, '%s(...): Expected the last optional `callback` argument to be a function. Instead received: %s.', callerName, formatUnexpectedArgument(callback)) : _prodInvariant('122', callerName, formatUnexpectedArgument(callback)) : void 0;
|
230 | }
|
231 | };
|
232 |
|
233 | module.exports = ReactUpdateQueue; |
\ | No newline at end of file |