2020-01-09 18:31:03 +01:00
|
|
|
export default class PubSub{
|
|
|
|
constructor() {
|
|
|
|
this.callbacks = {};
|
2020-01-12 05:00:32 +01:00
|
|
|
};
|
2020-01-09 18:31:03 +01:00
|
|
|
|
|
|
|
subscribe(channel, callback) {
|
|
|
|
this.callbacks[channel] = this.callbacks[channel] || [];
|
|
|
|
this.callbacks[channel].push(callback);
|
2020-01-12 05:00:32 +01:00
|
|
|
};
|
2020-01-09 18:31:03 +01:00
|
|
|
|
|
|
|
publish(channel, content) {
|
2020-01-17 02:01:15 +01:00
|
|
|
const callbacks = this.callbacks[channel] || [];
|
2020-01-09 18:31:03 +01:00
|
|
|
for (let clb of callbacks) {
|
|
|
|
new Promise(function(resolve, reject){
|
|
|
|
clb(channel, content);
|
|
|
|
resolve(true);
|
|
|
|
})
|
|
|
|
.catch(console.log);
|
|
|
|
}
|
2020-01-12 05:00:32 +01:00
|
|
|
};
|
2020-01-09 18:31:03 +01:00
|
|
|
|
|
|
|
}
|