Remove try catch from provided functions, user handles errors

This commit is contained in:
Jerry Kurian 2019-09-12 09:41:04 -04:00
parent 586f618e95
commit 517e281ce5
11 changed files with 26 additions and 74 deletions

View File

@ -18,12 +18,8 @@ function _accumulator<T>(
return new Transform({
...options,
transform(data: T, encoding, callback) {
try {
accumulateBy(data, buffer, this);
callback();
} catch (err) {
callback(err);
}
},
flush(callback) {
if (shouldFlush) {

View File

@ -9,24 +9,12 @@ export function filter<T>(
return new Transform({
...options,
async transform(chunk: T, encoding?: any, callback?: any) {
let isPromise = false;
try {
const result = predicate(chunk, encoding);
isPromise = result instanceof Promise;
if (!!(await result)) {
const result = await predicate(chunk, encoding);
if (result === true) {
callback(null, chunk);
} else {
callback();
}
} catch (err) {
if (isPromise) {
// Calling the callback asynchronously with an error wouldn't emit the error, so emit directly
this.emit("error", err);
callback();
} else {
callback(err);
}
}
},
});
}

View File

@ -13,21 +13,8 @@ export function flatMap<T, R>(
return new Transform({
...options,
async transform(chunk: T, encoding, callback) {
let isPromise = false;
try {
const mapped = mapper(chunk, encoding);
isPromise = mapped instanceof Promise;
(await mapped).forEach(c => this.push(c));
(await mapper(chunk, encoding)).forEach(c => this.push(c));
callback();
} catch (err) {
if (isPromise) {
// Calling the callback asynchronously with an error wouldn't emit the error, so emit directly
this.emit("error", err);
callback();
} else {
callback(err);
}
}
},
});
}

View File

@ -8,7 +8,6 @@ export function map<T, R>(
writableObjectMode: true,
},
): Transform {
// remove try catch
return new Transform({
...options,
async transform(chunk: T, encoding, callback) {

View File

@ -20,14 +20,9 @@ export function parallelMap<T, R>(
}
inflight += 1;
callback();
try {
const res = await mapper(data);
this.push(res);
} catch (e) {
this.emit(e);
} finally {
inflight -= 1;
}
},
async flush(callback) {
while (inflight > 0) {

View File

@ -16,21 +16,8 @@ export function reduce<T, R>(
readableObjectMode: options.readableObjectMode,
writableObjectMode: options.writableObjectMode,
async transform(chunk: T, encoding, callback) {
let isPromise = false;
try {
const result = iteratee(value, chunk, encoding);
isPromise = result instanceof Promise;
value = await result;
value = await iteratee(value, chunk, encoding);
callback();
} catch (err) {
if (isPromise) {
// Calling the callback asynchronously with an error wouldn't emit the error, so emit directly
this.emit("error", err);
callback();
} else {
callback(err);
}
}
},
flush(callback) {
// Best effort attempt at yielding the final value (will throw if e.g. yielding an object and

View File

@ -407,7 +407,7 @@ test.cb("accumulatorBy() rolling", t => {
source.push(null);
});
test.cb(
test.cb.skip(
"accumulatorBy() rolling should emit error when key iteratee throws",
t => {
t.plan(2);
@ -511,7 +511,7 @@ test.cb("accumulatorBy() sliding", t => {
source.push(null);
});
test.cb(
test.cb.skip(
"accumulatorBy() sliding should emit error when key iteratee throws",
t => {
t.plan(2);

View File

@ -354,7 +354,7 @@ test("demux() should emit one drain event when writing 6 items with highWaterMar
});
});
test.cb(
test.cb.only(
"demux() should emit drain event when third stream is bottleneck",
t => {
t.plan(8);
@ -405,7 +405,7 @@ test.cb(
t.end(err);
});
// This event should be received after at least 3 * slowProcessorSpeed (two are read immediately by first)
// This event should be received after at least 5 * slowProcessorSpeed (two are read immediately by first and second, 5 remaining in demux before drain event)
_demux.on("drain", () => {
expect(_demux._writableState.length).to.be.equal(0);
expect(performance.now() - start).to.be.greaterThan(
@ -445,7 +445,7 @@ test.cb(
const sink = new Writable({
objectMode: true,
write(chunk, encoding, cb) {
expect(chunk.mapped).to.deep.equal([1, 2, 3]);
expect(chunk.mapped).to.deep.equal([1, 2]);
t.pass();
pendingReads--;
if (pendingReads === 0) {
@ -493,7 +493,7 @@ test.cb(
t.end(err);
});
// This event should be received after at least 3 * slowProcessorSpeed (two are read immediately by first)
// This event should be received after at least 3 * slowProcessorSpeed (two are read immediately by first and second, 3 remaining in demux before drain event)
_demux.on("drain", () => {
expect(_demux._writableState.length).to.be.equal(0);
expect(performance.now() - start).to.be.greaterThan(

View File

@ -58,7 +58,7 @@ test.cb("filter() filters elements asynchronously", t => {
source.push(null);
});
test.cb("filter() emits errors during synchronous filtering", t => {
test.cb.skip("filter() emits errors during synchronous filtering", t => {
t.plan(2);
const source = new Readable({ objectMode: true });
source
@ -86,7 +86,7 @@ test.cb("filter() emits errors during synchronous filtering", t => {
source.push(null);
});
test.cb("filter() emits errors during asynchronous filtering", t => {
test.cb.skip("filter() emits errors during asynchronous filtering", t => {
t.plan(2);
const source = new Readable({ objectMode: true });
source

View File

@ -48,7 +48,7 @@ test.cb("flatMap() maps elements asynchronously", t => {
source.push(null);
});
test.cb("flatMap() emits errors during synchronous mapping", t => {
test.cb.skip("flatMap() emits errors during synchronous mapping", t => {
t.plan(2);
const source = new Readable({ objectMode: true });
source
@ -73,7 +73,7 @@ test.cb("flatMap() emits errors during synchronous mapping", t => {
source.push(null);
});
test.cb("flatMap() emits errors during asynchronous mapping", t => {
test.cb.skip("flatMap() emits errors during asynchronous mapping", t => {
t.plan(2);
const source = new Readable({ objectMode: true });
source

View File

@ -46,7 +46,7 @@ test.cb("reduce() reduces elements asynchronously", t => {
source.push(null);
});
test.cb("reduce() emits errors during synchronous reduce", t => {
test.cb.skip("reduce() emits errors during synchronous reduce", t => {
t.plan(2);
const source = new Readable({ objectMode: true });
source
@ -71,7 +71,7 @@ test.cb("reduce() emits errors during synchronous reduce", t => {
source.push(null);
});
test.cb("reduce() emits errors during asynchronous reduce", t => {
test.cb.skip("reduce() emits errors during asynchronous reduce", t => {
t.plan(2);
const source = new Readable({ objectMode: true });
source