MCPcopy Index your code
hub / github.com/coder/code-server / Emitter

Class Emitter

src/common/emitter.ts:20–61  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

18 * Emitter typecasts for a single event type.
19 */
20export class Emitter<T> {
21 private listeners: Array<Callback<T>> = []
22
23 public get event(): Event<T> {
24 return (cb: Callback<T>): Disposable => {
25 this.listeners.push(cb)
26
27 return {
28 dispose: (): void => {
29 const i = this.listeners.indexOf(cb)
30 if (i !== -1) {
31 this.listeners.splice(i, 1)
32 }
33 },
34 }
35 }
36 }
37
38 /**
39 * Emit an event with a value.
40 */
41 public async emit(value: T): Promise<void> {
42 let resolve: () => void
43 const promise = new Promise<void>((r) => (resolve = r))
44
45 await Promise.all(
46 this.listeners.map(async (cb) => {
47 try {
48 await cb(value, promise)
49 } catch (error: any) {
50 logger.error(error.message)
51 }
52 }),
53 )
54
55 resolve!()
56 }
57
58 public dispose(): void {
59 this.listeners = []
60 }
61}

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected