Skip to content

Commit 75353df

Browse files
authored
Merge pull request #240 from CacheControl/almanac-events
2 parents cc5e3fd + 667b115 commit 75353df

File tree

6 files changed

+33
-47
lines changed

6 files changed

+33
-47
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
#### 6.0. / 2020-12-XX
22
* BREAKING CHANGES
33
* Private `rule.event` property renamed. Use `rule.getEvent()` to avoid breaking changes in the future.
4-
* Engine and Rule events `on('success')`, `on('failure')`, and Rule callbacks `onSuccess` and `onFailure` now honor returned promises; any event handler that returns a promise will be waited upon to resolve before engine execution continues.
4+
* Engine and Rule events `on('success')`, `on('failure')`, and Rule callbacks `onSuccess` and `onFailure` now honor returned promises; any event handler that returns a promise will be waited upon to resolve before engine execution continues. (fixes #235)
5+
* The 'success-events' fact used to store successful events has been converted to an internal data structure and will no longer appear in the almanac's facts. (fixes #187)
56

67
#### 5.3.0 / 2020-12-02
78
* Allow facts to have a value of `undefined`

src/almanac.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ export default class Almanac {
1717
this.factMap = new Map(factMap)
1818
this.factResultsCache = new Map() // { cacheKey: Promise<factValu> }
1919
this.allowUndefinedFacts = Boolean(options.allowUndefinedFacts)
20+
this.successEvents = []
2021

2122
for (const factId in runtimeFacts) {
2223
let fact
@@ -31,6 +32,21 @@ export default class Almanac {
3132
}
3233
}
3334

35+
/**
36+
* Adds a success event
37+
* @param {Object} event
38+
*/
39+
addSuccessEvent (event) {
40+
this.successEvents.push(event)
41+
}
42+
43+
/**
44+
* retrieve successful events
45+
*/
46+
getSuccessEvents () {
47+
return this.successEvents
48+
}
49+
3450
/**
3551
* Retrieve fact by id, raising an exception if it DNE
3652
* @param {String} factId

src/engine-facts.js

Lines changed: 0 additions & 13 deletions
This file was deleted.

src/engine.js

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import Rule from './rule'
55
import Operator from './operator'
66
import Almanac from './almanac'
77
import EventEmitter from 'eventemitter2'
8-
import { SuccessEventFact } from './engine-facts'
98
import defaultOperators from './engine-default-operators'
109
import debug from './debug'
1110

@@ -192,10 +191,9 @@ class Engine extends EventEmitter {
192191
return rule.evaluate(almanac).then((ruleResult) => {
193192
debug(`engine::run ruleResult:${ruleResult.result}`)
194193
if (ruleResult.result) {
195-
return Promise.all([
196-
almanac.factValue('success-events', { event: ruleResult.event }),
197-
this.emitAsync('success', ruleResult.event, almanac, ruleResult)
198-
]).then(() => this.emitAsync(ruleResult.event.type, ruleResult.event.params, almanac, ruleResult))
194+
almanac.addSuccessEvent(ruleResult.event)
195+
return this.emitAsync('success', ruleResult.event, almanac, ruleResult)
196+
.then(() => this.emitAsync(ruleResult.event.type, ruleResult.event.params, almanac, ruleResult))
199197
} else {
200198
return this.emitAsync('failure', ruleResult.event, almanac, ruleResult)
201199
}
@@ -211,7 +209,6 @@ class Engine extends EventEmitter {
211209
*/
212210
run (runtimeFacts = {}) {
213211
debug('engine::run started')
214-
runtimeFacts['success-events'] = new Fact('success-events', SuccessEventFact(), { cache: false })
215212
this.status = RUNNING
216213
const almanac = new Almanac(this.facts, runtimeFacts, { allowUndefinedFacts: this.allowUndefinedFacts })
217214
const orderedSets = this.prioritizeRules()
@@ -228,7 +225,7 @@ class Engine extends EventEmitter {
228225
cursor.then(() => {
229226
this.status = FINISHED
230227
debug('engine::run completed')
231-
return almanac.factValue('success-events')
228+
return almanac.getSuccessEvents()
232229
}).then(events => {
233230
resolve({
234231
events,

test/almanac.test.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,17 @@ describe('Almanac', () => {
4141
})
4242
})
4343

44+
describe('addSuccessEvent() / getSuccessEvents()', () => {
45+
it('manages success events', () => {
46+
const event = {}
47+
almanac = new Almanac()
48+
expect(almanac.getSuccessEvents()).to.be.empty()
49+
almanac.addSuccessEvent(event)
50+
expect(almanac.getSuccessEvents()).to.have.a.lengthOf(1)
51+
expect(almanac.getSuccessEvents()[0]).to.equal(event)
52+
})
53+
})
54+
4455
describe('arguments', () => {
4556
beforeEach(() => {
4657
const fact = new Fact('foo', async (params, facts) => {

test/engine-internal-facts.test.js

Lines changed: 0 additions & 26 deletions
This file was deleted.

0 commit comments

Comments
 (0)