Skip to content

Commit d414e50

Browse files
committed
Add onChange hook
1 parent ac8ac11 commit d414e50

File tree

6 files changed

+53
-2
lines changed

6 files changed

+53
-2
lines changed

dist/index.esm.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,29 +10,40 @@ function getChannel(channels, channelName) {
1010
return channels[target];
1111
}
1212

13+
const hooks = [];
14+
function addHookFunction(func) {
15+
hooks.push(func);
16+
}
17+
function onChange(...params) {
18+
hooks.forEach(func => func(...params));
19+
}
1320
function join(channelName, options) {
1421
const {
1522
isPrivate
1623
} = options;
1724
console.debug('[Echo] joinChannel -', isPrivate ? 'private-' + channelName : channelName);
1825
isPrivate ? this.private(channelName) : this.channel(channelName);
26+
onChange();
1927
}
2028
function leave(channelName) {
2129
const targetChannel = getChannel(this.connector.channels, channelName);
2230
console.debug('[Echo] leaveChannel -', targetChannel.name);
2331
this.leave(channelName);
32+
onChange();
2433
}
2534
function subscribe(channelName, eventName, callback) {
2635
const targetChannel = getChannel(this.connector.channels, channelName);
2736
targetChannel.listen(eventName, res => {
2837
if (callback) callback(res);
2938
});
3039
console.debug('[Echo] subscribeEvent -', targetChannel.name, eventName);
40+
onChange();
3141
}
3242
function unsubscribe(channelName, eventName) {
3343
const targetChannel = getChannel(this.connector.channels, channelName);
3444
targetChannel.stopListening(eventName);
3545
console.debug('[Echo] unsubscribeEvent -', targetChannel.name, eventName);
46+
onChange();
3647
}
3748
function getChannels() {
3849
return this.connector.channels;
@@ -74,6 +85,7 @@ const Plugin = {
7485
Vue.prototype.$echo = Echo = { ...laravelEcho
7586
};
7687
Object.setPrototypeOf(Echo, {
88+
onChange: addHookFunction,
7789
join: join.bind(laravelEcho),
7890
leave: leave.bind(laravelEcho),
7991
subscribe: subscribe.bind(laravelEcho),

dist/index.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,29 +15,40 @@ var vueEcho = (function (exports, LaravelEcho) {
1515
return channels[target];
1616
}
1717

18+
const hooks = [];
19+
function addHookFunction(func) {
20+
hooks.push(func);
21+
}
22+
function onChange(...params) {
23+
hooks.forEach(func => func(...params));
24+
}
1825
function join(channelName, options) {
1926
const {
2027
isPrivate
2128
} = options;
2229
console.debug('[Echo] joinChannel -', isPrivate ? 'private-' + channelName : channelName);
2330
isPrivate ? this.private(channelName) : this.channel(channelName);
31+
onChange();
2432
}
2533
function leave(channelName) {
2634
const targetChannel = getChannel(this.connector.channels, channelName);
2735
console.debug('[Echo] leaveChannel -', targetChannel.name);
2836
this.leave(channelName);
37+
onChange();
2938
}
3039
function subscribe(channelName, eventName, callback) {
3140
const targetChannel = getChannel(this.connector.channels, channelName);
3241
targetChannel.listen(eventName, res => {
3342
if (callback) callback(res);
3443
});
3544
console.debug('[Echo] subscribeEvent -', targetChannel.name, eventName);
45+
onChange();
3646
}
3747
function unsubscribe(channelName, eventName) {
3848
const targetChannel = getChannel(this.connector.channels, channelName);
3949
targetChannel.stopListening(eventName);
4050
console.debug('[Echo] unsubscribeEvent -', targetChannel.name, eventName);
51+
onChange();
4152
}
4253
function getChannels() {
4354
return this.connector.channels;
@@ -79,6 +90,7 @@ var vueEcho = (function (exports, LaravelEcho) {
7990
Vue.prototype.$echo = exports.Echo = { ...laravelEcho
8091
};
8192
Object.setPrototypeOf(exports.Echo, {
93+
onChange: addHookFunction,
8294
join: join.bind(laravelEcho),
8395
leave: leave.bind(laravelEcho),
8496
subscribe: subscribe.bind(laravelEcho),

dist/index.ssr.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,29 +18,40 @@ function getChannel(channels, channelName) {
1818
return channels[target];
1919
}
2020

21+
const hooks = [];
22+
function addHookFunction(func) {
23+
hooks.push(func);
24+
}
25+
function onChange(...params) {
26+
hooks.forEach(func => func(...params));
27+
}
2128
function join(channelName, options) {
2229
const {
2330
isPrivate
2431
} = options;
2532
console.debug('[Echo] joinChannel -', isPrivate ? 'private-' + channelName : channelName);
2633
isPrivate ? this.private(channelName) : this.channel(channelName);
34+
onChange();
2735
}
2836
function leave(channelName) {
2937
const targetChannel = getChannel(this.connector.channels, channelName);
3038
console.debug('[Echo] leaveChannel -', targetChannel.name);
3139
this.leave(channelName);
40+
onChange();
3241
}
3342
function subscribe(channelName, eventName, callback) {
3443
const targetChannel = getChannel(this.connector.channels, channelName);
3544
targetChannel.listen(eventName, res => {
3645
if (callback) callback(res);
3746
});
3847
console.debug('[Echo] subscribeEvent -', targetChannel.name, eventName);
48+
onChange();
3949
}
4050
function unsubscribe(channelName, eventName) {
4151
const targetChannel = getChannel(this.connector.channels, channelName);
4252
targetChannel.stopListening(eventName);
4353
console.debug('[Echo] unsubscribeEvent -', targetChannel.name, eventName);
54+
onChange();
4455
}
4556
function getChannels() {
4657
return this.connector.channels;
@@ -82,6 +93,7 @@ const Plugin = {
8293
Vue.prototype.$echo = exports.Echo = { ...laravelEcho
8394
};
8495
Object.setPrototypeOf(exports.Echo, {
96+
onChange: addHookFunction,
8597
join: join.bind(laravelEcho),
8698
leave: leave.bind(laravelEcho),
8799
subscribe: subscribe.bind(laravelEcho),

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@phantasweng/vue-echo",
3-
"version": "0.1.2",
3+
"version": "0.2.1",
44
"main": "dist/index.esm.js",
55
"description": "Vue Laravel Echo - Simply import Laravel Echo to Vue instance",
66
"scripts": {

src/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import LaravelEcho from 'laravel-echo'
22
import { getChannel } from './helper'
3-
import { join, leave, subscribe, unsubscribe, getChannels, getEvents } from './methods'
3+
import { addHookFunction, join, leave, subscribe, unsubscribe, getChannels, getEvents } from './methods'
44
window.Pusher = require('pusher-js')
55

66
const defaultOptions = {
@@ -24,6 +24,7 @@ const Plugin = {
2424
...laravelEcho
2525
}
2626
Object.setPrototypeOf(Echo, {
27+
onChange: addHookFunction,
2728
join: join.bind(laravelEcho),
2829
leave: leave.bind(laravelEcho),
2930
subscribe: subscribe.bind(laravelEcho),

src/methods.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,27 @@
11
import { getChannel } from './helper'
22

3+
const hooks = []
4+
5+
export function addHookFunction (func) {
6+
hooks.push(func)
7+
}
8+
9+
export function onChange (...params) {
10+
hooks.forEach(func => func(...params))
11+
}
12+
313
export function join (channelName, options) {
414
const { isPrivate } = options
515
console.debug('[Echo] joinChannel -', isPrivate ? 'private-' + channelName : channelName)
616
isPrivate ? this.private(channelName) : this.channel(channelName)
17+
onChange()
718
}
819

920
export function leave (channelName) {
1021
const targetChannel = getChannel(this.connector.channels, channelName)
1122
console.debug('[Echo] leaveChannel -', targetChannel.name)
1223
this.leave(channelName)
24+
onChange()
1325
}
1426

1527
export function subscribe (channelName, eventName, callback) {
@@ -19,13 +31,15 @@ export function subscribe (channelName, eventName, callback) {
1931
})
2032

2133
console.debug('[Echo] subscribeEvent -', targetChannel.name, eventName)
34+
onChange()
2235
}
2336

2437
export function unsubscribe (channelName, eventName) {
2538
const targetChannel = getChannel(this.connector.channels, channelName)
2639
targetChannel.stopListening(eventName)
2740

2841
console.debug('[Echo] unsubscribeEvent -', targetChannel.name, eventName)
42+
onChange()
2943
}
3044

3145
export function getChannels () {

0 commit comments

Comments
 (0)