Skip to content

Commit f4914e1

Browse files
author
Cache Hamm
committed
remove all async/await references
1 parent 2783363 commit f4914e1

File tree

3 files changed

+41
-36
lines changed

3 files changed

+41
-36
lines changed

src/engine.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ class Engine extends EventEmitter {
139139
* @param {Rule[]} array of rules to be evaluated
140140
* @return {Promise} resolves when all rules in the array have been evaluated
141141
*/
142-
async evaluateRules (ruleArray, almanac) {
142+
evaluateRules (ruleArray, almanac) {
143143
return Promise.all(ruleArray.map((rule) => {
144144
if (this.status !== RUNNING) {
145145
debug(`engine::run status:${this.status}; skipping remaining rules`)
@@ -164,7 +164,7 @@ class Engine extends EventEmitter {
164164
* @param {Object} runOptions - run options
165165
* @return {Promise} resolves when the engine has completed running
166166
*/
167-
async run (runtimeFacts = {}) {
167+
run (runtimeFacts = {}) {
168168
debug(`engine::run started`)
169169
debug(`engine::run runtimeFacts:`, runtimeFacts)
170170
runtimeFacts['success-events'] = new Fact('success-events', SuccessEventFact(), { cache: false })

src/rule.js

Lines changed: 37 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -131,39 +131,43 @@ class Rule extends EventEmitter {
131131
* All evaluation is done within the context of an almanac
132132
* @return {Promise(RuleResult)} rule evaluation result
133133
*/
134-
async evaluate (almanac) {
134+
evaluate (almanac) {
135135
let ruleResult = new RuleResult(this.conditions, this.event, this.priority)
136136

137137
/**
138138
* Evaluates the rule conditions
139139
* @param {Condition} condition - condition to evaluate
140140
* @return {Promise(true|false)} - resolves with the result of the condition evaluation
141141
*/
142-
let evaluateCondition = async (condition) => {
143-
let comparisonValue
144-
let passes
142+
let evaluateCondition = (condition) => {
145143
if (condition.isBooleanOperator()) {
146144
let subConditions = condition[condition.operator]
145+
let comparisonPromise
147146
if (condition.operator === 'all') {
148-
comparisonValue = await all(subConditions)
147+
comparisonPromise = all(subConditions)
149148
} else {
150-
comparisonValue = await any(subConditions)
149+
comparisonPromise = any(subConditions)
151150
}
152151
// for booleans, rule passing is determined by the all/any result
153-
passes = comparisonValue === true
152+
return comparisonPromise.then(comparisonValue => {
153+
let passes = comparisonValue === true
154+
condition.result = passes
155+
return passes
156+
})
154157
} else {
155-
try {
156-
let evaluationResult = await condition.evaluate(almanac, this.engine.operators, comparisonValue)
157-
passes = evaluationResult.result
158-
condition.factResult = evaluationResult.leftHandSideValue
159-
} catch (err) {
160-
// any condition raising an undefined fact error is considered falsey when allowUndefinedFacts is enabled
161-
if (this.engine.allowUndefinedFacts && err.code === 'UNDEFINED_FACT') passes = false
162-
else throw err
163-
}
158+
return condition.evaluate(almanac, this.engine.operators)
159+
.then(evaluationResult => {
160+
let passes = evaluationResult.result
161+
condition.factResult = evaluationResult.leftHandSideValue
162+
condition.result = passes
163+
return passes
164+
})
165+
.catch(err => {
166+
// any condition raising an undefined fact error is considered falsey when allowUndefinedFacts is enabled
167+
if (this.engine.allowUndefinedFacts && err.code === 'UNDEFINED_FACT') return false
168+
throw err
169+
})
164170
}
165-
condition.result = passes
166-
return passes
167171
}
168172

169173
/**
@@ -172,13 +176,14 @@ class Rule extends EventEmitter {
172176
* @param {string(every|some)} array method to call for determining result
173177
* @return {Promise(boolean)} whether conditions evaluated truthy or falsey based on condition evaluation + method
174178
*/
175-
let evaluateConditions = async (conditions, method) => {
179+
let evaluateConditions = (conditions, method) => {
176180
if (!(Array.isArray(conditions))) conditions = [ conditions ]
177-
let conditionResults = await Promise.all(conditions.map((condition) => {
178-
return evaluateCondition(condition)
179-
}))
180-
debug(`rule::evaluateConditions results`, conditionResults)
181-
return method.call(conditionResults, (result) => result === true)
181+
182+
return Promise.all(conditions.map((condition) => evaluateCondition(condition)))
183+
.then(conditionResults => {
184+
debug(`rule::evaluateConditions results`, conditionResults)
185+
return method.call(conditionResults, (result) => result === true)
186+
})
182187
}
183188

184189
/**
@@ -191,9 +196,9 @@ class Rule extends EventEmitter {
191196
* @param {string('all'|'any')} operator
192197
* @return {Promise(boolean)} rule evaluation result
193198
*/
194-
let prioritizeAndRun = async (conditions, operator) => {
199+
let prioritizeAndRun = (conditions, operator) => {
195200
if (conditions.length === 0) {
196-
return true
201+
return Promise.resolve(true)
197202
}
198203
let method = Array.prototype.some
199204
if (operator === 'all') {
@@ -229,7 +234,7 @@ class Rule extends EventEmitter {
229234
* @param {Condition[]} conditions to be evaluated
230235
* @return {Promise(boolean)} condition evaluation result
231236
*/
232-
let any = async (conditions) => {
237+
let any = (conditions) => {
233238
return prioritizeAndRun(conditions, 'any')
234239
}
235240

@@ -238,7 +243,7 @@ class Rule extends EventEmitter {
238243
* @param {Condition[]} conditions to be evaluated
239244
* @return {Promise(boolean)} condition evaluation result
240245
*/
241-
let all = async (conditions) => {
246+
let all = (conditions) => {
242247
return prioritizeAndRun(conditions, 'all')
243248
}
244249

@@ -255,11 +260,11 @@ class Rule extends EventEmitter {
255260
}
256261

257262
if (ruleResult.conditions.any) {
258-
let result = await any(ruleResult.conditions.any)
259-
return processResult(result)
263+
return any(ruleResult.conditions.any)
264+
.then(result => processResult(result))
260265
} else {
261-
let result = await all(ruleResult.conditions.all)
262-
return processResult(result)
266+
return all(ruleResult.conditions.all)
267+
.then(result => processResult(result))
263268
}
264269
}
265270
}

test/performance.test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ describe('Performance', () => {
4747
perfy.start('any')
4848
await engine.run()
4949
const result = perfy.end('any')
50-
expect(result.time).to.be.greaterThan(0.05)
50+
expect(result.time).to.be.greaterThan(0.02)
5151
expect(result.time).to.be.lessThan(0.1)
5252
})
5353

@@ -59,7 +59,7 @@ describe('Performance', () => {
5959
perfy.start('all')
6060
await engine.run()
6161
const result = perfy.end('all')
62-
expect(result.time).to.be.greaterThan(0.05) // assert lower value
62+
expect(result.time).to.be.greaterThan(0.02) // assert lower value
6363
expect(result.time).to.be.lessThan(0.1)
6464
})
6565
})

0 commit comments

Comments
 (0)