From 1435ee119bdd3352c41743db9544a5a720d6ab10 Mon Sep 17 00:00:00 2001 From: Rene Date: Sun, 6 Dec 2020 02:30:36 +0100 Subject: [PATCH] improve cache code --- .../overlayscrollbars/src/support/cache/cache.ts | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/packages/overlayscrollbars/src/support/cache/cache.ts b/packages/overlayscrollbars/src/support/cache/cache.ts index 6a00351..caa08bc 100644 --- a/packages/overlayscrollbars/src/support/cache/cache.ts +++ b/packages/overlayscrollbars/src/support/cache/cache.ts @@ -10,13 +10,15 @@ type Cache = { [P in keyof T]: CacheEntry; }; -type UpdateCacheProp =

(prop: P, value: T[P], compare: (a?: T[P], b?: T[P]) => boolean) => void; +type UpdateCacheProp =

(prop: P, value: T[P], compare: CacheEqualFunction | null) => void; + +type PropsToUpdate = Array | keyof T; export type CacheUpdateFunction = (current?: T[P], previous?: T[P]) => T[P]; export type CacheEqualFunction = (a?: T[P], b?: T[P]) => boolean; -export type CacheUpdate = { +export type CacheChanged = { [P in keyof T]: boolean; }; @@ -42,9 +44,7 @@ export type CacheUpdateInfo = { * @returns A function which can be called with wither one ar an array of properties which shall be updated. Optionally it can be called with the force param. * This function returns a object which contains all cache properties as booleans which indicate whether the corresponding cache values really changed or not. */ -export const createCache = ( - cacheUpdateInfo: CacheUpdateInfo -): ((propsToUpdate?: Array | keyof T, force?: boolean) => CacheUpdate) => { +export const createCache = (cacheUpdateInfo: CacheUpdateInfo): ((propsToUpdate?: PropsToUpdate, force?: boolean) => CacheChanged) => { const cache: Cache = {} as T; const allProps: Array = keys(cacheUpdateInfo) as Array; @@ -60,8 +60,8 @@ export const createCache = ( cache[prop]._changed = equal ? !equal(curr, value) : curr !== value; }; - const flush = (force?: boolean): CacheUpdate => { - const result: CacheUpdate = {} as CacheUpdate; + const flush = (force?: boolean): CacheChanged => { + const result: CacheChanged = {} as CacheChanged; each(allProps, (prop: keyof T) => { result[prop] = !!(cache[prop]._changed || force); @@ -71,7 +71,7 @@ export const createCache = ( return result; }; - return (propsToUpdate?: Array | keyof T, force?: boolean) => { + return (propsToUpdate?: PropsToUpdate, force?: boolean) => { const finalPropsToUpdate: Array = (isString(propsToUpdate) ? ([propsToUpdate] as Array) : (propsToUpdate as Array)) || allProps; each(finalPropsToUpdate, (prop) => {