diff --git a/.changeset/fresh-seals-enjoy.md b/.changeset/fresh-seals-enjoy.md new file mode 100644 index 000000000..33cf75bdf --- /dev/null +++ b/.changeset/fresh-seals-enjoy.md @@ -0,0 +1,5 @@ +--- +'@signalwire/js': patch +--- + +CF SDK: Expose all events on the client object for internal usage diff --git a/packages/js/src/fabric/SignalWire.test.ts b/packages/js/src/fabric/SignalWire.test.ts index dc11160bb..15d4c516a 100644 --- a/packages/js/src/fabric/SignalWire.test.ts +++ b/packages/js/src/fabric/SignalWire.test.ts @@ -33,6 +33,8 @@ describe('SignalWire', () => { reattach: jest.fn(), handlePushNotification: jest.fn(), updateToken: jest.fn(), + on: jest.fn(), + off: jest.fn(), })) // Mock HTTPClient behavior diff --git a/packages/js/src/fabric/SignalWire.ts b/packages/js/src/fabric/SignalWire.ts index 6743728d1..893cb85e6 100644 --- a/packages/js/src/fabric/SignalWire.ts +++ b/packages/js/src/fabric/SignalWire.ts @@ -53,6 +53,8 @@ export const SignalWire = (() => { join: conversation.joinConversation.bind(conversation), }, // @ts-expect-error For debugging purposes + on: wsClient.on.bind(wsClient), + off: wsClient.off.bind(wsClient), __httpClient: httpClient, __wsClient: wsClient, }) diff --git a/packages/js/src/fabric/WSClient.ts b/packages/js/src/fabric/WSClient.ts index 05a80638c..76b55191b 100644 --- a/packages/js/src/fabric/WSClient.ts +++ b/packages/js/src/fabric/WSClient.ts @@ -2,7 +2,6 @@ import { actions, BaseClient, CallJoinedEventParams, - ClientEvents, VertoBye, VertoSubscribe, VideoRoomSubscribedEventParams, @@ -25,10 +24,7 @@ import { wsClientWorker } from './workers' import { createWSClient } from './createWSClient' import { WSClientContract } from './interfaces/wsClient' -export class WSClient - extends BaseClient - implements WSClientContract -{ +export class WSClient extends BaseClient<{}> implements WSClientContract { private _incomingCallManager: IncomingCallManager private _disconnected: boolean = false @@ -365,10 +361,10 @@ export class WSClient public updateToken(token: string) { return new Promise((resolve, reject) => { - this.once('session.auth_error', (error) => { + this.session.once('session.auth_error', (error) => { reject(error) }) - this.once('session.connected', () => { + this.session.once('session.connected', () => { resolve() }) this.store.dispatch(actions.reauthAction({ token })) diff --git a/packages/js/src/fabric/interfaces/wsClient.ts b/packages/js/src/fabric/interfaces/wsClient.ts index 21801526a..924672586 100644 --- a/packages/js/src/fabric/interfaces/wsClient.ts +++ b/packages/js/src/fabric/interfaces/wsClient.ts @@ -85,11 +85,11 @@ export interface CallParams { disableUdpIceServers?: boolean /** Audio constraints to use when joining the room. Default: `true`. */ audio?: MediaStreamConstraints['audio'] - /** Video constraints to use when joining the room. Default: `true`. */ + /** Video constraints to use when joining the room. Default: `true` for "video" channel. */ video?: MediaStreamConstraints['video'] /** Negotiate the incoming audio from the RTC. Default: `true`. */ negotiateAudio?: boolean - /** Negotiate the incoming video from the RTC. Default: `true`. */ + /** Negotiate the incoming video from the RTC. Default: `true` for "video" channel. */ negotiateVideo?: boolean /** User & UserAgent metadata */ userVariables?: WSClientOptions['userVariables'] diff --git a/packages/js/src/fabric/workers/wsClientWorker.ts b/packages/js/src/fabric/workers/wsClientWorker.ts index f09b26e41..71b832c4d 100644 --- a/packages/js/src/fabric/workers/wsClientWorker.ts +++ b/packages/js/src/fabric/workers/wsClientWorker.ts @@ -21,23 +21,46 @@ export type WSClientWorkerHooks = SDKWorkerHooks< export const wsClientWorker: SDKWorker = function* (options): SagaIterator { getLogger().debug('wsClientWorker started') - const { channels, initialState } = options + const { channels, initialState, instance: client } = options const { swEventChannel } = channels const { handleIncomingInvite } = initialState + function* fireHoseWorker(action: SDKActions) { + // @ts-expect-error Emit all events - This is for internal usage + client.emit(action.type, action.payload) + } + + function* vertoInviteWorker(action: MapToPubSubShape) { + // Invoke WSClient function to build and answer the invite + handleIncomingInvite(action.payload.params) + } + + const isVertoInvite = (action: SDKActions) => { + if (action.type === 'webrtc.message') { + return action.payload.method === 'verto.invite' + } + return false + } + try { while (true) { - const action: MapToPubSubShape = - yield sagaEffects.take(swEventChannel, (action: SDKActions) => { - if (action.type === 'webrtc.message') { - return action.payload.method === 'verto.invite' - } - return false - }) - getLogger().debug('Receiving a new call over WebSocket', action) - - // Invoke WSClient function to build and answer the invite - handleIncomingInvite(action.payload.params) + // Take all actions from the channel + const action: SDKActions = yield sagaEffects.take( + swEventChannel, + () => true + ) + + // Fire all the events with fireHoseWorker + yield sagaEffects.fork(fireHoseWorker, action) + + // If the event is verto.invite, handle that with vertoInviteWorker + if (isVertoInvite(action)) { + getLogger().debug('Receiving a call over WebSocket', action) + yield sagaEffects.fork( + vertoInviteWorker, + action as MapToPubSubShape + ) + } } } finally { getLogger().trace('wsClientWorker ended')