ant-design-vue/components/_util/cssinjs/Cache.ts

26 lines
746 B
TypeScript
Raw Normal View History

2023-01-24 14:51:59 +00:00
export type KeyType = string | number;
type ValueType = [number, any]; // [times, realValue]
class Entity {
/** @private Internal cache map. Do not access this directly */
cache = new Map<string, ValueType>();
2023-03-13 03:11:40 +00:00
get(keys: KeyType[] | string): ValueType | null {
return this.cache.get(Array.isArray(keys) ? keys.join('%') : keys) || null;
2023-01-24 14:51:59 +00:00
}
2023-03-13 03:11:40 +00:00
update(keys: KeyType[] | string, valueFn: (origin: ValueType | null) => ValueType | null) {
const path = Array.isArray(keys) ? keys.join('%') : keys;
2023-01-24 14:51:59 +00:00
const prevValue = this.cache.get(path)!;
const nextValue = valueFn(prevValue);
if (nextValue === null) {
this.cache.delete(path);
} else {
this.cache.set(path, nextValue);
}
}
}
export default Entity;