Created
March 14, 2020 15:23
-
-
Save wing-puah/364f377c1275b96a8b29a8972eaa03bb to your computer and use it in GitHub Desktop.
Pub-sub code gist example
This file contains hidden or 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 Cat { | |
| constructor(name, interests) { | |
| this.name = name; | |
| this.interests = interests; | |
| this.unsubscribe = {}; | |
| } | |
| addUnsubscription(keyName, method) { | |
| this.unsubscribe[keyName] = method; | |
| } | |
| } |
This file contains hidden or 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 { PubSub } = require('./PubSub'); | |
| const { Cat } = require('./Cat'); | |
| const catDomPubSub = new PubSub(); | |
| const cat1 = new Cat('Midnight', ['climb trees', 'hunt', 'weather']); | |
| const cat2 = new Cat('Bear', ['humour', 'weather', 'camera skills']); | |
| const cat3 = new Cat('Smokey', ['hunt', 'camera skills']); | |
| const allCat = [cat1, cat2, cat3]; | |
| allCat.forEach((singleCat, idx) => { | |
| const { name, interests } = singleCat; | |
| interests.forEach(interest => { | |
| const { unsubscribe } = catDomPubSub.subscribe(interest, data => | |
| printInterestReceived(name, interest, data), | |
| ); | |
| allCat[idx].addUnsubscription(interest, unsubscribe); | |
| }); | |
| }); | |
| function printInterestReceived(name, interest, data) { | |
| console.log(`${name} has received information for ${interest}: ${data}`); | |
| } | |
| catDomPubSub.publish('climb trees', 'Learn coordination'); | |
| catDomPubSub.publish('weather', 'Might rain tomorrow, stay indoors!'); | |
| catDomPubSub.publish( | |
| 'hunt', | |
| 'Predicted migration of house rats tomorrow, stay alert', | |
| ); | |
| cat1.unsubscribe.hunt(); | |
| catDomPubSub.publish( | |
| 'hunt', | |
| 'Predicted migration of house rats tomorrow, stay alert', | |
| ); |
This file contains hidden or 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 PubSub { | |
| constructor() { | |
| this.subscribers = {}; | |
| } | |
| subscribe(event, callback) { | |
| if (!this.subscribers[event]) { | |
| this.subscribers[event] = []; | |
| } | |
| const index = this.subscribers[event].push(callback) - 1; | |
| const { subscribers } = this; | |
| return { | |
| unsubscribe: function() { | |
| subscribers[event].splice(index, 1); | |
| }, | |
| }; | |
| } | |
| publish(event, data) { | |
| if (!this.subscribers[event]) { | |
| return; | |
| } | |
| this.subscribers[event].forEach(subscriberCallback => | |
| subscriberCallback(data), | |
| ); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment