diff --git a/packages/core/action_cable_compat/index.d.ts b/packages/core/action_cable_compat/index.d.ts index 9086427..fdf7e21 100644 --- a/packages/core/action_cable_compat/index.d.ts +++ b/packages/core/action_cable_compat/index.d.ts @@ -16,6 +16,7 @@ export class ActionCableSubscription { perform(action: string, payload?: object): void send(payload: object): void + whisper(payload: object): void unsubscribe(): void } diff --git a/packages/core/action_cable_compat/index.js b/packages/core/action_cable_compat/index.js index f6469fe..192c296 100644 --- a/packages/core/action_cable_compat/index.js +++ b/packages/core/action_cable_compat/index.js @@ -21,6 +21,10 @@ export class ActionCableSubscription { this.channel.send(data) } + whisper(data) { + return this.channel.whisper(data) + } + get identifier() { return this.channel.identifier } diff --git a/packages/core/create-cable/integration.test.ts b/packages/core/create-cable/integration.test.ts index bb54ab8..db25420 100644 --- a/packages/core/create-cable/integration.test.ts +++ b/packages/core/create-cable/integration.test.ts @@ -19,11 +19,13 @@ class CableTransport extends TestTransport { pongsCount: number = 0 nextSid: string = '' nextRestoredIds: string[] | null = null + whispers: any[] = [] constructor(url: string) { super(url) this.subscriptions = {} + this.whispers = [] } open() { @@ -88,6 +90,11 @@ class CableTransport extends TestTransport { identifier }) } + } else if (command === 'whisper') { + this.whispers.push({ + identifier, + data: msg.data + }) } } @@ -642,4 +649,21 @@ describe('Action Cable compatibility', () => { expect(transport.pongsCount).toEqual(1) }) + + it('whispers on subscription', async () => { + await consumer.ensureActiveConnection() + + let subscription = consumer.subscriptions.create({ + channel: 'TestChannel' + }) + + await subscription.channel.ensureSubscribed() + await subscription.whisper({ event: 'typing', user: 'john' }) + + expect(transport.whispers).toHaveLength(1) + expect(transport.whispers[0]).toMatchObject({ + identifier: subscription.channel.identifier, + data: { event: 'typing', user: 'john' } + }) + }) })