Skip to content

Commit 1d90d7d

Browse files
committed
General maintenance, code styling, and add stopListening() for socket.io
1 parent c829764 commit 1d90d7d

20 files changed

+74
-354
lines changed

package.json

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@
44
"description": "Laravel Echo library for beautiful Pusher and Socket.IO integration",
55
"main": "dist/echo.js",
66
"scripts": {
7+
"build": "npm run compile && npm run declarations",
78
"compile": "./node_modules/.bin/rollup -c",
8-
"prepublish": "npm run compile",
9+
"declarations": "./node_modules/.bin/tsc --emitDeclarationOnly",
10+
"prepublish": "npm run build",
911
"test": "jest"
1012
},
1113
"repository": {
@@ -32,10 +34,12 @@
3234
"babel-preset-stage-2": "^6.5.0",
3335
"jest": "^22.1.0",
3436
"pusher-js": "^3.2.1",
37+
"rollup": "^0.67.3",
3538
"rollup-plugin-babel": "^2.4.0",
36-
"rollup-plugin-typescript": "^0.7.5",
37-
"rollup": "^0.31.0",
38-
"ts-jest": "^22.0.0"
39+
"rollup-plugin-typescript": "^1.0.0",
40+
"ts-jest": "^22.0.0",
41+
"tslib": "^1.9.3",
42+
"typescript": "^3.2.1"
3943
},
4044
"jest": {
4145
"transform": {

rollup.config.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ import typescript from 'rollup-plugin-typescript';
22
import babel from 'rollup-plugin-babel';
33

44
export default {
5-
entry: './src/echo.ts',
6-
dest: './dist/echo.js',
5+
input: './src/echo.ts',
6+
output: [{ file: './dist/echo.js', format: 'esm' }],
77
plugins: [
88
typescript(),
99
babel({

src/channel/channel.ts

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,39 +4,30 @@
44
export abstract class Channel {
55
/**
66
* The Echo options.
7-
*
8-
* @type {any}
97
*/
108
options: any;
119

1210
/**
1311
* Listen for an event on the channel instance.
14-
*
15-
* @param {string} event
16-
* @param {Function} callback
17-
* @return {Channel}
1812
*/
1913
abstract listen(event: string, callback: Function): Channel;
2014

15+
/**
16+
* Listen for a whisper event on the channel instance.
17+
*/
18+
listenForWhisper(event: string, callback: Function): Channel {
19+
return this.listen('.client-' + event, callback);
20+
}
21+
2122
/**
2223
* Listen for an event on the channel instance.
23-
*
24-
* @param {string} event
25-
* @param {Function} callback
26-
* @return {Channel}
2724
*/
2825
notification(callback: Function): Channel {
2926
return this.listen('.Illuminate\\Notifications\\Events\\BroadcastNotificationCreated', callback);
3027
}
3128

3229
/**
33-
* Listen for a whisper event on the channel instance.
34-
*
35-
* @param {string} event
36-
* @param {Function} callback
37-
* @return {Channel}
30+
* Stop listening to an event on the channel instance.
3831
*/
39-
listenForWhisper(event: string, callback: Function): Channel {
40-
return this.listen('.client-' + event, callback);
41-
}
32+
abstract stopListening(event: string): Channel;
4233
}

src/channel/null-channel.ts

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,50 +7,34 @@ import { Channel } from './channel';
77
export class NullChannel extends Channel {
88
/**
99
* Subscribe to a channel.
10-
*
11-
* @param {string} channel
12-
* @return {object}
1310
*/
1411
subscribe(): any {
1512
//
1613
}
1714

1815
/**
1916
* Unsubscribe from a channel.
20-
*
21-
* @return {void}
2217
*/
2318
unsubscribe(): void {
2419
//
2520
}
2621

2722
/**
2823
* Listen for an event on the channel instance.
29-
*
30-
* @param {string} event
31-
* @param {Function} callback
32-
* @return {NullChannel}
3324
*/
3425
listen(event: string, callback: Function): NullChannel {
3526
return this;
3627
}
3728

3829
/**
3930
* Stop listening for an event on the channel instance.
40-
*
41-
* @param {string} event
42-
* @return {NullChannel}
4331
*/
4432
stopListening(event: string): NullChannel {
4533
return this;
4634
}
4735

4836
/**
4937
* Bind a channel to an event.
50-
*
51-
* @param {string} event
52-
* @param {Function} callback
53-
* @return {NullChannel}
5438
*/
5539
on(event: string, callback: Function): NullChannel {
5640
return this;

src/channel/null-presence-channel.ts

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,41 +7,29 @@ import { PresenceChannel } from './presence-channel';
77
export class NullPresenceChannel extends NullChannel implements PresenceChannel {
88
/**
99
* Register a callback to be called anytime the member list changes.
10-
*
11-
* @param {Function} callback
12-
* @return {object} this
1310
*/
14-
here(callback): NullPresenceChannel {
11+
here(callback: Function): NullPresenceChannel {
1512
return this;
1613
}
1714

1815
/**
1916
* Listen for someone joining the channel.
20-
*
21-
* @param {Function} callback
22-
* @return {NullPresenceChannel}
2317
*/
24-
joining(callback): NullPresenceChannel {
18+
joining(callback: Function): NullPresenceChannel {
2519
return this;
2620
}
2721

2822
/**
2923
* Listen for someone leaving the channel.
30-
*
31-
* @param {Function} callback
32-
* @return {NullPresenceChannel}
3324
*/
34-
leaving(callback): NullPresenceChannel {
25+
leaving(callback: Function): NullPresenceChannel {
3526
return this;
3627
}
3728

3829
/**
3930
* Trigger client event on the channel.
40-
*
41-
* @param {Function} callback
42-
* @return {NullPresenceChannel}
4331
*/
44-
whisper(eventName, data): NullPresenceChannel {
32+
whisper(eventName: string, data: any): NullPresenceChannel {
4533
return this;
4634
}
4735
}

src/channel/null-private-channel.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,8 @@ import { NullChannel } from './null-channel';
66
export class NullPrivateChannel extends NullChannel {
77
/**
88
* Trigger client event on the channel.
9-
*
10-
* @param {Function} callback
11-
* @return {NullPrivateChannel}
129
*/
13-
whisper(eventName, data): NullPrivateChannel {
10+
whisper(eventName: string, data: any): NullPrivateChannel {
1411
return this;
1512
}
1613
}

src/channel/presence-channel.ts

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,16 @@
44
export interface PresenceChannel {
55
/**
66
* Register a callback to be called anytime the member list changes.
7-
*
8-
* @param {Function} callback
9-
* @return {object} PresenceChannel
107
*/
118
here(callback: Function): PresenceChannel;
129

1310
/**
1411
* Listen for someone joining the channel.
15-
*
16-
* @param {Function} callback
17-
* @return {PresenceChannel}
1812
*/
1913
joining(callback: Function): PresenceChannel;
2014

2115
/**
2216
* Listen for someone leaving the channel.
23-
*
24-
* @param {Function} callback
25-
* @return {PresenceChannel}
2617
*/
2718
leaving(callback: Function): PresenceChannel;
2819
}

src/channel/pusher-channel.ts

Lines changed: 0 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -7,83 +7,59 @@ import { Channel } from './channel';
77
export class PusherChannel extends Channel {
88
/**
99
* The Pusher client instance.
10-
*
11-
* @type {any}
1210
*/
1311
pusher: any;
1412

1513
/**
1614
* The name of the channel.
17-
*
18-
* @type {object}
1915
*/
2016
name: any;
2117

2218
/**
2319
* Channel options.
24-
*
25-
* @type {any}
2620
*/
2721
options: any;
2822

2923
/**
3024
* The event formatter.
31-
*
32-
* @type {EventFormatter}
3325
*/
3426
eventFormatter: EventFormatter;
3527

3628
/**
3729
* The subsciption of the channel.
38-
*
39-
* @type {any}
4030
*/
4131
subscription: any;
4232

4333
/**
4434
* Create a new class instance.
45-
*
46-
* @param {any} pusher
47-
* @param {object} name
48-
* @param {any} options
4935
*/
5036
constructor(pusher: any, name: any, options: any) {
5137
super();
5238

5339
this.name = name;
5440
this.pusher = pusher;
5541
this.options = options;
56-
5742
this.eventFormatter = new EventFormatter(this.options.namespace);
5843

5944
this.subscribe();
6045
}
6146

6247
/**
6348
* Subscribe to a Pusher channel.
64-
*
65-
* @param {string} channel
66-
* @return {object}
6749
*/
6850
subscribe(): any {
6951
this.subscription = this.pusher.subscribe(this.name);
7052
}
7153

7254
/**
7355
* Unsubscribe from a Pusher channel.
74-
*
75-
* @return {void}
7656
*/
7757
unsubscribe(): void {
7858
this.pusher.unsubscribe(this.name);
7959
}
8060

8161
/**
8262
* Listen for an event on the channel instance.
83-
*
84-
* @param {string} event
85-
* @param {Function} callback
86-
* @return {PusherChannel}
8763
*/
8864
listen(event: string, callback: Function): PusherChannel {
8965
this.on(this.eventFormatter.format(event), callback);
@@ -93,9 +69,6 @@ export class PusherChannel extends Channel {
9369

9470
/**
9571
* Stop listening for an event on the channel instance.
96-
*
97-
* @param {string} event
98-
* @return {PusherChannel}
9972
*/
10073
stopListening(event: string): PusherChannel {
10174
this.subscription.unbind(this.eventFormatter.format(event));
@@ -105,10 +78,6 @@ export class PusherChannel extends Channel {
10578

10679
/**
10780
* Bind a channel to an event.
108-
*
109-
* @param {string} event
110-
* @param {Function} callback
111-
* @return {void}
11281
*/
11382
on(event: string, callback: Function): PusherChannel {
11483
this.subscription.bind(event, callback);

src/channel/pusher-presence-channel.ts

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,8 @@ import { PresenceChannel } from './presence-channel';
77
export class PusherPresenceChannel extends PusherChannel implements PresenceChannel {
88
/**
99
* Register a callback to be called anytime the member list changes.
10-
*
11-
* @param {Function} callback
12-
* @return {object} this
1310
*/
14-
here(callback): PusherPresenceChannel {
11+
here(callback: Function): PusherPresenceChannel {
1512
this.on('pusher:subscription_succeeded', (data) => {
1613
callback(Object.keys(data.members).map(k => data.members[k]));
1714
});
@@ -21,11 +18,8 @@ export class PusherPresenceChannel extends PusherChannel implements PresenceChan
2118

2219
/**
2320
* Listen for someone joining the channel.
24-
*
25-
* @param {Function} callback
26-
* @return {PusherPresenceChannel}
2721
*/
28-
joining(callback): PusherPresenceChannel {
22+
joining(callback: Function): PusherPresenceChannel {
2923
this.on('pusher:member_added', (member) => {
3024
callback(member.info);
3125
});
@@ -35,11 +29,8 @@ export class PusherPresenceChannel extends PusherChannel implements PresenceChan
3529

3630
/**
3731
* Listen for someone leaving the channel.
38-
*
39-
* @param {Function} callback
40-
* @return {PusherPresenceChannel}
4132
*/
42-
leaving(callback): PusherPresenceChannel {
33+
leaving(callback: Function): PusherPresenceChannel {
4334
this.on('pusher:member_removed', (member) => {
4435
callback(member.info);
4536
});
@@ -49,12 +40,10 @@ export class PusherPresenceChannel extends PusherChannel implements PresenceChan
4940

5041
/**
5142
* Trigger client event on the channel.
52-
*
53-
* @param {Function} callback
54-
* @return {PusherPresenceChannel}
5543
*/
56-
whisper(eventName, data): PusherPresenceChannel {
57-
this.pusher.channels.channels[this.name].trigger(`client-${eventName}`, data);
44+
whisper(eventName: string, data: any): PusherPresenceChannel {
45+
this.pusher.channels.channels[this.name]
46+
.trigger(`client-${eventName}`, data);
5847

5948
return this;
6049
}

src/channel/pusher-private-channel.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,10 @@ import { PusherChannel } from './pusher-channel';
66
export class PusherPrivateChannel extends PusherChannel {
77
/**
88
* Trigger client event on the channel.
9-
*
10-
* @param {Function} callback
11-
* @return {PusherPrivateChannel}
129
*/
13-
whisper(eventName, data): PusherPrivateChannel {
14-
this.pusher.channels.channels[this.name].trigger(`client-${eventName}`, data);
10+
whisper(eventName: string, data: any): PusherPrivateChannel {
11+
this.pusher.channels.channels[this.name]
12+
.trigger(`client-${eventName}`, data);
1513

1614
return this;
1715
}

0 commit comments

Comments
 (0)