All files / src/components/m-pub-sub.vue pubsub.js

6.66% Statements 1/15
0% Branches 0/10
0% Functions 0/5
7.14% Lines 1/14

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 287x                                                      
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);
    },
};