From d2149c93f80905849fa0944e06e714aa4d02d2f2 Mon Sep 17 00:00:00 2001 From: Chris Duncan Date: Wed, 23 Jul 2025 10:33:34 -0700 Subject: [PATCH] Consolidate testing suite queue into test globals file. --- test/GLOBALS.mjs | 34 ++++++++++++++++++++++++++++++++-- test/QUEUE.mjs | 38 -------------------------------------- 2 files changed, 32 insertions(+), 40 deletions(-) delete mode 100644 test/QUEUE.mjs diff --git a/test/GLOBALS.mjs b/test/GLOBALS.mjs index d5e012e..fc5c16e 100644 --- a/test/GLOBALS.mjs +++ b/test/GLOBALS.mjs @@ -2,9 +2,39 @@ //! SPDX-License-Identifier: GPL-3.0-or-later export { env } from '../env.mjs' -import { Queue } from './QUEUE.mjs' -const queue = new Queue() +/** + * Serially executes asynchronous functions. + */ +const queue = { + isIdle: true, + tasks: [{}], + + process: function () { + const next = queue.tasks.shift() + if (next == null) return + const { task, resolve, reject, args } = next + queue.isIdle = !task + task?.(...args).then(resolve).catch(reject).finally(queue.process) + }, + + add: async function (task, ...args) { + if (typeof task !== 'function') throw new TypeError('task is not a function') + return new Promise((resolve, reject) => { + queue.tasks.push({ task, resolve, reject, args }) + if (queue.isIdle) queue.process() + }) + }, + + prioritize: async function (task, ...args) { + if (typeof task !== 'function') throw new TypeError('task is not a function') + return new Promise((resolve, reject) => { + if (typeof task !== 'function') reject('task is not a function') + queue.tasks.unshift({ task, resolve, reject, args }) + if (queue.isIdle) queue.process() + }) + } +} export const isNode = globalThis !== globalThis.window diff --git a/test/QUEUE.mjs b/test/QUEUE.mjs deleted file mode 100644 index 68c6cf4..0000000 --- a/test/QUEUE.mjs +++ /dev/null @@ -1,38 +0,0 @@ -//! SPDX-FileCopyrightText: 2025 Chris Duncan -//! SPDX-License-Identifier: GPL-3.0-or-later - -/** - * Serially executes asynchronous functions. - */ -export class Queue { - #isIdle - #queue - - constructor () { - this.#isIdle = true - this.#queue = [] - } - - #process = () => { - const { task, resolve, reject, args } = this.#queue.shift() ?? {} - this.#isIdle = !task - task?.(...args).then(resolve).catch(reject).finally(this.#process) - } - - async add (task, ...args) { - if (typeof task !== 'function') throw new TypeError('task is not a function') - return new Promise((resolve, reject) => { - this.#queue.push({ task, resolve, reject, args }) - if (this.#isIdle) this.#process() - }) - } - - async prioritize (task, ...args) { - if (typeof task !== 'function') throw new TypeError('task is not a function') - return new Promise((resolve, reject) => { - if (typeof task !== 'function') reject('task is not a function') - this.#queue.unshift({ task, resolve, reject, args }) - if (this.#isIdle) this.#process() - }) - } -} -- 2.47.3