Skip to content

Commit bb80358

Browse files
committed
Allow null-like values in queue
1 parent 62fb183 commit bb80358

File tree

2 files changed

+15
-3
lines changed

2 files changed

+15
-3
lines changed

addon/helpers/queue.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,23 @@
11
import { helper } from '@ember/component/helper';
22
import isPromise from '../utils/is-promise';
33

4+
function invokeMaybeNullable(curr, args) {
5+
return curr == null ? undefined : curr(...args);
6+
}
7+
48
export function queue(actions = []) {
59
return function(...args) {
610
let invokeWithArgs = function(acc, curr) {
711
if (isPromise(acc)) {
8-
return acc.then(() => curr(...args));
12+
return acc.then(() => invokeMaybeNullable(curr, args));
913
}
1014

11-
return curr(...args);
15+
return invokeMaybeNullable(curr, args);
1216
};
1317

1418
return actions.reduce((acc, curr, idx) => {
1519
if (idx === 0) {
16-
return curr(...args);
20+
return invokeMaybeNullable(curr, args);
1721
}
1822

1923
return invokeWithArgs(acc, curr);

tests/unit/helpers/queue-test.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,14 @@ module('Unit | Helper | queue', function(hooks) {
3434
assert.ok(step3.calledOnce, 'step3 called once');
3535
});
3636

37+
test('it ignores null-like values', function(assert) {
38+
let queued = queue([undefined, step1, null, step2, undefined]);
39+
queued(2, 4);
40+
41+
assert.ok(step1.calledOnce, 'step1 called once');
42+
assert.ok(step2.calledOnce, 'step2 called once');
43+
});
44+
3745
test('it passes all functions the same arguments', function(assert) {
3846
let queued = queue([step1, step2, step3]);
3947
queued(2, 4);

0 commit comments

Comments
 (0)