-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontext.ts
More file actions
163 lines (148 loc) · 3.58 KB
/
context.ts
File metadata and controls
163 lines (148 loc) · 3.58 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
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
161
162
163
/**
* @fileoverview Elegant theme context management.
* Async-aware theming with automatic context isolation via AsyncLocalStorage.
*/
import type { Theme } from './types'
import { SOCKET_THEME, THEMES, type ThemeName } from './themes'
import { SetCtor } from '../primordials'
let _async_hooks: typeof import('node:async_hooks') | undefined
/**
* Theme change event listener signature.
*/
export type ThemeChangeListener = (theme: Theme) => void
/**
* Emit theme change event to listeners.
* @private
*/
function emitThemeChange(theme: Theme): void {
for (const listener of listeners) {
listener(theme)
}
}
/**
* Lazily load the async_hooks module to avoid Webpack errors.
* @private
*/
/*@__NO_SIDE_EFFECTS__*/
function getAsyncHooks() {
if (_async_hooks === undefined) {
// Use non-'node:' prefixed require to avoid Webpack errors.
_async_hooks = /*@__PURE__*/ require('node:async_hooks')
}
return _async_hooks as typeof import('node:async_hooks')
}
/**
* AsyncLocalStorage for theme context isolation.
*/
const { AsyncLocalStorage } = getAsyncHooks()
const themeStorage = new AsyncLocalStorage<Theme>()
/**
* Fallback theme for global context.
*/
let fallbackTheme: Theme = SOCKET_THEME
/**
* Registered theme change listeners.
*/
const listeners: Set<ThemeChangeListener> = new SetCtor()
/**
* Get the active theme from context.
*
* @returns Current theme
*
* @example
* ```ts
* const theme = getTheme()
* console.log(theme.displayName)
* ```
*/
export function getTheme(): Theme {
return themeStorage.getStore() ?? fallbackTheme
}
/**
* Subscribe to theme change events.
*
* @param listener - Change handler
* @returns Unsubscribe function
*
* @example
* ```ts
* const unsubscribe = onThemeChange((theme) => {
* console.log('Theme:', theme.displayName)
* })
*
* // Cleanup
* unsubscribe()
* ```
*/
export function onThemeChange(listener: ThemeChangeListener): () => void {
listeners.add(listener)
return () => {
listeners.delete(listener)
}
}
/**
* Set the global fallback theme.
*
* @param theme - Theme name or object
*
* @example
* ```ts
* setTheme('socket-firewall')
* ```
*/
export function setTheme(theme: Theme | ThemeName): void {
fallbackTheme =
typeof theme === 'string' ? (THEMES[theme] ?? fallbackTheme) : theme
emitThemeChange(fallbackTheme)
}
/**
* Execute async operation with scoped theme.
* Theme automatically restored on completion.
*
* @template T - Return type
* @param theme - Scoped theme
* @param fn - Async operation
* @returns Operation result
*
* @example
* ```ts
* await withTheme('ultra', async () => {
* // Operations use Ultra theme
* })
* ```
*/
export async function withTheme<T>(
theme: Theme | ThemeName,
fn: () => Promise<T>,
): Promise<T> {
const resolvedTheme: Theme =
typeof theme === 'string' ? (THEMES[theme] ?? fallbackTheme) : theme
return await themeStorage.run(resolvedTheme, async () => {
emitThemeChange(resolvedTheme)
return await fn()
})
}
/**
* Execute sync operation with scoped theme.
* Theme automatically restored on completion.
*
* @template T - Return type
* @param theme - Scoped theme
* @param fn - Sync operation
* @returns Operation result
*
* @example
* ```ts
* const result = withThemeSync('coana', () => {
* return processData()
* })
* ```
*/
export function withThemeSync<T>(theme: Theme | ThemeName, fn: () => T): T {
const resolvedTheme: Theme =
typeof theme === 'string' ? (THEMES[theme] ?? fallbackTheme) : theme
return themeStorage.run(resolvedTheme, () => {
emitThemeChange(resolvedTheme)
return fn()
})
}