@@ -14,15 +14,23 @@ var _condition = require('./condition');
1414
1515var _condition2 = _interopRequireDefault ( _condition ) ;
1616
17+ var _events = require ( 'events' ) ;
18+
1719function _interopRequireDefault ( obj ) { return obj && obj . __esModule ? obj : { default : obj } ; }
1820
1921function _asyncToGenerator ( fn ) { return function ( ) { var gen = fn . apply ( this , arguments ) ; return new Promise ( function ( resolve , reject ) { function step ( key , arg ) { try { var info = gen [ key ] ( arg ) ; var value = info . value ; } catch ( error ) { reject ( error ) ; return ; } if ( info . done ) { resolve ( value ) ; } else { return Promise . resolve ( value ) . then ( function ( value ) { return step ( "next" , value ) ; } , function ( err ) { return step ( "throw" , err ) ; } ) ; } } return step ( "next" ) ; } ) ; } ; }
2022
2123function _classCallCheck ( instance , Constructor ) { if ( ! ( instance instanceof Constructor ) ) { throw new TypeError ( "Cannot call a class as a function" ) ; } }
2224
25+ function _possibleConstructorReturn ( self , call ) { if ( ! self ) { throw new ReferenceError ( "this hasn't been initialised - super() hasn't been called" ) ; } return call && ( typeof call === "object" || typeof call === "function" ) ? call : self ; }
26+
27+ function _inherits ( subClass , superClass ) { if ( typeof superClass !== "function" && superClass !== null ) { throw new TypeError ( "Super expression must either be null or a function, not " + typeof superClass ) ; } subClass . prototype = Object . create ( superClass && superClass . prototype , { constructor : { value : subClass , enumerable : false , writable : true , configurable : true } } ) ; if ( superClass ) Object . setPrototypeOf ? Object . setPrototypeOf ( subClass , superClass ) : subClass . __proto__ = superClass ; }
28+
2329var debug = require ( 'debug' ) ( 'json-rules-engine' ) ;
2430
25- var Rule = function ( ) {
31+ var Rule = function ( _EventEmitter ) {
32+ _inherits ( Rule , _EventEmitter ) ;
33+
2634 /**
2735 * returns a new Rule instance
2836 * @param {object,string } options, or json string that can be parsed into options
@@ -36,18 +44,27 @@ var Rule = function () {
3644 function Rule ( options ) {
3745 _classCallCheck ( this , Rule ) ;
3846
47+ var _this = _possibleConstructorReturn ( this , Object . getPrototypeOf ( Rule ) . call ( this ) ) ;
48+
3949 if ( typeof options === 'string' ) {
4050 options = JSON . parse ( options ) ;
4151 }
4252 if ( options && options . conditions ) {
43- this . setConditions ( options . conditions ) ;
53+ _this . setConditions ( options . conditions ) ;
54+ }
55+ if ( options && options . onSuccess ) {
56+ _this . on ( 'success' , options . onSuccess ) ;
57+ }
58+ if ( options && options . onFailure ) {
59+ _this . on ( 'failure' , options . onFailure ) ;
4460 }
4561
4662 var priority = options && options . priority || 1 ;
47- this . setPriority ( priority ) ;
63+ _this . setPriority ( priority ) ;
4864
4965 var event = options && options . event || { type : 'unknown' } ;
50- this . setEvent ( event ) ;
66+ _this . setEvent ( event ) ;
67+ return _this ;
5168 }
5269
5370 /**
@@ -134,14 +151,14 @@ var Rule = function () {
134151 } , {
135152 key : 'prioritizeConditions' ,
136153 value : function prioritizeConditions ( conditions ) {
137- var _this = this ;
154+ var _this2 = this ;
138155
139156 var factSets = conditions . reduce ( function ( sets , condition ) {
140157 // if a priority has been set on this specific condition, honor that first
141158 // otherwise, use the fact's priority
142159 var priority = condition . priority ;
143160 if ( ! priority ) {
144- var fact = _this . engine . getFact ( condition . fact ) ;
161+ var fact = _this2 . engine . getFact ( condition . fact ) ;
145162 priority = fact && fact . priority || 1 ;
146163 }
147164 if ( ! sets [ priority ] ) sets [ priority ] = [ ] ;
@@ -165,7 +182,7 @@ var Rule = function () {
165182 key : 'evaluate' ,
166183 value : function ( ) {
167184 var _ref = _asyncToGenerator ( regeneratorRuntime . mark ( function _callee6 ( almanac ) {
168- var _this2 = this ;
185+ var _this3 = this ;
169186
170187 var evaluateCondition , evaluateConditions , prioritizeAndRun , any , all ;
171188 return regeneratorRuntime . wrap ( function _callee6$ ( _context6 ) {
@@ -179,7 +196,7 @@ var Rule = function () {
179196 */
180197 evaluateCondition = function ( ) {
181198 var _ref2 = _asyncToGenerator ( regeneratorRuntime . mark ( function _callee ( condition ) {
182- var comparisonValue , subConditions ;
199+ var comparisonValue , subConditions , passes ;
183200 return regeneratorRuntime . wrap ( function _callee$ ( _context ) {
184201 while ( 1 ) {
185202 switch ( _context . prev = _context . next ) {
@@ -225,14 +242,25 @@ var Rule = function () {
225242 comparisonValue = _context . sent ;
226243
227244 case 17 :
228- return _context . abrupt ( 'return' , condition . evaluate ( comparisonValue , _this2 . engine . operators ) ) ;
245+ _context . next = 19 ;
246+ return condition . evaluate ( comparisonValue , _this3 . engine . operators ) ;
247+
248+ case 19 :
249+ passes = _context . sent ;
250+
251+ if ( passes ) {
252+ _this3 . emit ( 'success' , _this3 . event , almanac ) ;
253+ } else {
254+ _this3 . emit ( 'failure' , _this3 . event , almanac ) ;
255+ }
256+ return _context . abrupt ( 'return' , passes ) ;
229257
230- case 18 :
258+ case 22 :
231259 case 'end' :
232260 return _context . stop ( ) ;
233261 }
234262 }
235- } , _callee , _this2 ) ;
263+ } , _callee , _this3 ) ;
236264 } ) ) ;
237265
238266 return function evaluateCondition ( _x3 ) {
@@ -274,7 +302,7 @@ var Rule = function () {
274302 return _context2 . stop ( ) ;
275303 }
276304 }
277- } , _callee2 , _this2 ) ;
305+ } , _callee2 , _this3 ) ;
278306 } ) ) ;
279307
280308 return function evaluateConditions ( _x4 , _x5 ) {
@@ -314,7 +342,7 @@ var Rule = function () {
314342 if ( operator === 'all' ) {
315343 method = Array . prototype . every ;
316344 }
317- orderedSets = _this2 . prioritizeConditions ( conditions ) ;
345+ orderedSets = _this3 . prioritizeConditions ( conditions ) ;
318346 cursor = Promise . resolve ( ) ;
319347
320348 orderedSets . forEach ( function ( set ) {
@@ -344,7 +372,7 @@ var Rule = function () {
344372 return _context3 . stop ( ) ;
345373 }
346374 }
347- } , _callee3 , _this2 ) ;
375+ } , _callee3 , _this3 ) ;
348376 } ) ) ;
349377
350378 return function prioritizeAndRun ( _x6 , _x7 ) {
@@ -372,7 +400,7 @@ var Rule = function () {
372400 return _context4 . stop ( ) ;
373401 }
374402 }
375- } , _callee4 , _this2 ) ;
403+ } , _callee4 , _this3 ) ;
376404 } ) ) ;
377405
378406 return function any ( _x8 ) {
@@ -400,7 +428,7 @@ var Rule = function () {
400428 return _context5 . stop ( ) ;
401429 }
402430 }
403- } , _callee5 , _this2 ) ;
431+ } , _callee5 , _this3 ) ;
404432 } ) ) ;
405433
406434 return function all ( _x9 ) {
@@ -443,6 +471,6 @@ var Rule = function () {
443471 } ] ) ;
444472
445473 return Rule ;
446- } ( ) ;
474+ } ( _events . EventEmitter ) ;
447475
448476exports . default = Rule ;
0 commit comments