-
Notifications
You must be signed in to change notification settings - Fork 24.3k
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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. */ | ||
|
@@ -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(); | ||
|
@@ -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)) { | ||
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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. prettier/prettier: Replace |
||
*/ | ||
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); | ||
}, | ||
|
||
/** | ||
|
@@ -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}`); | ||
} | ||
|
@@ -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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. prettier/prettier: Insert There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
|
There was a problem hiding this comment.
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·*/⏎···