From 70474b91543210fbe3d60cae34a4294908bed7cd Mon Sep 17 00:00:00 2001 From: Stephen Zhou <38493346+hyoban@users.noreply.github.com> Date: Wed, 7 May 2025 23:02:00 +0800 Subject: [PATCH] refactor: migrate to expo-background-task (#3679) --- apps/mobile/app.config.ts | 1 + apps/mobile/package.json | 2 +- apps/mobile/src/initialize/background.ts | 16 +++++++--------- apps/mobile/src/initialize/index.ts | 4 ++-- pnpm-lock.yaml | 12 ++++++------ 5 files changed, 17 insertions(+), 18 deletions(-) diff --git a/apps/mobile/app.config.ts b/apps/mobile/app.config.ts index dc9e1c31f..a1b3d81bf 100644 --- a/apps/mobile/app.config.ts +++ b/apps/mobile/app.config.ts @@ -144,6 +144,7 @@ export default ({ config }: ConfigContext): ExpoConfig => { enableBackgroundRemoteNotifications: true, }, ], + "expo-background-task", ], } diff --git a/apps/mobile/package.json b/apps/mobile/package.json index cdf4da20a..a13e7e922 100644 --- a/apps/mobile/package.json +++ b/apps/mobile/package.json @@ -54,7 +54,7 @@ "expo": "53.0.4", "expo-apple-authentication": "7.2.4", "expo-application": "6.1.4", - "expo-background-fetch": "13.1.5", + "expo-background-task": "0.2.6", "expo-blur": "14.1.4", "expo-build-properties": "0.14.6", "expo-clipboard": "7.1.4", diff --git a/apps/mobile/src/initialize/background.ts b/apps/mobile/src/initialize/background.ts index fc3ffe6b7..8683a56ec 100644 --- a/apps/mobile/src/initialize/background.ts +++ b/apps/mobile/src/initialize/background.ts @@ -1,4 +1,4 @@ -import * as BackgroundFetch from "expo-background-fetch" +import * as BackgroundTask from "expo-background-task" import * as TaskManager from "expo-task-manager" import { getUISettings } from "../atoms/settings/ui" @@ -7,28 +7,26 @@ import { whoami } from "../store/user/getters" const BACKGROUND_FETCH_TASK = "background-fetch" -export async function initBackgroundFetch() { +export async function initBackgroundTask() { TaskManager.defineTask(BACKGROUND_FETCH_TASK, async () => { // const now = Date.now() // console.log(`Got background fetch call at date: ${new Date(now).toISOString()}`) const user = whoami() const { showUnreadCountBadgeMobile } = getUISettings() if (!user || !showUnreadCountBadgeMobile) { - return BackgroundFetch.BackgroundFetchResult.NoData + return BackgroundTask.BackgroundTaskResult.Success } try { - const res = await unreadSyncService.updateBadgeAtBackground() - return res - ? BackgroundFetch.BackgroundFetchResult.NewData - : BackgroundFetch.BackgroundFetchResult.NoData + await unreadSyncService.updateBadgeAtBackground() + return BackgroundTask.BackgroundTaskResult.Success } catch (err) { console.error(err) - return BackgroundFetch.BackgroundFetchResult.Failed + return BackgroundTask.BackgroundTaskResult.Failed } }) - return BackgroundFetch.registerTaskAsync(BACKGROUND_FETCH_TASK, { + return BackgroundTask.registerTaskAsync(BACKGROUND_FETCH_TASK, { minimumInterval: 60 * 15, // 15 minutes }) } diff --git a/apps/mobile/src/initialize/index.ts b/apps/mobile/src/initialize/index.ts index abec960b3..1d0b20165 100644 --- a/apps/mobile/src/initialize/index.ts +++ b/apps/mobile/src/initialize/index.ts @@ -5,7 +5,7 @@ import { initializeDb } from "../database" import { settingSyncQueue } from "../modules/settings/sync-queue" import { initAnalytics } from "./analytics" import { initializeAppCheck } from "./app-check" -import { initBackgroundFetch } from "./background" +import { initBackgroundTask } from "./background" import { initCrashlytics } from "./crashlytics" import { initializeDayjs } from "./dayjs" import { initDeviceType } from "./device" @@ -50,7 +50,7 @@ export const initializeApp = async () => { using_indexed_db: true, }) initCrashlytics() - initBackgroundFetch() + initBackgroundTask() console.log(`Initialize done,`, `${loadingTime}ms`) } diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 20220854f..2fbe67765 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -849,9 +849,9 @@ importers: expo-application: specifier: 6.1.4 version: 6.1.4(expo@53.0.4(@babel/core@7.26.10)(@expo/metro-runtime@5.0.4(react-native@0.79.1(@babel/core@7.26.10)(@types/react@19.1.3)(bufferutil@4.0.9)(react@19.0.0)(utf-8-validate@6.0.5)))(bufferutil@4.0.9)(graphql@16.8.1)(react-native-webview@13.13.5(react-native@0.79.1(@babel/core@7.26.10)(@types/react@19.1.3)(bufferutil@4.0.9)(react@19.0.0)(utf-8-validate@6.0.5))(react@19.0.0))(react-native@0.79.1(@babel/core@7.26.10)(@types/react@19.1.3)(bufferutil@4.0.9)(react@19.0.0)(utf-8-validate@6.0.5))(react@19.0.0)(utf-8-validate@6.0.5)) - expo-background-fetch: - specifier: 13.1.5 - version: 13.1.5(expo@53.0.4(@babel/core@7.26.10)(@expo/metro-runtime@5.0.4(react-native@0.79.1(@babel/core@7.26.10)(@types/react@19.1.3)(bufferutil@4.0.9)(react@19.0.0)(utf-8-validate@6.0.5)))(bufferutil@4.0.9)(graphql@16.8.1)(react-native-webview@13.13.5(react-native@0.79.1(@babel/core@7.26.10)(@types/react@19.1.3)(bufferutil@4.0.9)(react@19.0.0)(utf-8-validate@6.0.5))(react@19.0.0))(react-native@0.79.1(@babel/core@7.26.10)(@types/react@19.1.3)(bufferutil@4.0.9)(react@19.0.0)(utf-8-validate@6.0.5))(react@19.0.0)(utf-8-validate@6.0.5))(react-native@0.79.1(@babel/core@7.26.10)(@types/react@19.1.3)(bufferutil@4.0.9)(react@19.0.0)(utf-8-validate@6.0.5)) + expo-background-task: + specifier: 0.2.6 + version: 0.2.6(expo@53.0.4(@babel/core@7.26.10)(@expo/metro-runtime@5.0.4(react-native@0.79.1(@babel/core@7.26.10)(@types/react@19.1.3)(bufferutil@4.0.9)(react@19.0.0)(utf-8-validate@6.0.5)))(bufferutil@4.0.9)(graphql@16.8.1)(react-native-webview@13.13.5(react-native@0.79.1(@babel/core@7.26.10)(@types/react@19.1.3)(bufferutil@4.0.9)(react@19.0.0)(utf-8-validate@6.0.5))(react@19.0.0))(react-native@0.79.1(@babel/core@7.26.10)(@types/react@19.1.3)(bufferutil@4.0.9)(react@19.0.0)(utf-8-validate@6.0.5))(react@19.0.0)(utf-8-validate@6.0.5))(react-native@0.79.1(@babel/core@7.26.10)(@types/react@19.1.3)(bufferutil@4.0.9)(react@19.0.0)(utf-8-validate@6.0.5)) expo-blur: specifier: 14.1.4 version: 14.1.4(expo@53.0.4(@babel/core@7.26.10)(@expo/metro-runtime@5.0.4(react-native@0.79.1(@babel/core@7.26.10)(@types/react@19.1.3)(bufferutil@4.0.9)(react@19.0.0)(utf-8-validate@6.0.5)))(bufferutil@4.0.9)(graphql@16.8.1)(react-native-webview@13.13.5(react-native@0.79.1(@babel/core@7.26.10)(@types/react@19.1.3)(bufferutil@4.0.9)(react@19.0.0)(utf-8-validate@6.0.5))(react@19.0.0))(react-native@0.79.1(@babel/core@7.26.10)(@types/react@19.1.3)(bufferutil@4.0.9)(react@19.0.0)(utf-8-validate@6.0.5))(react@19.0.0)(utf-8-validate@6.0.5))(react-native@0.79.1(@babel/core@7.26.10)(@types/react@19.1.3)(bufferutil@4.0.9)(react@19.0.0)(utf-8-validate@6.0.5))(react@19.0.0) @@ -9209,8 +9209,8 @@ packages: react: 19.0.0 react-native: '*' - expo-background-fetch@13.1.5: - resolution: {integrity: sha512-tQiQEH68o6iduDDOKdlTx7oa1cWf8d+RsT4vm2dFzoSEIr1gJtdNojN4U2qBxwTGYttJI1ug5/PYKpQUIS5VmA==} + expo-background-task@0.2.6: + resolution: {integrity: sha512-ZP7qurzAaOisfZRGIDnrAncVYynPQcLZqmWoaCeuBQNtHTUc5prnp7iRWRZo/OccPEiXuZNyZSJ/VMtw83Cfzw==} peerDependencies: expo: '*' @@ -24979,7 +24979,7 @@ snapshots: transitivePeerDependencies: - supports-color - expo-background-fetch@13.1.5(expo@53.0.4(@babel/core@7.26.10)(@expo/metro-runtime@5.0.4(react-native@0.79.1(@babel/core@7.26.10)(@types/react@19.1.3)(bufferutil@4.0.9)(react@19.0.0)(utf-8-validate@6.0.5)))(bufferutil@4.0.9)(graphql@16.8.1)(react-native-webview@13.13.5(react-native@0.79.1(@babel/core@7.26.10)(@types/react@19.1.3)(bufferutil@4.0.9)(react@19.0.0)(utf-8-validate@6.0.5))(react@19.0.0))(react-native@0.79.1(@babel/core@7.26.10)(@types/react@19.1.3)(bufferutil@4.0.9)(react@19.0.0)(utf-8-validate@6.0.5))(react@19.0.0)(utf-8-validate@6.0.5))(react-native@0.79.1(@babel/core@7.26.10)(@types/react@19.1.3)(bufferutil@4.0.9)(react@19.0.0)(utf-8-validate@6.0.5)): + expo-background-task@0.2.6(expo@53.0.4(@babel/core@7.26.10)(@expo/metro-runtime@5.0.4(react-native@0.79.1(@babel/core@7.26.10)(@types/react@19.1.3)(bufferutil@4.0.9)(react@19.0.0)(utf-8-validate@6.0.5)))(bufferutil@4.0.9)(graphql@16.8.1)(react-native-webview@13.13.5(react-native@0.79.1(@babel/core@7.26.10)(@types/react@19.1.3)(bufferutil@4.0.9)(react@19.0.0)(utf-8-validate@6.0.5))(react@19.0.0))(react-native@0.79.1(@babel/core@7.26.10)(@types/react@19.1.3)(bufferutil@4.0.9)(react@19.0.0)(utf-8-validate@6.0.5))(react@19.0.0)(utf-8-validate@6.0.5))(react-native@0.79.1(@babel/core@7.26.10)(@types/react@19.1.3)(bufferutil@4.0.9)(react@19.0.0)(utf-8-validate@6.0.5)): dependencies: expo: 53.0.4(@babel/core@7.26.10)(@expo/metro-runtime@5.0.4(react-native@0.79.1(@babel/core@7.26.10)(@types/react@19.1.3)(bufferutil@4.0.9)(react@19.0.0)(utf-8-validate@6.0.5)))(bufferutil@4.0.9)(graphql@16.8.1)(react-native-webview@13.13.5(react-native@0.79.1(@babel/core@7.26.10)(@types/react@19.1.3)(bufferutil@4.0.9)(react@19.0.0)(utf-8-validate@6.0.5))(react@19.0.0))(react-native@0.79.1(@babel/core@7.26.10)(@types/react@19.1.3)(bufferutil@4.0.9)(react@19.0.0)(utf-8-validate@6.0.5))(react@19.0.0)(utf-8-validate@6.0.5) expo-task-manager: 13.1.5(expo@53.0.4(@babel/core@7.26.10)(@expo/metro-runtime@5.0.4(react-native@0.79.1(@babel/core@7.26.10)(@types/react@19.1.3)(bufferutil@4.0.9)(react@19.0.0)(utf-8-validate@6.0.5)))(bufferutil@4.0.9)(graphql@16.8.1)(react-native-webview@13.13.5(react-native@0.79.1(@babel/core@7.26.10)(@types/react@19.1.3)(bufferutil@4.0.9)(react@19.0.0)(utf-8-validate@6.0.5))(react@19.0.0))(react-native@0.79.1(@babel/core@7.26.10)(@types/react@19.1.3)(bufferutil@4.0.9)(react@19.0.0)(utf-8-validate@6.0.5))(react@19.0.0)(utf-8-validate@6.0.5))(react-native@0.79.1(@babel/core@7.26.10)(@types/react@19.1.3)(bufferutil@4.0.9)(react@19.0.0)(utf-8-validate@6.0.5))