-
Notifications
You must be signed in to change notification settings - Fork 26.2k
/
Copy pathasync-stack-tagging.ts
61 lines (53 loc) · 1.48 KB
/
async-stack-tagging.ts
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
/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.dev/license
*/
interface ConsoleWithAsyncTagging {
createTask(name: string): ConsoleTask;
}
interface ConsoleTask {
run<T>(f: () => T): T;
}
interface ZoneConsoleTask extends Task {
consoleTask?: ConsoleTask;
}
export class AsyncStackTaggingZoneSpec implements ZoneSpec {
createTask: ConsoleWithAsyncTagging['createTask'];
constructor(
namePrefix: string,
consoleAsyncStackTaggingImpl: ConsoleWithAsyncTagging = console as any,
) {
this.name = 'asyncStackTagging for ' + namePrefix;
this.createTask = consoleAsyncStackTaggingImpl?.createTask ?? (() => null);
}
// ZoneSpec implementation below.
name: string;
onScheduleTask(
delegate: ZoneDelegate,
_current: Zone,
target: Zone,
task: ZoneConsoleTask,
): Task {
task.consoleTask = this.createTask(`Zone - ${task.source || task.type}`);
return delegate.scheduleTask(target, task);
}
onInvokeTask(
delegate: ZoneDelegate,
_currentZone: Zone,
targetZone: Zone,
task: ZoneConsoleTask,
applyThis: any,
applyArgs?: any[],
) {
let ret;
if (task.consoleTask) {
ret = task.consoleTask.run(() => delegate.invokeTask(targetZone, task, applyThis, applyArgs));
} else {
ret = delegate.invokeTask(targetZone, task, applyThis, applyArgs);
}
return ret;
}
}