Make InteractionManager tasks cancellable

Summary:
Returns a promise-like object with a new cancel function that will dig through the queue
and remove relevant tasks before they are executed. Handy when tasks are scheduled in react
components but should be cleaned up in unmount.

Reviewed By: devknoll

Differential Revision: D3406953

fbshipit-source-id: edf1157d831d5d6b63f13ee64cfd1c46843e79fa
This commit is contained in:
Spencer Ahrens
2016-06-08 22:54:19 -07:00
committed by Facebook Github Bot 2
parent b03a725447
commit be09cccb1f
4 changed files with 97 additions and 28 deletions

View File

@@ -1,5 +1,11 @@
/**
* Copyright 2004-present Facebook. All Rights Reserved.
* Copyright (c) 2013-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*
*/
'use strict';
@@ -49,7 +55,7 @@ describe('InteractionManager', () => {
});
it('notifies asynchronously when interaction stops', () => {
var handle = InteractionManager.createInteractionHandle();
const handle = InteractionManager.createInteractionHandle();
jest.runAllTimers();
interactionStart.mockClear();
InteractionManager.clearInteractionHandle(handle);
@@ -61,7 +67,7 @@ describe('InteractionManager', () => {
});
it('does not notify when started & stopped in same event loop', () => {
var handle = InteractionManager.createInteractionHandle();
const handle = InteractionManager.createInteractionHandle();
InteractionManager.clearInteractionHandle(handle);
jest.runAllTimers();
@@ -71,7 +77,7 @@ describe('InteractionManager', () => {
it('does not notify when going from two -> one active interactions', () => {
InteractionManager.createInteractionHandle();
var handle = InteractionManager.createInteractionHandle();
const handle = InteractionManager.createInteractionHandle();
jest.runAllTimers();
interactionStart.mockClear();
@@ -84,7 +90,7 @@ describe('InteractionManager', () => {
});
it('runs tasks asynchronously when there are interactions', () => {
var task = jest.fn();
const task = jest.fn();
InteractionManager.runAfterInteractions(task);
expect(task).not.toBeCalled();
@@ -93,8 +99,8 @@ describe('InteractionManager', () => {
});
it('runs tasks when interactions complete', () => {
var task = jest.fn();
var handle = InteractionManager.createInteractionHandle();
const task = jest.fn();
const handle = InteractionManager.createInteractionHandle();
InteractionManager.runAfterInteractions(task);
jest.runAllTimers();
@@ -106,8 +112,8 @@ describe('InteractionManager', () => {
});
it('does not run tasks twice', () => {
var task1 = jest.fn();
var task2 = jest.fn();
const task1 = jest.fn();
const task2 = jest.fn();
InteractionManager.runAfterInteractions(task1);
jest.runAllTimers();
@@ -118,10 +124,10 @@ describe('InteractionManager', () => {
});
it('runs tasks added while processing previous tasks', () => {
var task1 = jest.fn(() => {
const task1 = jest.fn(() => {
InteractionManager.runAfterInteractions(task2);
});
var task2 = jest.fn();
const task2 = jest.fn();
InteractionManager.runAfterInteractions(task1);
expect(task2).not.toBeCalled();
@@ -131,6 +137,20 @@ describe('InteractionManager', () => {
expect(task1).toBeCalled();
expect(task2).toBeCalled();
});
it('allows tasks to be cancelled', () => {
const task1 = jest.fn();
const task2 = jest.fn();
const promise1 = InteractionManager.runAfterInteractions(task1);
InteractionManager.runAfterInteractions(task2);
expect(task1).not.toBeCalled();
expect(task2).not.toBeCalled();
promise1.cancel();
jest.runAllTimers();
expect(task1).not.toBeCalled();
expect(task2).toBeCalled();
});
});
describe('promise tasks', () => {