Created
October 16, 2022 13:56
-
-
Save firthous-dev/eb522ce9b10ea2a6b84c2b2060876c4e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const eventMap = new Map(); | |
| export function publish(event){ | |
| let eventName = event.constructor.name | |
| if(eventName && eventMap.has(eventName)){ | |
| eventMap.get(eventName).forEach(listener => listener(event.args)) | |
| } | |
| } | |
| export function subscribe(eventClass, listener){ | |
| let eventName = eventClass.name | |
| if(!eventMap.has(eventName)){ | |
| eventMap.set(eventName, new Set()); | |
| } | |
| eventMap.get(eventName).add(listener); | |
| return{ | |
| unsubscribe: function unsubscribe(){ | |
| eventMap.get(eventName).delete(listener); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment