mirror of https://github.com/sveltejs/svelte
chore: speedup hydration around input and select values (#11717)
* chore: speedup hydration around input and select values * use idle tasks to do the work --------- Co-authored-by: Dominic Gannaway <dg@domgan.com>pull/11726/head
parent
d590cd8bea
commit
c21f019a4b
@ -0,0 +1,5 @@
|
||||
---
|
||||
"svelte": patch
|
||||
---
|
||||
|
||||
chore: speedup hydration around input and select values
|
@ -1,33 +1,63 @@
|
||||
import { run_all } from '../../shared/utils.js';
|
||||
|
||||
let is_task_queued = false;
|
||||
// Fallback for when requestIdleCallback is not available
|
||||
const request_idle_callback =
|
||||
typeof requestIdleCallback === 'undefined'
|
||||
? (/** @type {() => void} */ cb) => setTimeout(cb, 1)
|
||||
: requestIdleCallback;
|
||||
|
||||
let is_micro_task_queued = false;
|
||||
let is_idle_task_queued = false;
|
||||
|
||||
/** @type {Array<() => void>} */
|
||||
let current_queued_miro_tasks = [];
|
||||
/** @type {Array<() => void>} */
|
||||
let current_queued_tasks = [];
|
||||
let current_queued_idle_tasks = [];
|
||||
|
||||
function process_micro_tasks() {
|
||||
is_micro_task_queued = false;
|
||||
const tasks = current_queued_miro_tasks.slice();
|
||||
current_queued_miro_tasks = [];
|
||||
run_all(tasks);
|
||||
}
|
||||
|
||||
function process_task() {
|
||||
is_task_queued = false;
|
||||
const tasks = current_queued_tasks.slice();
|
||||
current_queued_tasks = [];
|
||||
function process_idle_tasks() {
|
||||
is_idle_task_queued = false;
|
||||
const tasks = current_queued_idle_tasks.slice();
|
||||
current_queued_idle_tasks = [];
|
||||
run_all(tasks);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {() => void} fn
|
||||
*/
|
||||
export function queue_task(fn) {
|
||||
if (!is_task_queued) {
|
||||
is_task_queued = true;
|
||||
queueMicrotask(process_task);
|
||||
export function queue_micro_task(fn) {
|
||||
if (!is_micro_task_queued) {
|
||||
is_micro_task_queued = true;
|
||||
queueMicrotask(process_micro_tasks);
|
||||
}
|
||||
current_queued_miro_tasks.push(fn);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {() => void} fn
|
||||
*/
|
||||
export function queue_idle_task(fn) {
|
||||
if (!is_idle_task_queued) {
|
||||
is_idle_task_queued = true;
|
||||
request_idle_callback(process_idle_tasks);
|
||||
}
|
||||
current_queued_tasks.push(fn);
|
||||
current_queued_idle_tasks.push(fn);
|
||||
}
|
||||
|
||||
/**
|
||||
* Synchronously run any queued tasks.
|
||||
*/
|
||||
export function flush_tasks() {
|
||||
if (is_task_queued) {
|
||||
process_task();
|
||||
if (is_micro_task_queued) {
|
||||
process_micro_tasks();
|
||||
}
|
||||
if (is_idle_task_queued) {
|
||||
process_idle_tasks();
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in new issue