UNPKG

@jjordy/swr-devtools

Version:

Devtools for SWR

24 lines (23 loc) 765 B
export const injectSWRCache = (cache, watcher) => { // intercept operations modifying the cache store const originalSet = cache.set; cache.set = (key, value) => { watcher(key, value); return originalSet.call(cache, key, value); }; const originalDelete = cache.delete; cache.delete = (key) => { watcher(key, undefined); return originalDelete.call(cache, key); }; }; export const isMetaCache = (key) => { return /^\$(?:req|err|ctx|len)\$/.test(key); }; export const isInfiniteCache = (key) => { return /^\$inf\$/.test(key); }; export const getInfiniteCacheKey = (key) => { const match = key.match(/^\$inf\$(?<cacheKey>.*)?/); return match?.groups?.cacheKey ?? key; };