Use an object instead of a 2-tuple.
This commit is contained in:
parent
de63443762
commit
43e7d1d2cc
@ -14,9 +14,10 @@ type SimpleCacheConfiguratorObj = {
|
|||||||
invalidate: <T>(handler: () => T) => T,
|
invalidate: <T>(handler: () => T) => T,
|
||||||
};
|
};
|
||||||
|
|
||||||
type CacheEntry<ResultT, SideChannel> = Array<
|
type CacheEntry<ResultT, SideChannel> = Array<{
|
||||||
[ResultT, (SideChannel) => boolean],
|
value: ResultT,
|
||||||
>;
|
valid: SideChannel => boolean,
|
||||||
|
}>;
|
||||||
|
|
||||||
export type { CacheConfigurator };
|
export type { CacheConfigurator };
|
||||||
|
|
||||||
@ -64,7 +65,7 @@ function makeCachedFunction<
|
|||||||
);
|
);
|
||||||
|
|
||||||
if (cachedValue) {
|
if (cachedValue) {
|
||||||
for (const [value, valid] of cachedValue) {
|
for (const { value, valid } of cachedValue) {
|
||||||
if (valid(data)) return value;
|
if (valid(data)) return value;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -79,18 +80,18 @@ function makeCachedFunction<
|
|||||||
|
|
||||||
switch (cache.mode()) {
|
switch (cache.mode()) {
|
||||||
case "forever":
|
case "forever":
|
||||||
cachedValue = [[value, () => true]];
|
cachedValue = [{ value, valid: () => true }];
|
||||||
callCache.set(arg, cachedValue);
|
callCache.set(arg, cachedValue);
|
||||||
break;
|
break;
|
||||||
case "invalidate":
|
case "invalidate":
|
||||||
cachedValue = [[value, cache.validator()]];
|
cachedValue = [{ value, valid: cache.validator() }];
|
||||||
callCache.set(arg, cachedValue);
|
callCache.set(arg, cachedValue);
|
||||||
break;
|
break;
|
||||||
case "valid":
|
case "valid":
|
||||||
if (cachedValue) {
|
if (cachedValue) {
|
||||||
cachedValue.push([value, cache.validator()]);
|
cachedValue.push({ value, valid: cache.validator() });
|
||||||
} else {
|
} else {
|
||||||
cachedValue = [[value, cache.validator()]];
|
cachedValue = [{ value, valid: cache.validator() }];
|
||||||
callCache.set(arg, cachedValue);
|
callCache.set(arg, cachedValue);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user