Skip to content

Commit 90979f3

Browse files
authored
Listen once and remove all added
1 parent 29c7300 commit 90979f3

File tree

1 file changed

+33
-29
lines changed

1 file changed

+33
-29
lines changed

src/index.js

Lines changed: 33 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,45 @@
11
function plugin(Vue) {
2-
32
if (plugin.installed) {
43
return;
54
}
6-
75
const events = new Vue({
8-
methods: {
9-
fire(name, data = null) {
10-
this.emit(name, data)
11-
},
12-
emit(name, data = null) {
13-
this.$emit(name, data);
14-
},
15-
listen(name, cb) {
16-
this.on(name, cb)
17-
},
18-
on(name, cb) {
19-
this.$on(name, cb);
20-
},
21-
off(name, cb) {
22-
this.$off(name, cb);
23-
},
24-
unlisten(name, cb) {
25-
this.off(name, cb)
26-
}
27-
}
6+
methods: {
7+
fire (name, data = null) {
8+
this.emit(name, data)
9+
},
10+
emit (name, data = null) {
11+
this.$emit(name, data)
12+
},
13+
listen (name, cb) {
14+
this.on(name, cb)
15+
},
16+
listenOnce (name, cb) {
17+
this.once(name, cb)
18+
},
19+
on (name, cb) {
20+
this.$on(name, cb)
21+
},
22+
once (name, cb) {
23+
this.$once(name, cb)
24+
},
25+
off (name, cb) {
26+
this.$off(name, cb)
27+
},
28+
unlisten (name, cb) {
29+
this.off(name, cb)
30+
},
31+
removeAll () {
32+
this.$off()
33+
}
34+
}
2835
});
29-
3036
Object.defineProperty(Vue.prototype, '$events', {
31-
get() {
32-
return events;
33-
}
37+
get() {
38+
return events;
39+
}
3440
});
3541
}
36-
3742
if (typeof window !== 'undefined' && window.Vue) {
38-
window.Vue.use(plugin);
43+
window.Vue.use(plugin);
3944
}
40-
4145
export default plugin;

0 commit comments

Comments
 (0)