Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class SomeClass{
- private events: Map<string, Set<any>> = new Map()
- on(event: string, callback: (p?: any) => any) {
- if (typeof event !== 'string' || typeof callback !== 'function') {
- return
- }
- if (!this.events.has(event)) {
- this.events.set(event, new Set())
- }
- const e = this.events.get(event)
- !!e && e.add(callback)
- }
- off(event: string, callback?: (p?: any) => any) {
- if (!this.events.has(event) || !this.events.get(event)) {
- return
- }
- if (typeof callback === 'function') {
- const e = this.events.get(event)
- !!e && e.delete(callback)
- } else {
- this.events.delete(event)
- }
- }
- emit(event: string, ...args: any) {
- if (!this.events.has(event)) {
- return
- }
- const e = this.events.get(event)
- !!e &&
- e.forEach((e: (...p: any) => void) => {
- e.apply(null, args)
- })
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement