cyp/app/js/lib/pubsub.js

18 lines
548 B
JavaScript
Raw Permalink Normal View History

2019-03-22 22:35:04 +08:00
let storage = new Map();
export function publish(message, publisher, data) {
console.log(message, publisher, data);
if (!storage.has(message)) { return; }
storage.get(message).forEach(listener => listener(message, publisher, data));
}
export function subscribe(message, listener) {
if (!storage.has(message)) { storage.set(message, new Set()); }
storage.get(message).add(listener);
}
export function unsubscribe(message, listener) {
if (!storage.has(message)) { storage.set(message, new Set()); }
storage.get(message).remove(listener);
}