|
|
|
@ -1,18 +1,34 @@
|
|
|
|
import { run_all } from '../../shared/utils.js';
|
|
|
|
import { run_all } from '../../shared/utils.js';
|
|
|
|
|
|
|
|
|
|
|
|
// Fallback for when requestIdleCallback is not available
|
|
|
|
// Fallback for when requestIdleCallback is not available
|
|
|
|
|
|
|
|
// TODO: find a proper polyfill for Safari, this doesn't work
|
|
|
|
export const request_idle_callback =
|
|
|
|
export const request_idle_callback =
|
|
|
|
typeof requestIdleCallback === 'undefined'
|
|
|
|
typeof requestIdleCallback === 'undefined'
|
|
|
|
? (/** @type {() => void} */ cb) => setTimeout(cb, 1)
|
|
|
|
? (/** @type {() => void} */ cb) => setTimeout(cb, 1)
|
|
|
|
: requestIdleCallback;
|
|
|
|
: requestIdleCallback;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Fallback for when scheduler.yield is not available
|
|
|
|
|
|
|
|
// TODO: find a proper polyfill for Safari, this doesn't work
|
|
|
|
|
|
|
|
export const scheduler_yield =
|
|
|
|
|
|
|
|
// @ts-ignore
|
|
|
|
|
|
|
|
typeof scheduler === 'undefined'
|
|
|
|
|
|
|
|
? (/** @type {() => void} */ cb) => setTimeout(cb, 1)
|
|
|
|
|
|
|
|
: async (/** @type {() => void} */ fn) => {
|
|
|
|
|
|
|
|
// @ts-ignore
|
|
|
|
|
|
|
|
await scheduler.yield();
|
|
|
|
|
|
|
|
fn();
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
let is_micro_task_queued = false;
|
|
|
|
let is_micro_task_queued = false;
|
|
|
|
let is_idle_task_queued = false;
|
|
|
|
let is_idle_task_queued = false;
|
|
|
|
|
|
|
|
let is_yield_task_queued = false;
|
|
|
|
|
|
|
|
|
|
|
|
/** @type {Array<() => void>} */
|
|
|
|
/** @type {Array<() => void>} */
|
|
|
|
let current_queued_micro_tasks = [];
|
|
|
|
let current_queued_micro_tasks = [];
|
|
|
|
/** @type {Array<() => void>} */
|
|
|
|
/** @type {Array<() => void>} */
|
|
|
|
let current_queued_idle_tasks = [];
|
|
|
|
let current_queued_idle_tasks = [];
|
|
|
|
|
|
|
|
/** @type {Array<() => void>} */
|
|
|
|
|
|
|
|
let current_queued_yield_tasks = [];
|
|
|
|
|
|
|
|
|
|
|
|
function process_micro_tasks() {
|
|
|
|
function process_micro_tasks() {
|
|
|
|
is_micro_task_queued = false;
|
|
|
|
is_micro_task_queued = false;
|
|
|
|
@ -28,6 +44,13 @@ function process_idle_tasks() {
|
|
|
|
run_all(tasks);
|
|
|
|
run_all(tasks);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function process_yield_tasks() {
|
|
|
|
|
|
|
|
is_yield_task_queued = false;
|
|
|
|
|
|
|
|
const tasks = current_queued_yield_tasks.slice();
|
|
|
|
|
|
|
|
current_queued_yield_tasks = [];
|
|
|
|
|
|
|
|
run_all(tasks);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* @param {() => void} fn
|
|
|
|
* @param {() => void} fn
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
@ -50,6 +73,17 @@ export function queue_idle_task(fn) {
|
|
|
|
current_queued_idle_tasks.push(fn);
|
|
|
|
current_queued_idle_tasks.push(fn);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* @param {() => void} fn
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
export function queue_yield_task(fn) {
|
|
|
|
|
|
|
|
if (!is_yield_task_queued) {
|
|
|
|
|
|
|
|
is_yield_task_queued = true;
|
|
|
|
|
|
|
|
scheduler_yield(process_yield_tasks);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
current_queued_yield_tasks.push(fn);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* Synchronously run any queued tasks.
|
|
|
|
* Synchronously run any queued tasks.
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
|