MCPcopy Create free account
hub / github.com/ThatOpen/engine_clay / Event

Class Event

packages/clay/src/utils/event.ts:10–42  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

8 * the callback as an arrow function.
9 */
10export class Event<T> {
11 /**
12 * Add a callback to this event instance.
13 * @param handler - the callback to be added to this event.
14 */
15 add(handler: T extends void ? { (): void } : { (data: T): void }): void {
16 this.handlers.push(handler);
17 }
18
19 /**
20 * Removes a callback from this event instance.
21 * @param handler - the callback to be removed from this event.
22 */
23 remove(handler: T extends void ? { (): void } : { (data: T): void }): void {
24 this.handlers = this.handlers.filter((h) => h !== handler);
25 }
26
27 /** Triggers all the callbacks assigned to this event. */
28 trigger = async (data?: T) => {
29 const handlers = this.handlers.slice(0);
30 for (const handler of handlers) {
31 await handler(data as any);
32 }
33 };
34
35 /** Gets rid of all the suscribed events. */
36 reset() {
37 this.handlers.length = 0;
38 }
39
40 private handlers: (T extends void ? { (): void } : { (data: T): void })[] =
41 [];
42}

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected