-
-
Notifications
You must be signed in to change notification settings - Fork 778
/
client.ts
64 lines (56 loc) · 1.5 KB
/
client.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
/// <reference types="vite/client" />
/// <reference lib="dom" />
function post(data: any, config: any) {
return fetch('__POST_PATH__', {
...config,
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(data),
})
}
function include<T>(set: Set<T>, v: T[] | Set<T>) {
for (const i of v)
set.add(i)
}
// eslint-disable-next-line no-console
console.log(
'%c[unocss] devtools support enabled %c\nread more at https://windicss.org',
'background:#0ea5e9; color:white; padding: 1px 4px; border-radius: 3px;',
'',
)
const visitedClasses = new Set()
const pendingClasses = new Set()
let _timer: number | undefined
function schedule() {
if (_timer != null)
clearTimeout(_timer)
_timer = setTimeout(() => {
if (pendingClasses.size) {
post(
{ type: 'add-classes', data: Array.from(pendingClasses) },
{ mode: '__POST_FETCH_MODE__' },
)
include(visitedClasses, pendingClasses)
pendingClasses.clear()
}
}, 10) as any
}
const mutationObserver = new MutationObserver((mutations) => {
mutations.forEach((mutation) => {
if (mutation.attributeName === 'class' && mutation.target) {
Array.from((mutation.target as Element).classList || [])
.forEach((i) => {
if (!visitedClasses.has(i))
pendingClasses.add(i)
})
schedule()
}
})
})
mutationObserver.observe(document.documentElement || document.body, {
childList: true,
subtree: true,
attributes: true,
})