-
-
Notifications
You must be signed in to change notification settings - Fork 103
Expand file tree
/
Copy pathindex.ts
More file actions
89 lines (70 loc) · 1.76 KB
/
Copy pathindex.ts
File metadata and controls
89 lines (70 loc) · 1.76 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
87
88
89
/* eslint-env node */
// ^^^^ This comment is need to prevent browser bundles of this file
/**
* [[include:src/server/README.md]]
*
* @packageDocumentation
* @module twind/server
*/
import { executionAsyncId, createHook } from 'async_hooks'
import type { Sheet, SheetInit } from 'twind'
import { virtualSheet } from 'twind/sheets'
export * from 'twind'
export * from 'twind/sheets'
export interface AsyncVirtualSheet extends Sheet {
readonly target: readonly string[]
init: SheetInit
reset: () => void
enable: () => void
disable: () => void
}
interface Snapshot {
state: unknown[] | undefined
}
export const asyncVirtualSheet = (): AsyncVirtualSheet => {
const sheet = virtualSheet()
const initial = sheet.reset()
const store = new Map<number, Snapshot>()
const asyncHook = createHook({
init(asyncId, type, triggerAsyncId) {
const snapshot = store.get(triggerAsyncId)
if (snapshot) {
store.set(asyncId, snapshot)
}
},
before(asyncId) {
const snapshot = store.get(asyncId)
if (snapshot) {
sheet.reset(snapshot.state)
}
},
after(asyncId) {
const snapshot = store.get(asyncId)
if (snapshot) {
snapshot.state = sheet.reset(initial)
}
},
destroy(asyncId) {
store.delete(asyncId)
},
}).enable()
return {
get target() {
return sheet.target
},
insert: sheet.insert,
init: sheet.init,
reset: () => {
const asyncId = executionAsyncId()
const snapshot = store.get(asyncId)
if (snapshot) {
snapshot.state = undefined
} else {
store.set(asyncId, { state: undefined })
}
sheet.reset()
},
enable: () => asyncHook.enable(),
disable: () => asyncHook.disable(),
}
}