-
Notifications
You must be signed in to change notification settings - Fork 100
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
1 parent
d07dba8
commit 9404feb
Showing
10 changed files
with
306 additions
and
169 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
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,77 @@ | ||
import { CACHE_MAX_RETRIES, STATUS } from './config'; | ||
import { request, sleep } from './helpers'; | ||
import { StorageItem } from './types'; | ||
|
||
export class CacheStore { | ||
private readonly cacheStore: Map<string, StorageItem>; | ||
|
||
constructor() { | ||
this.cacheStore = new Map<string, StorageItem>(); | ||
} | ||
|
||
public async get(url: string, fetchOptions?: RequestInit) { | ||
await this.fetchAndAddToInternalCache(url, fetchOptions); | ||
|
||
return this.cacheStore.get(url)?.content ?? ''; | ||
} | ||
|
||
public set(url: string, data: StorageItem) { | ||
this.cacheStore.set(url, data); | ||
} | ||
|
||
public isCached(url: string) { | ||
return this.cacheStore.get(url)?.status === STATUS.LOADED; | ||
} | ||
|
||
private async fetchAndAddToInternalCache(url: string, fetchOptions?: RequestInit) { | ||
const cache = this.cacheStore.get(url); | ||
|
||
if (cache?.status === STATUS.LOADING) { | ||
let retryCount = 0; | ||
|
||
// eslint-disable-next-line no-await-in-loop | ||
while ( | ||
this.cacheStore.get(url)?.status === STATUS.LOADING && | ||
retryCount < CACHE_MAX_RETRIES | ||
) { | ||
// eslint-disable-next-line no-await-in-loop | ||
await sleep(0.1); | ||
retryCount += 1; | ||
} | ||
|
||
if (retryCount >= CACHE_MAX_RETRIES) { | ||
this.cacheStore.set(url, { content: '', status: STATUS.IDLE }); | ||
await this.fetchAndAddToInternalCache(url, fetchOptions); | ||
} | ||
|
||
return; | ||
} | ||
|
||
if (!cache?.content) { | ||
this.cacheStore.set(url, { content: '', status: STATUS.LOADING }); | ||
|
||
try { | ||
const content = await request(url, fetchOptions); | ||
|
||
this.cacheStore.set(url, { content, status: STATUS.LOADED }); | ||
} catch (error: any) { | ||
this.cacheStore.set(url, { content: '', status: STATUS.FAILED }); | ||
throw error; | ||
} | ||
} | ||
} | ||
|
||
public keys(): Array<string> { | ||
return [...this.cacheStore.keys()]; | ||
} | ||
|
||
public delete(url: string) { | ||
this.cacheStore.delete(url); | ||
} | ||
|
||
public clear() { | ||
this.cacheStore.clear(); | ||
} | ||
} | ||
|
||
export default new CacheStore(); |
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,10 @@ | ||
export const CACHE_MAX_RETRIES = 10; | ||
|
||
export const STATUS = { | ||
IDLE: 'idle', | ||
LOADING: 'loading', | ||
LOADED: 'loaded', | ||
FAILED: 'failed', | ||
READY: 'ready', | ||
UNSUPPORTED: 'unsupported', | ||
} as const; |
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 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 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
Oops, something went wrong.