fix(core): update
This commit is contained in:
@@ -1,3 +1 @@
|
||||
import * as plugins from './smartrule.plugins';
|
||||
|
||||
export let standardExport = 'Hi there! :) This is an exported string';
|
||||
export * from './smartrule.classes.smartrule';
|
21
ts/smartrule.classes.rule.ts
Normal file
21
ts/smartrule.classes.rule.ts
Normal file
@@ -0,0 +1,21 @@
|
||||
import * as plugins from './smartrule.plugins';
|
||||
import { SmartRule } from './smartrule.classes.smartrule';
|
||||
|
||||
export type TTreeActionResult = 'continue' | 'apply-continue' | 'apply-stop' | 'stop';
|
||||
export type TActionFunc = (objectArg) => Promise<any>;
|
||||
|
||||
export type TCheckFunc<T> = (objectArg: T) => Promise<TTreeActionResult>;
|
||||
|
||||
export class Rule<T> {
|
||||
public smartRuleRef: SmartRule<T>;
|
||||
public priority: number;
|
||||
public checkFunction: TCheckFunc<T>;
|
||||
public actionFunction: TActionFunc;
|
||||
|
||||
constructor(smartRuleRef: SmartRule<T>, priorityArg: number, checkFunctionArg: TCheckFunc<T>, actionFunctionArg: TActionFunc) {
|
||||
this.smartRuleRef = smartRuleRef;
|
||||
this.priority = priorityArg;
|
||||
this.checkFunction = checkFunctionArg;
|
||||
this.actionFunction = actionFunctionArg;
|
||||
}
|
||||
}
|
63
ts/smartrule.classes.smartrule.ts
Normal file
63
ts/smartrule.classes.smartrule.ts
Normal file
@@ -0,0 +1,63 @@
|
||||
import * as plugins from './smartrule.plugins';
|
||||
import { Rule, TCheckFunc, TActionFunc, TTreeActionResult } from './smartrule.classes.rule';
|
||||
|
||||
export class SmartRule<T> {
|
||||
public rules: Array<Rule<T>> = [];
|
||||
|
||||
/**
|
||||
* makes a decision based on the given obect and the given rules
|
||||
* @param objectArg
|
||||
*/
|
||||
public async makeDecision(objectArg: T) {
|
||||
// lets sort the rules
|
||||
this.rules = this.rules.sort((a,b) => {
|
||||
if (a.priority > b.priority) {
|
||||
return 1;
|
||||
} else {
|
||||
return 0;
|
||||
}
|
||||
});
|
||||
|
||||
// gets the next batch with the same priority
|
||||
const getNextParallelBatch = (priorityStart: number) => {
|
||||
return this.rules.filter(rule => {
|
||||
return rule.priority === priorityStart;
|
||||
});
|
||||
};
|
||||
|
||||
// lets run the checks
|
||||
const runNextBatch = async (startPriority: number) => {
|
||||
const nextBatch = getNextParallelBatch(0);
|
||||
const outcomes: TTreeActionResult[] = [];
|
||||
for (const rule of nextBatch) {
|
||||
const checkResult = await rule.checkFunction(objectArg);
|
||||
if (checkResult.startsWith("apply")) {
|
||||
await rule.actionFunction(objectArg);
|
||||
}
|
||||
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) {
|
||||
runNextBatch(startPriority++);
|
||||
}
|
||||
};
|
||||
|
||||
await runNextBatch(0);
|
||||
}
|
||||
|
||||
public createRule(priorityArg: number, checkFunctionArg: TCheckFunc<T>, actionFunctionArg: TActionFunc) {
|
||||
const rule = new Rule<T>(this, priorityArg, checkFunctionArg, actionFunctionArg);
|
||||
this.rules.push(rule);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user