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
|
import mitt from 'mitt'
import { LanguageClient } from 'vscode-languageclient'
import crypto from 'crypto'
export interface NotificationEmitter {
on: (name: string, handler: (args: any) => void) => void
off: (name: string, handler: (args: any) => void) => void
emit: (name: string, args: any) => Promise<any>
}
export function createEmitter(client: LanguageClient): NotificationEmitter {
const emitter = mitt()
const registered: string[] = []
const on = (name: string, handler: (args: any) => void) => {
if (!registered.includes(name)) {
registered.push(name)
client.onNotification(`tailwindcss/${name}`, (args) =>
emitter.emit(name, args)
)
}
emitter.on(name, handler)
}
const off = (name: string, handler: (args: any) => void) => {
emitter.off(name, handler)
}
const emit = (name: string, params: any) => {
return new Promise((resolve, _reject) => {
const id = crypto.randomBytes(16).toString('hex')
on(`${name}Response`, (result) => {
const { _id, ...rest } = result
if (_id === id) {
resolve(rest)
}
})
client.sendNotification(`tailwindcss/${name}`, {
_id: id,
...params,
})
})
}
return {
on,
off,
emit,
}
}
|