From 8fe3385fb1ea6db961f29fdfd7323d2c591d3ed2 Mon Sep 17 00:00:00 2001 From: mrjvs Date: Fri, 1 Sep 2023 16:50:55 +0200 Subject: [PATCH] make emitter --- src/utils/events.ts | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 src/utils/events.ts diff --git a/src/utils/events.ts b/src/utils/events.ts new file mode 100644 index 00000000..820fea87 --- /dev/null +++ b/src/utils/events.ts @@ -0,0 +1,33 @@ +export type EventMap = Record; +type EventKey = string & keyof T; +type EventReceiver = (params: T) => void; + +export interface Emitter { + on>(eventName: K, fn: EventReceiver): void; + off>(eventName: K, fn: EventReceiver): void; + emit>(eventName: K, params: T[K]): void; +} + +export interface Listener { + on>(eventName: K, fn: EventReceiver): void; +} + +export function makeEmitter(): Emitter { + const listeners: Partial< + Record, ((...params: any[]) => void)[]> + > = {}; + + return { + on(eventName, fn) { + if (!listeners[eventName]) listeners[eventName] = []; + listeners[eventName]?.push(fn); + }, + off(eventName, fn) { + listeners[eventName] = + listeners[eventName]?.filter((v) => v !== fn) ?? []; + }, + emit(eventName, params) { + (listeners[eventName] ?? []).forEach((fn) => fn(params)); + }, + }; +}