-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.ts
62 lines (49 loc) · 1.29 KB
/
util.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
// Copyright 2022-2024 Yoshiya Hinosawa. All rights reserved. MIT license.
const READY_STATE_CHANGE = "readystatechange"
let p: Promise<void>
export function documentReady(doc = document) {
p ??= new Promise<void>((resolve) => {
const checkReady = () => {
if (doc.readyState === "complete") {
resolve()
doc.removeEventListener(READY_STATE_CHANGE, checkReady)
}
}
doc.addEventListener(READY_STATE_CHANGE, checkReady)
checkReady()
})
return p
}
interface LogEventMessage {
component: string
e: Event
module: string
color?: string
}
/** Gets the bold colored style */
const boldColor = (color: string): string =>
`color: ${color}; font-weight: bold;`
const defaultEventColor = "#f012be"
declare const __DEV__: boolean
declare const DEBUG_IGNORE: undefined | Set<string>
export function logEvent({
component,
e,
module,
color,
}: LogEventMessage) {
if (typeof __DEV__ === "boolean" && !__DEV__) return
const event = e.type
if (typeof DEBUG_IGNORE === "object" && DEBUG_IGNORE?.has(event)) return
console.groupCollapsed(
`${module}> %c${event}%c on %c${component}`,
boldColor(color || defaultEventColor),
"",
boldColor("#1a80cc"),
)
console.log(e)
if (e.target) {
console.log(e.target)
}
console.groupEnd()
}