SPB Git

Repository Public

events.ts 2.9 KB 90 lines typescript

1/**2 * =============================================================================3 *                  SP BOUCHER | SOFTWARE & RESEARCH4 *             ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING5 * =============================================================================6 *7 * Project          : KA Maps Engine8 * File             : sdk/web/src/events.ts9 * Module           : sdk10 * Repository       : https://git.spboucher.ai/ka-maps-engine11 * Created          : 2026-12-0112 * Last Modified    : 2026-12-0113 *14 * Author           : Simon-Pierre Boucher15 * Position         : Professor of Finance16 * Institution      : Université du Québec en Outaouais (UQO)17 * Organization     : SP Boucher18 * Website          : https://spboucher.ai19 * Git              : https://git.spboucher.ai20 *21 * Preferred Contact: support@spboucher.ai22 * Development      : dev@spboucher.ai23 * Support          : support@spboucher.ai24 * Security/Admin   : admin@spboucher.ai25 *26 * Copyright (c) 202627 * Simon-Pierre Boucher28 * All rights reserved where applicable. This file is governed by the29 * applicable project license, copyright notices and relevant law.30 * Existing third-party rights and notices remain unaffected.31 *32 * License          : None specified (no license file in this repository)33 *34 * Do not embed credentials, private keys, access tokens,35 * certificates or confidential personal information.36 *37 * =============================================================================38 * END OF SP BOUCHER SOURCE HEADER39 * =============================================================================40 */4142// Petit émetteur typé. Un écouteur qui lève une exception n'empêche pas les suivants.4344export class Emitter<M extends object> {45  private listeners = new Map<keyof M, Set<(e: any) => void>>();4647  on<K extends keyof M>(type: K, fn: (e: M[K] & { type: K }) => void): () => void {48    let set = this.listeners.get(type);49    if (!set) this.listeners.set(type, (set = new Set()));50    set.add(fn);51    return () => this.off(type, fn);52  }5354  once<K extends keyof M>(type: K, fn: (e: M[K] & { type: K }) => void): () => void {55    const off = this.on(type, (e) => {56      off();57      fn(e);58    });59    return off;60  }6162  off<K extends keyof M>(type: K, fn: (e: M[K] & { type: K }) => void): void {63    this.listeners.get(type)?.delete(fn);64  }6566  has(type: keyof M): boolean {67    return (this.listeners.get(type)?.size ?? 0) > 0;68  }6970  emit<K extends keyof M>(type: K, payload: M[K]): void {71    const set = this.listeners.get(type);72    if (!set || set.size === 0) return;73    const event = { ...(payload as object), type } as M[K] & { type: K };74    for (const fn of [...set]) {75      try {76        fn(event);77      } catch (err) {78        // Une erreur d'application ne doit pas arrêter la boucle de la carte.79        queueMicrotask(() => {80          throw err;81        });82      }83    }84  }8586  clear(): void {87    this.listeners.clear();88  }89}90
Files main
Menu

Theme