Skip to content

Commit

Permalink
Use mutex helper package (#1289)
Browse files Browse the repository at this point in the history
  • Loading branch information
lukasIO authored Oct 16, 2024
1 parent 0f7ca21 commit bde8c12
Show file tree
Hide file tree
Showing 13 changed files with 33 additions and 52 deletions.
5 changes: 5 additions & 0 deletions .changeset/warm-hornets-drive.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"livekit-client": patch
---

Use shared mutex helper package
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
"size-limit": "size-limit"
},
"dependencies": {
"@livekit/mutex": "1.0.0",
"@livekit/protocol": "1.24.0",
"events": "^3.3.0",
"loglevel": "^1.8.0",
Expand Down
16 changes: 12 additions & 4 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion src/api/SignalClient.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { Mutex } from '@livekit/mutex';
import {
AddTrackRequest,
AudioTrackFeature,
Expand Down Expand Up @@ -42,7 +43,7 @@ import log, { LoggerNames, getLogger } from '../logger';
import { ConnectionError, ConnectionErrorReason } from '../room/errors';
import CriticalTimers from '../room/timers';
import type { LoggerOptions } from '../room/types';
import { Mutex, getClientInfo, isReactNative, sleep, toWebsocketUrl } from '../room/utils';
import { getClientInfo, isReactNative, sleep, toWebsocketUrl } from '../room/utils';
import { AsyncQueue } from '../utils/AsyncQueue';

// internal options
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { Mutex } from '@livekit/mutex';
import { DataPacket_Kind, DisconnectReason, SubscriptionError } from '@livekit/protocol';
import { LogLevel, LoggerNames, getLogger, setLogExtension, setLogLevel } from './logger';
import DefaultReconnectPolicy from './room/DefaultReconnectPolicy';
Expand Down Expand Up @@ -26,7 +27,6 @@ import { TrackPublication } from './room/track/TrackPublication';
import type { LiveKitReactNativeInfo } from './room/types';
import type { AudioAnalyserOptions } from './room/utils';
import {
Mutex,
compareVersions,
createAudioAnalyser,
getEmptyAudioStreamTrack,
Expand Down
3 changes: 2 additions & 1 deletion src/room/PCTransportManager.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { Mutex } from '@livekit/mutex';
import { SignalTarget } from '@livekit/protocol';
import log, { LoggerNames, getLogger } from '../logger';
import PCTransport, { PCEvents } from './PCTransport';
import { roomConnectOptionDefaults } from './defaults';
import { ConnectionError, ConnectionErrorReason } from './errors';
import CriticalTimers from './timers';
import type { LoggerOptions } from './types';
import { Mutex, sleep } from './utils';
import { sleep } from './utils';

export enum PCTransportState {
NEW,
Expand Down
3 changes: 2 additions & 1 deletion src/room/RTCEngine.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { Mutex } from '@livekit/mutex';
import {
type AddTrackRequest,
ClientConfigSetting,
Expand Down Expand Up @@ -63,7 +64,7 @@ import type { Track } from './track/Track';
import type { TrackPublishOptions, VideoCodec } from './track/options';
import { getTrackPublicationInfo } from './track/utils';
import type { LoggerOptions } from './types';
import { Mutex, isVideoCodec, isWeb, sleep, supportsAddTrack, supportsTransceiver } from './utils';
import { isVideoCodec, isWeb, sleep, supportsAddTrack, supportsTransceiver } from './utils';

const lossyDataChannel = '_lossy';
const reliableDataChannel = '_reliable';
Expand Down
2 changes: 1 addition & 1 deletion src/room/Room.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { Mutex } from '@livekit/mutex';
import {
ChatMessage as ChatMessageModel,
ConnectionQualityUpdate,
Expand Down Expand Up @@ -77,7 +78,6 @@ import type {
} from './types';
import {
Future,
Mutex,
createDummyVideoStreamTrack,
extractChatMessage,
extractTranscriptionSegments,
Expand Down
3 changes: 2 additions & 1 deletion src/room/track/LocalTrack.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { Mutex } from '@livekit/mutex';
import { debounce } from 'ts-debounce';
import { getBrowser } from '../../utils/browserParser';
import DeviceManager from '../DeviceManager';
import { DeviceUnsupportedError, TrackInvalidError } from '../errors';
import { TrackEvent } from '../events';
import type { LoggerOptions } from '../types';
import { Mutex, compareVersions, isMobile, sleep } from '../utils';
import { compareVersions, isMobile, sleep } from '../utils';
import { Track, attachToElement, detachTrack } from './Track';
import type { VideoCodec } from './options';
import type { TrackProcessor } from './processor/types';
Expand Down
3 changes: 2 additions & 1 deletion src/room/track/LocalVideoTrack.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { Mutex } from '@livekit/mutex';
import {
VideoQuality as ProtoVideoQuality,
SubscribedCodec,
Expand All @@ -10,7 +11,7 @@ import { ScalabilityMode } from '../participant/publishUtils';
import type { VideoSenderStats } from '../stats';
import { computeBitrate, monitorFrequency } from '../stats';
import type { LoggerOptions } from '../types';
import { Mutex, isFireFox, isMobile, isWeb, unwrapConstraint } from '../utils';
import { isFireFox, isMobile, isWeb, unwrapConstraint } from '../utils';
import LocalTrack from './LocalTrack';
import { Track, VideoQuality } from './Track';
import type { VideoCaptureOptions, VideoCodec } from './options';
Expand Down
38 changes: 0 additions & 38 deletions src/room/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -454,44 +454,6 @@ export function createAudioAnalyser(
return { calculateVolume, analyser, cleanup };
}

/**
* @internal
*/
export class Mutex {
private _locking: Promise<void>;

private _locks: number;

constructor() {
this._locking = Promise.resolve();
this._locks = 0;
}

isLocked() {
return this._locks > 0;
}

lock() {
this._locks += 1;

let unlockNext: () => void;

const willLock = new Promise<void>(
(resolve) =>
(unlockNext = () => {
this._locks -= 1;
resolve();
}),
);

const willUnlock = this._locking.then(() => unlockNext);

this._locking = this._locking.then(() => willLock);

return willUnlock;
}
}

export function isVideoCodec(maybeCodec: string): maybeCodec is VideoCodec {
return videoCodecs.includes(maybeCodec as VideoCodec);
}
Expand Down
4 changes: 2 additions & 2 deletions src/utils/AsyncQueue.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { describe, expect, it } from 'vitest';
import { assert, describe, expect, it } from 'vitest';
import { sleep } from '../room/utils';
import { AsyncQueue } from './AsyncQueue';

Expand Down Expand Up @@ -49,7 +49,7 @@ describe('asyncQueue', () => {
task2Executed = true;
})
.catch(() => {
fail('task 2 should not have thrown');
assert.fail('task 2 should not have thrown');
});

expect(task1threw).toBeTruthy();
Expand Down
2 changes: 1 addition & 1 deletion src/utils/AsyncQueue.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Mutex } from '../room/utils';
import { Mutex } from '@livekit/mutex';

type QueueTask<T> = () => PromiseLike<T>;

Expand Down

0 comments on commit bde8c12

Please sign in to comment.