From 4dabb575b1b311ba541fae7eabbd49f08b5391b3 Mon Sep 17 00:00:00 2001 From: Spencer Ahrens Date: Mon, 11 Jul 2016 16:06:42 -0700 Subject: [PATCH] Fix bug in cancelling last task in TaskQueue Summary: We don't want to remove the last queue from the stack, it should just have no tasks in it. Fixes issue reported here: https://www.facebook.com/groups/reactnativeoss/permalink/1569170356712926/ Reviewed By: yungsters Differential Revision: D3539287 fbshipit-source-id: ea95673491fee0ea82f0f1b79b8f60e00cd3d035 --- Libraries/Interaction/TaskQueue.js | 15 +++++++++------ Libraries/Interaction/__tests__/TaskQueue-test.js | 9 +++++++++ 2 files changed, 18 insertions(+), 6 deletions(-) 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); + }); });