forked from jaggy/vue-pusher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
58 lines (50 loc) · 1.25 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
58
/**
* Importing pusher
*/
var Pusher = require('pusher-js');
/**
* VuePusher class.
*
* @param {String} api_key
* @param {Object} options
*/
function VuePusher (Pusher, api_key, options) {
this.pusher = new Pusher(api_key, 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 = function (channel_name, callback) {
var channel = this.pusher.subscribe(channel_name);
if (! this.channels.includes(channel)) {
this.channels.push(channel_name);
}
callback(channel);
};
/**
* Unsubscribe from the given channel.
*
* @param {String} channel
*/
VuePusher.prototype.unsubscribe = function (channel) {
this.pusher.unsubscribe(channel);
};
/**
* Return all the chanels.
*
* @return {Array}
*/
VuePusher.prototype.getChannels = function () {
return this.channels;
};
module.exports = {
install: function (Vue, Pusher, options) {
var pusher = new VuePusher(Pusher, options.api_key, options.options);
Vue.prototype.pusher = pusher;
Vue.prototype.$pusher = pusher.pusher; // Just in case they want to manage it themselves.
}
};