Skip to content

Commit

Permalink
Merge pull request #63 from InvTrack/native/offline-first-improvements
Browse files Browse the repository at this point in the history
native/offline-first-improvements/fix: possible big gains
  • Loading branch information
michalstruck authored Nov 9, 2023
2 parents 53e586b + 52300b3 commit d2dbffe
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 17 deletions.
19 changes: 12 additions & 7 deletions native/app/_layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,27 +35,28 @@ import * as ScreenOrientation from "expo-screen-orientation";
ScreenOrientation.lockAsync(ScreenOrientation.OrientationLock.PORTRAIT_UP);
SplashScreen.preventAutoHideAsync();

const ONE_SECOND = 1000;
const queryClient = new QueryClient({
defaultOptions: {
mutations: {
cacheTime: Infinity,
retry: 0,
retry: 100,
retryDelay: (attemptIndex) =>
Math.min(ONE_SECOND * 2 ** attemptIndex, 30 * ONE_SECOND),
},
queries: {
refetchOnWindowFocus: false,
retry: 2,
cacheTime: 1000 * 10,
retry: 5,
retryDelay: (attemptIndex) =>
Math.min(ONE_SECOND * 2 ** attemptIndex, 30 * ONE_SECOND),
cacheTime: Infinity,
staleTime: Infinity,
},
},
});

const asyncPersist = createAsyncStoragePersister({
storage: AsyncStorage,
// dehydrateOptions: {
// dehydrateMutations: true,
// dehydrateQueries: false,
// },
throttleTime: 1000,
});

Expand All @@ -78,6 +79,10 @@ const ProvideProviders = ({ children }: { children: React.ReactNode }) => {
persistOptions={{
maxAge: Infinity,
persister: asyncPersist,
dehydrateOptions: {
dehydrateMutations: true,
dehydrateQueries: false,
},
}}
onSuccess={() =>
queryClient
Expand Down
14 changes: 4 additions & 10 deletions native/utils/useOnlineManager.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,12 @@
import NetInfo from "@react-native-community/netinfo";
import { onlineManager } from "@tanstack/react-query";
import React from "react";
import { Platform } from "react-native";

export function useOnlineManager() {
React.useEffect(() => {
if (Platform.OS !== "web") {
return NetInfo.addEventListener((state) => {
const status =
state.isConnected != null &&
state.isConnected &&
Boolean(state.isInternetReachable);
onlineManager.setOnline(status);
});
}
return NetInfo.addEventListener((state) => {
const status = !!state.isConnected;
onlineManager.setOnline(status);
});
}, []);
}

0 comments on commit d2dbffe

Please sign in to comment.