Skip to content

Commit

Permalink
Merge pull request #8 from catbee/history-for-async-actions
Browse files Browse the repository at this point in the history
Async actions cache
  • Loading branch information
markuplab committed Jun 4, 2016
2 parents 9d532b8 + becdbb2 commit 1793ecb
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 6 deletions.
28 changes: 24 additions & 4 deletions src/appstate.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ module.exports = {
create (actions) {
analyze(actions);

return (state, services = {}, args = {}) => {
return (state, services = {}, args = {}, history) => {
return new Promise((resolve, reject) => {
var promise = { resolve, reject };
var start = Date.now();
Expand All @@ -43,7 +43,7 @@ module.exports = {

// Create signal definition
var signal = {
args,
args, history,
branches: tree.branches,
isExecuting: true,
duration: 0
Expand Down Expand Up @@ -124,9 +124,27 @@ function runAsyncBranch (index, currentBranch, options) {
action.isExecuting = true;
action.args = merge({}, args);

var next = createNextAsyncAction(actionFunc, outputs);
var next;

actionFunc.apply(null, actionArgs.concat(next.fn, services));
// If history provided, emulate action run,
// by passing to next action with found path and args
if (signal.history) {
var cachedAction = signal.history;

action.path.forEach((branchId) => {
cachedAction = cachedAction[branchId];
});

next = Object.create(null);

next.promise = Promise.resolve({
path: cachedAction.outputPath,
args: cachedAction.output
});
} else {
next = createNextAsyncAction(actionFunc, outputs);
actionFunc.apply(null, actionArgs.concat(next.fn, services));
}

return next.promise
.then(result => {
Expand Down Expand Up @@ -494,6 +512,8 @@ function transformSyncBranch (action, parentAction, path, actions, isSync) {
* @param {Array} actions
*/
function analyze (actions) {


actions.forEach((action, index) => {
if (typeof action === 'undefined' || typeof action === 'string') {
throw new Error(
Expand Down
60 changes: 58 additions & 2 deletions test/suites/appstate.js
Original file line number Diff line number Diff line change
Expand Up @@ -526,7 +526,7 @@ lab.experiment('#appstate', function () {
}).catch(done);
});

lab.test('should throw error if function defined in async action is miss', function(done) {
lab.test('should throw error if function defined in async action is miss', (done) => {
var asyncActionsGroup = {};

function async (args, state, output) {
Expand All @@ -551,7 +551,7 @@ lab.experiment('#appstate', function () {
done();
});

lab.test('should throw error if function defined in sync action is miss', function(done) {
lab.test('should throw error if function defined in sync action is miss', (done) => {
var syncActionsGroup = {};

function sync (args, state, output) {
Expand All @@ -571,4 +571,60 @@ lab.experiment('#appstate', function () {
assert.throws(appstate.create.bind(null, actions), Error);
done();
});

lab.test('should use history in async actions if it passed', (done) => {
var counter1 = 0;
var counter2 = 0;
var outputTimes1 = 0;
var outputTimes2 = 0;

function async (args, state, output) {
counter1 += 1;
output.success({ test: 'test' });
}

function sync (args) {
assert(args.test);
outputTimes1 += 1;
}

function async2 (args, state, output) {
counter2 += 1;
output.success({ test2: 'test' });
}

function sync2 (args) {
assert(args.test2);
outputTimes2 += 1;
}

var signal = appstate.create([
[
async, {
success: [
sync
]
},
async2, {
success: [
sync2
]
}
]
]);

signal(tree)
.then((result) => {
return signal(tree, {}, {}, result.branches);
})
.then(() => {
assert.equal(counter1, 1);
assert.equal(counter2, 1);
assert.equal(outputTimes1, 2);
assert.equal(outputTimes2, 2);

done();
})
.catch(done);
});
});

0 comments on commit 1793ecb

Please sign in to comment.