Something went wrong. Try again.
Offload functions to worker threads with shared memory primitives for Node.js.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051import { describe, it } from 'node:test';import assert from 'node:assert/strict';import { inert, isTask, workers } from 'moroutine';import type { Task } from 'moroutine';import { checkPrime, upper, countUp } from './fixtures/is-task.ts';
describe('isTask', () => { it('exposes a stable id on the moroutine', () => { assert.equal(typeof checkPrime.id, 'string'); assert.ok(checkPrime.id.length > 0); assert.notEqual(checkPrime.id, upper.id); });
it('identifies tasks produced by a moroutine', () => { const t = inert(checkPrime(7)); assert.ok(isTask(checkPrime, t)); assert.ok(!isTask(upper, t)); });
it('works across value and streaming moroutines', () => { const v = inert(checkPrime(7)); const s = inert(countUp(3)); assert.ok(isTask(checkPrime, v)); assert.ok(!isTask(countUp, v)); assert.ok(isTask(countUp, s)); assert.ok(!isTask(checkPrime, s)); });
it('narrows task args for downstream dispatch', async () => { using run = workers(1); const tasks: Array<Task<boolean, [n: number]> | Task<string, [s: string]>> = [ inert(checkPrime(5)), inert(upper('hi')), ]; const results: Array<boolean | string> = []; for (const t of tasks) { if (isTask(checkPrime, t)) { // t is Task<boolean, [n: number]> — args typed as [n: number] const [n] = t.args; assert.equal(typeof n, 'number'); results.push(await run(t)); } else { const [s] = t.args; assert.equal(typeof s, 'string'); results.push(await run(t)); } } assert.deepEqual(results, [true, 'HI']); });});