Home

tailwind-ctp-intellisense @16d9c1c60a14c4b35eb98c8d4bfe8b97ca44673a - refs - log -
-
https://git.jolheiser.com/tailwind-ctp-intellisense.git
Tailwind intellisense + Catppuccin
tailwind-ctp-intellisense / packages / tailwindcss-intellisense / src / lib / emitter.ts
- raw
 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
import mitt from 'mitt'
import { LanguageClient } from 'vscode-languageclient'
import crypto from 'crypto'
import { Connection } from 'vscode-languageserver'

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 | Connection
): 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: Record<string, 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,
  }
}