index : btroops | |
Virtual board game-aid for BattleTroops, an infantry combat simulator wargame published by FASA in 1989. |
aboutsummaryrefslogtreecommitdiff |
diff options
author | Catalin Mititiuc <webdevcat@proton.me> | 2024-05-21 21:05:54 -0700 |
---|---|---|
committer | Catalin Mititiuc <webdevcat@proton.me> | 2024-05-21 21:20:06 -0700 |
commit | 7395df376deac945fa8f10cbc05c318b1d8dc931 (patch) | |
tree | ee3b24de50fbe82a5612a5164a887b697e2fb513 /src/modules/observable.js | |
parent | d01e0aa92bdedce5c03245e583d096ea5ce1753d (diff) |
Remove select callbacks and use observable instead
Diffstat (limited to 'src/modules/observable.js')
-rw-r--r-- | src/modules/observable.js | 19 |
1 files changed, 19 insertions, 0 deletions
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); + } + }); + }, +}); |