Skip to content

Commit

Permalink
Fix types for the echo library (#403)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
dennisprudlo authored Nov 3, 2024
1 parent 1d4bef1 commit e8491aa
Show file tree
Hide file tree
Showing 20 changed files with 157 additions and 86 deletions.
14 changes: 7 additions & 7 deletions src/channel/channel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,41 +10,41 @@ export abstract class Channel {
/**
* Listen for an event on the channel instance.
*/
abstract listen(event: string, callback: Function): Channel;
abstract listen(event: string, callback: Function): this;

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

/**
* Listen for an event on the channel instance.
*/
notification(callback: Function): Channel {
notification(callback: Function): this {
return this.listen('.Illuminate\\Notifications\\Events\\BroadcastNotificationCreated', callback);
}

/**
* Stop listening to an event on the channel instance.
*/
abstract stopListening(event: string, callback?: Function): Channel;
abstract stopListening(event: string, callback?: Function): this;

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

/**
* Register a callback to be called anytime a subscription succeeds.
*/
abstract subscribed(callback: Function): Channel;
abstract subscribed(callback: Function): this;

/**
* Register a callback to be called anytime an error occurs.
*/
abstract error(callback: Function): Channel;
abstract error(callback: Function): this;
}
1 change: 1 addition & 0 deletions src/channel/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ export * from './socketio-private-channel';
export * from './socketio-presence-channel';
export * from './null-channel';
export * from './null-private-channel';
export * from './null-encrypted-private-channel';
export * from './null-presence-channel';
12 changes: 6 additions & 6 deletions src/channel/null-channel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,42 +21,42 @@ export class NullChannel extends Channel {
/**
* Listen for an event on the channel instance.
*/
listen(event: string, callback: Function): NullChannel {
listen(event: string, callback: Function): this {
return this;
}

/**
* Listen for all events on the channel instance.
*/
listenToAll(callback: Function): NullChannel {
listenToAll(callback: Function): this {
return this;
}

/**
* Stop listening for an event on the channel instance.
*/
stopListening(event: string, callback?: Function): NullChannel {
stopListening(event: string, callback?: Function): this {
return this;
}

/**
* Register a callback to be called anytime a subscription succeeds.
*/
subscribed(callback: Function): NullChannel {
subscribed(callback: Function): this {
return this;
}

/**
* Register a callback to be called anytime an error occurs.
*/
error(callback: Function): NullChannel {
error(callback: Function): this {
return this;
}

/**
* Bind a channel to an event.
*/
on(event: string, callback: Function): NullChannel {
on(event: string, callback: Function): this {
return this;
}
}
13 changes: 13 additions & 0 deletions src/channel/null-encrypted-private-channel.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { NullChannel } from './null-channel';

/**
* This class represents a null private channel.
*/
export class NullEncryptedPrivateChannel extends NullChannel {
/**
* Send a whisper event to other clients in the channel.
*/
whisper(eventName: string, data: any): this {
return this;
}
}
12 changes: 6 additions & 6 deletions src/channel/null-presence-channel.ts
Original file line number Diff line number Diff line change
@@ -1,35 +1,35 @@
import { NullChannel } from './null-channel';
import { NullPrivateChannel } from './null-private-channel';
import { PresenceChannel } from './presence-channel';

/**
* This class represents a null presence channel.
*/
export class NullPresenceChannel extends NullChannel implements PresenceChannel {
export class NullPresenceChannel extends NullPrivateChannel implements PresenceChannel {
/**
* Register a callback to be called anytime the member list changes.
*/
here(callback: Function): NullPresenceChannel {
here(callback: Function): this {
return this;
}

/**
* Listen for someone joining the channel.
*/
joining(callback: Function): NullPresenceChannel {
joining(callback: Function): this {
return this;
}

/**
* Send a whisper event to other clients in the channel.
*/
whisper(eventName: string, data: any): NullPresenceChannel {
whisper(eventName: string, data: any): this {
return this;
}

/**
* Listen for someone leaving the channel.
*/
leaving(callback: Function): NullPresenceChannel {
leaving(callback: Function): this {
return this;
}
}
2 changes: 1 addition & 1 deletion src/channel/null-private-channel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export class NullPrivateChannel extends NullChannel {
/**
* Send a whisper event to other clients in the channel.
*/
whisper(eventName: string, data: any): NullPrivateChannel {
whisper(eventName: string, data: any): this {
return this;
}
}
8 changes: 4 additions & 4 deletions src/channel/presence-channel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,20 @@ export interface PresenceChannel extends Channel {
/**
* Register a callback to be called anytime the member list changes.
*/
here(callback: Function): PresenceChannel;
here(callback: Function): this;

/**
* Listen for someone joining the channel.
*/
joining(callback: Function): PresenceChannel;
joining(callback: Function): this;

/**
* Send a whisper event to other clients in the channel.
*/
whisper(eventName: string, data: any): PresenceChannel;
whisper(eventName: string, data: any): this;

/**
* Listen for someone leaving the channel.
*/
leaving(callback: Function): PresenceChannel;
leaving(callback: Function): this;
}
14 changes: 7 additions & 7 deletions src/channel/pusher-channel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ export class PusherChannel extends Channel {
/**
* Listen for an event on the channel instance.
*/
listen(event: string, callback: Function): PusherChannel {
listen(event: string, callback: Function): this {
this.on(this.eventFormatter.format(event), callback);

return this;
Expand All @@ -70,7 +70,7 @@ export class PusherChannel extends Channel {
/**
* Listen for all events on the channel instance.
*/
listenToAll(callback: Function): PusherChannel {
listenToAll(callback: Function): this {
this.subscription.bind_global((event, data) => {
if (event.startsWith('pusher:')) {
return;
Expand All @@ -89,7 +89,7 @@ export class PusherChannel extends Channel {
/**
* Stop listening for an event on the channel instance.
*/
stopListening(event: string, callback?: Function): PusherChannel {
stopListening(event: string, callback?: Function): this {
if (callback) {
this.subscription.unbind(this.eventFormatter.format(event), callback);
} else {
Expand All @@ -102,7 +102,7 @@ export class PusherChannel extends Channel {
/**
* Stop listening for all events on the channel instance.
*/
stopListeningToAll(callback?: Function): PusherChannel {
stopListeningToAll(callback?: Function): this {
if (callback) {
this.subscription.unbind_global(callback);
} else {
Expand All @@ -115,7 +115,7 @@ export class PusherChannel extends Channel {
/**
* Register a callback to be called anytime a subscription succeeds.
*/
subscribed(callback: Function): PusherChannel {
subscribed(callback: Function): this {
this.on('pusher:subscription_succeeded', () => {
callback();
});
Expand All @@ -126,7 +126,7 @@ export class PusherChannel extends Channel {
/**
* Register a callback to be called anytime a subscription error occurs.
*/
error(callback: Function): PusherChannel {
error(callback: Function): this {
this.on('pusher:subscription_error', (status) => {
callback(status);
});
Expand All @@ -137,7 +137,7 @@ export class PusherChannel extends Channel {
/**
* Bind a channel to an event.
*/
on(event: string, callback: Function): PusherChannel {
on(event: string, callback: Function): this {
this.subscription.bind(event, callback);

return this;
Expand Down
2 changes: 1 addition & 1 deletion src/channel/pusher-encrypted-private-channel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export class PusherEncryptedPrivateChannel extends PusherChannel {
/**
* Send a whisper event to other clients in the channel.
*/
whisper(eventName: string, data: any): PusherEncryptedPrivateChannel {
whisper(eventName: string, data: any): this {
this.pusher.channels.channels[this.name].trigger(`client-${eventName}`, data);

return this;
Expand Down
12 changes: 6 additions & 6 deletions src/channel/pusher-presence-channel.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { PusherChannel } from './pusher-channel';
import { PresenceChannel } from './presence-channel';
import { PusherPrivateChannel } from './pusher-private-channel';

/**
* This class represents a Pusher presence channel.
*/
export class PusherPresenceChannel extends PusherChannel implements PresenceChannel {
export class PusherPresenceChannel extends PusherPrivateChannel implements PresenceChannel {
/**
* Register a callback to be called anytime the member list changes.
*/
here(callback: Function): PusherPresenceChannel {
here(callback: Function): this {
this.on('pusher:subscription_succeeded', (data) => {
callback(Object.keys(data.members).map((k) => data.members[k]));
});
Expand All @@ -19,7 +19,7 @@ export class PusherPresenceChannel extends PusherChannel implements PresenceChan
/**
* Listen for someone joining the channel.
*/
joining(callback: Function): PusherPresenceChannel {
joining(callback: Function): this {
this.on('pusher:member_added', (member) => {
callback(member.info);
});
Expand All @@ -30,7 +30,7 @@ export class PusherPresenceChannel extends PusherChannel implements PresenceChan
/**
* Send a whisper event to other clients in the channel.
*/
whisper(eventName: string, data: any): PusherPresenceChannel {
whisper(eventName: string, data: any): this {
this.pusher.channels.channels[this.name].trigger(`client-${eventName}`, data);

return this;
Expand All @@ -39,7 +39,7 @@ export class PusherPresenceChannel extends PusherChannel implements PresenceChan
/**
* Listen for someone leaving the channel.
*/
leaving(callback: Function): PusherPresenceChannel {
leaving(callback: Function): this {
this.on('pusher:member_removed', (member) => {
callback(member.info);
});
Expand Down
2 changes: 1 addition & 1 deletion src/channel/pusher-private-channel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export class PusherPrivateChannel extends PusherChannel {
/**
* Send a whisper event to other clients in the channel.
*/
whisper(eventName: string, data: any): PusherPrivateChannel {
whisper(eventName: string, data: any): this {
this.pusher.channels.channels[this.name].trigger(`client-${eventName}`, data);

return this;
Expand Down
10 changes: 5 additions & 5 deletions src/channel/socketio-channel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ export class SocketIoChannel extends Channel {
/**
* Listen for an event on the channel instance.
*/
listen(event: string, callback: Function): SocketIoChannel {
listen(event: string, callback: Function): this {
this.on(this.eventFormatter.format(event), callback);

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

return this;
Expand All @@ -92,7 +92,7 @@ export class SocketIoChannel extends Channel {
/**
* Register a callback to be called anytime a subscription succeeds.
*/
subscribed(callback: Function): SocketIoChannel {
subscribed(callback: Function): this {
this.on('connect', (socket) => {
callback(socket);
});
Expand All @@ -103,14 +103,14 @@ export class SocketIoChannel extends Channel {
/**
* Register a callback to be called anytime an error occurs.
*/
error(callback: Function): SocketIoChannel {
error(callback: Function): this {
return this;
}

/**
* Bind the channel's socket to an event and store the callback.
*/
on(event: string, callback: Function): SocketIoChannel {
on(event: string, callback: Function): this {
this.listeners[event] = this.listeners[event] || [];

if (!this.events[event]) {
Expand Down
8 changes: 4 additions & 4 deletions src/channel/socketio-presence-channel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export class SocketIoPresenceChannel extends SocketIoPrivateChannel implements P
/**
* Register a callback to be called anytime the member list changes.
*/
here(callback: Function): SocketIoPresenceChannel {
here(callback: Function): this {
this.on('presence:subscribed', (members: any[]) => {
callback(members.map((m) => m.user_info));
});
Expand All @@ -19,7 +19,7 @@ export class SocketIoPresenceChannel extends SocketIoPrivateChannel implements P
/**
* Listen for someone joining the channel.
*/
joining(callback: Function): SocketIoPresenceChannel {
joining(callback: Function): this {
this.on('presence:joining', (member) => callback(member.user_info));

return this;
Expand All @@ -28,7 +28,7 @@ export class SocketIoPresenceChannel extends SocketIoPrivateChannel implements P
/**
* Send a whisper event to other clients in the channel.
*/
whisper(eventName: string, data: any): SocketIoPresenceChannel {
whisper(eventName: string, data: any): this {
this.socket.emit('client event', {
channel: this.name,
event: `client-${eventName}`,
Expand All @@ -41,7 +41,7 @@ export class SocketIoPresenceChannel extends SocketIoPrivateChannel implements P
/**
* Listen for someone leaving the channel.
*/
leaving(callback: Function): SocketIoPresenceChannel {
leaving(callback: Function): this {
this.on('presence:leaving', (member) => callback(member.user_info));

return this;
Expand Down
Loading

0 comments on commit e8491aa

Please sign in to comment.