Skip to content

Commit 3bd14a4

Browse files
jpcamaraeliias
authored andcommitted
Update to the anycable/core 1.1.4 whisper interface and add informational comment
1 parent d4f2dd0 commit 3bd14a4

File tree

1 file changed

+12
-11
lines changed

1 file changed

+12
-11
lines changed

packages/yrb-actioncable/src/websocket-provider.ts

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,14 @@ enum MessageType {
4747

4848
type MessageHandlers = Record<MessageType, MessageHandler>;
4949

50-
interface SubscriptionWithWhisper extends ActionCable.Channel {
51-
channel: {
52-
whisper(data: any): boolean;
53-
};
50+
// AnyCable is an ActionCable compatible websocket implementation, which offers a "whisper" feature
51+
// for publishing transient broadcasts between clients, without touching your Rails backend
52+
// https://docs.anycable.io/rails/extensions?id=whispering
53+
// It was built with cursor position sharing in mind, and works great for awareness updates
54+
// Make sure to add `whisper: true` to your Rails channels when using this provider and AnyCable
55+
// https://docs.anycable.io/edge/rails/extensions?id=whispering
56+
interface ChannelWithWhisper extends ActionCable.Channel {
57+
whisper(data: any): boolean;
5458
}
5559

5660
const permissionDeniedHandler = (
@@ -219,7 +223,7 @@ export class WebsocketProvider {
219223
const update = encodeBinaryToBase64(buffer);
220224

221225
if (whisper && hasWhisper(this.channel)) {
222-
this.channel.channel.whisper({ update });
226+
this.channel.whisper({ update });
223227
} else {
224228
this.channel?.send({ update });
225229
}
@@ -368,13 +372,10 @@ function decodeBase64ToBinary(update: string) {
368372
return Uint8Array.from(atob(update), c => c.charCodeAt(0));
369373
}
370374

371-
function hasWhisper(channel: ActionCable.Channel | undefined): channel is SubscriptionWithWhisper {
375+
function hasWhisper(channel: ActionCable.Channel | undefined): channel is ChannelWithWhisper {
372376
return (
373377
channel !== undefined &&
374-
'channel' in channel &&
375-
channel.channel !== null &&
376-
typeof channel.channel === 'object' &&
377-
'whisper' in channel.channel &&
378-
typeof channel.channel.whisper === 'function'
378+
'whisper' in channel &&
379+
typeof channel.whisper === 'function'
379380
);
380381
}

0 commit comments

Comments
 (0)