-
Notifications
You must be signed in to change notification settings - Fork 310
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: web worker based timers (#2821)
adds a new Timer class that uses a SharedWorker to run `setInterval` calls on a background thread to avoid throttling due to a tab becoming inactive.
- Loading branch information
1 parent
e7c3050
commit 8eedbb5
Showing
5 changed files
with
111 additions
and
12 deletions.
There are no files selected for viewing
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,62 @@ | ||
import { v4 as uuid } from 'uuid'; | ||
import { TimerWork } from './timerWorker'; | ||
|
||
export interface Work { | ||
id: string; | ||
callback: () => void; | ||
} | ||
|
||
export class Timer { | ||
private readonly worker: SharedWorker; | ||
private readonly work = new Map<string, () => void>(); | ||
constructor() { | ||
this.worker = new SharedWorker(/* webpackChunkName: "timer-worker" */ new URL('./timerWorker.js', import.meta.url)); | ||
this.worker.port.onmessage = this.onMessage; | ||
} | ||
|
||
public setInterval(callback: () => void, delay: number): string { | ||
const intervalWork: TimerWork = { | ||
type: 'setInterval', | ||
id: uuid(), | ||
delay | ||
}; | ||
|
||
this.work.set(intervalWork.id, callback); | ||
|
||
this.worker.port.postMessage(intervalWork); | ||
|
||
return intervalWork.id; | ||
} | ||
|
||
public clearInterval(id: string): void { | ||
if (this.work.has(id)) { | ||
const intervalWork: TimerWork = { | ||
type: 'clearInterval', | ||
id | ||
}; | ||
this.worker.port.postMessage(intervalWork); | ||
this.work.delete(id); | ||
} | ||
} | ||
|
||
private readonly onMessage = (event: MessageEvent<TimerWork>): void => { | ||
const intervalWork = event.data; | ||
if (!intervalWork) { | ||
return; | ||
} | ||
|
||
switch (intervalWork.type) { | ||
case 'runCallback': { | ||
if (this.work.has(intervalWork.id)) { | ||
const work = this.work.get(intervalWork.id); | ||
work?.(); | ||
} | ||
break; | ||
} | ||
} | ||
}; | ||
|
||
public close() { | ||
this.worker.port.close(); | ||
} | ||
} |
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,32 @@ | ||
export interface TimerWork { | ||
id: string; | ||
type: 'clearInterval' | 'setInterval' | 'runCallback'; | ||
delay?: number; | ||
} | ||
|
||
const ctx: SharedWorkerGlobalScope = self as unknown as SharedWorkerGlobalScope; | ||
|
||
const intervals = new Map<string, ReturnType<typeof setInterval>>(); | ||
|
||
ctx.onconnect = (e: MessageEvent<unknown>) => { | ||
const port = e.ports[0]; | ||
const handleMessage = (event: MessageEvent<TimerWork>) => { | ||
const data: TimerWork = event.data; | ||
const delay = data.delay; | ||
const jobId = data.id; | ||
switch (data.type) { | ||
case 'setInterval': { | ||
const interval = setInterval(() => { | ||
const message: TimerWork = { ...event.data, ...{ type: 'runCallback' } }; | ||
port.postMessage(message); | ||
}, delay); | ||
intervals.set(jobId, interval); | ||
break; | ||
} | ||
case 'clearInterval': | ||
clearTimeout(intervals.get(jobId)); | ||
intervals.delete(jobId); | ||
} | ||
}; | ||
port.onmessage = handleMessage; | ||
}; |
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