UNPKG

3.38 kBJavaScriptView Raw
1import { Platform, UnavailabilityError } from 'expo-modules-core';
2import * as TaskManager from 'expo-task-manager';
3import { BackgroundFetchResult, BackgroundFetchStatus, } from './BackgroundFetch.types';
4import ExpoBackgroundFetch from './ExpoBackgroundFetch';
5// @needsAudit
6/**
7 * Gets a status of background fetch.
8 * @return Returns a promise which fulfils with one of `BackgroundFetchStatus` enum values.
9 */
10export async function getStatusAsync() {
11 if (Platform.OS === 'android') {
12 return BackgroundFetchStatus.Available;
13 }
14 return ExpoBackgroundFetch.getStatusAsync();
15}
16// @needsAudit
17/**
18 * Sets the minimum number of seconds that must elapse before another background fetch can be
19 * initiated. This value is advisory only and does not indicate the exact amount of time expected
20 * between fetch operations.
21 *
22 * > This method doesn't take any effect on Android. It is a global value which means that it can
23 * overwrite settings from another application opened through Expo Go.
24 *
25 * @param minimumInterval Number of seconds that must elapse before another background fetch can be called.
26 * @return A promise which fulfils once the minimum interval is set.
27 */
28export async function setMinimumIntervalAsync(minimumInterval) {
29 if (!ExpoBackgroundFetch.setMinimumIntervalAsync) {
30 return;
31 }
32 // iOS only
33 await ExpoBackgroundFetch.setMinimumIntervalAsync(minimumInterval);
34}
35// @needsAudit
36/**
37 * Registers background fetch task with given name. Registered tasks are saved in persistent storage and restored once the app is initialized.
38 * @param taskName Name of the task to register. The task needs to be defined first - see [`TaskManager.defineTask`](taskmanager#defineTask)
39 * for more details.
40 * @param options An object containing the background fetch options.
41 *
42 * @example
43 * ```ts
44 * import * as BackgroundFetch from 'expo-background-fetch';
45 * import * as TaskManager from 'expo-task-manager';
46 *
47 * TaskManager.defineTask(YOUR_TASK_NAME, () => {
48 * try {
49 * const receivedNewData = // do your background fetch here
50 * return receivedNewData ? BackgroundFetch.BackgroundFetchResult.NewData : BackgroundFetch.BackgroundFetchResult.NoData;
51 * } catch (error) {
52 * return BackgroundFetch.BackgroundFetchResult.Failed;
53 * }
54 * });
55 * ```
56 */
57export async function registerTaskAsync(taskName, options = {}) {
58 if (!ExpoBackgroundFetch.registerTaskAsync) {
59 throw new UnavailabilityError('BackgroundFetch', 'registerTaskAsync');
60 }
61 if (!TaskManager.isTaskDefined(taskName)) {
62 throw new Error(`Task '${taskName}' is not defined. You must define a task using TaskManager.defineTask before registering.`);
63 }
64 await ExpoBackgroundFetch.registerTaskAsync(taskName, options);
65}
66// @needsAudit
67/**
68 * Unregisters background fetch task, so the application will no longer be executing this task.
69 * @param taskName Name of the task to unregister.
70 * @return A promise which fulfils when the task is fully unregistered.
71 */
72export async function unregisterTaskAsync(taskName) {
73 if (!ExpoBackgroundFetch.unregisterTaskAsync) {
74 throw new UnavailabilityError('BackgroundFetch', 'unregisterTaskAsync');
75 }
76 await ExpoBackgroundFetch.unregisterTaskAsync(taskName);
77}
78export { BackgroundFetchResult, BackgroundFetchStatus };
79//# sourceMappingURL=BackgroundFetch.js.map
\No newline at end of file