Skip to content
This repository has been archived by the owner on Feb 28, 2022. It is now read-only.

Commit

Permalink
feat(pipe): remove once() in favor of use() (#377)
Browse files Browse the repository at this point in the history
  • Loading branch information
rofe committed Aug 19, 2019
1 parent d9fcb8d commit 1f11922
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 44 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ module.exports.pipe = function(cont, context, action) {

return pipeline()
.use(adjustContent)
.once(cont) // required: execute the continuation function
.use(cont) // execute the continuation function
.use(cleanupContent)
}
```
Expand Down
2 changes: 1 addition & 1 deletion src/defaults/default.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ const Pipeline = require('../pipeline.js');
*/
function pipe(next, context, action) {
const mypipeline = new Pipeline(action);
mypipeline.once(next);
mypipeline.use(next);
return mypipeline.run(context);
}

Expand Down
2 changes: 1 addition & 1 deletion src/defaults/html.pipe.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ const htmlpipe = (cont, context, action) => {
.use(selecttest)
.use(html).expose('html')
.use(sanitize).when(paranoid)
.once(cont)
.use(cont)
.use(type('text/html'))
.use(cache).when(uncached)
.use(key)
Expand Down
2 changes: 1 addition & 1 deletion src/defaults/json.pipe.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ const jsonpipe = (cont, context, action) => {
.use(smartypants)
.use(sections)
.use(meta).expose('meta')
.once(cont)
.use(cont)
.use(emit).expose('json')
.use(type('application/json'))
.use(timer.report)
Expand Down
2 changes: 1 addition & 1 deletion src/defaults/xml.pipe.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ const xmlpipe = (cont, context, action) => {
.use(smartypants)
.use(sections)
.use(meta).expose('meta')
.once(cont)
.use(cont)
.use(emit).expose('xml')
.use(type('application/xml'))
.use(check)
Expand Down
23 changes: 8 additions & 15 deletions src/pipeline.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,17 +88,14 @@ class Pipeline {

// function chain that was defined last. used for `when` and `unless`
this._last = null;
// function that is executed once
// function that is executed once and can register extensions
this._oncef = null;
// step functions to execute
this._steps = [];
// functions that are executed before each step
this._taps = [];

this.attach = (ext) => {
if (this.sealed) {
return;
}
if (ext && ext.before && typeof ext.before === 'object') {
Object.keys(ext.before).map((key) => this.attach.before(key, ext.before[key]));
}
Expand All @@ -108,7 +105,6 @@ class Pipeline {
if (ext && ext.replace && typeof ext.replace === 'object') {
Object.keys(ext.replace).map((key) => this.attach.replace(key, ext.replace[key]));
}
this.sealed = true;
};

/**
Expand Down Expand Up @@ -175,6 +171,13 @@ class Pipeline {
this.describe(f);
this._steps.push(f);
this._last = this._steps;
// check for extensions
if (f && (f.before || f.replace || f.after)) {
if (typeof this._oncef === 'function') {
throw new Error(`Step '${this._oncef.alias}' already registered extensions for this pipeline, refusing to add more with '${f.alias}'.`);
}
this._oncef = f;
}
return this;
}

Expand Down Expand Up @@ -249,16 +252,6 @@ class Pipeline {
return this.when(inverse);
}

/**
* Sets the `once` processing function.
* @param {pipelineFunction} f the `once` function to set
* @returns {Pipeline} this
*/
once(f) {
this._oncef = f;
return this.use(f);
}

/**
* Sets an error function. The error function is executed when an error is encountered.
* @param {pipelineFunction} f the error function.
Expand Down
48 changes: 24 additions & 24 deletions test/testPipeline.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,15 @@ describe('Testing Pipeline', () => {
});

it('Executes without logger', async () => {
await new Pipeline().once(() => {}).run({});
await new Pipeline().use(() => {}).run({});
});

it('Executes correct order', async () => {
const order = [];
await new Pipeline({ logger })
.use(() => { order.push('pre0'); })
.use(() => { order.push('pre1'); })
.once(() => { order.push('once'); })
.use(() => { order.push('once'); })
.use(() => { order.push('post0'); })
.use(() => { order.push('post1'); })
.run();
Expand All @@ -46,7 +46,7 @@ describe('Testing Pipeline', () => {
const pipe = new Pipeline({ logger })
.use(() => { order.push('pre0'); })
.use(() => { order.push('pre1'); })
.once(() => { order.push('once'); })
.use(() => { order.push('once'); })
.use(() => { order.push('post0'); })
.use(() => { order.push('post1'); });

Expand Down Expand Up @@ -87,7 +87,7 @@ describe('Testing Pipeline', () => {

const pipe = new Pipeline({ logger })
.use(second)
.once(() => {
.use(() => {
order.push('middle');
})
.use(third);
Expand Down Expand Up @@ -146,7 +146,7 @@ describe('Testing Pipeline', () => {

const pipe = new Pipeline({ logger })
.use(second)
.once(middle)
.use(middle)
.use(third);

await pipe.run();
Expand Down Expand Up @@ -184,7 +184,7 @@ describe('Testing Pipeline', () => {

await new Pipeline({ logger: validatinglogger })
.use(pre0)
.once(() => { order.push('once'); })
.use(() => { order.push('once'); })
.use(post0)
.run();
assert.deepEqual(order, ['pre0', 'once', 'post0']);
Expand All @@ -197,7 +197,7 @@ describe('Testing Pipeline', () => {
.use(() => { order.push('disabled'); })
.when(() => false)
.use(() => { order.push('pre1'); })
.once(() => { order.push('once'); })
.use(() => { order.push('once'); })
.use(() => { order.push('post0'); })
.use(() => { order.push('post1'); })
.run()
Expand All @@ -215,7 +215,7 @@ describe('Testing Pipeline', () => {
.use(() => { order.push('enabled'); })
.when(() => true)
.use(() => { order.push('pre1'); })
.once(() => { order.push('once'); })
.use(() => { order.push('once'); })
.use(() => { order.push('post0'); })
.use(() => { order.push('post1'); })
.run()
Expand All @@ -233,7 +233,7 @@ describe('Testing Pipeline', () => {
.use(() => { order.push('disabled'); })
.when(() => Promise.resolve(false))
.use(() => { order.push('pre1'); })
.once(() => { order.push('once'); })
.use(() => { order.push('once'); })
.use(() => { order.push('post0'); })
.use(() => { order.push('post1'); })
.run()
Expand All @@ -252,7 +252,7 @@ describe('Testing Pipeline', () => {
.use(() => { order.push('enabled'); })
.when(() => Promise.resolve(true))
.use(() => { order.push('pre1'); })
.once(() => { order.push('once'); })
.use(() => { order.push('once'); })
.use(() => { order.push('post0'); })
.use(() => { order.push('post1'); })
.run()
Expand All @@ -268,7 +268,7 @@ describe('Testing Pipeline', () => {
new Pipeline({ logger })
.use(() => { order.push('pre0'); })
.use(() => { order.push('pre1'); })
.once(() => { order.push('once'); })
.use(() => { order.push('once'); })
.use(() => { order.push('post0'); })
.use(() => { order.push('disabled'); })
.when(() => false)
Expand All @@ -286,7 +286,7 @@ describe('Testing Pipeline', () => {
const order = [];
new Pipeline({ logger })
.use(() => { order.push('pre0'); })
.once(() => { order.push('once'); })
.use(() => { order.push('once'); })
.when(() => false)
.use(() => { order.push('post1'); })
.run()
Expand Down Expand Up @@ -322,7 +322,7 @@ describe('Testing Pipeline', () => {
.use(() => { order.push('disabled'); })
.unless(() => true)
.use(() => { order.push('pre1'); })
.once(() => { order.push('once'); })
.use(() => { order.push('once'); })
.use(() => { order.push('post0'); })
.use(() => { order.push('post1'); })
.run()
Expand All @@ -340,7 +340,7 @@ describe('Testing Pipeline', () => {
.use(() => { order.push('enabled'); })
.when(() => true)
.use(() => { order.push('pre1'); })
.once(() => { order.push('once'); })
.use(() => { order.push('once'); })
.use(() => { order.push('post0'); })
.use(() => { order.push('post1'); })
.run()
Expand All @@ -358,7 +358,7 @@ describe('Testing Pipeline', () => {
.use(() => { order.push('enabled'); })
.unless(() => false)
.use(() => { order.push('pre1'); })
.once(() => { order.push('once'); })
.use(() => { order.push('once'); })
.use(() => { order.push('post0'); })
.use(() => { order.push('post1'); })
.run()
Expand All @@ -371,7 +371,7 @@ describe('Testing Pipeline', () => {

it('Executes promises', async () => {
await new Pipeline({ logger })
.once((v) => new Promise((resolve) => {
.use((v) => new Promise((resolve) => {
setTimeout(() => {
v.foo = 'bar';
resolve();
Expand All @@ -385,7 +385,7 @@ describe('Testing Pipeline', () => {
let cnt = 0;
await new Pipeline({ logger })
.use(() => {})
.once(() => {})
.use(() => {})
.use(() => {})
.every(() => {
cnt += 1;
Expand All @@ -398,7 +398,7 @@ describe('Testing Pipeline', () => {
let cnt = 0;
await new Pipeline({ logger })
.use(() => ({ foo: 'bar' }))
.once(() => {})
.use(() => {})
.use(() => ({ bar: 'baz' }))
.every(() => {
assert.fail('this should not be invoked');
Expand All @@ -417,7 +417,7 @@ describe('Testing Pipeline', () => {
new Pipeline({ logger })
.use(() => { order.push('pre0'); })
.use(() => { order.push('pre1'); })
.once(() => { order.push('once'); })
.use(() => { order.push('once'); })
.use(() => { order.push('post0'); })
.use(() => { order.push('post1'); })
.error(() => { order.push('error'); })
Expand All @@ -437,7 +437,7 @@ describe('Testing Pipeline', () => {
ctx.error = new Error();
})
.use(() => { order.push('pre1'); })
.once(() => { order.push('once'); })
.use(() => { order.push('once'); })
.use(() => { order.push('post0'); })
.use(() => { order.push('post1'); })
.error(() => { order.push('error'); })
Expand All @@ -457,7 +457,7 @@ describe('Testing Pipeline', () => {
throw new Error('stop');
})
.use(() => { order.push('pre1'); })
.once(() => { order.push('once'); })
.use(() => { order.push('once'); })
.use(() => { order.push('post0'); })
.use(() => { order.push('post1'); })
.error(() => { order.push('error'); })
Expand All @@ -481,7 +481,7 @@ describe('Testing Pipeline', () => {
order.push('error0');
ctx.error = null;
})
.once(() => { order.push('once'); })
.use(() => { order.push('once'); })
.use(() => { order.push('post0'); })
.use(() => { order.push('post1'); })
.error(() => { order.push('error1'); })
Expand All @@ -501,7 +501,7 @@ describe('Testing Pipeline', () => {
throw new Error('stop');
})
.use(() => { order.push('pre1'); })
.once(() => { order.push('once'); })
.use(() => { order.push('once'); })
.use(() => { order.push('post0'); })
.use(() => { order.push('post1'); })
.error(() => { throw Error('in error handler'); })
Expand All @@ -515,7 +515,7 @@ describe('Testing Pipeline', () => {
const order = [];
await new Pipeline({ logger })
.use(() => { order.push('pre1'); })
.once({
.use({
get errorHandler() {
throw new Error('generic error');
},
Expand Down

0 comments on commit 1f11922

Please sign in to comment.