Last active
April 18, 2022 22:24
-
-
Save frnkq/39a71f423a76c83e4c454c28eb907ed4 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
const event = { | |
value: null, | |
subscriptions: [], | |
subscribe: function (callback) { | |
this.subscriptions.push(callback); | |
}, | |
next: function (value) { | |
this.value = value; | |
this.subscriptions.forEach((subscription) => { | |
subscription(value); | |
}); | |
}, | |
}; | |
const moduleA = () => { | |
event.subscribe((value) => { | |
console.log("Hello from module A, " + value); | |
}); | |
}; | |
const moduleB = () => { | |
event.subscribe((value) => { | |
console.log("Hello from module B, " + value); | |
}); | |
}; | |
const a = moduleA(); | |
const b = moduleB(); | |
for (let i = 0; i < 10; i++) { | |
event.next(i); | |
} | |
/* Output: | |
Hello from module A, 0 | |
Hello from module B, 0 | |
Hello from module A, 1 | |
Hello from module B, 1 | |
Hello from module A, 2 | |
Hello from module B, 2 | |
Hello from module A, 3 | |
Hello from module B, 3 | |
Hello from module A, 4 | |
Hello from module B, 4 | |
Hello from module A, 5 | |
Hello from module B, 5 | |
Hello from module A, 6 | |
Hello from module B, 6 | |
Hello from module A, 7 | |
Hello from module B, 7 | |
Hello from module A, 8 | |
Hello from module B, 8 | |
Hello from module A, 9 | |
Hello from module B, 9 | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment