This commit is contained in:
Jerry Kurian 2019-08-15 15:42:54 -04:00
parent 27b4b2427b
commit 5a9fcc94a6
2 changed files with 35 additions and 31 deletions

View File

@ -44,6 +44,7 @@ function _sliding<T>(
)})`, )})`,
), ),
); );
stream.resume();
return; return;
} }
while ( while (
@ -107,6 +108,7 @@ function _rolling<T>(
)})`, )})`,
), ),
); );
stream.resume();
return; return;
} else if ( } else if (
buffer.length > 0 && buffer.length > 0 &&

View File

@ -6,10 +6,11 @@ import { map } from ".";
test.cb("map() maps elements synchronously", t => { test.cb("map() maps elements synchronously", t => {
t.plan(3); t.plan(3);
const source = new Readable({ objectMode: true }); const source = new Readable({ objectMode: true });
const mapStream = map((element: string) => element.toUpperCase());
const expectedElements = ["A", "B", "C"]; const expectedElements = ["A", "B", "C"];
let i = 0; let i = 0;
source source
.pipe(map((element: string) => element.toUpperCase())) .pipe(mapStream)
.on("data", (element: string) => { .on("data", (element: string) => {
expect(element).to.equal(expectedElements[i]); expect(element).to.equal(expectedElements[i]);
t.pass(); t.pass();
@ -27,15 +28,14 @@ test.cb("map() maps elements synchronously", t => {
test.cb("map() maps elements asynchronously", t => { test.cb("map() maps elements asynchronously", t => {
t.plan(3); t.plan(3);
const source = new Readable({ objectMode: true }); const source = new Readable({ objectMode: true });
const mapStream = map(async (element: string) => {
await Promise.resolve();
return element.toUpperCase();
});
const expectedElements = ["A", "B", "C"]; const expectedElements = ["A", "B", "C"];
let i = 0; let i = 0;
source source
.pipe( .pipe(mapStream)
map(async (element: string) => {
await Promise.resolve();
return element.toUpperCase();
}),
)
.on("data", (element: string) => { .on("data", (element: string) => {
expect(element).to.equal(expectedElements[i]); expect(element).to.equal(expectedElements[i]);
t.pass(); t.pass();
@ -51,19 +51,23 @@ test.cb("map() maps elements asynchronously", t => {
}); });
test.cb("map() emits errors during synchronous mapping", t => { test.cb("map() emits errors during synchronous mapping", t => {
t.plan(2); t.plan(3);
const source = new Readable({ objectMode: true }); const source = new Readable({ objectMode: true });
const mapStream = map((element: string) => {
if (element !== "b") {
throw new Error("Failed mapping");
}
return element.toUpperCase();
});
source source
.pipe( .pipe(mapStream)
map((element: string) => { .on("data", data => {
if (element !== "a") { expect(data).to.equal("B");
throw new Error("Failed mapping"); t.pass();
} })
return element.toUpperCase();
}),
)
.resume()
.on("error", err => { .on("error", err => {
source.pipe(mapStream);
mapStream.resume();
expect(err.message).to.equal("Failed mapping"); expect(err.message).to.equal("Failed mapping");
t.pass(); t.pass();
}) })
@ -77,31 +81,29 @@ test.cb("map() emits errors during synchronous mapping", t => {
test("map() emits errors during asynchronous mapping", t => { test("map() emits errors during asynchronous mapping", t => {
t.plan(1); t.plan(1);
return new Promise((resolve, reject) => { return new Promise((resolve, _) => {
const source = new Readable({ objectMode: true }); const source = new Readable({ objectMode: true });
const mapStream = map(async (element: string) => {
await Promise.resolve();
if (element === "b") {
throw new Error("Failed mapping");
}
return element.toUpperCase();
});
source source
.pipe( .pipe(mapStream)
map(async (element: string) => {
await Promise.resolve();
if (element !== "a") {
throw new Error("Failed mapping");
}
return element.toUpperCase();
}),
)
.resume()
.on("error", err => { .on("error", err => {
expect(err.message).to.equal("Failed mapping"); expect(err.message).to.equal("Failed mapping");
t.pass(); t.pass();
resolve(); resolve();
}) })
.on("end", () => { .on("end", () => t.fail);
t.fail();
});
source.push("a"); source.push("a");
source.push("b"); source.push("b");
source.push("c"); source.push("c");
source.push(null); source.push(null);
source.push(null);
source.push(null);
}); });
}); });