UNPKG

9 kBJavaScriptView Raw
1/** @license React v0.20.1
2 * scheduler-tracing.development.js
3 *
4 * Copyright (c) Facebook, Inc. and its affiliates.
5 *
6 * This source code is licensed under the MIT license found in the
7 * LICENSE file in the root directory of this source tree.
8 */
9
10'use strict';
11
12if (process.env.NODE_ENV !== "production") {
13 (function() {
14'use strict';
15
16var DEFAULT_THREAD_ID = 0; // Counters used to generate unique IDs.
17
18var interactionIDCounter = 0;
19var threadIDCounter = 0; // Set of currently traced interactions.
20// Interactions "stack"–
21// Meaning that newly traced interactions are appended to the previously active set.
22// When an interaction goes out of scope, the previous set (if any) is restored.
23
24exports.__interactionsRef = null; // Listener(s) to notify when interactions begin and end.
25
26exports.__subscriberRef = null;
27
28{
29 exports.__interactionsRef = {
30 current: new Set()
31 };
32 exports.__subscriberRef = {
33 current: null
34 };
35}
36function unstable_clear(callback) {
37
38 var prevInteractions = exports.__interactionsRef.current;
39 exports.__interactionsRef.current = new Set();
40
41 try {
42 return callback();
43 } finally {
44 exports.__interactionsRef.current = prevInteractions;
45 }
46}
47function unstable_getCurrent() {
48 {
49 return exports.__interactionsRef.current;
50 }
51}
52function unstable_getThreadID() {
53 return ++threadIDCounter;
54}
55function unstable_trace(name, timestamp, callback) {
56 var threadID = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : DEFAULT_THREAD_ID;
57
58 var interaction = {
59 __count: 1,
60 id: interactionIDCounter++,
61 name: name,
62 timestamp: timestamp
63 };
64 var prevInteractions = exports.__interactionsRef.current; // Traced interactions should stack/accumulate.
65 // To do that, clone the current interactions.
66 // The previous set will be restored upon completion.
67
68 var interactions = new Set(prevInteractions);
69 interactions.add(interaction);
70 exports.__interactionsRef.current = interactions;
71 var subscriber = exports.__subscriberRef.current;
72 var returnValue;
73
74 try {
75 if (subscriber !== null) {
76 subscriber.onInteractionTraced(interaction);
77 }
78 } finally {
79 try {
80 if (subscriber !== null) {
81 subscriber.onWorkStarted(interactions, threadID);
82 }
83 } finally {
84 try {
85 returnValue = callback();
86 } finally {
87 exports.__interactionsRef.current = prevInteractions;
88
89 try {
90 if (subscriber !== null) {
91 subscriber.onWorkStopped(interactions, threadID);
92 }
93 } finally {
94 interaction.__count--; // If no async work was scheduled for this interaction,
95 // Notify subscribers that it's completed.
96
97 if (subscriber !== null && interaction.__count === 0) {
98 subscriber.onInteractionScheduledWorkCompleted(interaction);
99 }
100 }
101 }
102 }
103 }
104
105 return returnValue;
106}
107function unstable_wrap(callback) {
108 var threadID = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : DEFAULT_THREAD_ID;
109
110 var wrappedInteractions = exports.__interactionsRef.current;
111 var subscriber = exports.__subscriberRef.current;
112
113 if (subscriber !== null) {
114 subscriber.onWorkScheduled(wrappedInteractions, threadID);
115 } // Update the pending async work count for the current interactions.
116 // Update after calling subscribers in case of error.
117
118
119 wrappedInteractions.forEach(function (interaction) {
120 interaction.__count++;
121 });
122 var hasRun = false;
123
124 function wrapped() {
125 var prevInteractions = exports.__interactionsRef.current;
126 exports.__interactionsRef.current = wrappedInteractions;
127 subscriber = exports.__subscriberRef.current;
128
129 try {
130 var returnValue;
131
132 try {
133 if (subscriber !== null) {
134 subscriber.onWorkStarted(wrappedInteractions, threadID);
135 }
136 } finally {
137 try {
138 returnValue = callback.apply(undefined, arguments);
139 } finally {
140 exports.__interactionsRef.current = prevInteractions;
141
142 if (subscriber !== null) {
143 subscriber.onWorkStopped(wrappedInteractions, threadID);
144 }
145 }
146 }
147
148 return returnValue;
149 } finally {
150 if (!hasRun) {
151 // We only expect a wrapped function to be executed once,
152 // But in the event that it's executed more than once–
153 // Only decrement the outstanding interaction counts once.
154 hasRun = true; // Update pending async counts for all wrapped interactions.
155 // If this was the last scheduled async work for any of them,
156 // Mark them as completed.
157
158 wrappedInteractions.forEach(function (interaction) {
159 interaction.__count--;
160
161 if (subscriber !== null && interaction.__count === 0) {
162 subscriber.onInteractionScheduledWorkCompleted(interaction);
163 }
164 });
165 }
166 }
167 }
168
169 wrapped.cancel = function cancel() {
170 subscriber = exports.__subscriberRef.current;
171
172 try {
173 if (subscriber !== null) {
174 subscriber.onWorkCanceled(wrappedInteractions, threadID);
175 }
176 } finally {
177 // Update pending async counts for all wrapped interactions.
178 // If this was the last scheduled async work for any of them,
179 // Mark them as completed.
180 wrappedInteractions.forEach(function (interaction) {
181 interaction.__count--;
182
183 if (subscriber && interaction.__count === 0) {
184 subscriber.onInteractionScheduledWorkCompleted(interaction);
185 }
186 });
187 }
188 };
189
190 return wrapped;
191}
192
193var subscribers = null;
194
195{
196 subscribers = new Set();
197}
198
199function unstable_subscribe(subscriber) {
200 {
201 subscribers.add(subscriber);
202
203 if (subscribers.size === 1) {
204 exports.__subscriberRef.current = {
205 onInteractionScheduledWorkCompleted: onInteractionScheduledWorkCompleted,
206 onInteractionTraced: onInteractionTraced,
207 onWorkCanceled: onWorkCanceled,
208 onWorkScheduled: onWorkScheduled,
209 onWorkStarted: onWorkStarted,
210 onWorkStopped: onWorkStopped
211 };
212 }
213 }
214}
215function unstable_unsubscribe(subscriber) {
216 {
217 subscribers.delete(subscriber);
218
219 if (subscribers.size === 0) {
220 exports.__subscriberRef.current = null;
221 }
222 }
223}
224
225function onInteractionTraced(interaction) {
226 var didCatchError = false;
227 var caughtError = null;
228 subscribers.forEach(function (subscriber) {
229 try {
230 subscriber.onInteractionTraced(interaction);
231 } catch (error) {
232 if (!didCatchError) {
233 didCatchError = true;
234 caughtError = error;
235 }
236 }
237 });
238
239 if (didCatchError) {
240 throw caughtError;
241 }
242}
243
244function onInteractionScheduledWorkCompleted(interaction) {
245 var didCatchError = false;
246 var caughtError = null;
247 subscribers.forEach(function (subscriber) {
248 try {
249 subscriber.onInteractionScheduledWorkCompleted(interaction);
250 } catch (error) {
251 if (!didCatchError) {
252 didCatchError = true;
253 caughtError = error;
254 }
255 }
256 });
257
258 if (didCatchError) {
259 throw caughtError;
260 }
261}
262
263function onWorkScheduled(interactions, threadID) {
264 var didCatchError = false;
265 var caughtError = null;
266 subscribers.forEach(function (subscriber) {
267 try {
268 subscriber.onWorkScheduled(interactions, threadID);
269 } catch (error) {
270 if (!didCatchError) {
271 didCatchError = true;
272 caughtError = error;
273 }
274 }
275 });
276
277 if (didCatchError) {
278 throw caughtError;
279 }
280}
281
282function onWorkStarted(interactions, threadID) {
283 var didCatchError = false;
284 var caughtError = null;
285 subscribers.forEach(function (subscriber) {
286 try {
287 subscriber.onWorkStarted(interactions, threadID);
288 } catch (error) {
289 if (!didCatchError) {
290 didCatchError = true;
291 caughtError = error;
292 }
293 }
294 });
295
296 if (didCatchError) {
297 throw caughtError;
298 }
299}
300
301function onWorkStopped(interactions, threadID) {
302 var didCatchError = false;
303 var caughtError = null;
304 subscribers.forEach(function (subscriber) {
305 try {
306 subscriber.onWorkStopped(interactions, threadID);
307 } catch (error) {
308 if (!didCatchError) {
309 didCatchError = true;
310 caughtError = error;
311 }
312 }
313 });
314
315 if (didCatchError) {
316 throw caughtError;
317 }
318}
319
320function onWorkCanceled(interactions, threadID) {
321 var didCatchError = false;
322 var caughtError = null;
323 subscribers.forEach(function (subscriber) {
324 try {
325 subscriber.onWorkCanceled(interactions, threadID);
326 } catch (error) {
327 if (!didCatchError) {
328 didCatchError = true;
329 caughtError = error;
330 }
331 }
332 });
333
334 if (didCatchError) {
335 throw caughtError;
336 }
337}
338
339exports.unstable_clear = unstable_clear;
340exports.unstable_getCurrent = unstable_getCurrent;
341exports.unstable_getThreadID = unstable_getThreadID;
342exports.unstable_subscribe = unstable_subscribe;
343exports.unstable_trace = unstable_trace;
344exports.unstable_unsubscribe = unstable_unsubscribe;
345exports.unstable_wrap = unstable_wrap;
346 })();
347}