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({ return new Transform({
...options, ...options,
transform(data: T, encoding, callback) { transform(data: T, encoding, callback) {
try { accumulateBy(data, buffer, this);
accumulateBy(data, buffer, this); callback();
callback();
} catch (err) {
callback(err);
}
}, },
flush(callback) { flush(callback) {
if (shouldFlush) { if (shouldFlush) {

View File

@ -9,23 +9,11 @@ export function filter<T>(
return new Transform({ return new Transform({
...options, ...options,
async transform(chunk: T, encoding?: any, callback?: any) { async transform(chunk: T, encoding?: any, callback?: any) {
let isPromise = false; const result = await predicate(chunk, encoding);
try { if (result === true) {
const result = predicate(chunk, encoding); callback(null, chunk);
isPromise = result instanceof Promise; } else {
if (!!(await result)) { callback();
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({ return new Transform({
...options, ...options,
async transform(chunk: T, encoding, callback) { async transform(chunk: T, encoding, callback) {
let isPromise = false; (await mapper(chunk, encoding)).forEach(c => this.push(c));
try { callback();
const mapped = mapper(chunk, encoding);
isPromise = mapped instanceof Promise;
(await mapped).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, writableObjectMode: true,
}, },
): Transform { ): Transform {
// remove try catch
return new Transform({ return new Transform({
...options, ...options,
async transform(chunk: T, encoding, callback) { async transform(chunk: T, encoding, callback) {

View File

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

View File

@ -16,21 +16,8 @@ export function reduce<T, R>(
readableObjectMode: options.readableObjectMode, readableObjectMode: options.readableObjectMode,
writableObjectMode: options.writableObjectMode, writableObjectMode: options.writableObjectMode,
async transform(chunk: T, encoding, callback) { async transform(chunk: T, encoding, callback) {
let isPromise = false; value = await iteratee(value, chunk, encoding);
try { callback();
const result = iteratee(value, chunk, encoding);
isPromise = result instanceof Promise;
value = await result;
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) { flush(callback) {
// Best effort attempt at yielding the final value (will throw if e.g. yielding an object and // 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); source.push(null);
}); });
test.cb( test.cb.skip(
"accumulatorBy() rolling should emit error when key iteratee throws", "accumulatorBy() rolling should emit error when key iteratee throws",
t => { t => {
t.plan(2); t.plan(2);
@ -511,7 +511,7 @@ test.cb("accumulatorBy() sliding", t => {
source.push(null); source.push(null);
}); });
test.cb( test.cb.skip(
"accumulatorBy() sliding should emit error when key iteratee throws", "accumulatorBy() sliding should emit error when key iteratee throws",
t => { t => {
t.plan(2); 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", "demux() should emit drain event when third stream is bottleneck",
t => { t => {
t.plan(8); t.plan(8);
@ -405,7 +405,7 @@ test.cb(
t.end(err); 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", () => { _demux.on("drain", () => {
expect(_demux._writableState.length).to.be.equal(0); expect(_demux._writableState.length).to.be.equal(0);
expect(performance.now() - start).to.be.greaterThan( expect(performance.now() - start).to.be.greaterThan(
@ -445,7 +445,7 @@ test.cb(
const sink = new Writable({ const sink = new Writable({
objectMode: true, objectMode: true,
write(chunk, encoding, cb) { write(chunk, encoding, cb) {
expect(chunk.mapped).to.deep.equal([1, 2, 3]); expect(chunk.mapped).to.deep.equal([1, 2]);
t.pass(); t.pass();
pendingReads--; pendingReads--;
if (pendingReads === 0) { if (pendingReads === 0) {
@ -493,7 +493,7 @@ test.cb(
t.end(err); 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", () => { _demux.on("drain", () => {
expect(_demux._writableState.length).to.be.equal(0); expect(_demux._writableState.length).to.be.equal(0);
expect(performance.now() - start).to.be.greaterThan( expect(performance.now() - start).to.be.greaterThan(

View File

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

View File

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

View File

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