Compare commits
6 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 17287a9ba3 | |||
| 11921a2864 | |||
| 9035fafdc2 | |||
| 773ae00517 | |||
| e67fbfebf6 | |||
| b2478d79f2 |
2
package-lock.json
generated
2
package-lock.json
generated
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@pushrocks/smartrule",
|
"name": "@pushrocks/smartrule",
|
||||||
"version": "1.0.6",
|
"version": "1.0.9",
|
||||||
"lockfileVersion": 1,
|
"lockfileVersion": 1,
|
||||||
"requires": true,
|
"requires": true,
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@pushrocks/smartrule",
|
"name": "@pushrocks/smartrule",
|
||||||
"version": "1.0.6",
|
"version": "1.0.9",
|
||||||
"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",
|
||||||
|
|||||||
@@ -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';
|
||||||
|
|||||||
@@ -26,13 +26,16 @@ export class SmartRule<T> {
|
|||||||
};
|
};
|
||||||
|
|
||||||
// lets run the checks
|
// lets run the checks
|
||||||
const runNextBatch = async (startPriority: number): Promise<void> => {
|
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);
|
||||||
|
checkResult ? null : console.log('WARNING!!! Please make sure your rule always returns a statement of how to continue!');
|
||||||
|
|
||||||
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);
|
||||||
}
|
}
|
||||||
@@ -50,14 +53,14 @@ export class SmartRule<T> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (startPriority < this.rules[this.rules.length-1].priority) {
|
if (runRulesAmount < this.rules.length) {
|
||||||
await runNextBatch(startPriority++);
|
await runNextBatch((startPriority + 1), runRulesAmount);
|
||||||
} else {
|
} else {
|
||||||
return;
|
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) {
|
||||||
|
|||||||
Reference in New Issue
Block a user