redioactive
Version:
Reactive streams for chaining overlapping promises.
200 lines (199 loc) • 8.93 kB
JavaScript
"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"));
describe('Zipping multiple streams', () => {
test('Zip two streams of equal length is as expected', async () => {
const numbers = (0, redio_1.default)([1, 2, 3]);
const words = (0, redio_1.default)(['one', 'two', 'three']);
await expect(numbers.zipEach([words]).toArray()).resolves.toEqual([
[1, 'one'],
[2, 'two'],
[3, 'three']
]);
});
test('Zip three streams of equal length is as expected', async () => {
const numbers = (0, redio_1.default)([1, 2, 3]);
const words = (0, redio_1.default)(['one', 'two', 'three']);
const wordsF = (0, redio_1.default)(['une', 'deux', 'trois']);
await expect(numbers.zipEach([words, wordsF]).toArray()).resolves.toEqual([
[1, 'one', 'une'],
[2, 'two', 'deux'],
[3, 'three', 'trois']
]);
});
test('Zip three empty streams', async () => {
const empty1 = (0, redio_1.default)([]);
const empty2 = (0, redio_1.default)([]);
const empty3 = (0, redio_1.default)([]);
await expect(empty1.zipEach([empty2, empty3]).toArray()).resolves.toEqual([]);
});
test('Zip three streams of different lengths - shorter first', async () => {
const numbers = (0, redio_1.default)([1, 2]);
const words = (0, redio_1.default)(['one', 'two', 'three']);
const wordsF = (0, redio_1.default)(['une', 'deux', 'trois']);
await expect(numbers.zipEach([words, wordsF]).toArray()).resolves.toEqual([
[1, 'one', 'une'],
[2, 'two', 'deux']
]);
});
test('Zip three streams of different lengths - shorter second', async () => {
const numbers = (0, redio_1.default)([1, 2, 3]);
const words = (0, redio_1.default)(['one', 'two']);
const wordsF = (0, redio_1.default)(['une', 'deux', 'trois']);
await expect(numbers.zipEach([words, wordsF]).toArray()).resolves.toEqual([
[1, 'one', 'une'],
[2, 'two', 'deux'],
[3, redio_1.end, 'trois']
]);
});
test('Zip three streams of different lengths - shorter third', async () => {
const numbers = (0, redio_1.default)([1, 2, 3]);
const words = (0, redio_1.default)(['one', 'two', 'three']);
const wordsF = (0, redio_1.default)(['une', 'deux']);
await expect(numbers.zipEach([words, wordsF]).toArray()).resolves.toEqual([
[1, 'one', 'une'],
[2, 'two', 'deux'],
[3, 'three', redio_1.end]
]);
});
test('Zip one stream with null array', async () => {
const numbers = (0, redio_1.default)([1, 2, 3]);
await expect(numbers.zipEach([]).toArray()).resolves.toEqual([[1], [2], [3]]);
});
test('Zip two streams with one empty - empty second', async () => {
const numbers = (0, redio_1.default)([1, 2, 3]);
const words = (0, redio_1.default)([]);
await expect(numbers.zipEach([words]).toArray()).resolves.toEqual([
[1, redio_1.end],
[2, redio_1.end],
[3, redio_1.end]
]);
});
test('Zip three streams with one empty - empty first', async () => {
const numbers = (0, redio_1.default)([]);
const words = (0, redio_1.default)(['one', 'two', 'three']);
const wordsF = (0, redio_1.default)(['une', 'deux', 'trois']);
await expect(numbers.zipEach([words, wordsF]).toArray()).resolves.toEqual([]);
});
test('Zip three streams with one empty - empty second', async () => {
const numbers = (0, redio_1.default)([1, 2, 3]);
const words = (0, redio_1.default)([]);
const wordsF = (0, redio_1.default)(['une', 'deux', 'trois']);
await expect(numbers.zipEach([words, wordsF]).toArray()).resolves.toEqual([
[1, redio_1.end, 'une'],
[2, redio_1.end, 'deux'],
[3, redio_1.end, 'trois']
]);
});
test('Zip three streams with one empty - empty third', async () => {
const numbers = (0, redio_1.default)([1, 2, 3]);
const words = (0, redio_1.default)(['one', 'two', 'three']);
const wordsF = (0, redio_1.default)([]);
await expect(numbers.zipEach([words, wordsF]).toArray()).resolves.toEqual([
[1, 'one', redio_1.end],
[2, 'two', redio_1.end],
[3, 'three', redio_1.end]
]);
});
test('Zip three streams with big difference in lengths', async () => {
const numbers = (0, redio_1.default)([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]);
const words = (0, redio_1.default)(['one', 'two', 'three']);
const wordsF = (0, redio_1.default)(['une', 'deux', 'trois']);
await expect(numbers.zipEach([words, wordsF]).toArray()).resolves.toEqual([
[1, 'one', 'une'],
[2, 'two', 'deux'],
[3, 'three', 'trois'],
[4, redio_1.end, redio_1.end],
[5, redio_1.end, redio_1.end],
[6, redio_1.end, redio_1.end],
[7, redio_1.end, redio_1.end],
[8, redio_1.end, redio_1.end],
[9, redio_1.end, redio_1.end],
[10, redio_1.end, redio_1.end],
[11, redio_1.end, redio_1.end],
[12, redio_1.end, redio_1.end]
]);
});
test('Zip with different speed streams', async () => {
const wait = (t) => new Promise((r) => setTimeout(() => r(t), t * 5));
function addWait(n) {
let count = 0;
return async () => (count < n ? await wait(++count) : redio_1.end);
}
await expect((0, redio_1.default)(addWait(3))
.zipEach([(0, redio_1.default)(['one', 'two', 'three'])])
.toArray()).resolves.toEqual([
[1, 'one'],
[2, 'two'],
[3, 'three']
]);
});
test('Zip with change of source stream', async () => {
const numbers = (0, redio_1.default)([1, 2, 3, 4, 5, 6, 7, 8], { bufferSizeMax: 2 });
const wordsEN = (0, redio_1.default)(['one', 'two', 'three'], { bufferSizeMax: 1 });
const wordsFR = (0, redio_1.default)(['une', 'deux', 'trois'], { bufferSizeMax: 1 });
let switched = false;
const sourcePipes = [wordsEN];
const endValve = async (frames) => {
if ((0, redio_1.isValue)(frames)) {
// console.log('end valve:', frames, frames.length)
if (frames.reduce((acc, f) => acc && (0, redio_1.isValue)(f), true)) {
return frames;
}
if (!switched) {
// a source has ended, switch to new source
sourcePipes.splice(0);
sourcePipes.push(wordsFR);
}
switched = true;
return redio_1.nil; //[frames[0], ...[]]
}
else {
return frames;
}
};
const strValve = async (frames) => {
if ((0, redio_1.isValue)(frames)) {
// console.log('str valve:', frames)
return frames;
}
else {
return frames;
}
};
const pipe = numbers.zipEach(sourcePipes).valve(endValve).valve(strValve);
await expect(pipe.toArray()).resolves.toEqual([
[1, 'one'],
[2, 'two'],
[3, 'three'],
// [4],
// [5],
[6, 'une'],
[7, 'deux'],
[8, 'trois']
]);
});
});