Skip to content

Commit

Permalink
fix: VB not working in firefox
Browse files Browse the repository at this point in the history
  • Loading branch information
jibon57 committed Jul 12, 2023
1 parent 502e645 commit 9f77891
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 28 deletions.
25 changes: 21 additions & 4 deletions src/components/virtual-background/helpers/timerHelper.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { CrossOriginWorkerMaker as Worker } from '../../../helpers/cross-origin-worker';

type TimerData = {
callbackId: number;
};
Expand All @@ -10,11 +8,30 @@ export type TimerWorker = {
terminate(): void;
};

// we'll have to write like this way, otherwise browser won't load because of cross origin error
const timerWorker = `
const timeoutIds = new Map();
self.onmessage = (event) => {
if (event.data.timeoutMs !== undefined) {
const timeoutId = self.setTimeout(() => {
self.postMessage({ callbackId: event.data.callbackId });
timeoutIds.delete(event.data.callbackId);
}, event.data.timeoutMs);
timeoutIds.set(event.data.callbackId, timeoutId);
} else {
const timeoutId = timeoutIds.get(event.data.callbackId);
self.clearTimeout(timeoutId);
timeoutIds.delete(event.data.callbackId);
}
};
`;

export function createTimerWorker(): TimerWorker {
const callbacks = new Map<number, () => void>();
const workerMaker = new Worker(new URL('./timerWorker', import.meta.url));
const worker = new Worker(
URL.createObjectURL(new Blob([timerWorker], { type: 'text/javascript' })),
);

const worker = workerMaker.worker;
worker.onmessage = (event: MessageEvent<TimerData>) => {
const callback = callbacks.get(event.data.callbackId);
if (!callback) {
Expand Down
24 changes: 0 additions & 24 deletions src/components/virtual-background/helpers/timerWorker.ts

This file was deleted.

0 comments on commit 9f77891

Please sign in to comment.