UNPKG

rvx

Version:

A signal based rendering library

162 lines (149 loc) 4.06 kB
/*! MIT License Copyright (c) 2025 Max J. Polster Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ import { View, ENV, uncapture, captureSelf, viewNodes } from './rvx.js'; import { Queue, AsyncContext, ASYNC } from './rvx.async.js'; function assertViewState(view) { if (!(view instanceof View)) { throw new Error("view is not a View"); } const env = ENV.current; if (!(view.first instanceof env.Node)) { throw new Error("view.first is not a Node"); } if (!(view.last instanceof env.Node)) { throw new Error("view.last is not a Node"); } if (view.first !== view.last) { const parent = view.first.parentNode; if (!(parent instanceof env.Node)) { throw new Error("view.first.parent is not a Node"); } let node = view.first.nextSibling; nodes: for (;;) { if (node === null) { throw new Error("view is unterminated"); } if (!(node instanceof env.Node)) { throw new Error("view contains non-Node"); } if (node === view.last) { break nodes; } node = node.nextSibling; } } } class PollTimeoutError extends Error { } async function poll(fn, timeout) { const ac = new AbortController(); let timer; if (timeout !== undefined) { timer = setTimeout(() => ac.abort(new PollTimeoutError()), timeout); } try { for (;;) { const value = await fn(ac.signal); if (value) { return value; } ac.signal.throwIfAborted(); await new Promise(r => setTimeout(r, 0)); } } finally { clearTimeout(timer); } } const KEY = Symbol.for("rvx:test:queues"); const QUEUES = globalThis[KEY] ?? (globalThis[KEY] = new Map()); function exclusive(key, action) { let queue = QUEUES.get(key); if (queue === undefined) { queue = uncapture(() => new Queue()); QUEUES.set(key, queue); } return queue.block(action); } async function runAsyncTest(fn) { const teardown = []; const asyncCtx = new AsyncContext(); async function cleanup() { for (let i = teardown.length - 1; i >= 0; i--) { teardown[i](); } return asyncCtx.complete(); } try { const result = await fn({ asyncCtx, use: fn => captureSelf(dispose => { teardown.push(dispose); return ASYNC.inject(asyncCtx, fn); }), }); await cleanup(); return result; } catch (error) { try { await cleanup(); } catch { } throw error; } } function runTest(fn) { return captureSelf(dispose => { try { return fn(); } finally { dispose(); } }); } function querySelector(view, selector) { for (const node of viewNodes(view)) { if (node.nodeType === 1) { if (node.matches(selector)) { return node; } const elem = node.querySelector(selector); if (elem !== null) { return elem; } } } return null; } function querySelectorAll(view, selector) { const elems = []; for (const node of viewNodes(view)) { if (node.nodeType === 1) { if (node.matches(selector)) { elems.push(node); } elems.push(...node.querySelectorAll(selector)); } } return elems; } export { PollTimeoutError, assertViewState, exclusive, poll, querySelector, querySelectorAll, runAsyncTest, runTest };