Offload functions to worker threads with shared memory primitives for Node.js.
1import { describe, it } from 'node:test';
2import assert from 'node:assert/strict';
3import { mo } from 'moroutine';
4
5describe('AsyncIterableTask', () => {
6 it('mo() with async function* returns AsyncIterableTask', async () => {
7 const { AsyncIterableTask } = await import('../src/stream-task.ts');
8 const gen = mo(import.meta, async function* () {
9 yield 1;
10 });
11 const task = gen();
12 assert.ok(task instanceof AsyncIterableTask);
13 });
14
15 it('mo() with regular function still returns PromiseLikeTask', async () => {
16 const { PromiseLikeTask } = await import('../src/task.ts');
17 const { AsyncIterableTask } = await import('../src/stream-task.ts');
18 const fn = mo(import.meta, (x: number) => x * 2);
19 const task = fn(2);
20 assert.ok(task instanceof PromiseLikeTask);
21 assert.ok(!(task instanceof AsyncIterableTask));
22 });
23
24 it('AsyncIterableTask has uid, id, and args', async () => {
25 const gen = mo(import.meta, async function* (n: number) {
26 yield n;
27 });
28 const task = gen(5);
29 assert.equal(typeof task.id, 'string');
30 assert.deepEqual(task.args, [5]);
31 assert.equal(typeof task.uid, 'number');
32 });
33
34 it('AsyncIterableTask uids are unique', async () => {
35 const gen = mo(import.meta, async function* () {
36 yield 1;
37 });
38 const a = gen();
39 const b = gen();
40 assert.notEqual(a.uid, b.uid);
41 });
42});