diff --git a/packages/analytics/src/combine.js b/packages/analytics/src/combine.js index 0bc5ea33..508f4a6c 100644 --- a/packages/analytics/src/combine.js +++ b/packages/analytics/src/combine.js @@ -64,13 +64,13 @@ function sha1(input, salt) { * @param {String} [script] the external pipeline is described in a string of characters * @param {String} [commands] the external pipeline is described in a object * @param {String} [command] the external pipeline is described in a URL-like command - * @param {String} [persistent=false] The internal database will be reused until it is deleted - * @param {String} [cache] Use a specific ezs statement to run commands (advanced) + * @param {String} [cacheName] Enable cache, with dedicated name * @returns {Object} */ export default function combine(data, feed) { const { ezs } = this; - const persistent = Boolean(this.getParam('persistent', false)); + const cacheName = this.getParam('cacheName'); + const persistent = Boolean(cacheName); let whenReady = Promise.resolve(true); if (this.isFirst()) { debug('ezs')('[combine] with sub pipeline.'); @@ -86,20 +86,14 @@ export default function combine(data, feed) { append: this.getParam('append'), }); if (persistent) { - this.store = createPersistentStore(ezs, sha1(commands, primer), location); + this.store = createPersistentStore(ezs, `combine${cacheName}`, location); } else { this.store = createStore(ezs, 'combine', location); } if (persistent && !this.store.isCreated()) { whenReady = Promise.resolve(true); } else { - const cache = this.getParam('cache'); - let statements; - if (cache) { - statements = [ezs(cache, { commands }, this.getEnv())]; - } else { - statements = ezs.compileCommands(commands, this.getEnv()); - } + const statements = ezs.compileCommands(commands, this.getEnv()); const output = ezs.createPipeline(input, statements) .pipe(ezs(saveIn, null, this.store)) .pipe(ezs.catch()) diff --git a/packages/analytics/test/combine.js b/packages/analytics/test/combine.js index 7d88d3fd..8c152433 100644 --- a/packages/analytics/test/combine.js +++ b/packages/analytics/test/combine.js @@ -377,3 +377,105 @@ describe('no combine', () => { }); }); }); + +const env = { + executed: false, +}; +const cacheName = Date.now(); + +test('combine with cache with script #1', (done) => { + ezs.use(statements); + const input = [ + { a: 1, b: 'a' }, + { a: 2, b: 'b' }, + { a: 3, b: 'c' }, + { a: 4, b: 'd' }, + { a: 5, b: 'e' }, + { a: 6, b: 'f' }, + ]; + const output = []; + const script = ` + [use] + plugin = analytics + + [env] + path = executed + value = fix(true) + + [replace] + path = value + value = fix({id:'a', value:'aa'},{id:'b', value:'bb'},{id:'c', value:'cc'},{id:'d', value:'dd'},{id:'e', value:'ee'},{id:'f', value:'ff'}) + + [exploding] + [value] + `; + + from(input) + .pipe(ezs('combine', { path: 'b', script, cacheName }, env)) + .pipe(ezs.catch()) + .on('error', done) + .on('data', (chunk) => { + output.push(chunk); + }) + .on('end', () => { + assert.equal(output.length, 6); + assert.equal(output[0].b.value, 'aa'); + assert.equal(output[1].b.value, 'bb'); + assert.equal(output[2].b.value, 'cc'); + assert.equal(output[3].b.value, 'dd'); + assert.equal(output[4].b.value, 'ee'); + assert.equal(output[5].b.value, 'ff'); + assert.equal(env.executed, true); + env.executed = false; + done(); + }); +}); + +test('combine with cache with script #2', (done) => { + ezs.use(statements); + const input = [ + { a: 1, b: 'a' }, + { a: 2, b: 'b' }, + { a: 3, b: 'c' }, + { a: 4, b: 'd' }, + { a: 5, b: 'e' }, + { a: 6, b: 'f' }, + ]; + const output = []; + const script = ` + [use] + plugin = analytics + + [env] + path = executed + value = fix(true) + + [replace] + path = value + value = fix({id:'a', value:'aa'},{id:'b', value:'bb'},{id:'c', value:'cc'},{id:'d', value:'dd'},{id:'e', value:'ee'},{id:'f', value:'ff'}) + + [exploding] + [value] + `; + + from(input) + .pipe(ezs('combine', { path: 'b', script, cacheName }, env)) + .pipe(ezs.catch()) + .on('error', done) + .on('data', (chunk) => { + output.push(chunk); + }) + .on('end', () => { + assert.equal(output.length, 6); + assert.equal(output[0].b.value, 'aa'); + assert.equal(output[1].b.value, 'bb'); + assert.equal(output[2].b.value, 'cc'); + assert.equal(output[3].b.value, 'dd'); + assert.equal(output[4].b.value, 'ee'); + assert.equal(output[5].b.value, 'ff'); + assert.equal(env.executed, false); + done(); + }); +}); + +