Skip to content

Instantly share code, notes, and snippets.

@alonat
Created July 17, 2020 08:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alonat/e54e9c3943aa8ff2a8fa4930944db209 to your computer and use it in GitHub Desktop.
Save alonat/e54e9c3943aa8ff2a8fa4930944db209 to your computer and use it in GitHub Desktop.
class Event {
constructor() {
this.cb = () => {};
}
on(newEvent, cb) {
const oldCb = this.cb;
this.cb = event => {
oldCb(event);
if (newEvent === event) {
cb();
}
};
}
emit(event) {
this.cb(event);
}
}
const EE = new Event();
EE.on("login", () => console.log("Hi1"));
EE.on("login", () => console.log("Hi2"));
EE.on("login", () => console.log("Hi3"));
EE.on("logout", () => console.log("Bye1"));
EE.on("logout", () => console.log("Bye2"));
EE.on("logout", () => console.log("Bye3"));
EE.emit("login");
EE.emit("logout");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment