Skip to content

Commit c7ef090

Browse files
author
Cache Hamm
committed
Revert to using "events" object returned from engine for examples
1 parent 8270c69 commit c7ef090

7 files changed

+19
-28
lines changed

CHANGELOG.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
* 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)
1414
* NEW FEATURES
1515
* Engine constructor now accepts a `pathResolver` option for resolving condition `path` properties. Read more [here](./docs/rules.md#condition-helpers-custom-path-resolver). (fixes #210)
16-
* Engine.run() now returns:
16+
* Engine.run() now returns three additional data structures:
1717
* `failureEvents`, an array of all failed rules events. (fixes #192)
1818
* `results`, an array of RuleResults for each successful rule (fixes #216)
1919
* `failureResults`, an array of RuleResults for each failed rule

README.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,8 @@ let facts = {
8686
// Run the engine to evaluate
8787
engine
8888
.run(facts)
89-
.then(({ results }) => {
90-
results.map(result => console.log(result.event.params.message))
89+
.then(({ events }) => {
90+
events.map(event => console.log(event.params.message))
9191
})
9292

9393
/*
@@ -170,8 +170,8 @@ engine.addFact('account-information', function (params, almanac) {
170170
let facts = { accountId: 'lincoln' }
171171
engine
172172
.run(facts)
173-
.then(({ results }) => {
174-
console.log(facts.accountId + ' is a ' + results.map(result => result.event.params.message))
173+
.then(({ events }) => {
174+
console.log(facts.accountId + ' is a ' + events.map(event => event.params.message))
175175
})
176176

177177
/*

docs/engine.md

+5-14
Original file line numberDiff line numberDiff line change
@@ -164,22 +164,13 @@ await engine.run()
164164
// with constant facts
165165
await engine.run({ userId: 1 })
166166

167-
// results
168167
const {
169-
results,
170-
failureResults,
171-
events,
172-
failureEvents,
173-
almanac
168+
results, // rule results for successful rules
169+
failureResults, // rule results for failed rules
170+
events, // array of successful rule events
171+
failureEvents, // array of failed rule events
172+
almanac // Almanac instance representing the run
174173
} = await engine.run({ userId: 1 })
175-
176-
/**
177-
* almanac: Almanac instance for the run
178-
* results: rule results for successful rules
179-
* failureResults: rule results for failed rules
180-
* events: successful rule events
181-
* failureEvents: failed rule events
182-
*/
183174
```
184175
Link to the [Almanac documentation](./almanac.md)
185176

docs/walkthrough.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ engine.on('young-adult-rocky-mnts', (params) => {
138138
// - OR -
139139

140140
// subscribe to any event emitted by the engine
141-
engine.on('success', function (event, engine) {
141+
engine.on('success', function (event, almanac, ruleResult) {
142142
console.log('Success event:\n', event);
143143
// event: {
144144
// type: "young-adult-rocky-mnts",
@@ -162,8 +162,8 @@ Running an engine executes the rules, and fires off event events for conditions
162162
engine.run({ userId: 1 }); // any time a rule condition requires 'userId', '1' will be returned
163163

164164
// run() returns a promise
165-
engine.run({ userId: 4 }).then(({ results }) => {
166-
console.log('all rules executed; the following events were triggered: ', results.map(result => JSON.stringify(result.event)))
165+
engine.run({ userId: 4 }).then(({ events }) => {
166+
console.log('all rules executed; the following events were triggered: ', events.map(result => JSON.stringify(event)))
167167
});
168168
```
169169
Helper methods (for this example)

examples/01-hello-world.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,9 @@ async function start () {
4747
const facts = { displayMessage: true }
4848

4949
// engine.run() evaluates the rule using the facts provided
50-
const { results } = await engine.run(facts)
50+
const { events } = await engine.run(facts)
5151

52-
results.map(result => console.log(result.event.params.data.green))
52+
events.map(event => console.log(event.params.data.green))
5353
}
5454

5555
start()

examples/02-nested-boolean-logic.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,9 @@ async function start () {
6161
gameDuration: 40
6262
}
6363

64-
const { results } = await engine.run(facts)
64+
const { events } = await engine.run(facts)
6565

66-
results.map(result => console.log(result.event.params.message.red))
66+
events.map(event => console.log(event.params.message.red))
6767
}
6868
start()
6969
/*

examples/03-dynamic-facts.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,9 @@ async function start () {
7070

7171
// define fact(s) known at runtime
7272
const facts = { accountId: 'lincoln' }
73-
const { results } = await engine.run(facts)
73+
const { events } = await engine.run(facts)
7474

75-
console.log(facts.accountId + ' is a ' + results.map(result => result.event.params.message))
75+
console.log(facts.accountId + ' is a ' + events.map(event => event.params.message))
7676
}
7777
start()
7878

0 commit comments

Comments
 (0)