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