Skip to content

Commit e8491aa

Browse files
authored
Fix types for the echo library (#403)
* Fix wrong channel types * Add generic types * Throw an error when attempting to use encrypted channels with socket.io * Fix types for the function broadcaster * Add generic to channel types * Fix function types * Make connectors generic * Remove new keyword for function broadcasters * Fix tests * Specify return type for function broadcasters * Clean up generics * Remove unnecessary cast * Update quotes * Use NullChannel return type explicitly
1 parent 1d4bef1 commit e8491aa

20 files changed

+157
-86
lines changed

src/channel/channel.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,41 +10,41 @@ export abstract class Channel {
1010
/**
1111
* Listen for an event on the channel instance.
1212
*/
13-
abstract listen(event: string, callback: Function): Channel;
13+
abstract listen(event: string, callback: Function): this;
1414

1515
/**
1616
* Listen for a whisper event on the channel instance.
1717
*/
18-
listenForWhisper(event: string, callback: Function): Channel {
18+
listenForWhisper(event: string, callback: Function): this {
1919
return this.listen('.client-' + event, callback);
2020
}
2121

2222
/**
2323
* Listen for an event on the channel instance.
2424
*/
25-
notification(callback: Function): Channel {
25+
notification(callback: Function): this {
2626
return this.listen('.Illuminate\\Notifications\\Events\\BroadcastNotificationCreated', callback);
2727
}
2828

2929
/**
3030
* Stop listening to an event on the channel instance.
3131
*/
32-
abstract stopListening(event: string, callback?: Function): Channel;
32+
abstract stopListening(event: string, callback?: Function): this;
3333

3434
/**
3535
* Stop listening for a whisper event on the channel instance.
3636
*/
37-
stopListeningForWhisper(event: string, callback?: Function): Channel {
37+
stopListeningForWhisper(event: string, callback?: Function): this {
3838
return this.stopListening('.client-' + event, callback);
3939
}
4040

4141
/**
4242
* Register a callback to be called anytime a subscription succeeds.
4343
*/
44-
abstract subscribed(callback: Function): Channel;
44+
abstract subscribed(callback: Function): this;
4545

4646
/**
4747
* Register a callback to be called anytime an error occurs.
4848
*/
49-
abstract error(callback: Function): Channel;
49+
abstract error(callback: Function): this;
5050
}

src/channel/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,5 @@ export * from './socketio-private-channel';
99
export * from './socketio-presence-channel';
1010
export * from './null-channel';
1111
export * from './null-private-channel';
12+
export * from './null-encrypted-private-channel';
1213
export * from './null-presence-channel';

src/channel/null-channel.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,42 +21,42 @@ export class NullChannel extends Channel {
2121
/**
2222
* Listen for an event on the channel instance.
2323
*/
24-
listen(event: string, callback: Function): NullChannel {
24+
listen(event: string, callback: Function): this {
2525
return this;
2626
}
2727

2828
/**
2929
* Listen for all events on the channel instance.
3030
*/
31-
listenToAll(callback: Function): NullChannel {
31+
listenToAll(callback: Function): this {
3232
return this;
3333
}
3434

3535
/**
3636
* Stop listening for an event on the channel instance.
3737
*/
38-
stopListening(event: string, callback?: Function): NullChannel {
38+
stopListening(event: string, callback?: Function): this {
3939
return this;
4040
}
4141

4242
/**
4343
* Register a callback to be called anytime a subscription succeeds.
4444
*/
45-
subscribed(callback: Function): NullChannel {
45+
subscribed(callback: Function): this {
4646
return this;
4747
}
4848

4949
/**
5050
* Register a callback to be called anytime an error occurs.
5151
*/
52-
error(callback: Function): NullChannel {
52+
error(callback: Function): this {
5353
return this;
5454
}
5555

5656
/**
5757
* Bind a channel to an event.
5858
*/
59-
on(event: string, callback: Function): NullChannel {
59+
on(event: string, callback: Function): this {
6060
return this;
6161
}
6262
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { NullChannel } from './null-channel';
2+
3+
/**
4+
* This class represents a null private channel.
5+
*/
6+
export class NullEncryptedPrivateChannel extends NullChannel {
7+
/**
8+
* Send a whisper event to other clients in the channel.
9+
*/
10+
whisper(eventName: string, data: any): this {
11+
return this;
12+
}
13+
}

src/channel/null-presence-channel.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,35 @@
1-
import { NullChannel } from './null-channel';
1+
import { NullPrivateChannel } from './null-private-channel';
22
import { PresenceChannel } from './presence-channel';
33

44
/**
55
* This class represents a null presence channel.
66
*/
7-
export class NullPresenceChannel extends NullChannel implements PresenceChannel {
7+
export class NullPresenceChannel extends NullPrivateChannel implements PresenceChannel {
88
/**
99
* Register a callback to be called anytime the member list changes.
1010
*/
11-
here(callback: Function): NullPresenceChannel {
11+
here(callback: Function): this {
1212
return this;
1313
}
1414

1515
/**
1616
* Listen for someone joining the channel.
1717
*/
18-
joining(callback: Function): NullPresenceChannel {
18+
joining(callback: Function): this {
1919
return this;
2020
}
2121

2222
/**
2323
* Send a whisper event to other clients in the channel.
2424
*/
25-
whisper(eventName: string, data: any): NullPresenceChannel {
25+
whisper(eventName: string, data: any): this {
2626
return this;
2727
}
2828

2929
/**
3030
* Listen for someone leaving the channel.
3131
*/
32-
leaving(callback: Function): NullPresenceChannel {
32+
leaving(callback: Function): this {
3333
return this;
3434
}
3535
}

src/channel/null-private-channel.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ export class NullPrivateChannel extends NullChannel {
77
/**
88
* Send a whisper event to other clients in the channel.
99
*/
10-
whisper(eventName: string, data: any): NullPrivateChannel {
10+
whisper(eventName: string, data: any): this {
1111
return this;
1212
}
1313
}

src/channel/presence-channel.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,20 @@ export interface PresenceChannel extends Channel {
77
/**
88
* Register a callback to be called anytime the member list changes.
99
*/
10-
here(callback: Function): PresenceChannel;
10+
here(callback: Function): this;
1111

1212
/**
1313
* Listen for someone joining the channel.
1414
*/
15-
joining(callback: Function): PresenceChannel;
15+
joining(callback: Function): this;
1616

1717
/**
1818
* Send a whisper event to other clients in the channel.
1919
*/
20-
whisper(eventName: string, data: any): PresenceChannel;
20+
whisper(eventName: string, data: any): this;
2121

2222
/**
2323
* Listen for someone leaving the channel.
2424
*/
25-
leaving(callback: Function): PresenceChannel;
25+
leaving(callback: Function): this;
2626
}

src/channel/pusher-channel.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ export class PusherChannel extends Channel {
6161
/**
6262
* Listen for an event on the channel instance.
6363
*/
64-
listen(event: string, callback: Function): PusherChannel {
64+
listen(event: string, callback: Function): this {
6565
this.on(this.eventFormatter.format(event), callback);
6666

6767
return this;
@@ -70,7 +70,7 @@ export class PusherChannel extends Channel {
7070
/**
7171
* Listen for all events on the channel instance.
7272
*/
73-
listenToAll(callback: Function): PusherChannel {
73+
listenToAll(callback: Function): this {
7474
this.subscription.bind_global((event, data) => {
7575
if (event.startsWith('pusher:')) {
7676
return;
@@ -89,7 +89,7 @@ export class PusherChannel extends Channel {
8989
/**
9090
* Stop listening for an event on the channel instance.
9191
*/
92-
stopListening(event: string, callback?: Function): PusherChannel {
92+
stopListening(event: string, callback?: Function): this {
9393
if (callback) {
9494
this.subscription.unbind(this.eventFormatter.format(event), callback);
9595
} else {
@@ -102,7 +102,7 @@ export class PusherChannel extends Channel {
102102
/**
103103
* Stop listening for all events on the channel instance.
104104
*/
105-
stopListeningToAll(callback?: Function): PusherChannel {
105+
stopListeningToAll(callback?: Function): this {
106106
if (callback) {
107107
this.subscription.unbind_global(callback);
108108
} else {
@@ -115,7 +115,7 @@ export class PusherChannel extends Channel {
115115
/**
116116
* Register a callback to be called anytime a subscription succeeds.
117117
*/
118-
subscribed(callback: Function): PusherChannel {
118+
subscribed(callback: Function): this {
119119
this.on('pusher:subscription_succeeded', () => {
120120
callback();
121121
});
@@ -126,7 +126,7 @@ export class PusherChannel extends Channel {
126126
/**
127127
* Register a callback to be called anytime a subscription error occurs.
128128
*/
129-
error(callback: Function): PusherChannel {
129+
error(callback: Function): this {
130130
this.on('pusher:subscription_error', (status) => {
131131
callback(status);
132132
});
@@ -137,7 +137,7 @@ export class PusherChannel extends Channel {
137137
/**
138138
* Bind a channel to an event.
139139
*/
140-
on(event: string, callback: Function): PusherChannel {
140+
on(event: string, callback: Function): this {
141141
this.subscription.bind(event, callback);
142142

143143
return this;

src/channel/pusher-encrypted-private-channel.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ export class PusherEncryptedPrivateChannel extends PusherChannel {
77
/**
88
* Send a whisper event to other clients in the channel.
99
*/
10-
whisper(eventName: string, data: any): PusherEncryptedPrivateChannel {
10+
whisper(eventName: string, data: any): this {
1111
this.pusher.channels.channels[this.name].trigger(`client-${eventName}`, data);
1212

1313
return this;

src/channel/pusher-presence-channel.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1-
import { PusherChannel } from './pusher-channel';
21
import { PresenceChannel } from './presence-channel';
2+
import { PusherPrivateChannel } from './pusher-private-channel';
33

44
/**
55
* This class represents a Pusher presence channel.
66
*/
7-
export class PusherPresenceChannel extends PusherChannel implements PresenceChannel {
7+
export class PusherPresenceChannel extends PusherPrivateChannel implements PresenceChannel {
88
/**
99
* Register a callback to be called anytime the member list changes.
1010
*/
11-
here(callback: Function): PusherPresenceChannel {
11+
here(callback: Function): this {
1212
this.on('pusher:subscription_succeeded', (data) => {
1313
callback(Object.keys(data.members).map((k) => data.members[k]));
1414
});
@@ -19,7 +19,7 @@ export class PusherPresenceChannel extends PusherChannel implements PresenceChan
1919
/**
2020
* Listen for someone joining the channel.
2121
*/
22-
joining(callback: Function): PusherPresenceChannel {
22+
joining(callback: Function): this {
2323
this.on('pusher:member_added', (member) => {
2424
callback(member.info);
2525
});
@@ -30,7 +30,7 @@ export class PusherPresenceChannel extends PusherChannel implements PresenceChan
3030
/**
3131
* Send a whisper event to other clients in the channel.
3232
*/
33-
whisper(eventName: string, data: any): PusherPresenceChannel {
33+
whisper(eventName: string, data: any): this {
3434
this.pusher.channels.channels[this.name].trigger(`client-${eventName}`, data);
3535

3636
return this;
@@ -39,7 +39,7 @@ export class PusherPresenceChannel extends PusherChannel implements PresenceChan
3939
/**
4040
* Listen for someone leaving the channel.
4141
*/
42-
leaving(callback: Function): PusherPresenceChannel {
42+
leaving(callback: Function): this {
4343
this.on('pusher:member_removed', (member) => {
4444
callback(member.info);
4545
});

src/channel/pusher-private-channel.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ export class PusherPrivateChannel extends PusherChannel {
77
/**
88
* Send a whisper event to other clients in the channel.
99
*/
10-
whisper(eventName: string, data: any): PusherPrivateChannel {
10+
whisper(eventName: string, data: any): this {
1111
this.pusher.channels.channels[this.name].trigger(`client-${eventName}`, data);
1212

1313
return this;

src/channel/socketio-channel.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ export class SocketIoChannel extends Channel {
7474
/**
7575
* Listen for an event on the channel instance.
7676
*/
77-
listen(event: string, callback: Function): SocketIoChannel {
77+
listen(event: string, callback: Function): this {
7878
this.on(this.eventFormatter.format(event), callback);
7979

8080
return this;
@@ -83,7 +83,7 @@ export class SocketIoChannel extends Channel {
8383
/**
8484
* Stop listening for an event on the channel instance.
8585
*/
86-
stopListening(event: string, callback?: Function): SocketIoChannel {
86+
stopListening(event: string, callback?: Function): this {
8787
this.unbindEvent(this.eventFormatter.format(event), callback);
8888

8989
return this;
@@ -92,7 +92,7 @@ export class SocketIoChannel extends Channel {
9292
/**
9393
* Register a callback to be called anytime a subscription succeeds.
9494
*/
95-
subscribed(callback: Function): SocketIoChannel {
95+
subscribed(callback: Function): this {
9696
this.on('connect', (socket) => {
9797
callback(socket);
9898
});
@@ -103,14 +103,14 @@ export class SocketIoChannel extends Channel {
103103
/**
104104
* Register a callback to be called anytime an error occurs.
105105
*/
106-
error(callback: Function): SocketIoChannel {
106+
error(callback: Function): this {
107107
return this;
108108
}
109109

110110
/**
111111
* Bind the channel's socket to an event and store the callback.
112112
*/
113-
on(event: string, callback: Function): SocketIoChannel {
113+
on(event: string, callback: Function): this {
114114
this.listeners[event] = this.listeners[event] || [];
115115

116116
if (!this.events[event]) {

src/channel/socketio-presence-channel.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ export class SocketIoPresenceChannel extends SocketIoPrivateChannel implements P
88
/**
99
* Register a callback to be called anytime the member list changes.
1010
*/
11-
here(callback: Function): SocketIoPresenceChannel {
11+
here(callback: Function): this {
1212
this.on('presence:subscribed', (members: any[]) => {
1313
callback(members.map((m) => m.user_info));
1414
});
@@ -19,7 +19,7 @@ export class SocketIoPresenceChannel extends SocketIoPrivateChannel implements P
1919
/**
2020
* Listen for someone joining the channel.
2121
*/
22-
joining(callback: Function): SocketIoPresenceChannel {
22+
joining(callback: Function): this {
2323
this.on('presence:joining', (member) => callback(member.user_info));
2424

2525
return this;
@@ -28,7 +28,7 @@ export class SocketIoPresenceChannel extends SocketIoPrivateChannel implements P
2828
/**
2929
* Send a whisper event to other clients in the channel.
3030
*/
31-
whisper(eventName: string, data: any): SocketIoPresenceChannel {
31+
whisper(eventName: string, data: any): this {
3232
this.socket.emit('client event', {
3333
channel: this.name,
3434
event: `client-${eventName}`,
@@ -41,7 +41,7 @@ export class SocketIoPresenceChannel extends SocketIoPrivateChannel implements P
4141
/**
4242
* Listen for someone leaving the channel.
4343
*/
44-
leaving(callback: Function): SocketIoPresenceChannel {
44+
leaving(callback: Function): this {
4545
this.on('presence:leaving', (member) => callback(member.user_info));
4646

4747
return this;

0 commit comments

Comments
 (0)