From f8e5671216277d1c53b8e422ec05843afec115af Mon Sep 17 00:00:00 2001 From: Boris Petrov Date: Mon, 29 Aug 2022 14:00:34 +0300 Subject: [PATCH] Allow null-like values in `queue` --- addon/helpers/queue.js | 10 +++++++--- tests/unit/helpers/queue-test.js | 8 ++++++++ 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/addon/helpers/queue.js b/addon/helpers/queue.js index bec1a14c..5905a672 100644 --- a/addon/helpers/queue.js +++ b/addon/helpers/queue.js @@ -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); diff --git a/tests/unit/helpers/queue-test.js b/tests/unit/helpers/queue-test.js index 3b5bd8fe..6a0d5efd 100644 --- a/tests/unit/helpers/queue-test.js +++ b/tests/unit/helpers/queue-test.js @@ -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, undefined, 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);