All files / src/internal/client/dom task.js

100% Statements 34/34
100% Branches 5/5
100% Functions 3/3
100% Lines 33/33

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 342x 2x 2x 2x 2x 2x 2x 220x 220x 220x 220x 220x 220x 2x 2x 2x 2x 2x 292x 186x 186x 186x 292x 292x 2x 2x 2x 2x 2x 13621x 34x 34x 13621x  
import { run_all } from '../../shared/utils.js';
 
let is_task_queued = false;
 
/** @type {Array<() => void>} */
let current_queued_tasks = [];
 
function process_task() {
	is_task_queued = false;
	const tasks = current_queued_tasks.slice();
	current_queued_tasks = [];
	run_all(tasks);
}
 
/**
 * @param {() => void} fn
 */
export function queue_task(fn) {
	if (!is_task_queued) {
		is_task_queued = true;
		queueMicrotask(process_task);
	}
	current_queued_tasks.push(fn);
}
 
/**
 * Synchronously run any queued tasks.
 */
export function flush_tasks() {
	if (is_task_queued) {
		process_task();
	}
}