Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
13 changes: 13 additions & 0 deletions packages/core/realtime-js/src/RealtimeClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -645,6 +645,7 @@ export default class RealtimeClient {
this.conn = null
}
this._clearAllTimers()
this._terminateWorker()
this.channels.forEach((channel) => channel.teardown())
}

Expand Down Expand Up @@ -710,6 +711,18 @@ export default class RealtimeClient {
interval: this.heartbeatIntervalMs,
})
}

/**
* Terminate the Web Worker and clear the reference
* @internal
*/
private _terminateWorker(): void {
if (this.workerRef) {
this.log('worker', 'terminating worker')
this.workerRef.terminate()
this.workerRef = undefined
}
}
/** @internal */
private _onConnClose(event: any) {
this._setConnectionState('disconnected')
Expand Down
24 changes: 24 additions & 0 deletions packages/core/realtime-js/test/RealtimeClient.worker.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,3 +98,27 @@ test('creates worker with blob URL when no workerUrl provided', () => {
global.URL.createObjectURL = originalCreateObjectURL
}
})

test('terminates worker on disconnect', () => {
// Establish connection first
client.connect()

// Trigger worker creation
client._onConnOpen()

// Verify worker was created
assert.ok(client.workerRef)
const worker = client.workerRef

// Spy on worker terminate method
const terminateSpy = vi.spyOn(worker, 'terminate')

// Disconnect the client
client.disconnect()

// Verify worker was terminated
expect(terminateSpy).toHaveBeenCalled()

// Verify workerRef was cleared
assert.equal(client.workerRef, undefined)
})
Loading