-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
made improvement for virtual background (#436)
- Loading branch information
Showing
14 changed files
with
159 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
Binary file not shown.
Large diffs are not rendered by default.
Oops, something went wrong.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
type TimerData = { | ||
callbackId: number; | ||
}; | ||
|
||
export type TimerWorker = { | ||
setTimeout(callback: () => void, timeoutMs?: number): number; | ||
clearTimeout(callbackId: number): void; | ||
terminate(): void; | ||
}; | ||
|
||
export function createTimerWorker(): TimerWorker { | ||
const callbacks = new Map<number, () => void>(); | ||
|
||
const worker = new Worker(new URL('./timerWorker', import.meta.url)); | ||
|
||
worker.onmessage = (event: MessageEvent<TimerData>) => { | ||
const callback = callbacks.get(event.data.callbackId); | ||
if (!callback) { | ||
return; | ||
} | ||
callbacks.delete(event.data.callbackId); | ||
callback(); | ||
}; | ||
|
||
let nextCallbackId = 1; | ||
|
||
function setTimeout(callback: () => void, timeoutMs = 0) { | ||
const callbackId = nextCallbackId++; | ||
callbacks.set(callbackId, callback); | ||
worker.postMessage({ callbackId, timeoutMs }); | ||
return callbackId; | ||
} | ||
|
||
function clearTimeout(callbackId: number) { | ||
if (!callbacks.has(callbackId)) { | ||
return; | ||
} | ||
worker.postMessage({ callbackId }); | ||
callbacks.delete(callbackId); | ||
} | ||
|
||
function terminate() { | ||
callbacks.clear(); | ||
worker.terminate(); | ||
} | ||
|
||
return { setTimeout, clearTimeout, terminate }; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
declare const self: DedicatedWorkerGlobalScope; | ||
|
||
type TimerData = { | ||
callbackId: number; | ||
timeoutMs?: number; | ||
}; | ||
|
||
const timeoutIds = new Map<number, number>(); | ||
|
||
self.onmessage = (event: MessageEvent<TimerData>) => { | ||
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 {}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.