-
Notifications
You must be signed in to change notification settings - Fork 100
/
async.ts
139 lines (117 loc) · 2.81 KB
/
async.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
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
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
const {
op_async_barrier_create,
op_async_barrier_await,
op_async_yield,
op_async_spin_on_state,
op_stats_capture,
op_stats_diff,
op_stats_dump,
op_stats_delete,
op_async_never_resolves,
} = Deno
.core
.ops;
export function barrierCreate(name: string, count: number) {
op_async_barrier_create(name, count);
}
export function barrierAwait(name: string) {
return op_async_barrier_await(name);
}
export async function asyncYield() {
await op_async_yield();
}
// This function never returns.
export async function asyncSpin() {
await op_async_spin_on_state();
}
export function asyncNeverResolves() {
const prom = op_async_never_resolves();
Deno.core.refOpPromise(prom);
return prom;
}
let nextStats = 0;
export class Stats {
constructor(public name: string) {
op_stats_capture(this.name);
}
dump(): StatsCollection {
return new StatsCollection(op_stats_dump(this.name).active);
}
[Symbol.dispose]() {
op_stats_delete(this.name);
}
}
export class StatsDiff {
#appeared;
#disappeared;
// deno-lint-ignore no-explicit-any
constructor(private diff: any) {
this.#appeared = new StatsCollection(this.diff.appeared);
this.#disappeared = new StatsCollection(this.diff.disappeared);
}
get empty(): boolean {
return this.#appeared.empty && this.#disappeared.empty;
}
get appeared(): StatsCollection {
return this.#appeared;
}
get disappeared(): StatsCollection {
return this.#disappeared;
}
}
export enum LeakType {
AsyncOp = "AsyncOp",
Resource = "Resource",
Timer = "Timer",
Interval = "Interval",
}
// This contains an array of serialized RuntimeActivity structs.
export class StatsCollection {
// deno-lint-ignore no-explicit-any
constructor(private data: any[]) {
console.log(data);
}
count(...types: LeakType[]): number {
let count = 0;
for (const item of this.data) {
for (const type of types) {
if (type in item) {
count++;
}
}
}
return count;
}
countWithTraces(...types: LeakType[]): number {
let count = 0;
for (const item of this.data) {
for (const type of types) {
if (type in item) {
// Make sure it's a non-empty stack trace
if (
typeof item[type][1] === "string" &&
item[type][1].trim().length > 0
) {
count++;
}
}
}
}
return count;
}
get rawData() {
return this.data;
}
get empty(): boolean {
return this.data.length == 0;
}
}
export class StatsFactory {
static capture(): Stats {
return new Stats(`stats-${nextStats++}`);
}
static diff(before: Stats, after: Stats): StatsDiff {
return new StatsDiff(op_stats_diff(before.name, after.name));
}
}