Skip to content

Commit

Permalink
refactor: cacheUpdater as generic function
Browse files Browse the repository at this point in the history
  • Loading branch information
arthurfiorette committed Oct 12, 2021
1 parent 49f80d7 commit 2fa2557
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 13 deletions.
2 changes: 1 addition & 1 deletion src/interceptors/response.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ export class CacheResponseInterceptor<R>

// Update other entries before updating himself
if (response.config.cache?.update) {
updateCache(this.axios, response.data, response.config.cache.update);
updateCache(this.axios.storage, response.data, response.config.cache.update);
}

const deferred = this.axios.waiting[key];
Expand Down
17 changes: 9 additions & 8 deletions src/util/update-cache.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
import type { AxiosCacheInstance, CacheUpdater } from '../axios/types';
import type { CacheUpdater } from '../axios/types';
import type { CacheStorage } from '../storage/types';

export async function updateCache(
axios: AxiosCacheInstance,
data: any,
export async function updateCache<T = any>(
storage: CacheStorage,
data: T,
entries: Record<string, CacheUpdater>
): Promise<void> {
for (const cacheKey in entries) {
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
const value = entries[cacheKey]!;

if (value == 'delete') {
await axios.storage.remove(cacheKey);
await storage.remove(cacheKey);
continue;
}

const oldValue = await axios.storage.get(cacheKey);
const oldValue = await storage.get(cacheKey);

if (oldValue.state === 'loading') {
throw new Error('cannot update the cache while loading');
Expand All @@ -23,10 +24,10 @@ export async function updateCache(
const newValue = value(oldValue, data);

if (newValue === undefined) {
await axios.storage.remove(cacheKey);
await storage.remove(cacheKey);
continue;
}

await axios.storage.set(cacheKey, newValue);
await storage.set(cacheKey, newValue);
}
}
8 changes: 4 additions & 4 deletions test/util/update-cache.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ describe('Tests update-cache', () => {
});

it('tests for delete key', async () => {
await updateCache(axios, DEFAULT_DATA, {
await updateCache(axios.storage, DEFAULT_DATA, {
[KEY]: 'delete'
});

Expand All @@ -32,7 +32,7 @@ describe('Tests update-cache', () => {
});

it('tests for returning undefined', async () => {
await updateCache(axios, DEFAULT_DATA, {
await updateCache(axios.storage, DEFAULT_DATA, {
[KEY]: () => undefined
});

Expand All @@ -43,7 +43,7 @@ describe('Tests update-cache', () => {
});

it('tests for returning an new value', async () => {
await updateCache(axios, DEFAULT_DATA, {
await updateCache(axios.storage, DEFAULT_DATA, {
[KEY]: (cached, newData) => ({
state: 'cached',
ttl: Infinity,
Expand All @@ -64,7 +64,7 @@ describe('Tests update-cache', () => {
it('check if the state is loading while updating', async () => {
axios.storage.set(KEY, { state: 'loading' });

const result = updateCache(axios, DEFAULT_DATA, {
const result = updateCache(axios.storage, DEFAULT_DATA, {
[KEY]: (cached, newData) => ({
state: 'cached',
ttl: Infinity,
Expand Down

0 comments on commit 2fa2557

Please sign in to comment.