Skip to content

Commit ee85634

Browse files
author
Cache Hamm
committed
Convert success events to be stored in almanac rather than runtime facts
1 parent cc5e3fd commit ee85634

File tree

5 files changed

+29
-43
lines changed

5 files changed

+29
-43
lines changed

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: 2 additions & 4 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

@@ -193,7 +192,7 @@ class Engine extends EventEmitter {
193192
debug(`engine::run ruleResult:${ruleResult.result}`)
194193
if (ruleResult.result) {
195194
return Promise.all([
196-
almanac.factValue('success-events', { event: ruleResult.event }),
195+
almanac.addSuccessEvent(ruleResult.event),
197196
this.emitAsync('success', ruleResult.event, almanac, ruleResult)
198197
]).then(() => this.emitAsync(ruleResult.event.type, ruleResult.event.params, almanac, ruleResult))
199198
} else {
@@ -211,7 +210,6 @@ class Engine extends EventEmitter {
211210
*/
212211
run (runtimeFacts = {}) {
213212
debug('engine::run started')
214-
runtimeFacts['success-events'] = new Fact('success-events', SuccessEventFact(), { cache: false })
215213
this.status = RUNNING
216214
const almanac = new Almanac(this.facts, runtimeFacts, { allowUndefinedFacts: this.allowUndefinedFacts })
217215
const orderedSets = this.prioritizeRules()
@@ -228,7 +226,7 @@ class Engine extends EventEmitter {
228226
cursor.then(() => {
229227
this.status = FINISHED
230228
debug('engine::run completed')
231-
return almanac.factValue('success-events')
229+
return almanac.getSuccessEvents()
232230
}).then(events => {
233231
resolve({
234232
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)