Inline the factory

This commit is contained in:
Manav Rathi
2024-04-12 14:45:03 +05:30
parent 495ff99874
commit c83dc87d5d

View File

@@ -8,10 +8,6 @@ const cacheNames = [
/** Namespaces into which our caches data is divided */ /** Namespaces into which our caches data is divided */
export type CacheName = (typeof cacheNames)[number]; export type CacheName = (typeof cacheNames)[number];
interface LimitedCacheStorage {
open: (cacheName: string) => Promise<LimitedCache>;
}
export interface LimitedCache { export interface LimitedCache {
match: ( match: (
key: string, key: string,
@@ -21,34 +17,22 @@ export interface LimitedCache {
delete: (key: string) => Promise<boolean>; delete: (key: string) => Promise<boolean>;
} }
class cacheStorageFactory { const openCache = async (name: CacheName) => {
getCacheStorage(): LimitedCacheStorage { const cache = await caches.open(name);
return { return {
async open(cacheName) { match: (key) => {
const cache = await caches.open(cacheName); // options are not supported in the browser
return { return cache.match(key);
match: (key) => { },
// options are not supported in the browser put: cache.put.bind(cache),
return cache.match(key); delete: cache.delete.bind(cache),
}, };
put: cache.put.bind(cache), };
delete: cache.delete.bind(cache),
};
},
};
}
}
export const CacheStorageFactory = new cacheStorageFactory();
async function openCache(cacheName: string) {
return await CacheStorageFactory.getCacheStorage().open(cacheName);
}
export const CacheStorageService = { open: openCache }; export const CacheStorageService = { open: openCache };
export async function cached( export async function cached(
cacheName: string, cacheName: CacheName,
id: string, id: string,
get: () => Promise<Blob>, get: () => Promise<Blob>,
): Promise<Blob> { ): Promise<Blob> {