@zykeco/expo-background-task 1.0.2 → 1.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/android/src/main/java/expo/modules/backgroundtask/BackgroundTaskModule.kt +6 -0
- package/android/src/main/java/expo/modules/backgroundtask/BackgroundTaskScheduler.kt +20 -0
- package/build/BackgroundTask.d.ts +9 -2
- package/build/BackgroundTask.d.ts.map +1 -1
- package/build/BackgroundTask.js +13 -0
- package/build/BackgroundTask.js.map +1 -1
- package/build/BackgroundTask.types.d.ts +64 -0
- package/build/BackgroundTask.types.d.ts.map +1 -1
- package/build/BackgroundTask.types.js.map +1 -1
- package/build/ExpoBackgroundTaskModule.d.ts +2 -1
- package/build/ExpoBackgroundTaskModule.d.ts.map +1 -1
- package/build/ExpoBackgroundTaskModule.js.map +1 -1
- package/ios/BackgroundTaskModule.swift +4 -0
- package/ios/BackgroundTaskScheduler.swift +32 -0
- package/package.json +2 -1
- package/src/BackgroundTask.ts +16 -1
- package/src/BackgroundTask.types.ts +65 -0
- package/src/ExpoBackgroundTaskModule.ts +2 -1
|
@@ -23,6 +23,12 @@ class BackgroundTaskModule : Module() {
|
|
|
23
23
|
return@AsyncFunction 2 // WorkManager is always available on Android.
|
|
24
24
|
}
|
|
25
25
|
|
|
26
|
+
AsyncFunction("getSchedulerDiagnosticsAsync") Coroutine { ->
|
|
27
|
+
appContext.reactContext?.let {
|
|
28
|
+
return@Coroutine BackgroundTaskScheduler.diagnostics(it)
|
|
29
|
+
} ?: throw MissingContextException()
|
|
30
|
+
}
|
|
31
|
+
|
|
26
32
|
AsyncFunction("triggerTaskWorkerForTestingAsync") Coroutine { ->
|
|
27
33
|
if (ReactBuildConfig.DEBUG) {
|
|
28
34
|
Log.d(TAG, "Triggering tasks for testing")
|
|
@@ -259,6 +259,26 @@ object BackgroundTaskScheduler {
|
|
|
259
259
|
}
|
|
260
260
|
}
|
|
261
261
|
|
|
262
|
+
/**
|
|
263
|
+
* Returns diagnostic information about the scheduler state.
|
|
264
|
+
* Useful for debugging background task scheduling issues.
|
|
265
|
+
*/
|
|
266
|
+
suspend fun diagnostics(context: Context): Map<String, Any?> {
|
|
267
|
+
val workInfo = getWorkerInfo(context)
|
|
268
|
+
|
|
269
|
+
val result = mutableMapOf<String, Any?>(
|
|
270
|
+
"identifier" to WORKER_IDENTIFIER,
|
|
271
|
+
"registeredConsumers" to numberOfRegisteredTasksOfThisType,
|
|
272
|
+
"intervalMinutes" to intervalMinutes,
|
|
273
|
+
"requiresNetworkConnectivity" to requiresNetwork,
|
|
274
|
+
"inForeground" to inForeground,
|
|
275
|
+
"workerState" to workInfo?.state?.name,
|
|
276
|
+
"hasPendingRequest" to (workInfo?.state == WorkInfo.State.ENQUEUED || workInfo?.state == WorkInfo.State.RUNNING),
|
|
277
|
+
)
|
|
278
|
+
|
|
279
|
+
return result
|
|
280
|
+
}
|
|
281
|
+
|
|
262
282
|
/**
|
|
263
283
|
* Returns the worker info object from the WorkManager if the worker has been
|
|
264
284
|
* registered, otherwise returns null
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { BackgroundTaskOptions, BackgroundTaskStatus } from './BackgroundTask.types';
|
|
1
|
+
import { BackgroundTaskOptions, BackgroundTaskStatus, SchedulerDiagnostics } from './BackgroundTask.types';
|
|
2
2
|
/**
|
|
3
3
|
* Returns the status for the Background Task API. On web, it always returns `BackgroundTaskStatus.Restricted`,
|
|
4
4
|
* while on native platforms it returns `BackgroundTaskStatus.Available`.
|
|
@@ -48,6 +48,13 @@ export declare function unregisterTaskAsync(taskName: string): Promise<void>;
|
|
|
48
48
|
* @returns A promise which fulfils when the task is triggered.
|
|
49
49
|
*/
|
|
50
50
|
export declare function triggerTaskWorkerForTestingAsync(): Promise<boolean>;
|
|
51
|
+
/**
|
|
52
|
+
* Returns diagnostic information about the background task scheduler.
|
|
53
|
+
* Useful for debugging whether tasks are registered and scheduled correctly.
|
|
54
|
+
*
|
|
55
|
+
* @returns A promise that resolves with scheduler diagnostics. Fields vary by platform.
|
|
56
|
+
*/
|
|
57
|
+
export declare function getSchedulerDiagnosticsAsync(): Promise<SchedulerDiagnostics>;
|
|
51
58
|
/**
|
|
52
59
|
* Adds a listener that is called when the background executor expires. On iOS, tasks can run
|
|
53
60
|
* for minutes, but the system can interrupt the process at any time. This listener is called
|
|
@@ -59,5 +66,5 @@ export declare function triggerTaskWorkerForTestingAsync(): Promise<boolean>;
|
|
|
59
66
|
export declare function addExpirationListener(listener: () => void): {
|
|
60
67
|
remove: () => void;
|
|
61
68
|
};
|
|
62
|
-
export { BackgroundTaskStatus, BackgroundTaskResult, BackgroundTaskOptions, } from './BackgroundTask.types';
|
|
69
|
+
export { BackgroundTaskStatus, BackgroundTaskResult, BackgroundTaskOptions, SchedulerDiagnostics, } from './BackgroundTask.types';
|
|
63
70
|
//# sourceMappingURL=BackgroundTask.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"BackgroundTask.d.ts","sourceRoot":"","sources":["../src/BackgroundTask.ts"],"names":[],"mappings":"AAIA,OAAO,EAAE,qBAAqB,EAAE,oBAAoB,EAAE,MAAM,wBAAwB,CAAC;
|
|
1
|
+
{"version":3,"file":"BackgroundTask.d.ts","sourceRoot":"","sources":["../src/BackgroundTask.ts"],"names":[],"mappings":"AAIA,OAAO,EAAE,qBAAqB,EAAE,oBAAoB,EAAE,oBAAoB,EAAE,MAAM,wBAAwB,CAAC;AAwB3G;;;;;GAKG;AACH,eAAO,MAAM,cAAc,QAAa,OAAO,CAAC,oBAAoB,CAQnE,CAAC;AAGF;;;;;;;;;;;;;;;;;;;;;;;;;;;GA2BG;AACH,wBAAsB,iBAAiB,CACrC,QAAQ,EAAE,MAAM,EAChB,OAAO,GAAE,qBAA0B,GAClC,OAAO,CAAC,IAAI,CAAC,CA0Bf;AAGD;;;;GAIG;AACH,wBAAsB,mBAAmB,CAAC,QAAQ,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,CASzE;AAGD;;;;;GAKG;AACH,wBAAsB,gCAAgC,IAAI,OAAO,CAAC,OAAO,CAAC,CAUzE;AAGD;;;;;GAKG;AACH,wBAAsB,4BAA4B,IAAI,OAAO,CAAC,oBAAoB,CAAC,CAKlF;AAGD;;;;;;;GAOG;AACH,wBAAgB,qBAAqB,CAAC,QAAQ,EAAE,MAAM,IAAI,GAAG;IAAE,MAAM,EAAE,MAAM,IAAI,CAAA;CAAE,CAKlF;AAGD,OAAO,EACL,oBAAoB,EACpB,oBAAoB,EACpB,qBAAqB,EACrB,oBAAoB,GACrB,MAAM,wBAAwB,CAAC"}
|
package/build/BackgroundTask.js
CHANGED
|
@@ -122,6 +122,19 @@ export async function triggerTaskWorkerForTestingAsync() {
|
|
|
122
122
|
}
|
|
123
123
|
}
|
|
124
124
|
// @needsAudit
|
|
125
|
+
/**
|
|
126
|
+
* Returns diagnostic information about the background task scheduler.
|
|
127
|
+
* Useful for debugging whether tasks are registered and scheduled correctly.
|
|
128
|
+
*
|
|
129
|
+
* @returns A promise that resolves with scheduler diagnostics. Fields vary by platform.
|
|
130
|
+
*/
|
|
131
|
+
export async function getSchedulerDiagnosticsAsync() {
|
|
132
|
+
if (!ExpoBackgroundTaskModule.getSchedulerDiagnosticsAsync) {
|
|
133
|
+
throw new UnavailabilityError('BackgroundTask', 'getSchedulerDiagnosticsAsync');
|
|
134
|
+
}
|
|
135
|
+
return await ExpoBackgroundTaskModule.getSchedulerDiagnosticsAsync();
|
|
136
|
+
}
|
|
137
|
+
// @needsAudit
|
|
125
138
|
/**
|
|
126
139
|
* Adds a listener that is called when the background executor expires. On iOS, tasks can run
|
|
127
140
|
* for minutes, but the system can interrupt the process at any time. This listener is called
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"BackgroundTask.js","sourceRoot":"","sources":["../src/BackgroundTask.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,iBAAiB,EAAE,MAAM,MAAM,CAAC;AACzC,OAAO,EAAE,QA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AAED,cAAc;AACd;;;;;GAKG;AACH,MAAM,CAAC,KAAK,UAAU,gCAAgC;IACpD,IAAI,OAAO,EAAE,CAAC;QACZ,IAAI,CAAC,wBAAwB,CAAC,gCAAgC,EAAE,CAAC;YAC/D,MAAM,IAAI,mBAAmB,CAAC,gBAAgB,EAAE,kCAAkC,CAAC,CAAC;QACtF,CAAC;QACD,OAAO,CAAC,GAAG,CAAC,0CAA0C,CAAC,CAAC;QACxD,OAAO,MAAM,wBAAwB,CAAC,gCAAgC,EAAE,CAAC;IAC3E,CAAC;SAAM,CAAC;QACN,OAAO,OAAO,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC;IAChC,CAAC;AACH,CAAC;AAED,cAAc;AACd;;;;;;;GAOG;AACH,MAAM,UAAU,qBAAqB,CAAC,QAAoB;IACxD,IAAI,CAAC,wBAAwB,CAAC,WAAW,EAAE,CAAC;QAC1C,MAAM,IAAI,mBAAmB,CAAC,gBAAgB,EAAE,aAAa,CAAC,CAAC;IACjE,CAAC;IACD,OAAO,wBAAwB,CAAC,WAAW,CAAC,gBAAgB,EAAE,QAAQ,CAAC,CAAC;AAC1E,CAAC;AAED,eAAe;AACf,OAAO,EACL,oBAAoB,EACpB,oBAAoB,GAErB,MAAM,wBAAwB,CAAC","sourcesContent":["import { isRunningInExpoGo } from 'expo';\nimport { Platform, UnavailabilityError } from 'expo-modules-core';\nimport * as TaskManager from 'expo-task-manager';\n\nimport { BackgroundTaskOptions, BackgroundTaskStatus } from './BackgroundTask.types';\nimport ExpoBackgroundTaskModule from './ExpoBackgroundTaskModule';\n\n// Flag to warn about running on Apple simulator\nlet warnAboutRunningOniOSSimulator = false;\n\nlet warnedAboutExpoGo = false;\n\nfunction _validate(taskName: unknown) {\n if (isRunningInExpoGo()) {\n if (!warnedAboutExpoGo) {\n const message =\n '`Background Task` functionality is not available in Expo Go:\\n' +\n 'You can use this API and any others in a development build. Learn more: https://expo.fyi/dev-client.';\n console.warn(message);\n warnedAboutExpoGo = true;\n }\n }\n if (!taskName || typeof taskName !== 'string') {\n throw new TypeError('`taskName` must be a non-empty string.');\n }\n}\n\n// @needsAudit\n/**\n * Returns the status for the Background Task API. On web, it always returns `BackgroundTaskStatus.Restricted`,\n * while on native platforms it returns `BackgroundTaskStatus.Available`.\n *\n * @returns A BackgroundTaskStatus enum value or `null` if not available.\n */\nexport const getStatusAsync = async (): Promise<BackgroundTaskStatus> => {\n if (!ExpoBackgroundTaskModule.getStatusAsync) {\n throw new UnavailabilityError('BackgroundTask', 'getStatusAsync');\n }\n\n return isRunningInExpoGo()\n ? BackgroundTaskStatus.Restricted\n : ExpoBackgroundTaskModule.getStatusAsync();\n};\n\n// @needsAudit\n/**\n * Registers a background task with the given name. Registered tasks are saved in persistent storage and restored once the app is initialized.\n * @param taskName Name of the task to register. The task needs to be defined first - see [`TaskManager.defineTask`](task-manager/#taskmanagerdefinetasktaskname-taskexecutor)\n * for more details.\n * @param options An object containing the background task options.\n *\n * @example\n * ```ts\n * import * as TaskManager from 'expo-task-manager';\n *\n * // Register the task outside of the component\n * TaskManager.defineTask(BACKGROUND_TASK_IDENTIFIER, () => {\n * try {\n * await AsyncStorage.setItem(LAST_TASK_DATE_KEY, Date.now().toString());\n * } catch (error) {\n * console.error('Failed to save the last fetch date', error);\n * return BackgroundTaskResult.Failed;\n * }\n * return BackgroundTaskResult.Success;\n * });\n * ```\n *\n * You can now use the `registerTaskAsync` function to register the task:\n *\n * ```ts\n * BackgroundTask.registerTaskAsync(BACKGROUND_TASK_IDENTIFIER, {});\n * ```\n */\nexport async function registerTaskAsync(\n taskName: string,\n options: BackgroundTaskOptions = {}\n): Promise<void> {\n if (!ExpoBackgroundTaskModule.registerTaskAsync) {\n throw new UnavailabilityError('BackgroundTask', 'registerTaskAsync');\n }\n if (!TaskManager.isTaskDefined(taskName)) {\n throw new Error(\n `Task '${taskName}' is not defined. You must define a task using TaskManager.defineTask before registering.`\n );\n }\n\n if ((await ExpoBackgroundTaskModule.getStatusAsync()) === BackgroundTaskStatus.Restricted) {\n if (!warnAboutRunningOniOSSimulator) {\n const message =\n Platform.OS === 'ios'\n ? `Background tasks are not supported on iOS simulators. Skipped registering task: ${taskName}.`\n : `Background tasks are not available in the current environment. Skipped registering task: ${taskName}.`;\n console.warn(message);\n warnAboutRunningOniOSSimulator = true;\n }\n return;\n }\n _validate(taskName);\n if (await TaskManager.isTaskRegisteredAsync(taskName)) {\n return;\n }\n await ExpoBackgroundTaskModule.registerTaskAsync(taskName, options);\n}\n\n// @needsAudit\n/**\n * Unregisters a background task, so the application will no longer be executing this task.\n * @param taskName Name of the task to unregister.\n * @return A promise which fulfils when the task is fully unregistered.\n */\nexport async function unregisterTaskAsync(taskName: string): Promise<void> {\n if (!ExpoBackgroundTaskModule.unregisterTaskAsync) {\n throw new UnavailabilityError('BackgroundTask', 'unregisterTaskAsync');\n }\n _validate(taskName);\n if (!(await TaskManager.isTaskRegisteredAsync(taskName))) {\n return;\n }\n await ExpoBackgroundTaskModule.unregisterTaskAsync(taskName);\n}\n\n// @needsAudit\n/**\n * When in debug mode this function will trigger running the background tasks.\n * This function will only work for apps built in debug mode.\n * This method is only available in development mode. It will not work in production builds.\n * @returns A promise which fulfils when the task is triggered.\n */\nexport async function triggerTaskWorkerForTestingAsync(): Promise<boolean> {\n if (__DEV__) {\n if (!ExpoBackgroundTaskModule.triggerTaskWorkerForTestingAsync) {\n throw new UnavailabilityError('BackgroundTask', 'triggerTaskWorkerForTestingAsync');\n }\n console.log('Calling triggerTaskWorkerForTestingAsync');\n return await ExpoBackgroundTaskModule.triggerTaskWorkerForTestingAsync();\n } else {\n return Promise.resolve(false);\n }\n}\n\n// @needsAudit\n/**\n * Adds a listener that is called when the background executor expires. On iOS, tasks can run\n * for minutes, but the system can interrupt the process at any time. This listener is called\n * when the system decides to stop the background tasks and should be used to clean up resources\n * or save state. When the expiry handler is called, the main task runner is rescheduled automatically.\n * @platform ios\n * @return An object with a `remove` method to unsubscribe the listener.\n */\nexport function addExpirationListener(listener: () => void): { remove: () => void } {\n if (!ExpoBackgroundTaskModule.addListener) {\n throw new UnavailabilityError('BackgroundTask', 'addListener');\n }\n return ExpoBackgroundTaskModule.addListener('onTasksExpired', listener);\n}\n\n// Export types\nexport {\n BackgroundTaskStatus,\n BackgroundTaskResult,\n BackgroundTaskOptions,\n} from './BackgroundTask.types';\n"]}
|
|
1
|
+
{"version":3,"file":"BackgroundTask.js","sourceRoot":"","sources":["../src/BackgroundTask.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,iBAAiB,EAAE,MAAM,MAAM,CAAC;AACzC,OAAO,EAAE,QAAQ,EAAE,mBAAmB,EAAE,MAAM,mBAAmB,CAAC;AAClE,OAAO,KAAK,WAAW,MAAM,mBAAmB,CAAC;AAEjD,OAAO,EAAyB,oBAAoB,EAAwB,MAAM,wBAAwB,CAAC;AAC3G,OAAO,wBAAwB,MAAM,4BAA4B,CAAC;AAElE,gDAAgD;AAChD,IAAI,8BAA8B,GAAG,KAAK,CAAC;AAE3C,IAAI,iBAAiB,GAAG,KAAK,CAAC;AAE9B,SAAS,SAAS,CAAC,QAAiB;IAClC,IAAI,iBAAiB,EAAE,EAAE,CAAC;QACxB,IAAI,CAAC,iBAAiB,EAAE,CAAC;YACvB,MAAM,OAAO,GACX,gEAAgE;gBAChE,sGAAsG,CAAC;YACzG,OAAO,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC;YACtB,iBAAiB,GAAG,IAAI,CAAC;QAC3B,CAAC;IACH,CAAC;IACD,IAAI,CAAC,QAAQ,IAAI,OAAO,QAAQ,KAAK,QAAQ,EAAE,CAAC;QAC9C,MAAM,IAAI,SAAS,CAAC,wCAAwC,CAAC,CAAC;IAChE,CAAC;AACH,CAAC;AAED,cAAc;AACd;;;;;GAKG;AACH,MAAM,CAAC,MAAM,cAAc,GAAG,KAAK,IAAmC,EAAE;IACtE,IAAI,CAAC,wBAAwB,CAAC,cAAc,EAAE,CAAC;QAC7C,MAAM,IAAI,mBAAmB,CAAC,gBAAg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Aa,CAAC,CAAC;IACjE,CAAC;IACD,OAAO,wBAAwB,CAAC,WAAW,CAAC,gBAAgB,EAAE,QAAQ,CAAC,CAAC;AAC1E,CAAC;AAED,eAAe;AACf,OAAO,EACL,oBAAoB,EACpB,oBAAoB,GAGrB,MAAM,wBAAwB,CAAC","sourcesContent":["import { isRunningInExpoGo } from 'expo';\nimport { Platform, UnavailabilityError } from 'expo-modules-core';\nimport * as TaskManager from 'expo-task-manager';\n\nimport { BackgroundTaskOptions, BackgroundTaskStatus, SchedulerDiagnostics } from './BackgroundTask.types';\nimport ExpoBackgroundTaskModule from './ExpoBackgroundTaskModule';\n\n// Flag to warn about running on Apple simulator\nlet warnAboutRunningOniOSSimulator = false;\n\nlet warnedAboutExpoGo = false;\n\nfunction _validate(taskName: unknown) {\n if (isRunningInExpoGo()) {\n if (!warnedAboutExpoGo) {\n const message =\n '`Background Task` functionality is not available in Expo Go:\\n' +\n 'You can use this API and any others in a development build. Learn more: https://expo.fyi/dev-client.';\n console.warn(message);\n warnedAboutExpoGo = true;\n }\n }\n if (!taskName || typeof taskName !== 'string') {\n throw new TypeError('`taskName` must be a non-empty string.');\n }\n}\n\n// @needsAudit\n/**\n * Returns the status for the Background Task API. On web, it always returns `BackgroundTaskStatus.Restricted`,\n * while on native platforms it returns `BackgroundTaskStatus.Available`.\n *\n * @returns A BackgroundTaskStatus enum value or `null` if not available.\n */\nexport const getStatusAsync = async (): Promise<BackgroundTaskStatus> => {\n if (!ExpoBackgroundTaskModule.getStatusAsync) {\n throw new UnavailabilityError('BackgroundTask', 'getStatusAsync');\n }\n\n return isRunningInExpoGo()\n ? BackgroundTaskStatus.Restricted\n : ExpoBackgroundTaskModule.getStatusAsync();\n};\n\n// @needsAudit\n/**\n * Registers a background task with the given name. Registered tasks are saved in persistent storage and restored once the app is initialized.\n * @param taskName Name of the task to register. The task needs to be defined first - see [`TaskManager.defineTask`](task-manager/#taskmanagerdefinetasktaskname-taskexecutor)\n * for more details.\n * @param options An object containing the background task options.\n *\n * @example\n * ```ts\n * import * as TaskManager from 'expo-task-manager';\n *\n * // Register the task outside of the component\n * TaskManager.defineTask(BACKGROUND_TASK_IDENTIFIER, () => {\n * try {\n * await AsyncStorage.setItem(LAST_TASK_DATE_KEY, Date.now().toString());\n * } catch (error) {\n * console.error('Failed to save the last fetch date', error);\n * return BackgroundTaskResult.Failed;\n * }\n * return BackgroundTaskResult.Success;\n * });\n * ```\n *\n * You can now use the `registerTaskAsync` function to register the task:\n *\n * ```ts\n * BackgroundTask.registerTaskAsync(BACKGROUND_TASK_IDENTIFIER, {});\n * ```\n */\nexport async function registerTaskAsync(\n taskName: string,\n options: BackgroundTaskOptions = {}\n): Promise<void> {\n if (!ExpoBackgroundTaskModule.registerTaskAsync) {\n throw new UnavailabilityError('BackgroundTask', 'registerTaskAsync');\n }\n if (!TaskManager.isTaskDefined(taskName)) {\n throw new Error(\n `Task '${taskName}' is not defined. You must define a task using TaskManager.defineTask before registering.`\n );\n }\n\n if ((await ExpoBackgroundTaskModule.getStatusAsync()) === BackgroundTaskStatus.Restricted) {\n if (!warnAboutRunningOniOSSimulator) {\n const message =\n Platform.OS === 'ios'\n ? `Background tasks are not supported on iOS simulators. Skipped registering task: ${taskName}.`\n : `Background tasks are not available in the current environment. Skipped registering task: ${taskName}.`;\n console.warn(message);\n warnAboutRunningOniOSSimulator = true;\n }\n return;\n }\n _validate(taskName);\n if (await TaskManager.isTaskRegisteredAsync(taskName)) {\n return;\n }\n await ExpoBackgroundTaskModule.registerTaskAsync(taskName, options);\n}\n\n// @needsAudit\n/**\n * Unregisters a background task, so the application will no longer be executing this task.\n * @param taskName Name of the task to unregister.\n * @return A promise which fulfils when the task is fully unregistered.\n */\nexport async function unregisterTaskAsync(taskName: string): Promise<void> {\n if (!ExpoBackgroundTaskModule.unregisterTaskAsync) {\n throw new UnavailabilityError('BackgroundTask', 'unregisterTaskAsync');\n }\n _validate(taskName);\n if (!(await TaskManager.isTaskRegisteredAsync(taskName))) {\n return;\n }\n await ExpoBackgroundTaskModule.unregisterTaskAsync(taskName);\n}\n\n// @needsAudit\n/**\n * When in debug mode this function will trigger running the background tasks.\n * This function will only work for apps built in debug mode.\n * This method is only available in development mode. It will not work in production builds.\n * @returns A promise which fulfils when the task is triggered.\n */\nexport async function triggerTaskWorkerForTestingAsync(): Promise<boolean> {\n if (__DEV__) {\n if (!ExpoBackgroundTaskModule.triggerTaskWorkerForTestingAsync) {\n throw new UnavailabilityError('BackgroundTask', 'triggerTaskWorkerForTestingAsync');\n }\n console.log('Calling triggerTaskWorkerForTestingAsync');\n return await ExpoBackgroundTaskModule.triggerTaskWorkerForTestingAsync();\n } else {\n return Promise.resolve(false);\n }\n}\n\n// @needsAudit\n/**\n * Returns diagnostic information about the background task scheduler.\n * Useful for debugging whether tasks are registered and scheduled correctly.\n *\n * @returns A promise that resolves with scheduler diagnostics. Fields vary by platform.\n */\nexport async function getSchedulerDiagnosticsAsync(): Promise<SchedulerDiagnostics> {\n if (!ExpoBackgroundTaskModule.getSchedulerDiagnosticsAsync) {\n throw new UnavailabilityError('BackgroundTask', 'getSchedulerDiagnosticsAsync');\n }\n return await ExpoBackgroundTaskModule.getSchedulerDiagnosticsAsync();\n}\n\n// @needsAudit\n/**\n * Adds a listener that is called when the background executor expires. On iOS, tasks can run\n * for minutes, but the system can interrupt the process at any time. This listener is called\n * when the system decides to stop the background tasks and should be used to clean up resources\n * or save state. When the expiry handler is called, the main task runner is rescheduled automatically.\n * @platform ios\n * @return An object with a `remove` method to unsubscribe the listener.\n */\nexport function addExpirationListener(listener: () => void): { remove: () => void } {\n if (!ExpoBackgroundTaskModule.addListener) {\n throw new UnavailabilityError('BackgroundTask', 'addListener');\n }\n return ExpoBackgroundTaskModule.addListener('onTasksExpired', listener);\n}\n\n// Export types\nexport {\n BackgroundTaskStatus,\n BackgroundTaskResult,\n BackgroundTaskOptions,\n SchedulerDiagnostics,\n} from './BackgroundTask.types';\n"]}
|
|
@@ -68,4 +68,68 @@ export type BackgroundTaskOptions = {
|
|
|
68
68
|
*/
|
|
69
69
|
requiresExternalPower?: boolean;
|
|
70
70
|
};
|
|
71
|
+
/**
|
|
72
|
+
* Diagnostic information about the background task scheduler state.
|
|
73
|
+
* Fields vary by platform — iOS-only fields are `undefined` on Android and vice versa.
|
|
74
|
+
*/
|
|
75
|
+
export type SchedulerDiagnostics = {
|
|
76
|
+
/** Native worker/task identifier */
|
|
77
|
+
identifier: string;
|
|
78
|
+
/** Number of currently registered task consumers */
|
|
79
|
+
registeredConsumers: number;
|
|
80
|
+
/** Whether a pending background task request exists */
|
|
81
|
+
hasPendingRequest: boolean;
|
|
82
|
+
/** Whether the task requires network connectivity */
|
|
83
|
+
requiresNetworkConnectivity: boolean;
|
|
84
|
+
/**
|
|
85
|
+
* Whether the device supports background tasks.
|
|
86
|
+
* @platform ios
|
|
87
|
+
*/
|
|
88
|
+
supportsBackgroundTasks?: boolean;
|
|
89
|
+
/**
|
|
90
|
+
* The BGTask type used ('refresh' or 'processing').
|
|
91
|
+
* @platform ios
|
|
92
|
+
*/
|
|
93
|
+
taskType?: string;
|
|
94
|
+
/**
|
|
95
|
+
* Configured interval in seconds.
|
|
96
|
+
* @platform ios
|
|
97
|
+
*/
|
|
98
|
+
intervalSeconds?: number;
|
|
99
|
+
/**
|
|
100
|
+
* Whether the task requires external power.
|
|
101
|
+
* @platform ios
|
|
102
|
+
*/
|
|
103
|
+
requiresExternalPower?: boolean;
|
|
104
|
+
/**
|
|
105
|
+
* ISO-8601 date string of the earliest time the next task can begin.
|
|
106
|
+
* @platform ios
|
|
107
|
+
*/
|
|
108
|
+
earliestBeginDate?: string;
|
|
109
|
+
/**
|
|
110
|
+
* Seconds until the next scheduled run (can be negative if overdue).
|
|
111
|
+
* @platform ios
|
|
112
|
+
*/
|
|
113
|
+
secondsUntilNextRun?: number;
|
|
114
|
+
/**
|
|
115
|
+
* The type of the pending request ('refresh' or 'processing').
|
|
116
|
+
* @platform ios
|
|
117
|
+
*/
|
|
118
|
+
pendingRequestType?: string;
|
|
119
|
+
/**
|
|
120
|
+
* Configured interval in minutes.
|
|
121
|
+
* @platform android
|
|
122
|
+
*/
|
|
123
|
+
intervalMinutes?: number;
|
|
124
|
+
/**
|
|
125
|
+
* Whether the app is currently in the foreground.
|
|
126
|
+
* @platform android
|
|
127
|
+
*/
|
|
128
|
+
inForeground?: boolean;
|
|
129
|
+
/**
|
|
130
|
+
* WorkManager worker state (e.g. 'ENQUEUED', 'RUNNING').
|
|
131
|
+
* @platform android
|
|
132
|
+
*/
|
|
133
|
+
workerState?: string;
|
|
134
|
+
};
|
|
71
135
|
//# sourceMappingURL=BackgroundTask.types.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"BackgroundTask.types.d.ts","sourceRoot":"","sources":["../src/BackgroundTask.types.ts"],"names":[],"mappings":"AACA;;GAEG;AACH,oBAAY,oBAAoB;IAC9B;;OAEG;IACH,UAAU,IAAI;IACd;;OAEG;IACH,SAAS,IAAI;CACd;AAGD;;GAEG;AACH,oBAAY,oBAAoB;IAC9B;;OAEG;IACH,OAAO,IAAI;IACX;;OAEG;IACH,MAAM,IAAI;CACX;AAGD;;GAEG;AACH,MAAM,MAAM,qBAAqB,GAAG;IAClC;;;;;;;;;OASG;IACH,eAAe,CAAC,EAAE,MAAM,CAAC;IACzB;;;;;OAKG;IACH,2BAA2B,CAAC,EAAE,OAAO,CAAC;IACtC;;;;;;;;;;;OAWG;IACH,WAAW,CAAC,EAAE,SAAS,GAAG,YAAY,CAAC;IACvC;;;;;;OAMG;IACH,qBAAqB,CAAC,EAAE,OAAO,CAAC;CACjC,CAAC"}
|
|
1
|
+
{"version":3,"file":"BackgroundTask.types.d.ts","sourceRoot":"","sources":["../src/BackgroundTask.types.ts"],"names":[],"mappings":"AACA;;GAEG;AACH,oBAAY,oBAAoB;IAC9B;;OAEG;IACH,UAAU,IAAI;IACd;;OAEG;IACH,SAAS,IAAI;CACd;AAGD;;GAEG;AACH,oBAAY,oBAAoB;IAC9B;;OAEG;IACH,OAAO,IAAI;IACX;;OAEG;IACH,MAAM,IAAI;CACX;AAGD;;GAEG;AACH,MAAM,MAAM,qBAAqB,GAAG;IAClC;;;;;;;;;OASG;IACH,eAAe,CAAC,EAAE,MAAM,CAAC;IACzB;;;;;OAKG;IACH,2BAA2B,CAAC,EAAE,OAAO,CAAC;IACtC;;;;;;;;;;;OAWG;IACH,WAAW,CAAC,EAAE,SAAS,GAAG,YAAY,CAAC;IACvC;;;;;;OAMG;IACH,qBAAqB,CAAC,EAAE,OAAO,CAAC;CACjC,CAAC;AAEF;;;GAGG;AACH,MAAM,MAAM,oBAAoB,GAAG;IACjC,oCAAoC;IACpC,UAAU,EAAE,MAAM,CAAC;IACnB,oDAAoD;IACpD,mBAAmB,EAAE,MAAM,CAAC;IAC5B,uDAAuD;IACvD,iBAAiB,EAAE,OAAO,CAAC;IAC3B,qDAAqD;IACrD,2BAA2B,EAAE,OAAO,CAAC;IACrC;;;OAGG;IACH,uBAAuB,CAAC,EAAE,OAAO,CAAC;IAClC;;;OAGG;IACH,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB;;;OAGG;IACH,eAAe,CAAC,EAAE,MAAM,CAAC;IACzB;;;OAGG;IACH,qBAAqB,CAAC,EAAE,OAAO,CAAC;IAChC;;;OAGG;IACH,iBAAiB,CAAC,EAAE,MAAM,CAAC;IAC3B;;;OAGG;IACH,mBAAmB,CAAC,EAAE,MAAM,CAAC;IAC7B;;;OAGG;IACH,kBAAkB,CAAC,EAAE,MAAM,CAAC;IAC5B;;;OAGG;IACH,eAAe,CAAC,EAAE,MAAM,CAAC;IACzB;;;OAGG;IACH,YAAY,CAAC,EAAE,OAAO,CAAC;IACvB;;;OAGG;IACH,WAAW,CAAC,EAAE,MAAM,CAAC;CACtB,CAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"BackgroundTask.types.js","sourceRoot":"","sources":["../src/BackgroundTask.types.ts"],"names":[],"mappings":"AAAA,cAAc;AACd;;GAEG;AACH,MAAM,CAAN,IAAY,oBASX;AATD,WAAY,oBAAoB;IAC9B;;OAEG;IACH,2EAAc,CAAA;IACd;;OAEG;IACH,yEAAa,CAAA;AACf,CAAC,EATW,oBAAoB,KAApB,oBAAoB,QAS/B;AAED,cAAc;AACd;;GAEG;AACH,MAAM,CAAN,IAAY,oBASX;AATD,WAAY,oBAAoB;IAC9B;;OAEG;IACH,qEAAW,CAAA;IACX;;OAEG;IACH,mEAAU,CAAA;AACZ,CAAC,EATW,oBAAoB,KAApB,oBAAoB,QAS/B","sourcesContent":["// @needsAudit\n/**\n * Availability status for background tasks\n */\nexport enum BackgroundTaskStatus {\n /**\n * Background tasks are unavailable.\n */\n Restricted = 1,\n /**\n * Background tasks are available for the app.\n */\n Available = 2,\n}\n\n// @needsAudit\n/**\n * Return value for background tasks.\n */\nexport enum BackgroundTaskResult {\n /**\n * The task finished successfully.\n */\n Success = 1,\n /**\n * The task failed.\n */\n Failed = 2,\n}\n\n// @needsAudit\n/**\n * Options for registering a background task\n */\nexport type BackgroundTaskOptions = {\n /**\n * Inexact interval in minutes between subsequent repeats of the background tasks. The final\n * interval may differ from the specified one to minimize wakeups and battery usage.\n * - Defaults to once every 12 hours (The minimum interval is 15 minutes)\n * - The system controls the background task execution interval and treats the\n * specified value as a minimum delay. Tasks won't run exactly on schedule. On iOS, short\n * intervals are often ignored—the system typically runs background tasks during\n * specific windows, such as overnight.\n *\n */\n minimumInterval?: number;\n /**\n * Whether the task requires network connectivity.\n *\n * @default true\n * @platform ios, android\n */\n requiresNetworkConnectivity?: boolean;\n /**\n * iOS-only: Which BGTask type to use.\n * - `'refresh'` (default): BGAppRefreshTaskRequest — aggressive scheduling (~15-30 min),\n * ~30 s execution limit. Suitable for periodic syncs and content updates.\n * - `'processing'`: BGProcessingTaskRequest — iOS defers at its own discretion\n * (hours/days), but allows several minutes of runtime. Suitable for heavy work\n * (ML training, DB migration). Supports `requiresNetworkConnectivity` and\n * `requiresExternalPower`.\n *\n * @default 'refresh'\n * @platform ios\n */\n iosTaskType?: 'refresh' | 'processing';\n /**\n * iOS-only: Whether the task requires external power.\n * Only relevant when `iosTaskType: 'processing'`.\n *\n * @default false\n * @platform ios\n */\n requiresExternalPower?: boolean;\n};\n"]}
|
|
1
|
+
{"version":3,"file":"BackgroundTask.types.js","sourceRoot":"","sources":["../src/BackgroundTask.types.ts"],"names":[],"mappings":"AAAA,cAAc;AACd;;GAEG;AACH,MAAM,CAAN,IAAY,oBASX;AATD,WAAY,oBAAoB;IAC9B;;OAEG;IACH,2EAAc,CAAA;IACd;;OAEG;IACH,yEAAa,CAAA;AACf,CAAC,EATW,oBAAoB,KAApB,oBAAoB,QAS/B;AAED,cAAc;AACd;;GAEG;AACH,MAAM,CAAN,IAAY,oBASX;AATD,WAAY,oBAAoB;IAC9B;;OAEG;IACH,qEAAW,CAAA;IACX;;OAEG;IACH,mEAAU,CAAA;AACZ,CAAC,EATW,oBAAoB,KAApB,oBAAoB,QAS/B","sourcesContent":["// @needsAudit\n/**\n * Availability status for background tasks\n */\nexport enum BackgroundTaskStatus {\n /**\n * Background tasks are unavailable.\n */\n Restricted = 1,\n /**\n * Background tasks are available for the app.\n */\n Available = 2,\n}\n\n// @needsAudit\n/**\n * Return value for background tasks.\n */\nexport enum BackgroundTaskResult {\n /**\n * The task finished successfully.\n */\n Success = 1,\n /**\n * The task failed.\n */\n Failed = 2,\n}\n\n// @needsAudit\n/**\n * Options for registering a background task\n */\nexport type BackgroundTaskOptions = {\n /**\n * Inexact interval in minutes between subsequent repeats of the background tasks. The final\n * interval may differ from the specified one to minimize wakeups and battery usage.\n * - Defaults to once every 12 hours (The minimum interval is 15 minutes)\n * - The system controls the background task execution interval and treats the\n * specified value as a minimum delay. Tasks won't run exactly on schedule. On iOS, short\n * intervals are often ignored—the system typically runs background tasks during\n * specific windows, such as overnight.\n *\n */\n minimumInterval?: number;\n /**\n * Whether the task requires network connectivity.\n *\n * @default true\n * @platform ios, android\n */\n requiresNetworkConnectivity?: boolean;\n /**\n * iOS-only: Which BGTask type to use.\n * - `'refresh'` (default): BGAppRefreshTaskRequest — aggressive scheduling (~15-30 min),\n * ~30 s execution limit. Suitable for periodic syncs and content updates.\n * - `'processing'`: BGProcessingTaskRequest — iOS defers at its own discretion\n * (hours/days), but allows several minutes of runtime. Suitable for heavy work\n * (ML training, DB migration). Supports `requiresNetworkConnectivity` and\n * `requiresExternalPower`.\n *\n * @default 'refresh'\n * @platform ios\n */\n iosTaskType?: 'refresh' | 'processing';\n /**\n * iOS-only: Whether the task requires external power.\n * Only relevant when `iosTaskType: 'processing'`.\n *\n * @default false\n * @platform ios\n */\n requiresExternalPower?: boolean;\n};\n\n/**\n * Diagnostic information about the background task scheduler state.\n * Fields vary by platform — iOS-only fields are `undefined` on Android and vice versa.\n */\nexport type SchedulerDiagnostics = {\n /** Native worker/task identifier */\n identifier: string;\n /** Number of currently registered task consumers */\n registeredConsumers: number;\n /** Whether a pending background task request exists */\n hasPendingRequest: boolean;\n /** Whether the task requires network connectivity */\n requiresNetworkConnectivity: boolean;\n /**\n * Whether the device supports background tasks.\n * @platform ios\n */\n supportsBackgroundTasks?: boolean;\n /**\n * The BGTask type used ('refresh' or 'processing').\n * @platform ios\n */\n taskType?: string;\n /**\n * Configured interval in seconds.\n * @platform ios\n */\n intervalSeconds?: number;\n /**\n * Whether the task requires external power.\n * @platform ios\n */\n requiresExternalPower?: boolean;\n /**\n * ISO-8601 date string of the earliest time the next task can begin.\n * @platform ios\n */\n earliestBeginDate?: string;\n /**\n * Seconds until the next scheduled run (can be negative if overdue).\n * @platform ios\n */\n secondsUntilNextRun?: number;\n /**\n * The type of the pending request ('refresh' or 'processing').\n * @platform ios\n */\n pendingRequestType?: string;\n /**\n * Configured interval in minutes.\n * @platform android\n */\n intervalMinutes?: number;\n /**\n * Whether the app is currently in the foreground.\n * @platform android\n */\n inForeground?: boolean;\n /**\n * WorkManager worker state (e.g. 'ENQUEUED', 'RUNNING').\n * @platform android\n */\n workerState?: string;\n};\n"]}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { type NativeModule } from 'expo';
|
|
2
|
-
import { BackgroundTaskOptions, BackgroundTaskStatus } from './BackgroundTask.types';
|
|
2
|
+
import { BackgroundTaskOptions, BackgroundTaskStatus, SchedulerDiagnostics } from './BackgroundTask.types';
|
|
3
3
|
type ExpoBackgroundTaskEvents = {
|
|
4
4
|
onTasksExpired(): void;
|
|
5
5
|
};
|
|
@@ -8,6 +8,7 @@ declare class ExpoBackgroundTaskModule extends NativeModule<ExpoBackgroundTaskEv
|
|
|
8
8
|
registerTaskAsync(name: string, options: BackgroundTaskOptions): Promise<void>;
|
|
9
9
|
unregisterTaskAsync(name: string): Promise<void>;
|
|
10
10
|
triggerTaskWorkerForTestingAsync(): Promise<boolean>;
|
|
11
|
+
getSchedulerDiagnosticsAsync(): Promise<SchedulerDiagnostics>;
|
|
11
12
|
}
|
|
12
13
|
declare const _default: ExpoBackgroundTaskModule;
|
|
13
14
|
export default _default;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ExpoBackgroundTaskModule.d.ts","sourceRoot":"","sources":["../src/ExpoBackgroundTaskModule.ts"],"names":[],"mappings":"AAAA,OAAO,EAAuB,KAAK,YAAY,EAAE,MAAM,MAAM,CAAC;AAE9D,OAAO,EAAE,qBAAqB,EAAE,oBAAoB,EAAE,MAAM,wBAAwB,CAAC;
|
|
1
|
+
{"version":3,"file":"ExpoBackgroundTaskModule.d.ts","sourceRoot":"","sources":["../src/ExpoBackgroundTaskModule.ts"],"names":[],"mappings":"AAAA,OAAO,EAAuB,KAAK,YAAY,EAAE,MAAM,MAAM,CAAC;AAE9D,OAAO,EAAE,qBAAqB,EAAE,oBAAoB,EAAE,oBAAoB,EAAE,MAAM,wBAAwB,CAAC;AAE3G,KAAK,wBAAwB,GAAG;IAC9B,cAAc,IAAI,IAAI,CAAC;CACxB,CAAC;AAEF,OAAO,OAAO,wBAAyB,SAAQ,YAAY,CAAC,wBAAwB,CAAC;IACnF,cAAc,IAAI,OAAO,CAAC,oBAAoB,CAAC;IAC/C,iBAAiB,CAAC,IAAI,EAAE,MAAM,EAAE,OAAO,EAAE,qBAAqB,GAAG,OAAO,CAAC,IAAI,CAAC;IAC9E,mBAAmB,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;IAChD,gCAAgC,IAAI,OAAO,CAAC,OAAO,CAAC;IACpD,4BAA4B,IAAI,OAAO,CAAC,oBAAoB,CAAC;CAC9D;;AAED,wBAAmF"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ExpoBackgroundTaskModule.js","sourceRoot":"","sources":["../src/ExpoBackgroundTaskModule.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,mBAAmB,EAAqB,MAAM,MAAM,CAAC;
|
|
1
|
+
{"version":3,"file":"ExpoBackgroundTaskModule.js","sourceRoot":"","sources":["../src/ExpoBackgroundTaskModule.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,mBAAmB,EAAqB,MAAM,MAAM,CAAC;AAgB9D,eAAe,mBAAmB,CAA2B,oBAAoB,CAAC,CAAC","sourcesContent":["import { requireNativeModule, type NativeModule } from 'expo';\n\nimport { BackgroundTaskOptions, BackgroundTaskStatus, SchedulerDiagnostics } from './BackgroundTask.types';\n\ntype ExpoBackgroundTaskEvents = {\n onTasksExpired(): void;\n};\n\ndeclare class ExpoBackgroundTaskModule extends NativeModule<ExpoBackgroundTaskEvents> {\n getStatusAsync(): Promise<BackgroundTaskStatus>;\n registerTaskAsync(name: string, options: BackgroundTaskOptions): Promise<void>;\n unregisterTaskAsync(name: string): Promise<void>;\n triggerTaskWorkerForTestingAsync(): Promise<boolean>;\n getSchedulerDiagnosticsAsync(): Promise<SchedulerDiagnostics>;\n}\n\nexport default requireNativeModule<ExpoBackgroundTaskModule>('ExpoBackgroundTask');\n"]}
|
|
@@ -69,6 +69,10 @@ public class BackgroundTaskModule: Module {
|
|
|
69
69
|
return BackgroundTaskScheduler.supportsBackgroundTasks()
|
|
70
70
|
? BackgroundTaskStatus.available : .restricted
|
|
71
71
|
}
|
|
72
|
+
|
|
73
|
+
AsyncFunction("getSchedulerDiagnosticsAsync") {
|
|
74
|
+
return await BackgroundTaskScheduler.diagnostics()
|
|
75
|
+
}
|
|
72
76
|
}
|
|
73
77
|
|
|
74
78
|
@objc func handleTasksExpiredNotification(_ notification: Notification) {
|
|
@@ -192,6 +192,38 @@ public class BackgroundTaskScheduler {
|
|
|
192
192
|
return requests.contains(where: { $0.identifier == BackgroundTaskConstants.BackgroundWorkerIdentifier })
|
|
193
193
|
}
|
|
194
194
|
|
|
195
|
+
/**
|
|
196
|
+
Returns diagnostic information about the scheduler state.
|
|
197
|
+
Useful for debugging background task scheduling issues.
|
|
198
|
+
*/
|
|
199
|
+
public static func diagnostics() async -> [String: Any] {
|
|
200
|
+
let pending = await BGTaskScheduler.shared.pendingTaskRequests()
|
|
201
|
+
let matchingRequest = pending.first(where: {
|
|
202
|
+
$0.identifier == BackgroundTaskConstants.BackgroundWorkerIdentifier
|
|
203
|
+
})
|
|
204
|
+
|
|
205
|
+
var result: [String: Any] = [
|
|
206
|
+
"identifier": BackgroundTaskConstants.BackgroundWorkerIdentifier,
|
|
207
|
+
"registeredConsumers": numberOfRegisteredTasksOfThisType,
|
|
208
|
+
"taskType": taskType,
|
|
209
|
+
"intervalSeconds": intervalSeconds,
|
|
210
|
+
"requiresNetworkConnectivity": requiresNetwork,
|
|
211
|
+
"requiresExternalPower": requiresExternalPower,
|
|
212
|
+
"hasPendingRequest": matchingRequest != nil,
|
|
213
|
+
"supportsBackgroundTasks": supportsBackgroundTasks(),
|
|
214
|
+
]
|
|
215
|
+
|
|
216
|
+
if let request = matchingRequest {
|
|
217
|
+
if let earliestBeginDate = request.earliestBeginDate {
|
|
218
|
+
result["earliestBeginDate"] = ISO8601DateFormatter().string(from: earliestBeginDate)
|
|
219
|
+
result["secondsUntilNextRun"] = earliestBeginDate.timeIntervalSinceNow
|
|
220
|
+
}
|
|
221
|
+
result["pendingRequestType"] = request is BGProcessingTaskRequest ? "processing" : "refresh"
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
return result
|
|
225
|
+
}
|
|
226
|
+
|
|
195
227
|
/**
|
|
196
228
|
Returns true if we're on a device that supports background tasks
|
|
197
229
|
*/
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@zykeco/expo-background-task",
|
|
3
|
-
"version": "1.0
|
|
3
|
+
"version": "1.1.0",
|
|
4
4
|
"description": "Expo Android and iOS module for Background Task APIs",
|
|
5
5
|
"main": "build/BackgroundTask.js",
|
|
6
6
|
"types": "build/BackgroundTask.d.ts",
|
|
@@ -28,6 +28,7 @@
|
|
|
28
28
|
"bugs": {
|
|
29
29
|
"url": "https://github.com/expo/expo/issues"
|
|
30
30
|
},
|
|
31
|
+
"author": "Zyke",
|
|
31
32
|
"license": "MIT",
|
|
32
33
|
"homepage": "https://docs.expo.dev/versions/latest/sdk/background-task/",
|
|
33
34
|
"dependencies": {
|
package/src/BackgroundTask.ts
CHANGED
|
@@ -2,7 +2,7 @@ import { isRunningInExpoGo } from 'expo';
|
|
|
2
2
|
import { Platform, UnavailabilityError } from 'expo-modules-core';
|
|
3
3
|
import * as TaskManager from 'expo-task-manager';
|
|
4
4
|
|
|
5
|
-
import { BackgroundTaskOptions, BackgroundTaskStatus } from './BackgroundTask.types';
|
|
5
|
+
import { BackgroundTaskOptions, BackgroundTaskStatus, SchedulerDiagnostics } from './BackgroundTask.types';
|
|
6
6
|
import ExpoBackgroundTaskModule from './ExpoBackgroundTaskModule';
|
|
7
7
|
|
|
8
8
|
// Flag to warn about running on Apple simulator
|
|
@@ -138,6 +138,20 @@ export async function triggerTaskWorkerForTestingAsync(): Promise<boolean> {
|
|
|
138
138
|
}
|
|
139
139
|
}
|
|
140
140
|
|
|
141
|
+
// @needsAudit
|
|
142
|
+
/**
|
|
143
|
+
* Returns diagnostic information about the background task scheduler.
|
|
144
|
+
* Useful for debugging whether tasks are registered and scheduled correctly.
|
|
145
|
+
*
|
|
146
|
+
* @returns A promise that resolves with scheduler diagnostics. Fields vary by platform.
|
|
147
|
+
*/
|
|
148
|
+
export async function getSchedulerDiagnosticsAsync(): Promise<SchedulerDiagnostics> {
|
|
149
|
+
if (!ExpoBackgroundTaskModule.getSchedulerDiagnosticsAsync) {
|
|
150
|
+
throw new UnavailabilityError('BackgroundTask', 'getSchedulerDiagnosticsAsync');
|
|
151
|
+
}
|
|
152
|
+
return await ExpoBackgroundTaskModule.getSchedulerDiagnosticsAsync();
|
|
153
|
+
}
|
|
154
|
+
|
|
141
155
|
// @needsAudit
|
|
142
156
|
/**
|
|
143
157
|
* Adds a listener that is called when the background executor expires. On iOS, tasks can run
|
|
@@ -159,4 +173,5 @@ export {
|
|
|
159
173
|
BackgroundTaskStatus,
|
|
160
174
|
BackgroundTaskResult,
|
|
161
175
|
BackgroundTaskOptions,
|
|
176
|
+
SchedulerDiagnostics,
|
|
162
177
|
} from './BackgroundTask.types';
|
|
@@ -73,3 +73,68 @@ export type BackgroundTaskOptions = {
|
|
|
73
73
|
*/
|
|
74
74
|
requiresExternalPower?: boolean;
|
|
75
75
|
};
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* Diagnostic information about the background task scheduler state.
|
|
79
|
+
* Fields vary by platform — iOS-only fields are `undefined` on Android and vice versa.
|
|
80
|
+
*/
|
|
81
|
+
export type SchedulerDiagnostics = {
|
|
82
|
+
/** Native worker/task identifier */
|
|
83
|
+
identifier: string;
|
|
84
|
+
/** Number of currently registered task consumers */
|
|
85
|
+
registeredConsumers: number;
|
|
86
|
+
/** Whether a pending background task request exists */
|
|
87
|
+
hasPendingRequest: boolean;
|
|
88
|
+
/** Whether the task requires network connectivity */
|
|
89
|
+
requiresNetworkConnectivity: boolean;
|
|
90
|
+
/**
|
|
91
|
+
* Whether the device supports background tasks.
|
|
92
|
+
* @platform ios
|
|
93
|
+
*/
|
|
94
|
+
supportsBackgroundTasks?: boolean;
|
|
95
|
+
/**
|
|
96
|
+
* The BGTask type used ('refresh' or 'processing').
|
|
97
|
+
* @platform ios
|
|
98
|
+
*/
|
|
99
|
+
taskType?: string;
|
|
100
|
+
/**
|
|
101
|
+
* Configured interval in seconds.
|
|
102
|
+
* @platform ios
|
|
103
|
+
*/
|
|
104
|
+
intervalSeconds?: number;
|
|
105
|
+
/**
|
|
106
|
+
* Whether the task requires external power.
|
|
107
|
+
* @platform ios
|
|
108
|
+
*/
|
|
109
|
+
requiresExternalPower?: boolean;
|
|
110
|
+
/**
|
|
111
|
+
* ISO-8601 date string of the earliest time the next task can begin.
|
|
112
|
+
* @platform ios
|
|
113
|
+
*/
|
|
114
|
+
earliestBeginDate?: string;
|
|
115
|
+
/**
|
|
116
|
+
* Seconds until the next scheduled run (can be negative if overdue).
|
|
117
|
+
* @platform ios
|
|
118
|
+
*/
|
|
119
|
+
secondsUntilNextRun?: number;
|
|
120
|
+
/**
|
|
121
|
+
* The type of the pending request ('refresh' or 'processing').
|
|
122
|
+
* @platform ios
|
|
123
|
+
*/
|
|
124
|
+
pendingRequestType?: string;
|
|
125
|
+
/**
|
|
126
|
+
* Configured interval in minutes.
|
|
127
|
+
* @platform android
|
|
128
|
+
*/
|
|
129
|
+
intervalMinutes?: number;
|
|
130
|
+
/**
|
|
131
|
+
* Whether the app is currently in the foreground.
|
|
132
|
+
* @platform android
|
|
133
|
+
*/
|
|
134
|
+
inForeground?: boolean;
|
|
135
|
+
/**
|
|
136
|
+
* WorkManager worker state (e.g. 'ENQUEUED', 'RUNNING').
|
|
137
|
+
* @platform android
|
|
138
|
+
*/
|
|
139
|
+
workerState?: string;
|
|
140
|
+
};
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { requireNativeModule, type NativeModule } from 'expo';
|
|
2
2
|
|
|
3
|
-
import { BackgroundTaskOptions, BackgroundTaskStatus } from './BackgroundTask.types';
|
|
3
|
+
import { BackgroundTaskOptions, BackgroundTaskStatus, SchedulerDiagnostics } from './BackgroundTask.types';
|
|
4
4
|
|
|
5
5
|
type ExpoBackgroundTaskEvents = {
|
|
6
6
|
onTasksExpired(): void;
|
|
@@ -11,6 +11,7 @@ declare class ExpoBackgroundTaskModule extends NativeModule<ExpoBackgroundTaskEv
|
|
|
11
11
|
registerTaskAsync(name: string, options: BackgroundTaskOptions): Promise<void>;
|
|
12
12
|
unregisterTaskAsync(name: string): Promise<void>;
|
|
13
13
|
triggerTaskWorkerForTestingAsync(): Promise<boolean>;
|
|
14
|
+
getSchedulerDiagnosticsAsync(): Promise<SchedulerDiagnostics>;
|
|
14
15
|
}
|
|
15
16
|
|
|
16
17
|
export default requireNativeModule<ExpoBackgroundTaskModule>('ExpoBackgroundTask');
|