jiti-meet/react/features/stream-effects/virtual-background/TimerWorker.js

60 lines
1.4 KiB
JavaScript
Raw Normal View History

2019-06-28 17:18:47 +00:00
/**
* SET_TIMEOUT constant is used to set interval and it is set in
2021-11-04 21:10:43 +00:00
* the id property of the request.data property. TimeMs property must
* also be set. Request.data example:
2019-06-28 17:18:47 +00:00
*
* {
* id: SET_TIMEOUT,
2019-06-28 17:18:47 +00:00
* timeMs: 33
2021-11-04 21:10:43 +00:00
* }.
2019-06-28 17:18:47 +00:00
*/
export const SET_TIMEOUT = 1;
2019-06-28 17:18:47 +00:00
/**
* CLEAR_TIMEOUT constant is used to clear the interval and it is set in
2019-06-28 17:18:47 +00:00
* the id property of the request.data property.
*
* {
* id: CLEAR_TIMEOUT
2021-11-04 21:10:43 +00:00
* }.
2019-06-28 17:18:47 +00:00
*/
export const CLEAR_TIMEOUT = 2;
2019-06-28 17:18:47 +00:00
/**
* TIMEOUT_TICK constant is used as response and it is set in the id property.
2019-06-28 17:18:47 +00:00
*
* {
* id: TIMEOUT_TICK
2021-11-04 21:10:43 +00:00
* }.
2019-06-28 17:18:47 +00:00
*/
export const TIMEOUT_TICK = 3;
2019-06-28 17:18:47 +00:00
/**
* The following code is needed as string to create a URL from a Blob.
* The URL is then passed to a WebWorker. Reason for this is to enable
* use of setInterval that is not throttled when tab is inactive.
*/
2019-07-03 15:38:25 +00:00
const code = `
var timer;
2019-06-28 17:18:47 +00:00
onmessage = function(request) {
switch (request.data.id) {
case ${SET_TIMEOUT}: {
timer = setTimeout(() => {
postMessage({ id: ${TIMEOUT_TICK} });
2019-06-28 17:18:47 +00:00
}, request.data.timeMs);
break;
}
case ${CLEAR_TIMEOUT}: {
2019-07-03 15:38:25 +00:00
if (timer) {
clearTimeout(timer);
2019-07-03 15:38:25 +00:00
}
2019-06-28 17:18:47 +00:00
break;
}
}
};
`;
2019-07-03 15:38:25 +00:00
export const timerWorkerScript = URL.createObjectURL(new Blob([ code ], { type: 'application/javascript' }));