diff --git a/Libraries/Interaction/TaskQueue.js b/Libraries/Interaction/TaskQueue.js index 33838b15a15..660241fb35b 100644 --- a/Libraries/Interaction/TaskQueue.js +++ b/Libraries/Interaction/TaskQueue.js @@ -15,12 +15,12 @@ const infoLog = require('infoLog'); const invariant = require('fbjs/lib/invariant'); type SimpleTask = { - name: string; - run: () => void; + name: string, + run: () => void, }; type PromiseTask = { - name: string; - gen: () => Promise; + name: string, + gen: () => Promise, }; export type Task = Function | SimpleTask | PromiseTask; @@ -75,7 +75,7 @@ class TaskQueue { ...queue, tasks: queue.tasks.filter((task) => tasksToCancel.indexOf(task) === -1), })) - .filter((queue) => queue.tasks.length > 0); + .filter((queue, idx) => (queue.tasks.length > 0 || idx === 0)); } /** @@ -151,7 +151,10 @@ class TaskQueue { DEBUG && infoLog('exec gen task ' + task.name); task.gen() .then(() => { - DEBUG && infoLog('onThen for gen task ' + task.name, {stackIdx, queueStackSize: this._queueStack.length}); + DEBUG && infoLog( + 'onThen for gen task ' + task.name, + {stackIdx, queueStackSize: this._queueStack.length}, + ); this._queueStack[stackIdx].popable = true; this.hasTasksToProcess() && this._onMoreTasks(); }) diff --git a/Libraries/Interaction/__tests__/TaskQueue-test.js b/Libraries/Interaction/__tests__/TaskQueue-test.js index 1da2e4430f2..e1a24009f01 100644 --- a/Libraries/Interaction/__tests__/TaskQueue-test.js +++ b/Libraries/Interaction/__tests__/TaskQueue-test.js @@ -142,4 +142,13 @@ describe('TaskQueue', () => { expectToBeCalledOnce(task4); expect(taskQueue.hasTasksToProcess()).toBe(false); }); + + it('should not crash when last task is cancelled', () => { + const task1 = jest.fn(); + taskQueue.enqueue(task1); + taskQueue.cancelTasks([task1]); + clearTaskQueue(taskQueue); + expect(task1).not.toBeCalled(); + expect(taskQueue.hasTasksToProcess()).toBe(false); + }); });