Skip to content

Commit 3f1db67

Browse files
sahrensgrabbou
authored andcommitted
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
1 parent 78eec74 commit 3f1db67

2 files changed

Lines changed: 18 additions & 6 deletions

File tree

Libraries/Interaction/TaskQueue.js

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,12 @@ const infoLog = require('infoLog');
1515
const invariant = require('fbjs/lib/invariant');
1616

1717
type SimpleTask = {
18-
name: string;
19-
run: () => void;
18+
name: string,
19+
run: () => void,
2020
};
2121
type PromiseTask = {
22-
name: string;
23-
gen: () => Promise<any>;
22+
name: string,
23+
gen: () => Promise<any>,
2424
};
2525
export type Task = Function | SimpleTask | PromiseTask;
2626

@@ -75,7 +75,7 @@ class TaskQueue {
7575
...queue,
7676
tasks: queue.tasks.filter((task) => tasksToCancel.indexOf(task) === -1),
7777
}))
78-
.filter((queue) => queue.tasks.length > 0);
78+
.filter((queue, idx) => (queue.tasks.length > 0 || idx === 0));
7979
}
8080

8181
/**
@@ -151,7 +151,10 @@ class TaskQueue {
151151
DEBUG && infoLog('exec gen task ' + task.name);
152152
task.gen()
153153
.then(() => {
154-
DEBUG && infoLog('onThen for gen task ' + task.name, {stackIdx, queueStackSize: this._queueStack.length});
154+
DEBUG && infoLog(
155+
'onThen for gen task ' + task.name,
156+
{stackIdx, queueStackSize: this._queueStack.length},
157+
);
155158
this._queueStack[stackIdx].popable = true;
156159
this.hasTasksToProcess() && this._onMoreTasks();
157160
})

Libraries/Interaction/__tests__/TaskQueue-test.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,4 +142,13 @@ describe('TaskQueue', () => {
142142
expectToBeCalledOnce(task4);
143143
expect(taskQueue.hasTasksToProcess()).toBe(false);
144144
});
145+
146+
it('should not crash when last task is cancelled', () => {
147+
const task1 = jest.fn();
148+
taskQueue.enqueue(task1);
149+
taskQueue.cancelTasks([task1]);
150+
clearTaskQueue(taskQueue);
151+
expect(task1).not.toBeCalled();
152+
expect(taskQueue.hasTasksToProcess()).toBe(false);
153+
});
145154
});

0 commit comments

Comments
 (0)