UNPKG

redioactive

Version:

Reactive streams for chaining overlapping promises.

112 lines (111 loc) 4.69 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; Object.defineProperty(exports, "__esModule", { value: true }); const redio_1 = __importStar(require("../redio")); const toArrayWait = (src, delay, options) => { const wait = (t) => new Promise((r) => setTimeout(() => r(t), t)); const result = []; const promisedArray = new Promise((resolve, reject) => { const sp = src.spout(async (tt) => { if ((0, redio_1.isEnd)(tt)) { resolve(result); } else if ((0, redio_1.isValue)(tt)) { if (delay > 0) await wait(delay); result.push(tt); } }, options); sp.catch(reject); return sp; }); return promisedArray; }; describe('Forking values in a stream', () => { test('Simple single fork', async () => { const src = (0, redio_1.default)([1, 2, 3, 4, 5, 6], { bufferSizeMax: 2 }); const result = await src.fork().toArray(); expect(result).toEqual([1, 2, 3, 4, 5, 6]); }); test('Fork to two streams', async () => { const src = (0, redio_1.default)([1, 2, 3, 4, 5, 6], { bufferSizeMax: 2 }); const result = await Promise.all([src.fork().toArray(), src.fork().toArray()]); expect(result).toEqual([ [1, 2, 3, 4, 5, 6], [1, 2, 3, 4, 5, 6] ]); }); test('Fork to three streams', async () => { const src = (0, redio_1.default)([1, 2, 3, 4, 5, 6], { bufferSizeMax: 2 }); const result = await Promise.all([ src.fork().toArray(), src.fork().toArray(), src.fork().toArray() ]); expect(result).toEqual([ [1, 2, 3, 4, 5, 6], [1, 2, 3, 4, 5, 6], [1, 2, 3, 4, 5, 6] ]); }); test('Fork to two streams with different delays', async () => { const src = (0, redio_1.default)([1, 2, 3, 4, 5, 6], { bufferSizeMax: 2 }); const f1 = toArrayWait(src.fork({ bufferSizeMax: 1 }), 10, { bufferSizeMax: 1 }); const f2 = toArrayWait(src.fork({ bufferSizeMax: 1 }), 50, { bufferSizeMax: 1 }); const result = await Promise.all([f1, f2]); expect(result).toEqual([ [1, 2, 3, 4, 5, 6], [1, 2, 3, 4, 5, 6] ]); }, 1000); test('Fork to three streams with different delays', async () => { const src = (0, redio_1.default)([1, 2, 3, 4, 5, 6], { bufferSizeMax: 2 }); const f1 = toArrayWait(src.fork({ bufferSizeMax: 1 }), 10, { bufferSizeMax: 1 }); const f2 = toArrayWait(src.fork({ bufferSizeMax: 1 }), 30, { bufferSizeMax: 1 }); const f3 = toArrayWait(src.fork({ bufferSizeMax: 1 }), 50, { bufferSizeMax: 1 }); const result = await Promise.all([f1, f2, f3]); expect(result).toEqual([ [1, 2, 3, 4, 5, 6], [1, 2, 3, 4, 5, 6], [1, 2, 3, 4, 5, 6] ]); }, 1000); test('Async single fork & unfork', async () => { const src = (0, redio_1.default)([1, 2, 3, 4, 5, 6]); const f1 = src.fork({ bufferSizeMax: 1 }); const f1Result = toArrayWait(f1, 10); const result = await new Promise((resolve) => { setTimeout(() => { const f2 = src.fork({ bufferSizeMax: 1 }); setTimeout(() => src.unfork(f2), 10); resolve(Promise.all([f1Result, toArrayWait(f2, 4)])); }, 10); }); expect(result).toEqual([ [1, 2, 3, 4, 5, 6], [3, 4] ]); }); });