Created
March 27, 2020 10:58
-
-
Save satishbabariya/4bf9a757f07b74813dd6dd8d832d6736 to your computer and use it in GitHub Desktop.
Events
This file contains 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
export class Events { | |
static _Listeners = { | |
count: 0, | |
refs: {} | |
}; | |
static addEventListener(eventName: string, callback: () => any) { | |
Events._Listeners.count++; | |
const eventId = "l" + Events._Listeners.count; | |
Events._Listeners.refs[eventId] = { | |
name: eventName, | |
callback | |
}; | |
return eventId; | |
} | |
static removeEventListener(id) { | |
return delete Events._Listeners.refs[id]; | |
} | |
static removeAllListeners() { | |
let removeError = false; | |
Object.keys(Events._Listeners.refs).forEach(_id => { | |
const removed = delete Events._Listeners.refs[_id]; | |
removeError = !removeError ? !removed : removeError; | |
}); | |
return !removeError; | |
} | |
static emit(eventName: string, data: any) { | |
Object.keys(Events._Listeners.refs).forEach(_id => { | |
if ( | |
Events._Listeners.refs[_id] && | |
eventName === Events._Listeners.refs[_id].name | |
) | |
Events._Listeners.refs[_id].callback(data); | |
}); | |
} | |
} |
Author
satishbabariya
commented
Mar 27, 2020
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment