Skip to content

Commit 80e84b3

Browse files
author
Cache Hamm
committed
Rename rule.event => rule.ruleEvent for event-emitter-2 compatibility
1 parent 054e196 commit 80e84b3

4 files changed

Lines changed: 16 additions & 15 deletions

File tree

src/engine.js

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,14 @@ class Engine extends EventEmitter {
4040
*/
4141
addRule (properties) {
4242
if (!properties) throw new Error('Engine: addRule() requires options')
43-
if (!Object.prototype.hasOwnProperty.call(properties, 'conditions')) throw new Error('Engine: addRule() argument requires "conditions" property')
44-
if (!Object.prototype.hasOwnProperty.call(properties, 'event')) throw new Error('Engine: addRule() argument requires "event" property')
4543

4644
let rule
4745
if (properties instanceof Rule) {
4846
rule = properties
4947
} else {
48+
if (!Object.prototype.hasOwnProperty.call(properties, 'event')) throw new Error('Engine: addRule() argument requires "event" property')
49+
if (!Object.prototype.hasOwnProperty.call(properties, 'conditions')) throw new Error('Engine: addRule() argument requires "conditions" property')
50+
5051
rule = new Rule(properties)
5152
}
5253
rule.setEngine(this)
@@ -191,13 +192,13 @@ class Engine extends EventEmitter {
191192
return rule.evaluate(almanac).then((ruleResult) => {
192193
debug(`engine::run ruleResult:${ruleResult.result}`)
193194
if (ruleResult.result) {
194-
almanac.factValue('success-events', { event: rule.event })
195+
almanac.factValue('success-events', { event: ruleResult.event })
195196
return Promise.all([
196-
this.emitAsync('success', rule.event, almanac, ruleResult),
197-
this.emitAsync(rule.event.type, rule.event.params, almanac, ruleResult)
197+
this.emitAsync('success', ruleResult.event, almanac, ruleResult),
198+
this.emitAsync(ruleResult.event.type, ruleResult.event.params, almanac, ruleResult)
198199
])
199200
} else {
200-
return this.emitAsync('failure', rule.event, almanac, ruleResult)
201+
return this.emitAsync('failure', ruleResult.event, almanac, ruleResult)
201202
}
202203
})
203204
}))

src/rule.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -86,10 +86,10 @@ class Rule extends EventEmitter {
8686
setEvent (event) {
8787
if (!event) throw new Error('Rule: setEvent() requires event object')
8888
if (!Object.prototype.hasOwnProperty.call(event, 'type')) throw new Error('Rule: setEvent() requires event object with "type" property')
89-
this.event = {
89+
this.ruleEvent = {
9090
type: event.type
9191
}
92-
if (event.params) this.event.params = event.params
92+
if (event.params) this.ruleEvent.params = event.params
9393
return this
9494
}
9595

@@ -107,7 +107,7 @@ class Rule extends EventEmitter {
107107
const props = {
108108
conditions: this.conditions.toJSON(false),
109109
priority: this.priority,
110-
event: this.event,
110+
event: this.ruleEvent,
111111
name: this.name
112112
}
113113
if (stringify) {
@@ -148,7 +148,7 @@ class Rule extends EventEmitter {
148148
* @return {Promise(RuleResult)} rule evaluation result
149149
*/
150150
evaluate (almanac) {
151-
const ruleResult = new RuleResult(this.conditions, this.event, this.priority, this.name)
151+
const ruleResult = new RuleResult(this.conditions, this.ruleEvent, this.priority, this.name)
152152

153153
/**
154154
* Evaluates the rule conditions

test/engine-failure.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ describe('Engine: failure', () => {
3232
const failureSpy = sandbox.spy()
3333
engine.on('failure', failureSpy)
3434
await engine.run()
35-
expect(failureSpy).to.have.been.calledWith(engine.rules[0].event)
35+
expect(failureSpy).to.have.been.calledWith(engine.rules[0].ruleEvent)
3636
})
3737

3838
it('does not emit when a rule passes', async () => {

test/rule.test.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ describe('Rule', () => {
2929
const rule = new Rule(opts)
3030
expect(rule.priority).to.eql(opts.priority)
3131
expect(rule.conditions).to.eql(opts.conditions)
32-
expect(rule.event).to.eql(opts.event)
32+
expect(rule.ruleEvent).to.eql(opts.event)
3333
expect(rule.name).to.eql(opts.name)
3434
})
3535

@@ -51,7 +51,7 @@ describe('Rule', () => {
5151
const rule = new Rule(json)
5252
expect(rule.priority).to.eql(opts.priority)
5353
expect(rule.conditions).to.eql(opts.conditions)
54-
expect(rule.event).to.eql(opts.event)
54+
expect(rule.ruleEvent).to.eql(opts.event)
5555
expect(rule.name).to.eql(opts.name)
5656
})
5757
})
@@ -287,7 +287,7 @@ describe('Rule', () => {
287287
const hydratedRule = new Rule(jsonString)
288288
expect(hydratedRule.conditions).to.eql(rule.conditions)
289289
expect(hydratedRule.priority).to.eql(rule.priority)
290-
expect(hydratedRule.event).to.eql(rule.event)
290+
expect(hydratedRule.ruleEvent).to.eql(rule.ruleEvent)
291291
expect(hydratedRule.name).to.eql(rule.name)
292292
})
293293

@@ -298,7 +298,7 @@ describe('Rule', () => {
298298
const hydratedRule = new Rule(json)
299299
expect(hydratedRule.conditions).to.eql(rule.conditions)
300300
expect(hydratedRule.priority).to.eql(rule.priority)
301-
expect(hydratedRule.event).to.eql(rule.event)
301+
expect(hydratedRule.ruleEvent).to.eql(rule.ruleEvent)
302302
expect(hydratedRule.name).to.eql(rule.name)
303303
})
304304
})

0 commit comments

Comments
 (0)