]> git.codecow.com Git - libnemo.git/commitdiff
Consolidate testing suite queue into test globals file.
authorChris Duncan <chris@zoso.dev>
Wed, 23 Jul 2025 17:33:34 +0000 (10:33 -0700)
committerChris Duncan <chris@zoso.dev>
Wed, 23 Jul 2025 17:33:34 +0000 (10:33 -0700)
test/GLOBALS.mjs
test/QUEUE.mjs [deleted file]

index d5e012e854aed5d11aaa18e8867a6efae6c8a61c..fc5c16e7d4aa7231cfb493bced922161f659ad14 100644 (file)
@@ -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 (file)
index 68c6cf4..0000000
+++ /dev/null
@@ -1,38 +0,0 @@
-//! SPDX-FileCopyrightText: 2025 Chris Duncan <chris@zoso.dev>
-//! 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()
-               })
-       }
-}