Skip to content

Instantly share code, notes, and snippets.

@eporroa
Created July 28, 2022 21:48
Show Gist options
  • Save eporroa/620e2e146f2332a2eabf8ebe06c8d6d8 to your computer and use it in GitHub Desktop.
Save eporroa/620e2e146f2332a2eabf8ebe06c8d6d8 to your computer and use it in GitHub Desktop.
// helpful stuff for the console
console.clear();
console.log(new Date().toUTCString());
class EventEmitter {
listeners = {};
on(eventName, callbackFn) {
if(!this.listeners[eventName]){
this.listeners[eventName] = [];
}
this.listeners[eventName].push(callbackFn)
}
once(eventName, callbackFn) {
const onceCallback = () => {
callbackFn();
this.off(eventName, onceCallback);
}
this.on(eventName, onceCallback)
}
off(eventName, callbackFn){
if(this.listeners[eventName]){
this.listeners[eventName].forEach( (fn, i) => {
if(fn === callbackFn) {
this.listeners[eventName].splice(i, 1)
}
} )
}
}
emit(eventName, ...rest){
if(this.listeners[eventName]){
this.listeners[eventName].forEach( fn => {
fn(...rest);
} )
}
}
}
const events = new EventEmitter();
const callback1 = (firstName, orderTotal) => {
console.log('do somethign else :(', firstName, orderTotal);
}
const callbackOnce1 = () => { console.log("once") };
events.once('order_cancelled', callbackOnce1);
events.off('order_cancelled', callbackOnce1);
// events.once('order_placed', () => { console.log("once for order placed") });
// events.on('order_cancelled', callback1);
// events.emit('order_cancelled', 'Logan', 123.45)
// events.on('order_cancelled', () => {
// console.log('no order :(');
// events.off('order_cancelled', callback1);
// });
events.emit('order_cancelled')
/*
--- Event Emitter ---
Events are a common and widely used concept in javascript. We are going to build a class that allows users to set callbacks to be executed in response to an emitted "topic". Here is a really basic api description of the class' on and emit functions:
class EventEmitter
// saves the provided callback to be invoked for the specified topic
on(topic:string, callback:Func)
// checks for any callbacks saved for the specified topic and executes the callbacks
emit(topic:string)
Example Usage:
const events = new EventEmitter();
events.on('order_cancelled', () => {
console.log('no order :(');
});
events.on('order_cancelled', () => {
console.log('do somethign else :(');
});
events.on('order_placed', () => {
console.log('order placed');
// send order confirm email
});
// no output
events.emit('order_placed');
// should log to console 'order placed'
Callback order should be maintained
We will start with implementing the two functions above, but feel free to think about what other functionality you would want to see out of the above two functions. Build this like you would as if it would be used in a production application so feel free to use google, stack overflow, mdn and think about what sort of edge cases we should handle.
Codepen's console is kind of glitchy sometimes, so if you have issues with it, try the browser console directly in devtools.
Okay, now let's create a function that will register a callback function to be executed only once in response to a topic. Here is the basic api signature.
once(topic: string, callback: Func)
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment