aboutsummaryrefslogtreecommitdiffstats
path: root/packages/excalidraw/emitter.ts
diff options
context:
space:
mode:
authorkj_sh6042026-03-15 16:19:35 -0400
committerkj_sh6042026-03-15 16:19:35 -0400
commit6ec259a0e71174651bae95d4628138bf6fd68742 (patch)
tree5e33c6a5ec091ecabfcb257fdc7b6a88ed8754ac /packages/excalidraw/emitter.ts
parent16c8578b15c727f22921f8a80a56ee4d4e7f2272 (diff)
refactor: packages/
Diffstat (limited to 'packages/excalidraw/emitter.ts')
-rw-r--r--packages/excalidraw/emitter.ts51
1 files changed, 51 insertions, 0 deletions
diff --git a/packages/excalidraw/emitter.ts b/packages/excalidraw/emitter.ts
new file mode 100644
index 0000000..9382697
--- /dev/null
+++ b/packages/excalidraw/emitter.ts
@@ -0,0 +1,51 @@
+import type { UnsubscribeCallback } from "./types";
+
+type Subscriber<T extends any[]> = (...payload: T) => void;
+
+export class Emitter<T extends any[] = []> {
+ public subscribers: Subscriber<T>[] = [];
+
+ /**
+ * Attaches subscriber
+ *
+ * @returns unsubscribe function
+ */
+ on(...handlers: Subscriber<T>[] | Subscriber<T>[][]): UnsubscribeCallback {
+ const _handlers = handlers
+ .flat()
+ .filter((item) => typeof item === "function");
+
+ this.subscribers.push(..._handlers);
+
+ return () => this.off(_handlers);
+ }
+
+ once(...handlers: Subscriber<T>[] | Subscriber<T>[][]): UnsubscribeCallback {
+ const _handlers = handlers
+ .flat()
+ .filter((item) => typeof item === "function");
+
+ _handlers.push(() => detach());
+
+ const detach = this.on(..._handlers);
+ return detach;
+ }
+
+ off(...handlers: Subscriber<T>[] | Subscriber<T>[][]) {
+ const _handlers = handlers.flat();
+ this.subscribers = this.subscribers.filter(
+ (handler) => !_handlers.includes(handler),
+ );
+ }
+
+ trigger(...payload: T) {
+ for (const handler of this.subscribers) {
+ handler(...payload);
+ }
+ return this;
+ }
+
+ clear() {
+ this.subscribers = [];
+ }
+}