-
Notifications
You must be signed in to change notification settings - Fork 40
Expand file tree
/
Copy pathdebugger.ts
More file actions
86 lines (74 loc) · 2.18 KB
/
Copy pathdebugger.ts
File metadata and controls
86 lines (74 loc) · 2.18 KB
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
import type { Hookable } from "./hookable.ts";
export interface CreateDebuggerOptions {
/** An optional tag to prefix console logs with */
tag?: string;
/**
* Show hook params to the console output
*
* Enabled for browsers by default
*/
inspect?: boolean;
/**
* Use group/groupEnd wrapper around logs happening during a specific hook
*
* Enabled for browsers by default
*/
group?: boolean;
/** Filter which hooks to enable debugger for. Can be a string prefix or fn. */
filter?: string | ((event: string) => boolean);
}
// eslint-disable-next-line unicorn/prefer-global-this
const isBrowser = typeof window !== "undefined";
/** Start debugging hook names and timing in console */
export function createDebugger(
hooks: Hookable<any>,
_options: CreateDebuggerOptions = {},
): {
/** Stop debugging and remove listeners */
close: () => void;
} {
const options = {
inspect: isBrowser,
group: isBrowser,
filter: () => true,
..._options,
} satisfies CreateDebuggerOptions;
const _filter = options.filter;
const filter = typeof _filter === "string" ? (name: string) => name.startsWith(_filter) : _filter;
const _tag = options.tag ? `[${options.tag}] ` : "";
const logPrefix = (event: any) => _tag + event.name + "".padEnd(event._id, "\0");
const _idCtr: Record<string, number> = {};
// Before each
const unsubscribeBefore = hooks.beforeEach((event: any) => {
if (filter !== undefined && !filter(event.name)) {
return;
}
_idCtr[event.name] = _idCtr[event.name] || 0;
event._id = _idCtr[event.name]++;
console.time(logPrefix(event));
});
// After each
const unsubscribeAfter = hooks.afterEach((event) => {
if (filter !== undefined && !filter(event.name)) {
return;
}
if (options.group) {
console.groupCollapsed(event.name);
}
if (options.inspect) {
console.timeLog(logPrefix(event), event.args);
}
console.timeEnd(logPrefix(event));
if (options.group) {
console.groupEnd();
}
_idCtr[event.name]--;
});
return {
/** Stop debugging and remove listeners */
close: () => {
unsubscribeBefore();
unsubscribeAfter();
},
};
}