UNPKG

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