Skip to content

Commit

Permalink
fix: pre-built timer worker (#1617)
Browse files Browse the repository at this point in the history
Dropping the `experimental_` prefix since this feature was confirmed
working.

Since the timer worker (introduced in #1557) is not going to be updated
too often, we can skip an additional built step and just check in the
prebuilt worker.

The "source" file `worker.ts` and the script to generate worker are
still there, but they are not used during build.
  • Loading branch information
myandrienko authored Dec 11, 2024
1 parent cb2f218 commit 94dacef
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 11 deletions.
1 change: 1 addition & 0 deletions packages/client/generate-timer-worker.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ npx tsc src/timers/worker.ts \
--skipLibCheck \
--removeComments \
--module preserve \
--target ES2020 \
--lib ES2020,WebWorker \
--outDir worker-dist

Expand Down
2 changes: 1 addition & 1 deletion packages/client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"scripts": {
"clean": "rimraf dist",
"start": "rollup -w -c",
"build": "yarn clean && ./generate-timer-worker.sh && rollup -c",
"build": "yarn clean && rollup -c",
"test": "vitest",
"clean:docs": "rimraf generated-docs",
"test-ci": "vitest --coverage",
Expand Down
5 changes: 2 additions & 3 deletions packages/client/src/StreamVideoClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,12 +89,11 @@ export class StreamVideoClient {
if (typeof apiKeyOrArgs === 'string') {
logLevel = opts?.logLevel || logLevel;
logger = opts?.logger || logger;
if (opts?.expertimental_enableTimerWorker) enableTimerWorker();
if (opts?.enableTimerWorker) enableTimerWorker();
} else {
logLevel = apiKeyOrArgs.options?.logLevel || logLevel;
logger = apiKeyOrArgs.options?.logger || logger;
if (apiKeyOrArgs.options?.expertimental_enableTimerWorker)
enableTimerWorker();
if (apiKeyOrArgs.options?.enableTimerWorker) enableTimerWorker();
}

setLogger(logger, logLevel);
Expand Down
2 changes: 1 addition & 1 deletion packages/client/src/coordinator/connection/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ export type StreamClientOptions = Partial<AxiosRequestConfig> & {
* Create Web Worker to initiate timer events like health checks. Can possibly prevent
* timer throttling issues in inactive browser tabs.
*/
expertimental_enableTimerWorker?: boolean;
enableTimerWorker?: boolean;
};

export type TokenProvider = () => Promise<string>;
Expand Down
31 changes: 25 additions & 6 deletions packages/client/src/timers/worker.build.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,28 @@
// Do not modify this file manually. You can edit worker.ts if necessary
// Do not modify this file manually. Instead, edit worker.ts
// and the run ./generate-timer-worker.sh
export const timerWorker = {
get src(): string {
throw new Error(
'Timer worker source missing. Did you forget to run generate-timer-worker.sh?',
);
},
src: `const timerIdMapping = new Map();
self.addEventListener('message', (event) => {
const request = event.data;
switch (request.type) {
case 'setTimeout':
case 'setInterval':
timerIdMapping.set(request.id, (request.type === 'setTimeout' ? setTimeout : setInterval)(() => {
tick(request.id);
if (request.type === 'setTimeout') {
timerIdMapping.delete(request.id);
}
}, request.timeout));
break;
case 'clearTimeout':
case 'clearInterval':
(request.type === 'clearTimeout' ? clearTimeout : clearInterval)(timerIdMapping.get(request.id));
timerIdMapping.delete(request.id);
break;
}
});
function tick(id) {
const message = { type: 'tick', id };
self.postMessage(message);
}`,
};

0 comments on commit 94dacef

Please sign in to comment.