Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding support to cancel a headless task #20416

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 33 additions & 6 deletions Libraries/ReactNative/AppRegistry.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ const renderApplication = require('renderApplication');

type Task = (taskData: any) => Promise<void>;
type TaskProvider = () => Task;
type TaskCanceller = () => void;
type TaskCancelProvider = () => TaskCanceller;

/* $FlowFixMe(>=0.90.0 site=react_native_fb) This comment suppresses an
* error found when Flow v0.90 was deployed. To see the error, delete this
* comment and run Flow. */
Expand Down Expand Up @@ -50,7 +53,8 @@ export type WrapperComponentProvider = any => React$ComponentType<*>;
const runnables: Runnables = {};
let runCount = 1;
const sections: Runnables = {};
const tasks: Map<string, TaskProvider> = new Map();
const taskProviders: Map<string, TaskProvider> = new Map();
const taskCancelProviders: Map<string, TaskCancelProvider> = new Map();
let componentProviderInstrumentationHook: ComponentProviderInstrumentationHook = (
component: ComponentProvider,
) => component();
Expand Down Expand Up @@ -212,13 +216,23 @@ const AppRegistry = {
*
* See http://facebook.github.io/react-native/docs/appregistry.html#registerheadlesstask
*/
registerHeadlessTask(taskKey: string, task: TaskProvider): void {
if (tasks.has(taskKey)) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

prettier/prettier: Replace ·/*·Cancel·is·no-op·*/ with ⏎······/*·Cancel·is·no-op·*/⏎···

registerHeadlessTask(taskKey: string, taskProvider: TaskProvider): void {
this.registerCancellableHeadlessTask(taskKey, taskProvider, () => () => { /* Cancel is no-op */ });
},

/**
* Register a cancellable headless task. A headless task is a bit of code that runs without a UI.
*
* See http://facebook.github.io/react-native/docs/appregistry.html#registercancellableheadlesstask

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

prettier/prettier: Replace taskKey:·string,·taskProvider:·TaskProvider,·taskCancelProvider:·TaskCancelProvider with ⏎····taskKey:·string,⏎····taskProvider:·TaskProvider,⏎····taskCancelProvider:·TaskCancelProvider,⏎··

*/
registerCancellableHeadlessTask(taskKey: string, taskProvider: TaskProvider, taskCancelProvider: TaskCancelProvider): void {
if (taskProviders.has(taskKey)) {
console.warn(
`registerHeadlessTask called multiple times for same key '${taskKey}'`,
`registerHeadlessTask or registerCancellableHeadlessTask called multiple times for same key '${taskKey}'`,
);
}
tasks.set(taskKey, task);
taskProviders.set(taskKey, taskProvider);
taskCancelProviders.set(taskKey, taskCancelProvider);
},

/**
Expand All @@ -227,7 +241,7 @@ const AppRegistry = {
* See http://facebook.github.io/react-native/docs/appregistry.html#startheadlesstask
*/
startHeadlessTask(taskId: number, taskKey: string, data: any): void {
const taskProvider = tasks.get(taskKey);
const taskProvider = taskProviders.get(taskKey);
if (!taskProvider) {
throw new Error(`No task registered for key ${taskKey}`);
}
Expand All @@ -240,6 +254,19 @@ const AppRegistry = {
NativeModules.HeadlessJsTaskSupport.notifyTaskFinished(taskId);
});
},

/**
* Only called from native code. Cancels a headless task.
*
* See http://facebook.github.io/react-native/docs/appregistry.html#cancelheadlesstask
*/
cancelHeadlessTask(taskId: number, taskKey: string): void {
const taskCancelProvider = taskCancelProviders.get(taskKey);
if (!taskCancelProvider) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

prettier/prettier: Insert ,

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

comma-dangle: Missing trailing comma.

throw new Error(`No task canceller registered for key '${taskKey}'`);
}
taskCancelProvider()();
psivaram marked this conversation as resolved.
Show resolved Hide resolved
}
};

BatchedBridge.registerCallableModule('AppRegistry', AppRegistry);
Expand Down