-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(libs): invalidate cache on collection change
- Loading branch information
1 parent
1664a59
commit 3d1d290
Showing
12 changed files
with
170 additions
and
106 deletions.
There are no files selected for viewing
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import { getHashFromCollection } from '../../utils/getHashFromCollection' | ||
|
||
import type { CustomSearchCache, CustomSearchCacheRecord } from './types' | ||
import type { AnyCollection } from '../../types' | ||
|
||
/** | ||
* We take advantage of the global JS scope to use this constant as a "singleton" cache | ||
* to avoid re-normalizing and re-search-indexing each time `CustomSearch` is instanciated. | ||
* | ||
* This cache will only be used if the `cacheKey` option is set while instanciating `CustomSearch`. | ||
*/ | ||
const FUSE_SEARCH_CACHE: CustomSearchCache = {} | ||
|
||
export function findCacheRecord( | ||
currentCollection: AnyCollection, | ||
cacheKey: string | undefined, | ||
withCacheInvalidation: boolean | ||
): CustomSearchCacheRecord | undefined { | ||
if (!cacheKey) { | ||
return undefined | ||
} | ||
|
||
const cacheRecord = FUSE_SEARCH_CACHE[cacheKey] | ||
if (!cacheRecord) { | ||
return undefined | ||
} | ||
|
||
if (withCacheInvalidation) { | ||
const currentCollectionHash = getHashFromCollection(currentCollection) | ||
if (currentCollectionHash !== cacheRecord.originalCollectionHash) { | ||
return undefined | ||
} | ||
} | ||
|
||
return cacheRecord | ||
} | ||
|
||
export function storeCacheRecord(cacheKey: string, cacheRecord: CustomSearchCacheRecord) { | ||
FUSE_SEARCH_CACHE[cacheKey] = cacheRecord | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.