type FunctionParameters any> = T extends (...args: infer P) => any ? P : never; // once() wrapper → cb; a property on the wrapper would change its map and slow invoke() const onceTargets = new WeakMap(); export class EventEmitter any> { handlers: Array = []; register(cb: CallbackSignature, once: boolean = false) { this.handlers.push(cb); return this; } invoke(...args: FunctionParameters) { // not forEach: its per-call closure costs 2–8x; handlers added mid-invoke run from the next invoke const handlers = this.handlers; let thrown = false; let error: unknown; for (let i = 0, l = handlers.length; i < l; i++) { // a throwing handler doesn't skip the rest; the first error is rethrown once all have run try { handlers[i].apply(this, args); } catch (e) { if (!thrown) { thrown = true; error = e; } } } if (thrown) { throw error; } } invokeAsync(...args: FunctionParameters) { return Promise.all(this.handlers.map((handler) => handler.apply(this, args))); } remove (cb: CallbackSignature) { let index = this.handlers.indexOf(cb); if (index === -1) { index = this.handlers.findIndex((h) => onceTargets.get(h) === cb); } if (index !== -1) { // copy-on-write: an in-flight invoke() keeps iterating the old array this.handlers = this.handlers.filter((_, i) => i !== index); } } clear() { this.handlers = []; } } export function createSignal void | Promise>() : { once: (cb: CallbackSignature) => void; remove: (cb: CallbackSignature) => void, invoke: (...args: FunctionParameters) => void, invokeAsync: (...args: FunctionParameters) => Promise, clear: () => void, } & ((this: any, cb: CallbackSignature) => EventEmitter ) { const emitter = new EventEmitter(); function register(this: any, cb: CallbackSignature): EventEmitter { return emitter.register(cb, this === null); }; register.once = (cb: CallbackSignature) => { const callback: any = function (this: any, ...args: any[]) { emitter.remove(callback); // first, so a throw or re-entrant invoke can't fire it twice cb.apply(this, args); } onceTargets.set(callback, cb); emitter.register(callback); } register.remove = (cb: CallbackSignature) => emitter.remove(cb) register.invoke = (...args: FunctionParameters) => emitter.invoke(...args); register.invokeAsync = (...args: FunctionParameters) => emitter.invokeAsync(...args); register.clear = () => emitter.clear(); return register; }