expo-background-task 0.0.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/.eslintrc.js +2 -0
- package/CHANGELOG.md +17 -0
- package/README.md +43 -0
- package/android/build.gradle +24 -0
- package/android/src/main/AndroidManifest.xml +3 -0
- package/android/src/main/java/expo/modules/backgroundtask/BackgroundTaskConsumer.kt +54 -0
- package/android/src/main/java/expo/modules/backgroundtask/BackgroundTaskExceptions.kt +13 -0
- package/android/src/main/java/expo/modules/backgroundtask/BackgroundTaskModule.kt +66 -0
- package/android/src/main/java/expo/modules/backgroundtask/BackgroundTaskScheduler.kt +212 -0
- package/android/src/main/java/expo/modules/backgroundtask/BackgroundTaskWork.kt +36 -0
- package/app.plugin.js +1 -0
- package/build/BackgroundTask.d.ts +51 -0
- package/build/BackgroundTask.d.ts.map +1 -0
- package/build/BackgroundTask.js +89 -0
- package/build/BackgroundTask.js.map +1 -0
- package/build/BackgroundTask.types.d.ts +41 -0
- package/build/BackgroundTask.types.d.ts.map +1 -0
- package/build/BackgroundTask.types.js +31 -0
- package/build/BackgroundTask.types.js.map +1 -0
- package/build/ExpoBackgroundTaskModule.d.ts +11 -0
- package/build/ExpoBackgroundTaskModule.d.ts.map +1 -0
- package/build/ExpoBackgroundTaskModule.js +3 -0
- package/build/ExpoBackgroundTaskModule.js.map +1 -0
- package/build/ExpoBackgroundTaskModule.web.d.ts +6 -0
- package/build/ExpoBackgroundTaskModule.web.d.ts.map +1 -0
- package/build/ExpoBackgroundTaskModule.web.js +7 -0
- package/build/ExpoBackgroundTaskModule.web.js.map +1 -0
- package/expo-module.config.json +11 -0
- package/ios/BackgorundTaskExceptions.swift +48 -0
- package/ios/BackgroundTaskAppDelegate.swift +44 -0
- package/ios/BackgroundTaskConstants.swift +13 -0
- package/ios/BackgroundTaskConsumer.swift +57 -0
- package/ios/BackgroundTaskDebugHelper.swift +21 -0
- package/ios/BackgroundTaskModule.swift +78 -0
- package/ios/BackgroundTaskRecords.swift +12 -0
- package/ios/BackgroundTaskScheduler.swift +101 -0
- package/ios/ExpoBackgroundTask.podspec +32 -0
- package/package.json +43 -0
- package/plugin/build/withBackgroundTask.d.ts +3 -0
- package/plugin/build/withBackgroundTask.js +29 -0
- package/plugin/src/withBackgroundTask.ts +36 -0
- package/plugin/tsconfig.json +9 -0
- package/src/BackgroundTask.ts +105 -0
- package/src/BackgroundTask.types.ts +45 -0
- package/src/ExpoBackgroundTaskModule.ts +12 -0
- package/src/ExpoBackgroundTaskModule.web.ts +7 -0
- package/tsconfig.json +9 -0
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { ConfigPlugin, createRunOncePlugin, withInfoPlist } from 'expo/config-plugins';
|
|
2
|
+
|
|
3
|
+
const pkg = require('expo-background-task/package.json');
|
|
4
|
+
|
|
5
|
+
const withBackgroundTask: ConfigPlugin = (config) => {
|
|
6
|
+
return withInfoPlist(config, (config) => {
|
|
7
|
+
// Enable background mode processing
|
|
8
|
+
if (!Array.isArray(config.modResults.UIBackgroundModes)) {
|
|
9
|
+
config.modResults.UIBackgroundModes = [];
|
|
10
|
+
}
|
|
11
|
+
if (!config.modResults.UIBackgroundModes.includes('processing')) {
|
|
12
|
+
config.modResults.UIBackgroundModes.push('processing');
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
// With the new background task module we need to install the identifier in the Info.plist:
|
|
16
|
+
// BGTaskSchedulerPermittedIdentifiers should be an array of strings - we need to
|
|
17
|
+
// define our own identifier: com.expo.modules.backgroundtask.taskidentifer
|
|
18
|
+
if (!Array.isArray(config.modResults.BGTaskSchedulerPermittedIdentifiers)) {
|
|
19
|
+
config.modResults.BGTaskSchedulerPermittedIdentifiers = [];
|
|
20
|
+
}
|
|
21
|
+
if (
|
|
22
|
+
!(config.modResults.BGTaskSchedulerPermittedIdentifiers as string[]).includes(
|
|
23
|
+
'com.expo.modules.backgroundtask.processing'
|
|
24
|
+
)
|
|
25
|
+
) {
|
|
26
|
+
config.modResults.BGTaskSchedulerPermittedIdentifiers = [
|
|
27
|
+
...((config.modResults.BGTaskSchedulerPermittedIdentifiers as string[]) || []),
|
|
28
|
+
'com.expo.modules.backgroundtask.processing',
|
|
29
|
+
];
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
return config;
|
|
33
|
+
});
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
export default createRunOncePlugin(withBackgroundTask, pkg.name, pkg.version);
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
import { UnavailabilityError } from 'expo-modules-core';
|
|
2
|
+
import * as TaskManager from 'expo-task-manager';
|
|
3
|
+
|
|
4
|
+
import { BackgroundTaskOptions, BackgroundTaskStatus } from './BackgroundTask.types';
|
|
5
|
+
import ExpoBackgroundTaskModule from './ExpoBackgroundTaskModule';
|
|
6
|
+
|
|
7
|
+
// @needsAudit
|
|
8
|
+
/**
|
|
9
|
+
* Returns the status for the Background Task API. On web, it always returns `BackgroundTaskStatus.Restricted`,
|
|
10
|
+
* while on native platforms it returns `BackgroundTaskStatus.Available`.
|
|
11
|
+
*
|
|
12
|
+
* @returns A BackgroundTaskStatus enum value or `null` if not available.
|
|
13
|
+
*/
|
|
14
|
+
export const getStatusAsync = async (): Promise<BackgroundTaskStatus> => {
|
|
15
|
+
if (!ExpoBackgroundTaskModule.getStatusAsync) {
|
|
16
|
+
throw new UnavailabilityError('BackgroundTask', 'getStatusAsync');
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
return ExpoBackgroundTaskModule.getStatusAsync();
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
// @needsAudit
|
|
23
|
+
/**
|
|
24
|
+
* Registers a background task with the given name. Registered tasks are saved in persistent storage and restored once the app is initialized.
|
|
25
|
+
* @param taskName Name of the task to register. The task needs to be defined first - see [`TaskManager.defineTask`](task-manager/#taskmanagerdefinetasktaskname-taskexecutor)
|
|
26
|
+
* for more details.
|
|
27
|
+
* @param options An object containing the background task options.
|
|
28
|
+
*
|
|
29
|
+
* @example
|
|
30
|
+
* ```ts
|
|
31
|
+
* import * as TaskManager from 'expo-task-manager';
|
|
32
|
+
*
|
|
33
|
+
* // Register the task outside of the component
|
|
34
|
+
* TaskManager.defineTask(BACKGROUND_TASK_IDENTIFIER, () => {
|
|
35
|
+
* try {
|
|
36
|
+
* await AsyncStorage.setItem(LAST_TASK_DATE_KEY, Date.now().toString());
|
|
37
|
+
* } catch (error) {
|
|
38
|
+
* console.error('Failed to save the last fetch date', error);
|
|
39
|
+
* return BackgroundTaskResult.Failed;
|
|
40
|
+
* }
|
|
41
|
+
* return BackgroundTaskResult.Success;
|
|
42
|
+
* });
|
|
43
|
+
* ```
|
|
44
|
+
*
|
|
45
|
+
* You can now use the `registerTaskAsync` function to register the task:
|
|
46
|
+
*
|
|
47
|
+
* ```ts
|
|
48
|
+
* BackgroundTask.registerTaskAsync(BACKGROUND_TASK_IDENTIFIER, {});
|
|
49
|
+
* ```
|
|
50
|
+
*/
|
|
51
|
+
export async function registerTaskAsync(
|
|
52
|
+
taskName: string,
|
|
53
|
+
options: BackgroundTaskOptions = {}
|
|
54
|
+
): Promise<void> {
|
|
55
|
+
if (!ExpoBackgroundTaskModule.registerTaskAsync) {
|
|
56
|
+
throw new UnavailabilityError('BackgroundTask', 'registerTaskAsync');
|
|
57
|
+
}
|
|
58
|
+
if (!TaskManager.isTaskDefined(taskName)) {
|
|
59
|
+
throw new Error(
|
|
60
|
+
`Task '${taskName}' is not defined. You must define a task using TaskManager.defineTask before registering.`
|
|
61
|
+
);
|
|
62
|
+
}
|
|
63
|
+
console.log('Calling ExpoBackgroundTaskModule.registerTaskAsync', { taskName, options });
|
|
64
|
+
await ExpoBackgroundTaskModule.registerTaskAsync(taskName, options);
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
// @needsAudit
|
|
68
|
+
/**
|
|
69
|
+
* Unregisters a background task, so the application will no longer be executing this task.
|
|
70
|
+
* @param taskName Name of the task to unregister.
|
|
71
|
+
* @return A promise which fulfils when the task is fully unregistered.
|
|
72
|
+
*/
|
|
73
|
+
export async function unregisterTaskAsync(taskName: string): Promise<void> {
|
|
74
|
+
if (!ExpoBackgroundTaskModule.unregisterTaskAsync) {
|
|
75
|
+
throw new UnavailabilityError('BackgroundTask', 'unregisterTaskAsync');
|
|
76
|
+
}
|
|
77
|
+
console.log('Calling ExpoBackgroundTaskModule.unregisterTaskAsync', taskName);
|
|
78
|
+
await ExpoBackgroundTaskModule.unregisterTaskAsync(taskName);
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
// @needsAudit
|
|
82
|
+
/**
|
|
83
|
+
* When in debug mode this function will trigger running the background tasks.
|
|
84
|
+
* This function will only work for apps built in debug mode.
|
|
85
|
+
* @todo(chrfalch): When we have a usable devtools plugin we can enable this function.
|
|
86
|
+
* @returns A promise which fulfils when the task is triggered.
|
|
87
|
+
*/
|
|
88
|
+
// export async function triggerTaskWorkerForTestingAsync(): Promise<boolean> {
|
|
89
|
+
// if (__DEV__) {
|
|
90
|
+
// if (!ExpoBackgroundTaskModule.triggerTaskWorkerForTestingAsync) {
|
|
91
|
+
// throw new UnavailabilityError('BackgroundTask', 'triggerTaskWorkerForTestingAsync');
|
|
92
|
+
// }
|
|
93
|
+
// console.log('Calling triggerTaskWorkerForTestingAsync');
|
|
94
|
+
// return await ExpoBackgroundTaskModule.triggerTaskWorkerForTestingAsync();
|
|
95
|
+
// } else {
|
|
96
|
+
// return Promise.resolve(false);
|
|
97
|
+
// }
|
|
98
|
+
// }
|
|
99
|
+
|
|
100
|
+
// Export types
|
|
101
|
+
export {
|
|
102
|
+
BackgroundTaskStatus,
|
|
103
|
+
BackgroundTaskResult,
|
|
104
|
+
BackgroundTaskOptions,
|
|
105
|
+
} from './BackgroundTask.types';
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
// @needsAudit
|
|
2
|
+
/**
|
|
3
|
+
* Availability status for background tasks
|
|
4
|
+
*/
|
|
5
|
+
export enum BackgroundTaskStatus {
|
|
6
|
+
/**
|
|
7
|
+
* Background tasks are unavailable.
|
|
8
|
+
*/
|
|
9
|
+
Restricted = 1,
|
|
10
|
+
/**
|
|
11
|
+
* Background tasks are available for the app.
|
|
12
|
+
*/
|
|
13
|
+
Available = 2,
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
// @needsAudit
|
|
17
|
+
/**
|
|
18
|
+
* Return value for background tasks.
|
|
19
|
+
*/
|
|
20
|
+
export enum BackgroundTaskResult {
|
|
21
|
+
/**
|
|
22
|
+
* The task finished successfully.
|
|
23
|
+
*/
|
|
24
|
+
Success = 1,
|
|
25
|
+
/**
|
|
26
|
+
* The task failed.
|
|
27
|
+
*/
|
|
28
|
+
Failed = 2,
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
// @needsAudit
|
|
32
|
+
/**
|
|
33
|
+
* Options for registering a background task
|
|
34
|
+
*/
|
|
35
|
+
export type BackgroundTaskOptions = {
|
|
36
|
+
/**
|
|
37
|
+
* Inexact interval in minutes between subsequent repeats of the background tasks. The final
|
|
38
|
+
* interval may differ from the specified one to minimize wakeups and battery usage.
|
|
39
|
+
* - Defaults to once every 12 hours (The minimum interval is 15 minutes)
|
|
40
|
+
* - On iOS, the system determines the interval for background task execution,
|
|
41
|
+
* but will wait until the specified minimum interval has elapsed before starting a task.
|
|
42
|
+
* @platform android
|
|
43
|
+
*/
|
|
44
|
+
minimumInterval?: number;
|
|
45
|
+
};
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { requireNativeModule, type NativeModule } from 'expo';
|
|
2
|
+
|
|
3
|
+
import { BackgroundTaskOptions, BackgroundTaskStatus } from './BackgroundTask.types';
|
|
4
|
+
|
|
5
|
+
declare class ExpoBackgroundTaskModule extends NativeModule {
|
|
6
|
+
getStatusAsync(): Promise<BackgroundTaskStatus>;
|
|
7
|
+
registerTaskAsync(name: string, options: BackgroundTaskOptions): Promise<void>;
|
|
8
|
+
unregisterTaskAsync(name: string): Promise<void>;
|
|
9
|
+
triggerTaskWorkerForTestingAsync(): Promise<boolean>;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export default requireNativeModule<ExpoBackgroundTaskModule>('ExpoBackgroundTask');
|
package/tsconfig.json
ADDED