Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const events = {
- topics: {},
- subscribe: function(topic, listener) {
- if(!this.topics.hasOwnProperty(topic)) {
- this.topics = {
- ...this.topics,
- [topic]: []
- }
- }
- let index = this.topics[topic].push(listener) -1;
- const remove = function(topic) {
- delete this.topics[topic][index]
- }.bind(this)
- return remove;
- },
- publish: function(topic, info) {
- this.topics[topic].forEach(fn => {
- fn(info)
- })
- }
- }
- // TEST
- const remove = events.subscribe('test', (info) => console.log(info))
- events.publish('test', '1')
- events.publish('test', '2')
- events.publish('test', '3')
- events.publish('test', '4')
- remove('test')
- events.publish('test', '5')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement