Skip to content

Commit

Permalink
remove co dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
aitrusgit committed Oct 9, 2017
1 parent 4f51bdc commit 251eca3
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 26 deletions.
24 changes: 15 additions & 9 deletions lib/closure/ClosureReducer.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
"use strict";

const Closure = require("./Closure"),
co = require("co"),
util = require("../util");

/**
Expand All @@ -16,17 +15,24 @@ module.exports = class ClosureReducer extends Closure {
constructor(name, closures, options) {
super(name, options);
this.closures = closures || util.raise(`Cannot build closure reducer [${name}] without closure chain`);
this.do = co.wrap(this.do.bind(this));
}

* do(fact, context) {
for (let closure of this.closures) {
fact = yield closure.process(fact, context);
if (this.options.matchOnce && context.currentRuleFlowActivated) {
return fact;
}
do(fact, context) {
return this.reduce(0, fact, context);
}

reduce(index, fact, context) {
if (this.closures.length <= index) {
return Promise.resolve(fact);
}
return fact;

return this.closures[index].process(fact, context)
.then(fact => {
if (this.options.matchOnce && context.currentRuleFlowActivated) {
return fact;
}
return this.reduce(index + 1, fact, context);
});
}

}
19 changes: 6 additions & 13 deletions lib/closure/RuleFlow.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,15 @@
"use strict";

const co = require("co"),
ClosureReducer = require("./ClosureReducer");

const ClosureReducer = require("./ClosureReducer");

module.exports = class RuleFlow extends ClosureReducer {

constructor(name, closures, options) {
super(name, closures, options);
this.do = co.wrap(this.do.bind(this));
}

* do(fact, context) {
do(fact, context) {
context.initiateFlow();
fact = yield super.do(fact, context);
context.endFlow();

return fact
return super.do(fact, context).then(fact => {
context.endFlow();
return fact;
});
}

}
6 changes: 2 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "rules-js",
"version": "0.0.10",
"version": "0.0.11",
"description": "A simple rule-engine for javascript",
"main": "index.js",
"author": "Claudio Fernandez <[email protected]>",
Expand All @@ -23,9 +23,7 @@
"sinon": "^4.0.0",
"sinon-chai": "^2.14.0"
},
"dependencies": {
"co": "^4.6.0"
},
"dependencies": {},
"repository": {
"closure": "git",
"url": "https://github.com/bluealba/rules-js"
Expand Down

0 comments on commit 251eca3

Please sign in to comment.