Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CF SDK: Expose events on the client object #1166

Merged
merged 3 commits into from
Feb 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/fresh-seals-enjoy.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@signalwire/js': patch
---

CF SDK: Expose all events on the client object for internal usage
2 changes: 2 additions & 0 deletions packages/js/src/fabric/SignalWire.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ describe('SignalWire', () => {
reattach: jest.fn(),
handlePushNotification: jest.fn(),
updateToken: jest.fn(),
on: jest.fn(),
off: jest.fn(),
}))

// Mock HTTPClient behavior
Expand Down
2 changes: 2 additions & 0 deletions packages/js/src/fabric/SignalWire.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
})
Expand Down
10 changes: 3 additions & 7 deletions packages/js/src/fabric/WSClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import {
actions,
BaseClient,
CallJoinedEventParams,
ClientEvents,
VertoBye,
VertoSubscribe,
VideoRoomSubscribedEventParams,
Expand All @@ -25,10 +24,7 @@ import { wsClientWorker } from './workers'
import { createWSClient } from './createWSClient'
import { WSClientContract } from './interfaces/wsClient'

export class WSClient
extends BaseClient<ClientEvents>
implements WSClientContract
{
export class WSClient extends BaseClient<{}> implements WSClientContract {
private _incomingCallManager: IncomingCallManager
private _disconnected: boolean = false

Expand Down Expand Up @@ -365,10 +361,10 @@ export class WSClient

public updateToken(token: string) {
return new Promise<void>((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 }))
Expand Down
4 changes: 2 additions & 2 deletions packages/js/src/fabric/interfaces/wsClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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']
Expand Down
47 changes: 35 additions & 12 deletions packages/js/src/fabric/workers/wsClientWorker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,23 +21,46 @@ export type WSClientWorkerHooks = SDKWorkerHooks<
export const wsClientWorker: SDKWorker<WSClient, WSClientWorkerHooks> =
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<WebRTCMessageParams>) {
// 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<WebRTCMessageParams> =
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<WebRTCMessageParams>
)
}
}
} finally {
getLogger().trace('wsClientWorker ended')
Expand Down
Loading