Skip to content

Commit

Permalink
fix: 🐛 [combine] cache works like [expand]
Browse files Browse the repository at this point in the history
  • Loading branch information
touv committed Mar 30, 2022
1 parent 7c26e58 commit bb5db12
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 11 deletions.
16 changes: 5 additions & 11 deletions packages/analytics/src/combine.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.');
Expand All @@ -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())
Expand Down
102 changes: 102 additions & 0 deletions packages/analytics/test/combine.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
});


0 comments on commit bb5db12

Please sign in to comment.