forked from jaggy/vue-pusher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
57 lines (50 loc) · 1.21 KB
/
index.js
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
/**
* Importing pusher
*/
const Pusher = require('pusher-js');
/**
* VuePusher class.
*
* @param {String} api_key
* @param {Object} options
*/
function VuePusher(apiKey, options) {
this.pusher = new Pusher(apiKey, options);
this.channels = [];
}
/**
* Subscribe to the given channel and give a fallback to bind events to the channel.
*
* @param {String} channel_name
* @param {Function} callback
*/
VuePusher.prototype.subscribe = (channelName, callback) => {
const channel = this.pusher.subscribe(channelName);
if (!this.channels.includes(channel)) {
this.channels.push(channelName);
}
callback(channel);
};
/**
* Unsubscribe from the given channel.
*
* @param {String} channel
*/
VuePusher.prototype.unsubscribe = (channel) => {
this.pusher.unsubscribe(channel);
};
/**
* Return all the chanels.
*
* @return {Array}
*/
VuePusher.prototype.getChannels = () => this.channels;
export default {
install(Vue, options) {
const pusher = new VuePusher(options.api_key, options.options);
// eslint-disable-next-line
Vue.prototype.pusher = pusher;
// eslint-disable-next-line
Vue.prototype.$pusher = pusher.pusher; // Just in case they want to manage it themselves.
},
};