-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
20 changed files
with
246 additions
and
99 deletions.
There are no files selected for viewing
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,64 +1,75 @@ | ||
export default class EventEmitter { | ||
constructor() { | ||
this.events = {}; | ||
} | ||
constructor() { | ||
this.events = {}; | ||
} | ||
|
||
// Register an event handler | ||
on(event, listener) { | ||
/////console.log('Subscriber', event); | ||
if (!this.events[event]) { | ||
this.events[event] = []; | ||
} | ||
this.events[event].push(listener); | ||
return ()=>this.removeListener(event, listener); | ||
} | ||
stats() { | ||
return Object.fromEntries(Object.entries(this.events).map(([name, listeners])=>[name,listeners.length])) | ||
} | ||
|
||
// Register a one-time event handler | ||
once(event, listener) { | ||
const onceWrapper = (...args) => { | ||
listener(...args); | ||
this.removeListener(event, onceWrapper); | ||
}; | ||
this.on(event, onceWrapper); | ||
// Register an event handler | ||
on(event, listener) { | ||
if (!this.events[event]) { | ||
this.events[event] = []; | ||
} | ||
this.events[event].push(listener); | ||
return () => this.removeListener(event, listener); | ||
} | ||
|
||
// Trigger an event | ||
emit(event, ...args) { | ||
//console.log('Emitting...', event, ...args); | ||
if (this.events[event]) { | ||
this.events[event].forEach(listener => { | ||
listener(...args); | ||
}); | ||
} | ||
} | ||
// Register a one-time event handler | ||
once(event, listener) { | ||
const onceWrapper = (...args) => { | ||
listener(...args); | ||
this.removeListener(event, onceWrapper); | ||
}; | ||
this.on(event, onceWrapper); | ||
} | ||
|
||
// Remove a specific listener | ||
removeListener(event, handlerToRemove) { | ||
if (this.events[event]) { | ||
this.events[event] = this.events[event].filter(handler => handler !== handlerToRemove); | ||
} | ||
// Trigger an event | ||
emit(event, ...args) { | ||
if (this.events[event]) { | ||
this.events[event].forEach((listener) => { | ||
listener(...args); | ||
}); | ||
} | ||
|
||
|
||
// Remove all listeners for a specific event | ||
removeAllListeners(event = null) { | ||
if (event) { | ||
if (this.events[event]) { | ||
delete this.events[event]; | ||
} | ||
} else { | ||
// Remove all listeners for all events | ||
this.events = {}; | ||
} | ||
if (this.events["*"]) { | ||
this.events["*"].forEach((listener) => { | ||
listener(event, ...args); | ||
}); | ||
} | ||
} | ||
|
||
send(...a){ | ||
this.emit(...a) | ||
} | ||
off(...a){ | ||
this.removeListener(...a) | ||
// Remove a specific listener | ||
removeListener(event, handlerToRemove) { | ||
console.log(`removeListener`, event) | ||
if (this.events[event]) { | ||
this.events[event] = this.events[event].filter( | ||
(handler) => handler !== handlerToRemove, | ||
); | ||
} | ||
destroy(){ | ||
this.removeAllListeners(); | ||
} | ||
|
||
// Remove all listeners for a specific event | ||
removeAllListeners(event = null) { | ||
if (event) { | ||
if (this.events[event]) { | ||
console.log(`removeAllListener`, event) | ||
delete this.events[event]; | ||
} | ||
} else { | ||
// Remove all listeners for all events | ||
Object.keys(this.events).map(o=>console.log(`removeAllListener`, o)) | ||
this.events = {}; | ||
} | ||
} | ||
|
||
send(...a) { | ||
this.emit(...a); | ||
} | ||
off(...a) { | ||
this.removeListener(...a); | ||
} | ||
destroy() { | ||
this.removeAllListeners(); | ||
} | ||
} |
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
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
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
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
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
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
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
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
Oops, something went wrong.