From 7395df376deac945fa8f10cbc05c318b1d8dc931 Mon Sep 17 00:00:00 2001
From: Catalin Mititiuc
Date: Tue, 21 May 2024 21:05:54 -0700
Subject: Remove select callbacks and use observable instead
---
src/modules/observable.js | 19 +++++++++++++++++++
1 file changed, 19 insertions(+)
create mode 100644 src/modules/observable.js
(limited to 'src/modules/observable.js')
diff --git a/src/modules/observable.js b/src/modules/observable.js
new file mode 100644
index 0000000..e4f6797
--- /dev/null
+++ b/src/modules/observable.js
@@ -0,0 +1,19 @@
+const observers = {};
+
+export const Observable = Object.freeze({
+ notify: (event, data) => {
+ observers[event].forEach((observer) => observer(data))
+ },
+
+ subscribe: (event, func) => {
+ (observers[event] ??= []).push(func);
+ },
+
+ unsubscribe: (event, func) => {
+ [...observers[event] || []].forEach((observer, index) => {
+ if (observer === func) {
+ observers[event].splice(index, 1);
+ }
+ });
+ },
+});
--
cgit v1.2.3