Skip to content

Commit

Permalink
refactor: move CacheStorage into a module
Browse files Browse the repository at this point in the history
  • Loading branch information
amoshydra committed Feb 20, 2024
1 parent 22e8e66 commit c7c9eb1
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 48 deletions.
49 changes: 1 addition & 48 deletions src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { EspruinoComms } from './services/Espruino/Comms';
import { useEspruinoDeviceInfoStore } from './services/Espruino/stores/EspruinoDevice';
import { useEffect } from 'react';
import { useGadgetBridgeConnector } from './services/GadgetbridgeConnector';
import { localStorageProvider } from './services/CacheStorage';

const router = createHashRouter(
createRoutesFromElements(
Expand Down Expand Up @@ -65,51 +66,3 @@ function Entry() {

export default Entry

interface CacheStorage {
caches: [string, any][];
created: number;
}
const CACHE_KEY = "app-cache"
const ONE_DAY = (
1 // ms
* 1000 // ss
* 60 // mm
* 60 // hh
* 24
)
const DEFAULT_CACHE = JSON.stringify({
caches: [] as [string, any][],
created: 0,
});
function localStorageProvider() {
const working = {
map: new Map<string, any>(),
created: Date.now(),
};

try {
// When initializing, we restore the data from `localStorage` into a map.
const { caches, created } = JSON.parse(localStorage.getItem(CACHE_KEY) || DEFAULT_CACHE) as CacheStorage;
if (Date.now() - created < ONE_DAY) {
// restore if cache was created in less than a day
working.map = new Map(caches);
working.created = created;
} else {
// using empty cache map
}
} catch (e) {
// using empty cache map
}

// Before unloading the app, we write back all the data into `localStorage`.
window.addEventListener('beforeunload', () => {
const cacheStorage: CacheStorage = {
caches: [...working.map.entries()],
created: working.created,
};
localStorage.setItem(CACHE_KEY, JSON.stringify(cacheStorage))
})

// We still use the map for write & read for performance.
return working.map;
}
51 changes: 51 additions & 0 deletions src/services/CacheStorage/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// eslint-disable-next-line @typescript-eslint/no-explicit-any
type CacheValue = any;

interface CacheStorage {
caches: [string, CacheValue][];
created: number;
}
const CACHE_KEY = "app-cache"
const ONE_DAY = (
1 // ms
* 1000 // ss
* 60 // mm
* 60 // hh
* 24
)
const DEFAULT_CACHE = JSON.stringify({
caches: [] as [string, CacheValue][],
created: 0,
});
export function localStorageProvider() {
const working = {
map: new Map<string, CacheValue>(),
created: Date.now(),
};

try {
// When initializing, we restore the data from `localStorage` into a map.
const { caches, created } = JSON.parse(localStorage.getItem(CACHE_KEY) || DEFAULT_CACHE) as CacheStorage;
if (Date.now() - created < ONE_DAY) {
// restore if cache was created in less than a day
working.map = new Map(caches);
working.created = created;
} else {
// using empty cache map
}
} catch (e) {
// using empty cache map
}

// Before unloading the app, we write back all the data into `localStorage`.
window.addEventListener('beforeunload', () => {
const cacheStorage: CacheStorage = {
caches: [...working.map.entries()],
created: working.created,
};
localStorage.setItem(CACHE_KEY, JSON.stringify(cacheStorage))
})

// We still use the map for write & read for performance.
return working.map;
}

0 comments on commit c7c9eb1

Please sign in to comment.