Code coverage report for events.ts

# All files events.ts

100% Statements 59/59
100% Branches 23/23
100% Functions 10/10
100% Lines 59/59

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
    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
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120

    1x
    13x

    1x
    20x
    15x
    15x
    20x
    20x

    20x
    5x
    5x
    4x
    4x
    5x
    20x

    1x
    2x
    1x
    1x
    1x
    2x
    2x
    2x

    1x
    10x
    10x

    6x
    8x
    8x
    8x
    1x
    1x
    6x
    10x

    1x
    3x
    2x
    3x
    1x
    1x
    3x

    1x
    10x
    10x
    10x

    1x
    3x
    3x

    1x
    1x
    1x
    1x

    1x

    1x
    4x
    1x
    1x
    4x
    4x
     /**
     * AuroraView SDK Event System
     *
     * Provides a type-safe event emitter with proper unsubscribe support.
     */

    import type { EventHandler, Unsubscribe } from './types';

    /**
     * Event emitter with unsubscribe support
     */
    export class EventEmitter {
      private handlers = new Map<string, Set<EventHandler>>();

      /**
       * Subscribe to an event
       * @param event - Event name
       * @param handler - Event handler function
       * @returns Unsubscribe function
       */
      on<T = unknown>(event: string, handler: EventHandler<T>): Unsubscribe {
        if (!this.handlers.has(event)) {
          this.handlers.set(event, new Set());
        }
        const handlers = this.handlers.get(event)!;
        handlers.add(handler as EventHandler);

        // Return unsubscribe function
        return () => {
          handlers.delete(handler as EventHandler);
          if (handlers.size === 0) {
            this.handlers.delete(event);
          }
        };
      }

      /**
       * Subscribe to an event once
       * @param event - Event name
       * @param handler - Event handler function
       * @returns Unsubscribe function
       */
      once<T = unknown>(event: string, handler: EventHandler<T>): Unsubscribe {
        const wrapper: EventHandler<T> = (data) => {
          unsubscribe();
          handler(data);
        };
        const unsubscribe = this.on(event, wrapper);
        return unsubscribe;
      }

      /**
       * Emit an event to all handlers
       * @param event - Event name
       * @param data - Event data
       */
      emit<T = unknown>(event: string, data: T): void {
        const handlers = this.handlers.get(event);
        if (!handlers) return;

        handlers.forEach((handler) => {
          try {
            handler(data);
          } catch (e) {
            console.error(`[AuroraView] Error in event handler for "${event}":`, e);
          }
        });
      }

      /**
       * Remove event handler(s)
       * @param event - Event name
       * @param handler - Optional specific handler to remove
       */
      off(event: string, handler?: EventHandler): void {
        if (handler) {
          this.handlers.get(event)?.delete(handler);
        } else {
          this.handlers.delete(event);
        }
      }

      /**
       * Check if event has handlers
       * @param event - Event name
       */
      hasHandlers(event: string): boolean {
        const handlers = this.handlers.get(event);
        return handlers !== undefined && handlers.size > 0;
      }

      /**
       * Get handler count for an event
       * @param event - Event name
       */
      handlerCount(event: string): number {
        return this.handlers.get(event)?.size ?? 0;
      }

      /**
       * Remove all handlers
       */
      clear(): void {
        this.handlers.clear();
      }
    }

    /** Singleton event emitter instance */
    let globalEmitter: EventEmitter | null = null;

    /**
     * Get the global event emitter instance
     */
    export function getGlobalEmitter(): EventEmitter {
      if (!globalEmitter) {
        globalEmitter = new EventEmitter();
      }
      return globalEmitter;
    }