8 Commits

Author SHA1 Message Date
9035fafdc2 1.0.8 2020-01-23 16:52:22 +00:00
773ae00517 fix(core): update 2020-01-23 16:52:22 +00:00
e67fbfebf6 1.0.7 2020-01-23 15:24:47 +00:00
b2478d79f2 fix(core): update 2020-01-23 15:24:46 +00:00
917e630554 1.0.6 2020-01-23 15:12:55 +00:00
b3b02fee70 fix(core): update 2020-01-23 15:12:55 +00:00
8c257bc0fd 1.0.5 2020-01-23 15:11:30 +00:00
a4ff5c26e2 fix(core): update 2020-01-23 15:11:29 +00:00
4 changed files with 25 additions and 20 deletions

2
package-lock.json generated
View File

@@ -1,6 +1,6 @@
{ {
"name": "@pushrocks/smartrule", "name": "@pushrocks/smartrule",
"version": "1.0.4", "version": "1.0.8",
"lockfileVersion": 1, "lockfileVersion": 1,
"requires": true, "requires": true,
"dependencies": { "dependencies": {

View File

@@ -1,6 +1,6 @@
{ {
"name": "@pushrocks/smartrule", "name": "@pushrocks/smartrule",
"version": "1.0.4", "version": "1.0.8",
"private": false, "private": false,
"description": "a smart rule library for handling decision trees.", "description": "a smart rule library for handling decision trees.",
"main": "dist/index.js", "main": "dist/index.js",

View File

@@ -11,7 +11,7 @@ let testSmartruleInstance: smartrule.SmartRule<ITestMessage>;
tap.test('first test', async () => { tap.test('first test', async () => {
testSmartruleInstance = new smartrule.SmartRule<ITestMessage>(); testSmartruleInstance = new smartrule.SmartRule<ITestMessage>();
testSmartruleInstance.createRule( testSmartruleInstance.createRule(
0, 2,
async messageArg => { async messageArg => {
if (messageArg.body.startsWith('hello')) { if (messageArg.body.startsWith('hello')) {
return 'apply-stop'; return 'apply-stop';

View File

@@ -26,34 +26,39 @@ export class SmartRule<T> {
}; };
// lets run the checks // lets run the checks
const runNextBatch = async (startPriority: number) => { const runNextBatch = async (startPriority: number, runRulesAmount: number): Promise<void> => {
const nextBatch = getNextParallelBatch(0); const nextBatch = getNextParallelBatch(startPriority);
runRulesAmount = runRulesAmount + nextBatch.length;
const outcomes: TTreeActionResult[] = []; const outcomes: TTreeActionResult[] = [];
for (const rule of nextBatch) { for (const rule of nextBatch) {
const checkResult = await rule.checkFunction(objectArg); const checkResult = await rule.checkFunction(objectArg);
if (checkResult.startsWith("apply")) { if (checkResult.startsWith("apply")) {
await rule.actionFunction(objectArg); await rule.actionFunction(objectArg); // here the action function is run
} }
outcomes.push(checkResult); outcomes.push(checkResult);
};
const finalOutcome: TTreeActionResult = outcomes.reduce((previous, current, index, array) => {
if (current.includes('continue') || previous.includes('continue')) {
return 'continue';
} else {
return 'stop';
}
});
if (finalOutcome === 'stop') {
return;
} }
if (startPriority < this.rules[this.rules.length-1].priority) { if (outcomes.length > 0) {
runNextBatch(startPriority++); const finalOutcomeOfBatch: TTreeActionResult = outcomes.reduce((previous, current, index, array) => {
if (current.includes('continue') || previous.includes('continue')) {
return 'continue';
} else {
return 'stop';
}
});
if (finalOutcomeOfBatch === 'stop') {
return;
}
}
if (runRulesAmount < this.rules.length) {
await runNextBatch((startPriority + 1), runRulesAmount);
} else {
return;
} }
}; };
await runNextBatch(0); await runNextBatch(0,0);
} }
public createRule(priorityArg: number, checkFunctionArg: TCheckFunc<T>, actionFunctionArg: TActionFunc) { public createRule(priorityArg: number, checkFunctionArg: TCheckFunc<T>, actionFunctionArg: TActionFunc) {