-
Notifications
You must be signed in to change notification settings - Fork 47k
/
Tracing.js
267 lines (220 loc) · 7.47 KB
/
Tracing.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow
*/
import {enableSchedulerTracing} from 'shared/ReactFeatureFlags';
export type Interaction = {|
__count: number,
id: number,
name: string,
timestamp: number,
|};
export type Subscriber = {
// A new interaction has been created via the trace() method.
onInteractionTraced: (interaction: Interaction) => void,
// All scheduled async work for an interaction has finished.
onInteractionScheduledWorkCompleted: (interaction: Interaction) => void,
// New async work has been scheduled for a set of interactions.
// When this work is later run, onWorkStarted/onWorkStopped will be called.
// A batch of async/yieldy work may be scheduled multiple times before completing.
// In that case, onWorkScheduled may be called more than once before onWorkStopped.
// Work is scheduled by a "thread" which is identified by a unique ID.
onWorkScheduled: (interactions: Set<Interaction>, threadID: number) => void,
// A batch of scheduled work has been canceled.
// Work is done by a "thread" which is identified by a unique ID.
onWorkCanceled: (interactions: Set<Interaction>, threadID: number) => void,
// A batch of work has started for a set of interactions.
// When this work is complete, onWorkStopped will be called.
// Work is not always completed synchronously; yielding may occur in between.
// A batch of async/yieldy work may also be re-started before completing.
// In that case, onWorkStarted may be called more than once before onWorkStopped.
// Work is done by a "thread" which is identified by a unique ID.
onWorkStarted: (interactions: Set<Interaction>, threadID: number) => void,
// A batch of work has completed for a set of interactions.
// Work is done by a "thread" which is identified by a unique ID.
onWorkStopped: (interactions: Set<Interaction>, threadID: number) => void,
};
export type InteractionsRef = {
current: Set<Interaction>,
};
export type SubscriberRef = {
current: Subscriber | null,
};
const DEFAULT_THREAD_ID = 0;
// Counters used to generate unique IDs.
let interactionIDCounter: number = 0;
let threadIDCounter: number = 0;
// Set of currently traced interactions.
// Interactions "stack"–
// Meaning that newly traced interactions are appended to the previously active set.
// When an interaction goes out of scope, the previous set (if any) is restored.
let interactionsRef: InteractionsRef = (null: any);
// Listener(s) to notify when interactions begin and end.
let subscriberRef: SubscriberRef = (null: any);
if (enableSchedulerTracing) {
interactionsRef = {
current: new Set(),
};
subscriberRef = {
current: null,
};
}
export {interactionsRef as __interactionsRef, subscriberRef as __subscriberRef};
export function unstable_clear(callback: Function): any {
if (!enableSchedulerTracing) {
return callback();
}
const prevInteractions = interactionsRef.current;
interactionsRef.current = new Set();
try {
return callback();
} finally {
interactionsRef.current = prevInteractions;
}
}
export function unstable_getCurrent(): Set<Interaction> | null {
if (!enableSchedulerTracing) {
return null;
} else {
return interactionsRef.current;
}
}
export function unstable_getThreadID(): number {
return ++threadIDCounter;
}
export function unstable_trace(
name: string,
timestamp: number,
callback: Function,
threadID: number = DEFAULT_THREAD_ID,
): any {
if (!enableSchedulerTracing) {
return callback();
}
const interaction: Interaction = {
__count: 1,
id: interactionIDCounter++,
name,
timestamp,
};
const prevInteractions = interactionsRef.current;
// Traced interactions should stack/accumulate.
// To do that, clone the current interactions.
// The previous set will be restored upon completion.
const interactions = new Set(prevInteractions);
interactions.add(interaction);
interactionsRef.current = interactions;
const subscriber = subscriberRef.current;
let returnValue;
try {
if (subscriber !== null) {
subscriber.onInteractionTraced(interaction);
}
} finally {
try {
if (subscriber !== null) {
subscriber.onWorkStarted(interactions, threadID);
}
} finally {
try {
returnValue = callback();
} finally {
interactionsRef.current = prevInteractions;
try {
if (subscriber !== null) {
subscriber.onWorkStopped(interactions, threadID);
}
} finally {
interaction.__count--;
// If no async work was scheduled for this interaction,
// Notify subscribers that it's completed.
if (subscriber !== null && interaction.__count === 0) {
subscriber.onInteractionScheduledWorkCompleted(interaction);
}
}
}
}
}
return returnValue;
}
export function unstable_wrap(
callback: Function,
threadID: number = DEFAULT_THREAD_ID,
): Function {
if (!enableSchedulerTracing) {
return callback;
}
const wrappedInteractions = interactionsRef.current;
let subscriber = subscriberRef.current;
if (subscriber !== null) {
subscriber.onWorkScheduled(wrappedInteractions, threadID);
}
// Update the pending async work count for the current interactions.
// Update after calling subscribers in case of error.
wrappedInteractions.forEach(interaction => {
interaction.__count++;
});
let hasRun = false;
function wrapped() {
const prevInteractions = interactionsRef.current;
interactionsRef.current = wrappedInteractions;
subscriber = subscriberRef.current;
try {
let returnValue;
try {
if (subscriber !== null) {
subscriber.onWorkStarted(wrappedInteractions, threadID);
}
} finally {
try {
returnValue = callback.apply(undefined, arguments);
} finally {
interactionsRef.current = prevInteractions;
if (subscriber !== null) {
subscriber.onWorkStopped(wrappedInteractions, threadID);
}
}
}
return returnValue;
} finally {
if (!hasRun) {
// We only expect a wrapped function to be executed once,
// But in the event that it's executed more than once–
// Only decrement the outstanding interaction counts once.
hasRun = true;
// Update pending async counts for all wrapped interactions.
// If this was the last scheduled async work for any of them,
// Mark them as completed.
wrappedInteractions.forEach(interaction => {
interaction.__count--;
if (subscriber !== null && interaction.__count === 0) {
subscriber.onInteractionScheduledWorkCompleted(interaction);
}
});
}
}
}
wrapped.cancel = function cancel() {
subscriber = subscriberRef.current;
try {
if (subscriber !== null) {
subscriber.onWorkCanceled(wrappedInteractions, threadID);
}
} finally {
// Update pending async counts for all wrapped interactions.
// If this was the last scheduled async work for any of them,
// Mark them as completed.
wrappedInteractions.forEach(interaction => {
interaction.__count--;
if (subscriber && interaction.__count === 0) {
subscriber.onInteractionScheduledWorkCompleted(interaction);
}
});
}
};
return wrapped;
}