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 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 | 7x 7x 7x 7x 7x | <script>
import pubsub from './pubsub';
export const MPublisher = {
created() {
const pubs = this.$options.publish;
if (!pubs)
return;
Object.keys(pubs).forEach((topic) => {
let pub = pubs[topic];
if (typeof pub === 'string' || typeof pub === 'function')
pub = { expOrFn: pub };
this.$watch(pub.expOrFn, (value) => {
pubsub.publish(topic, value);
}, { deep: pub.deep, immediate: pub.immediate });
});
},
beforeDestroy() {
const pubs = this.$options.publish;
if (!pubs)
return;
Object.keys(pubs).forEach((topic) => {
pubsub.unpublish(topic);
});
},
methods: {
$publish(topic, ...args) {
return pubsub.publish(topic, ...args);
},
$unpublish(topic) {
return pubsub.unpublish(topic);
},
},
};
export const MSubscriber = {
created() {
const subs = this.$options.subscribe;
if (!subs)
return;
this.$options.subscriptions = {};
Object.keys(subs).forEach((topic) => {
let sub = subs[topic];
if (typeof sub === 'function')
sub = { handler: sub.bind(this) };
else if (typeof sub === 'string')
sub = { handler: this[sub] };
else
sub.handler = sub.handler.bind(this);
let handler;
if (sub.once) {
handler = () => {
pubsub.unsubscribe(topic, handler);
return sub.handler();
};
} else
handler = sub.handler;
this.$options.subscriptions[topic] = handler;
pubsub.subscribe(topic, handler);
});
},
beforeDestroy() {
const subs = this.$options.subscribe;
if (!subs)
return;
Object.keys(subs).forEach((topic) => {
pubsub.unsubscribe(topic, this.$options.subscriptions[topic]);
});
},
methods: {
$subscribe(topic, func) {
return pubsub.subscribe(topic, func);
},
$unsubscribe(topic, func) {
return pubsub.unsubscribe(topic, func);
},
},
};
export default { mixins: [MSubscriber, MPublisher] };
</script>
|