Skip to content

Commit

Permalink
Allow null-like values in queue
Browse files Browse the repository at this point in the history
  • Loading branch information
boris-petrov committed Aug 29, 2022
1 parent 62fb183 commit bb80358
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
10 changes: 7 additions & 3 deletions addon/helpers/queue.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,23 @@
import { helper } from '@ember/component/helper';
import isPromise from '../utils/is-promise';

function invokeMaybeNullable(curr, args) {
return curr == null ? undefined : curr(...args);
}

export function queue(actions = []) {
return function(...args) {
let invokeWithArgs = function(acc, curr) {
if (isPromise(acc)) {
return acc.then(() => curr(...args));
return acc.then(() => invokeMaybeNullable(curr, args));
}

return curr(...args);
return invokeMaybeNullable(curr, args);
};

return actions.reduce((acc, curr, idx) => {
if (idx === 0) {
return curr(...args);
return invokeMaybeNullable(curr, args);
}

return invokeWithArgs(acc, curr);
Expand Down
8 changes: 8 additions & 0 deletions tests/unit/helpers/queue-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,14 @@ module('Unit | Helper | queue', function(hooks) {
assert.ok(step3.calledOnce, 'step3 called once');
});

test('it ignores null-like values', function(assert) {
let queued = queue([undefined, step1, null, step2, undefined]);
queued(2, 4);

assert.ok(step1.calledOnce, 'step1 called once');
assert.ok(step2.calledOnce, 'step2 called once');
});

test('it passes all functions the same arguments', function(assert) {
let queued = queue([step1, step2, step3]);
queued(2, 4);
Expand Down

0 comments on commit bb80358

Please sign in to comment.