All files / src/components/m-emitter.vue index.vue

50% Statements 4/8
25% Branches 1/4
0% Functions 0/2
66.66% Lines 4/6

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 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56      7x     7x   7x                                                                                              
<script>
const broadcast = function (condition, eventName, ...args) {
    this.$children.forEach(($child) => {
        if (condition($child))
            $child.$emit(eventName, ...args);
        else
            broadcast.apply($child, [condition, eventName].concat(args));
    });
};

export default {
    name: 'm-emitter',
    methods: {
        $dispatch(condition, eventName, ...args) {
            if (typeof condition === 'string') {
                const name = condition;
                condition = ($parent) => $parent.$options.name === name;
            }
            let $parent = this.$parent || this.$root;
            while ($parent && !condition($parent))
                $parent = $parent.$parent;
            $parent && $parent.$emit(eventName, ...args);
        },
        $broadcast(condition, eventName, ...args) {
            if (typeof condition === 'string') {
                const name = condition;
                condition = ($child) => $child.$options.name === name;
            }
            broadcast.apply(this, [condition, eventName].concat(args));
        },
        $contact(condition, callback) {
            if (typeof condition === 'string') {
                const name = condition;
                condition = ($parent) => $parent.$options.name === name;
            }
            let $parent = this.$parent || this.$root;
            while ($parent && !condition($parent))
                $parent = $parent.$parent;
            return $parent && callback($parent);
        },
        $emitPrevent(name, $event, senderVM, ...args) {
            let cancel = false;
            this.$emit(
                name,
                Object.assign($event || {}, {
                    preventDefault: () => (cancel = true),
                }),
                senderVM,
                ...args,
            );
            return cancel;
        },
    },
};
</script>