All files / src/models eventHandlers.ts

100% Statements 10/10
100% Branches 6/6
100% Functions 3/3
100% Lines 10/10

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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  666x     1068x 747x   321x         141x 140x   1x       840x 421x 290x          
export class EventHandlers<Key, Callback extends CallableFunction> {
  private callbacks = new Map<Key, Set<Callback>>();
 
  add(key: Key, callback: Callback): void {
    if (this.callbacks.has(key)) {
      this.callbacks.get(key)!.add(callback);
    } else {
      this.callbacks.set(key, new Set([callback]));
    }
  }
 
  remove(key: Key, callback: Callback): boolean {
    if (this.callbacks.has(key)) {
      return this.callbacks.get(key)!.delete(callback);
    }
    return false;
  }
 
  async call(key: Key, ...args: unknown[]): Promise<void> {
    if (this.callbacks.has(key)) {
      for (const callback of this.callbacks.get(key)!) {
        await callback(...args);
      }
    }
  }
}