Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: pre-built timer worker #1617

Merged
merged 2 commits into from
Dec 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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();
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops, it used to say "expertimental" the whole time!..

} 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);
}`,
};
Loading