Skip to content

Commit

Permalink
hide behind feature toggle
Browse files Browse the repository at this point in the history
  • Loading branch information
myandrienko committed Dec 3, 2024
1 parent 0aefda0 commit 1e325db
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 2 deletions.
4 changes: 4 additions & 0 deletions packages/client/src/StreamVideoClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import { getLogger, logToConsole, setLogger } from './logger';
import { getSdkInfo } from './client-details';
import { SdkType } from './gen/video/sfu/models/models';
import { withoutConcurrency } from './helpers/concurrency';
import { enableTimerWorker } from './timers';

/**
* A `StreamVideoClient` instance lets you communicate with our API, and authenticate users.
Expand Down Expand Up @@ -86,9 +87,12 @@ export class StreamVideoClient {
if (typeof apiKeyOrArgs === 'string') {
logLevel = opts?.logLevel || logLevel;
logger = opts?.logger || logger;
if (opts?.expertimental_enableTimerWorker) enableTimerWorker();
} else {
logLevel = apiKeyOrArgs.options?.logLevel || logLevel;
logger = apiKeyOrArgs.options?.logger || logger;
if (apiKeyOrArgs.options?.expertimental_enableTimerWorker)
enableTimerWorker();
}

setLogger(logger, logLevel);
Expand Down
6 changes: 6 additions & 0 deletions packages/client/src/coordinator/connection/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,12 @@ export type StreamClientOptions = Partial<AxiosRequestConfig> & {
* In Node.js environment, you can use the `ws` package.
*/
WebSocketImpl?: typeof WebSocket;

/**
* Create Web Worker to initiate timer events like health checks. Can possibly prevent
* timer throttling issues in inactive browser tabs.
*/
expertimental_enableTimerWorker?: boolean;
};

export type TokenProvider = () => Promise<string>;
Expand Down
15 changes: 13 additions & 2 deletions packages/client/src/timers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,12 @@ class TimerWorker {
private worker: Worker | undefined;
private fallback = false;

setup(): void {
setup({ useTimerWorker = true }: { useTimerWorker?: boolean } = {}): void {
if (!useTimerWorker) {
this.fallback = true;
return;
}

try {
const source = timerWorker.src;
const blob = new Blob([source], {
Expand Down Expand Up @@ -119,8 +124,14 @@ class TimerWorker {
}
}

let timerWorkerEnabled = false;

export const enableTimerWorker = () => {
timerWorkerEnabled = true;
};

export const getTimers = lazy(() => {
const instance = new TimerWorker();
instance.setup();
instance.setup({ useTimerWorker: timerWorkerEnabled });
return instance;
});

0 comments on commit 1e325db

Please sign in to comment.