javascript / expert
Snippet
Priority-Queued Test Scheduler for Simulating Concurrent React Interruption
Combines priority queue control flow logic and unit testing utilities to deterministically simulate high-priority user input preempting low-priority deferred tasks in React Concurrent Mode test scenarios.
snippet.js
javascript
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class ConcurrentTestScheduler {#queue = [];schedule(priority, task) {this.#queue.push({ priority, task });this.#queue.sort((a, b) => b.priority - a.priority);}flushUntil(maxPriorityThreshold) {const executed = [];while (this.#queue.length > 0 && this.#queue[0].priority >= maxPriorityThreshold) {const { task } = this.#queue.shift();executed.push(task());}return executed;}}
react
Breakdown
1
schedule(priority, task) {
Registers a test task callback alongside a priority weight integer.
2
this.#queue.sort((a, b) => b.priority - a.priority);
Sorts queued tasks in descending order of priority so high-priority updates execute first.
3
while (this.#queue.length > 0 && this.#queue[0].priority >= maxPriorityThreshold) {
Evaluates priority thresholds dynamically to simulate partial execution and render interruptions.
4
executed.push(task());
Executes the highest-priority task in isolation and records its return payload for assertions.