Created
July 17, 2020 08:20
-
-
Save alonat/e54e9c3943aa8ff2a8fa4930944db209 to your computer and use it in GitHub Desktop.
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
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