Skip to content

Commit

Permalink
fix: test
Browse files Browse the repository at this point in the history
  • Loading branch information
santhoshvai committed Nov 1, 2024
1 parent f563e56 commit 9f31de7
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 7 deletions.
13 changes: 6 additions & 7 deletions packages/client/src/devices/InputMediaDeviceManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,10 @@ export abstract class InputMediaDeviceManager<
* Starts stream.
*/
async enable() {
this.state.prevStatus = this.state.status;
if (this.state.optimisticStatus === 'enabled') {
return;
}

this.state.prevStatus = this.state.status;
this.state.setPendingStatus('enabled');

await withCancellation(this.statusChangeConcurrencyTag, async (signal) => {
Expand All @@ -98,18 +97,18 @@ export abstract class InputMediaDeviceManager<
* @param {boolean} [forceStop=false] when true, stops the tracks regardless of the state.disableMode
*/
async disable(forceStop: boolean = false) {
if (!forceStop && this.state.optimisticStatus === 'disabled') {
return;
}
this.state.prevStatus = this.state.status;
if (
this.state.prevStatus === 'disabled' &&
this.state.prevStatus !== 'enabled' &&
this.state.optimisticStatus === 'enabled'
) {
// if previously enabled but its not done yet, set the prevStatus to enabled
// this makes resume() work correctly
this.state.prevStatus = 'enabled';
}
if (!forceStop && this.state.optimisticStatus === 'disabled') {
return;
}

this.state.setPendingStatus('disabled');

Expand Down Expand Up @@ -140,7 +139,7 @@ export abstract class InputMediaDeviceManager<
async resume() {
if (
this.state.prevStatus === 'enabled' &&
this.state.status === 'disabled'
this.state.status !== 'enabled'
) {
await this.enable();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,29 @@ describe('InputMediaDeviceManager.test', () => {
expect(manager.enable).toHaveBeenCalledOnce();
});

it(`should resume if enable was cancelled due to disable call`, async () => {
vi.spyOn(manager, 'enable');

manager.enable();

expect(manager.enable).toHaveBeenCalledOnce();

// enable was not awaited so cancelled by disabled
await manager.disable();

manager.resume();

expect(manager.enable).toBeCalledTimes(2);

// this disable is not awaited, but will cancel the enable anyway
// so resume must work here too
manager.disable();

manager.resume();

expect(manager.enable).toBeCalledTimes(3);
});

it('should provide default constraints to `getStream` method', () => {
manager.setDefaultConstraints({
echoCancellation: true,
Expand Down

0 comments on commit 9f31de7

Please sign in to comment.