File tree Expand file tree Collapse file tree 2 files changed +18
-5
lines changed Expand file tree Collapse file tree 2 files changed +18
-5
lines changed Original file line number Diff line number Diff line change @@ -51,9 +51,7 @@ export class Engine {
51
51
removeFact ( factOrId : string | Fact ) : boolean ;
52
52
getFact < T > ( factId : string ) : Fact < T > ;
53
53
54
- on ( eventName : "success" , handler : EventHandler ) : this;
55
- on ( eventName : "failure" , handler : EventHandler ) : this;
56
- on ( eventName : string , handler : EventHandler ) : this;
54
+ on < TEvent = Event > ( eventName : string , handler : EventHandler < TEvent > ) : this;
57
55
58
56
run ( facts ?: Record < string , any > , runOptions ?: RunOptions ) : Promise < EngineResult > ;
59
57
stop ( ) : this;
@@ -119,8 +117,8 @@ export interface Event {
119
117
120
118
export type PathResolver = ( value : object , path : string ) => any ;
121
119
122
- export type EventHandler = (
123
- event : Event ,
120
+ export type EventHandler < TEvent = Event > = (
121
+ event : TEvent ,
124
122
almanac : Almanac ,
125
123
ruleResult : RuleResult
126
124
) => void ;
Original file line number Diff line number Diff line change @@ -4,11 +4,13 @@ import rulesEngine, {
4
4
Almanac ,
5
5
EngineResult ,
6
6
Engine ,
7
+ Event ,
7
8
Fact ,
8
9
Operator ,
9
10
OperatorEvaluator ,
10
11
PathResolver ,
11
12
Rule ,
13
+ RuleResult ,
12
14
RuleProperties ,
13
15
RuleSerializable
14
16
} from "../" ;
@@ -94,6 +96,19 @@ expectType<Engine>(engine.addFact(dynamicFact));
94
96
expectType < boolean > ( engine . removeFact ( fact ) ) ;
95
97
expectType < Fact < string > > ( engine . getFact < string > ( "test" ) ) ;
96
98
99
+ // event handlers
100
+ engine . on ( "failure" , ( event , almanac , ruleResult ) => {
101
+ expectType < Event > ( event )
102
+ expectType < Almanac > ( almanac )
103
+ expectType < RuleResult > ( ruleResult )
104
+ } ) ;
105
+
106
+ engine . on < { test : true } > ( "test" , ( event , almanac , ruleResult ) => {
107
+ expectType < { test : true } > ( event ) ;
108
+ expectType < Almanac > ( almanac ) ;
109
+ expectType < RuleResult > ( ruleResult ) ;
110
+ } ) ;
111
+
97
112
// Run the Engine
98
113
expectType < Promise < EngineResult > > ( engine . run ( { displayMessage : true } ) ) ;
99
114
You can’t perform that action at this time.
0 commit comments