Skip to content
This repository was archived by the owner on Nov 2, 2023. It is now read-only.

Commit 133082e

Browse files
committed
feat(core): Handover cache information to cacheInvalidation method
1 parent 89fd847 commit 133082e

File tree

3 files changed

+18
-9
lines changed

3 files changed

+18
-9
lines changed

core/src/index.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,12 @@ export interface EndpointGetOptions<TKeys, TResult, TKeysBind = TKeys>
3232
/**
3333
* A function which returns false if the cache is invalid
3434
*
35-
* TODO: Change to cacheValidator(api, cacheKey, keys) ?
3635
*/
3736
cacheValidator?: (
37+
url: string,
38+
headers: { [keys: string]: string },
39+
cacheKey: string,
40+
cache: Cache<TResult>,
3841
api: EndpointWithoutRequestBodyFunction<TKeysBind, TResult, "GET">
3942
) => boolean;
4043
/**

core/src/lib/CacheStore.ts

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
1-
import {
2-
EndpointHeadersTemplate,
3-
EndpointWithoutRequestBodyFunction
4-
} from "./Endpoint";
1+
import { EndpointHeadersTemplate } from "./Endpoint";
52
import { Emitter } from "./Emitter";
63

74
export interface Cache<TResult> {
@@ -73,11 +70,13 @@ export interface EndpointCacheOptions<
7370
cache?: Cache<TResult>;
7471
/**
7572
* A function which returns false if the cache is invalid
76-
*
77-
* TODO: Change to cacheValidator(api, cacheKey, keys) ?
7873
*/
7974
cacheValidator?: (
80-
api: EndpointWithoutRequestBodyFunction<TKeysBind, TResult, "GET">
75+
url: string,
76+
headers: { [keys: string]: string },
77+
cacheKey: string,
78+
cache: Cache<TResult>,
79+
api: any
8180
) => boolean;
8281
}
8382

core/src/lib/Endpoint.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,14 @@ export function createEndpoint<
163163
cacheKey = getCacheKey(url, headers);
164164
// Check if cache is still valid
165165
const skipCache =
166-
cacheValidator && cacheValidator(api as any) === false;
166+
cacheValidator &&
167+
cacheValidator(
168+
url.value,
169+
headers.value,
170+
cacheKey,
171+
cache,
172+
api as any
173+
) === false;
167174
if (cacheKey && !skipCache) {
168175
const ajaxResultFromCache = cache!.get(cacheKey);
169176
if (ajaxResultFromCache) {

0 commit comments

Comments
 (0)