Skip to content

Commit 9b603f5

Browse files
chris-pardychris-pardy-newfire
authored andcommitted
Significantly simplify the prioritizeAndRun Method
Significantly simplify the method by leveraging the short-circuiting of the || and && operators. Replaces #368 Co-Author Mahi <[email protected]>
1 parent 194ebdb commit 9b603f5

File tree

1 file changed

+5
-25
lines changed

1 file changed

+5
-25
lines changed

Diff for: src/rule.js

+5-25
Original file line numberDiff line numberDiff line change
@@ -274,36 +274,16 @@ class Rule extends EventEmitter {
274274
// this also covers the 'not' case which should only ever have a single condition
275275
return evaluateCondition(conditions[0])
276276
}
277-
let method = Array.prototype.some
278-
if (operator === 'all') {
279-
method = Array.prototype.every
280-
}
281277
const orderedSets = this.prioritizeConditions(conditions)
282-
let cursor = Promise.resolve()
278+
let cursor = Promise.resolve(operator === 'all')
283279
// use for() loop over Array.forEach to support IE8 without polyfill
284280
for (let i = 0; i < orderedSets.length; i++) {
285281
const set = orderedSets[i]
286-
let stop = false
287282
cursor = cursor.then((setResult) => {
288-
// after the first set succeeds, don't fire off the remaining promises
289-
if ((operator === 'any' && setResult === true) || stop) {
290-
debug(
291-
'prioritizeAndRun::detected truthy result; skipping remaining conditions'
292-
)
293-
stop = true
294-
return true
295-
}
296-
297-
// after the first set fails, don't fire off the remaining promises
298-
if ((operator === 'all' && setResult === false) || stop) {
299-
debug(
300-
'prioritizeAndRun::detected falsey result; skipping remaining conditions'
301-
)
302-
stop = true
303-
return false
304-
}
305-
// all conditions passed; proceed with running next set in parallel
306-
return evaluateConditions(set, method)
283+
// rely on the short-circuiting behavior of || and && to avoid evaluating subsequent conditions
284+
return operator === 'any'
285+
? (setResult || evaluateConditions(set, Array.prototype.some))
286+
: (setResult && evaluateConditions(set, Array.prototype.every))
307287
})
308288
}
309289
return cursor

0 commit comments

Comments
 (0)