-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
147 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
import * as FileSystem from "expo-file-system"; | ||
import { useEffect, useMemo, useRef, useState, ReactNode } from "react"; | ||
import { | ||
AppState, | ||
AppStateStatus, | ||
InteractionManager, | ||
Platform, | ||
} from "react-native"; | ||
import "react-native-gesture-handler"; | ||
import { SWRConfig } from "swr"; | ||
|
||
async function fileSystemProvider(cacheData: Map<unknown, unknown> | null) { | ||
InteractionManager.runAfterInteractions(async () => { | ||
const cacheDir = FileSystem.cacheDirectory + "hcb-mobile-cache/"; | ||
const file = `${cacheDir}cache.json`; | ||
|
||
async function ensureDirExists() { | ||
const dirInfo = await FileSystem.getInfoAsync(cacheDir); | ||
if (!dirInfo.exists) { | ||
console.log("Cache directory doesn't exist, creating…"); | ||
await FileSystem.makeDirectoryAsync(cacheDir, { intermediates: true }); | ||
} | ||
} | ||
|
||
const map = cacheData || new Map(); | ||
|
||
if (map.size === 0) { | ||
console.log("⛔ Cache is empty, not saving to disk."); | ||
return; | ||
} | ||
// console.log("💾 Saving cache to disk…"); | ||
await ensureDirExists(); | ||
await FileSystem.writeAsStringAsync( | ||
file, | ||
JSON.stringify(Array.from(map.entries())), | ||
); | ||
// console.log(`💾 Saved ${map.size} API routes to cache! `); | ||
}); | ||
} | ||
|
||
interface SWRWrapperProps { | ||
fetcher: (url: string, options: RequestInit) => Promise<unknown>; | ||
children: ReactNode; | ||
} | ||
|
||
export function SWRWrapper({ fetcher, children }: SWRWrapperProps) { | ||
const cacheData = useRef(new Map()); | ||
const lastSaveTime = useRef(Date.now()); | ||
const [cacheLoaded, setCacheLoaded] = useState(Platform.OS === "web"); | ||
|
||
// Effect to handle periodic cache saving | ||
useEffect(() => { | ||
const interval = setInterval(() => { | ||
if (cacheData.current && Date.now() - lastSaveTime.current >= 10000) { | ||
fileSystemProvider(cacheData.current); | ||
lastSaveTime.current = Date.now(); | ||
} | ||
}, 10000); | ||
|
||
return () => clearInterval(interval); | ||
}, []); | ||
|
||
// Effect to handle app state changes | ||
useEffect(() => { | ||
const save = () => { | ||
if (cacheData.current) { | ||
fileSystemProvider(cacheData.current); | ||
lastSaveTime.current = Date.now(); | ||
} | ||
}; | ||
|
||
const subscription = AppState.addEventListener("change", save); | ||
|
||
return () => { | ||
subscription.remove(); | ||
}; | ||
}, []); | ||
|
||
// Effect to load initial cache | ||
useEffect(() => { | ||
(async () => { | ||
if (cacheLoaded) return; | ||
const cacheDir = FileSystem.cacheDirectory + "hcb-mobile-cache/"; | ||
const file = `${cacheDir}cache.json`; | ||
const fileInfo = await FileSystem.getInfoAsync(file); | ||
if (fileInfo.exists) { | ||
console.log("📂 Cache file exists, restoring cache…"); | ||
const data = await FileSystem.readAsStringAsync(file); | ||
const entries = JSON.parse(data); | ||
cacheData.current = new Map(entries); | ||
console.log(`📂 Restored ${cacheData.current.size} API routes!`); | ||
} else { | ||
console.log("📂 Cache file doesn't exist, creating new cache…"); | ||
cacheData.current = new Map(); | ||
} | ||
if (!cacheLoaded) setCacheLoaded(true); | ||
})(); | ||
}, [cacheData, cacheLoaded]); | ||
|
||
const contextValue = useMemo( | ||
() => ({ | ||
provider: () => cacheData.current, | ||
isVisible: () => true, | ||
fetcher, | ||
initFocus(callback: () => void) { | ||
let appState: AppStateStatus = AppState.currentState as AppStateStatus; | ||
|
||
const onAppStateChange = (nextAppState: string) => { | ||
if ( | ||
appState.match(/inactive|background/) && | ||
nextAppState === "active" | ||
) { | ||
callback(); | ||
} | ||
appState = nextAppState as AppStateStatus; | ||
}; | ||
|
||
const subscription = AppState.addEventListener( | ||
"change", | ||
onAppStateChange, | ||
); | ||
|
||
return () => { | ||
subscription.remove(); | ||
}; | ||
}, | ||
}), | ||
[fetcher], | ||
); | ||
|
||
return cacheLoaded ? ( | ||
<SWRConfig value={contextValue}>{children}</SWRConfig> | ||
) : null; | ||
} |