From 2172e10ec23d381158a8fc92fafd2c004157800c Mon Sep 17 00:00:00 2001 From: Matvei Andrienko Date: Wed, 11 Dec 2024 11:39:13 +0100 Subject: [PATCH 1/2] fix: pre-built timer worker --- packages/client/generate-timer-worker.sh | 1 + packages/client/package.json | 2 +- packages/client/src/timers/worker.build.ts | 31 +++++++++++++++++----- 3 files changed, 27 insertions(+), 7 deletions(-) diff --git a/packages/client/generate-timer-worker.sh b/packages/client/generate-timer-worker.sh index 157f20486a..78111fb8c3 100755 --- a/packages/client/generate-timer-worker.sh +++ b/packages/client/generate-timer-worker.sh @@ -4,6 +4,7 @@ npx tsc src/timers/worker.ts \ --skipLibCheck \ --removeComments \ --module preserve \ + --target ES2020 \ --lib ES2020,WebWorker \ --outDir worker-dist diff --git a/packages/client/package.json b/packages/client/package.json index 681540eab4..1a9aaef81e 100644 --- a/packages/client/package.json +++ b/packages/client/package.json @@ -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", diff --git a/packages/client/src/timers/worker.build.ts b/packages/client/src/timers/worker.build.ts index eeddcf28e0..1e5905d1e0 100644 --- a/packages/client/src/timers/worker.build.ts +++ b/packages/client/src/timers/worker.build.ts @@ -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); +}`, }; From 6c59989f4b9ccce1ad8597755460b9ca978e2954 Mon Sep 17 00:00:00 2001 From: Matvei Andrienko Date: Wed, 11 Dec 2024 15:19:48 +0100 Subject: [PATCH 2/2] drop "experimental" --- packages/client/src/StreamVideoClient.ts | 5 ++--- packages/client/src/coordinator/connection/types.ts | 2 +- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/packages/client/src/StreamVideoClient.ts b/packages/client/src/StreamVideoClient.ts index 5fd02b2473..a057a1758e 100644 --- a/packages/client/src/StreamVideoClient.ts +++ b/packages/client/src/StreamVideoClient.ts @@ -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); diff --git a/packages/client/src/coordinator/connection/types.ts b/packages/client/src/coordinator/connection/types.ts index 5956ecd70e..c1d8a85541 100644 --- a/packages/client/src/coordinator/connection/types.ts +++ b/packages/client/src/coordinator/connection/types.ts @@ -151,7 +151,7 @@ export type StreamClientOptions = Partial & { * 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;