Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow null-like values in queue #434

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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