Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | 7x | const topics = {};
export default {
publish(topic, ...args) {
if (!topics[topic])
topics[topic] = [];
else
topics[topic].forEach((func) => func(...args));
// Record the last arguments
topics[topic].lastArgs = args;
},
unpublish(topic) {
if (topics[topic])
delete topics[topic].lastArgs;
},
subscribe(topic, func) {
if (!topics[topic])
topics[topic] = [];
else if ('lastArgs' in topics[topic])
func(...topics[topic].lastArgs);
topics[topic].push(func);
},
unsubscribe(topic, func) {
if (topics[topic])
topics[topic].splice(topics[topic].indexOf(func), 1);
},
};
|