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