Comparing version 4.10.0 to 4.11.0
@@ -126,2 +126,3 @@ (function webpackUniversalModuleDefinition(root, factory) { | ||
this.branches = (0, _convertToBranches.convertToBranches)(branches); | ||
this.chainBranches = []; | ||
} | ||
@@ -141,2 +142,14 @@ | ||
}, { | ||
key: "chain", | ||
value: function chain(branch) { | ||
var chainBranch = (0, _convertToBranches.convertToBranch)(branch); | ||
this.chainBranches.push(chainBranch); | ||
return this; | ||
} | ||
}, { | ||
key: "getChain", | ||
value: function getChain() { | ||
return this.chainBranches; | ||
} | ||
}, { | ||
key: "isExceptionHandler", | ||
@@ -304,8 +317,8 @@ value: function isExceptionHandler() { | ||
return _bluebird2.default.resolve() | ||
// TODO: consider if we want check condition of main/root branch | ||
.then(function () { | ||
return _bluebird2.default.resolve().then(function () { | ||
return currentBranch.condition(value); | ||
}).then(function (conditionResult) { | ||
return conditionResult ? executeBranchAction(value, { branch: currentBranch }) : value; | ||
}).then(function (value) { | ||
return executeChain(currentBranch.getChain(), value); | ||
}); | ||
@@ -346,2 +359,14 @@ } | ||
function executeChain(branches, value) { | ||
var index = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 0; | ||
if (branches.length <= index) { | ||
return _bluebird2.default.resolve(value); | ||
} | ||
var branch = branches[index]; | ||
return executeBranch(value, { branch: branch }).then(function (value) { | ||
return executeChain(branches, value, index + 1); | ||
}); | ||
} | ||
/***/ }), | ||
@@ -348,0 +373,0 @@ /* 6 */ |
{ | ||
"name": "horpyna", | ||
"version": "4.10.0", | ||
"version": "4.11.0", | ||
"description": "utility to manage async processes", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
@@ -42,3 +42,4 @@ # HORPYNA | ||
action: Function, | ||
branches: Array<Branch> | ||
branches: Array<Branch>, | ||
exceptionHandler: Bool | ||
} | ||
@@ -210,2 +211,35 @@ ``` | ||
### branch.chain(branch): Branch | ||
Add another branch to queue. All branches in queue will be invoked one by one after all child branches | ||
are done. Return current branch | ||
#### example | ||
```javascript | ||
import { Branch } from "Horpyna"; | ||
const mainBranch = new Branch({ | ||
name: "branchA", | ||
condition: () => true, | ||
action: value => value + "A", | ||
branches: [{ | ||
name: "branchB", | ||
condition: () => true, | ||
action: value => value + "B", | ||
}] | ||
}); | ||
mainBranch.chain({ | ||
name: "branchC", | ||
condition: () => true, | ||
action: value => value + "C", | ||
}) | ||
mainBranch.chain({ | ||
name: "branchD", | ||
condition: () => true, | ||
action: value => value + "D", | ||
}) | ||
mainBranch.execute("") | ||
.then(console.log)//"ABCD" | ||
``` | ||
### branch,setName(name: string): branch | ||
@@ -212,0 +246,0 @@ |
@@ -19,2 +19,3 @@ import { convertToBranches, convertToBranch } from "./convertToBranches"; | ||
this.branches = convertToBranches(branches); | ||
this.chainBranches = []; | ||
} | ||
@@ -30,2 +31,10 @@ clone() { | ||
} | ||
chain(branch) { | ||
const chainBranch = convertToBranch(branch); | ||
this.chainBranches.push(chainBranch); | ||
return this; | ||
} | ||
getChain() { | ||
return this.chainBranches; | ||
} | ||
isExceptionHandler() { | ||
@@ -32,0 +41,0 @@ return this.exceptionHandler; |
@@ -192,2 +192,49 @@ /* eslint-disable no-unused-vars, no-new */ | ||
}); | ||
describe("chain", () => { | ||
it("should execute all branches in chain", () => { | ||
const mainBranch = new Branch({ | ||
name: "mainBranch", | ||
action: value => value + "A" | ||
}); | ||
return mainBranch | ||
.chain({ | ||
name: "branch1", | ||
action: value => value + "B" | ||
}) | ||
.chain({ | ||
name: "branch2", | ||
action: value => value + "C" | ||
}) | ||
.chain({ | ||
name: "branch3", | ||
action: value => value + "D" | ||
}) | ||
.execute("") | ||
.then(value => expect(value).to.be.equal("ABCD")); | ||
}); | ||
it("should execute all branches in chain except one", () => { | ||
const mainBranch = new Branch({ | ||
name: "mainBranch", | ||
action: value => value + "A" | ||
}); | ||
return mainBranch | ||
.chain({ | ||
name: "branch1", | ||
action: value => value + "B" | ||
}) | ||
.chain({ | ||
name: "branch2", | ||
condition: () => false, | ||
action: value => value + "C" | ||
}) | ||
.chain({ | ||
name: "branch3", | ||
action: value => value + "D" | ||
}) | ||
.execute("") | ||
.then(value => expect(value).to.be.equal("ABD")); | ||
}); | ||
}); | ||
}); |
import Promise from "bluebird"; | ||
export default function executeBranch(value, { branch: currentBranch }) { | ||
return ( | ||
Promise.resolve() | ||
// TODO: consider if we want check condition of main/root branch | ||
.then(() => currentBranch.condition(value)) | ||
.then(conditionResult => (conditionResult ? executeBranchAction(value, { branch: currentBranch }) : value)) | ||
); | ||
return Promise.resolve() | ||
.then(() => currentBranch.condition(value)) | ||
.then(conditionResult => (conditionResult ? executeBranchAction(value, { branch: currentBranch }) : value)) | ||
.then(value => executeChain(currentBranch.getChain(), value)); | ||
} | ||
@@ -43,1 +41,9 @@ | ||
} | ||
function executeChain(branches, value, index = 0) { | ||
if (branches.length <= index) { | ||
return Promise.resolve(value); | ||
} | ||
const branch = branches[index]; | ||
return executeBranch(value, { branch }).then(value => executeChain(branches, value, index + 1)); | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
118821
1035
274