Skip to content

Commit ae1c8ae

Browse files
Fixes #20 and Fixes #21. Added an API to interact with the rule engine flow like .stop(), .restart() from the condition and consequence level. No more callbacks passed as parameter and user doesnt need to call cb(). User just needs to indicate the flow engine needs follow via the API.
1 parent 259aedc commit ae1c8ae

File tree

1 file changed

+38
-36
lines changed

1 file changed

+38
-36
lines changed

lib/node-rules.js

Lines changed: 38 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -69,64 +69,66 @@
6969
var _rules = this.activeRules;
7070

7171

72-
(function doit(x) {
72+
(function FnRuleLoop(x) {
7373

7474

75-
var R = {};
76-
77-
R.when = function(outcome) {
75+
var API = {
76+
77+
"when" : function(outcome) {
7878

79-
if (outcome) {
79+
if (outcome) {
8080

81-
var _consequence = _rules[x].consequence;
82-
_consequence.call(session, R);
81+
var _consequence = _rules[x].consequence;
82+
_consequence.call(session, API);
8383

84-
} else {
84+
} else {
8585

86-
process.nextTick(function(){
87-
R.next();
88-
});
86+
process.nextTick(function(){
87+
R.next();
88+
});
8989

90-
}
91-
92-
}
90+
}
91+
92+
},
9393

94-
R.restart = function() {
94+
"restart" : function() {
9595

96-
return doit(0);
96+
return FnRuleLoop(0);
9797

98-
};
98+
},
9999

100-
R.stop = function() {
100+
"stop" : function() {
101101

102-
complete = true;
103-
return doit(0);
102+
complete = true;
103+
return FnRuleLoop(0);
104104

105-
};
105+
},
106106

107-
R.next = function () {
107+
"next" : function () {
108108

109-
if (!_.isEqual(lastSession,session)) {
109+
if (!_.isEqual(lastSession,session)) {
110110

111-
lastSession = _.clone(session);
112-
process.nextTick(function(){
113-
R.restart();
114-
});
111+
lastSession = _.clone(session);
112+
process.nextTick(function(){
113+
R.restart();
114+
});
115115

116-
} else {
117-
118-
process.nextTick(function(){
119-
return doit(x+1);
120-
});
121-
122-
}
116+
} else {
117+
118+
process.nextTick(function(){
119+
return FnRuleLoop(x+1);
120+
});
121+
122+
}
123123

124-
}
124+
}
125125

126+
};
127+
126128
if (x < _rules.length && complete === false) {
127129

128130
var _rule = _rules[x].condition;
129-
_rule.call(session, R);
131+
_rule.call(session, API);
130132

131133
} else {
132134

0 commit comments

Comments
 (0)