-
Notifications
You must be signed in to change notification settings - Fork 22
/
_performance.ts
160 lines (137 loc) · 4.7 KB
/
_performance.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
import { createNotImplementedError } from "../../_internal/utils";
import mock from "../../mock/proxy";
import { _PerformanceMark, _PerformanceMeasure } from "./_entry";
const _timeOrigin = Date.now();
// https://developer.mozilla.org/en-US/docs/Web/API/Performance
export class _Performance<
PerformanceEntryT extends PerformanceEntry = PerformanceEntry,
> implements globalThis.Performance
{
readonly __unenv__ = true;
timeOrigin: number = _timeOrigin;
eventCounts: EventCounts = new Map<string, number>();
_entries: PerformanceEntry[] = [];
_resourceTimingBufferSize = 0;
navigation = mock.__createMock__("PerformanceNavigation");
timing = mock.__createMock__("PerformanceTiming");
onresourcetimingbufferfull: ((this: Performance, ev: Event) => any) | null =
null;
now(): number {
// https://developer.mozilla.org/en-US/docs/Web/API/Performance/now
// Prefer performance.now() if available
if (globalThis?.performance?.now && this.timeOrigin === _timeOrigin) {
return globalThis.performance.now();
}
// performance.now() - (Date.now()-performance.timeOrigin) ~= 0
return Date.now() - this.timeOrigin;
}
clearMarks(markName?: string | undefined): void {
this._entries = markName
? this._entries.filter((e) => e.name !== markName)
: this._entries.filter((e) => e.entryType !== "mark");
}
clearMeasures(measureName?: string | undefined): void {
this._entries = measureName
? this._entries.filter((e) => e.name !== measureName)
: this._entries.filter((e) => e.entryType !== "measure");
}
clearResourceTimings(): void {
this._entries = this._entries.filter(
(e) =>
e.entryType !== "resource" || (e.entryType as any) !== "navigation",
);
}
getEntries(): PerformanceEntryT[] {
return this._entries as PerformanceEntryT[];
}
getEntriesByName(
name: string,
type?: string | undefined,
): PerformanceEntryT[] {
return this._entries.filter(
(e) => e.name === name && (!type || e.entryType === type),
) as PerformanceEntryT[];
}
getEntriesByType(type: string): PerformanceEntryT[] {
return this._entries.filter(
(e) => e.entryType === type,
) as PerformanceEntryT[];
}
mark(
name: string,
options?: PerformanceMarkOptions | undefined,
): PerformanceMark {
const entry = new _PerformanceMark(name, options);
this._entries.push(entry);
return entry;
}
measure(
measureName: string,
startOrMeasureOptions?: string | PerformanceMeasureOptions,
endMark?: string,
): PerformanceMeasure {
let start: number;
let end: number;
if (typeof startOrMeasureOptions === "string") {
start = this.getEntriesByName(startOrMeasureOptions, "mark")[0]
?.startTime;
end = this.getEntriesByName(endMark!, "mark")[0]?.startTime;
} else {
start =
Number.parseFloat(startOrMeasureOptions?.start as string) ||
performance.now();
end =
Number.parseFloat(startOrMeasureOptions?.end as string) ||
performance.now();
}
const entry = new _PerformanceMeasure(measureName, {
startTime: start,
detail: { start, end },
});
this._entries.push(entry);
return entry;
}
setResourceTimingBufferSize(maxSize: number): void {
this._resourceTimingBufferSize = maxSize;
}
toJSON() {
return this;
}
addEventListener<K extends "resourcetimingbufferfull">(
type: K,
listener: (this: Performance, ev: PerformanceEventMap[K]) => any,
options?: boolean | AddEventListenerOptions | undefined,
): void;
addEventListener(
type: string,
listener: EventListenerOrEventListenerObject,
options?: boolean | AddEventListenerOptions | undefined,
): void;
addEventListener(type: unknown, listener: unknown, options?: unknown): void {
throw createNotImplementedError("Performance.addEventListener");
}
removeEventListener<K extends "resourcetimingbufferfull">(
type: K,
listener: (this: Performance, ev: PerformanceEventMap[K]) => any,
options?: boolean | EventListenerOptions | undefined,
): void;
removeEventListener(
type: string,
listener: EventListenerOrEventListenerObject,
options?: boolean | EventListenerOptions | undefined,
): void;
removeEventListener(
type: unknown,
listener: unknown,
options?: unknown,
): void {
throw createNotImplementedError("Performance.removeEventListener");
}
dispatchEvent(event: Event): boolean {
throw createNotImplementedError("Performance.dispatchEvent");
}
}
export const Performance: typeof globalThis.Performance =
globalThis.Performance || _Performance;
export const performance: typeof globalThis.performance =
globalThis.performance || new Performance();