-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add CacheInterface type * Create defautl cache and use it in the code * Add test to custom cache support * Replace custom cache with exposed cache * Fix error in comment * Update README.md (#235) * test(typo): fix typo (#244) * fix weakmap key is `null` (#251) * fix race condition and add test (#261) * 0.1.17 * Move getKeyArgs and getErrorKey to serializeKey in Cache * Make the Map instance in Cache private * Add methods to subscribe to cache updates and get all keys * Apply suggestions from code review Co-Authored-By: Shu Ding <[email protected]> * Don't ignore tests * Cache methods receive keyInterface and serialize it if it's not a string * Don't expose Cache class anymore Co-authored-by: Shu Ding <[email protected]> Co-authored-by: Darren Jennings <[email protected]>
- Loading branch information
1 parent
fd0196e
commit bdb4324
Showing
7 changed files
with
217 additions
and
74 deletions.
There are no files selected for viewing
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,99 @@ | ||
import { CacheInterface, keyInterface, cacheListener } from './types' | ||
import { mutate } from './use-swr' | ||
import hash from './libs/hash' | ||
|
||
export default class Cache implements CacheInterface { | ||
private __cache: Map<string, any> | ||
private __listeners: cacheListener[] | ||
|
||
constructor(initialData: any = {}) { | ||
this.__cache = new Map(Object.entries(initialData)) | ||
this.__listeners = [] | ||
} | ||
|
||
get(key: keyInterface): any { | ||
const [_key] = this.serializeKey(key) | ||
return this.__cache.get(_key) | ||
} | ||
|
||
set(key: keyInterface, value: any, shouldNotify = true): any { | ||
const [_key] = this.serializeKey(key) | ||
this.__cache.set(_key, value) | ||
if (shouldNotify) mutate(key, value, false) | ||
this.notify() | ||
} | ||
|
||
keys() { | ||
return Array.from(this.__cache.keys()) | ||
} | ||
|
||
has(key: keyInterface) { | ||
const [_key] = this.serializeKey(key) | ||
return this.__cache.has(_key) | ||
} | ||
|
||
clear(shouldNotify = true) { | ||
if (shouldNotify) this.__cache.forEach(key => mutate(key, null, false)) | ||
this.__cache.clear() | ||
this.notify() | ||
} | ||
|
||
delete(key: keyInterface, shouldNotify = true) { | ||
const [_key] = this.serializeKey(key) | ||
if (shouldNotify) mutate(key, null, false) | ||
this.__cache.delete(_key) | ||
this.notify() | ||
} | ||
|
||
// TODO: introduce namespace for the cache | ||
serializeKey(key: keyInterface): [string, any, string] { | ||
let args = null | ||
if (typeof key === 'function') { | ||
try { | ||
key = key() | ||
} catch (err) { | ||
// dependencies not ready | ||
key = '' | ||
} | ||
} | ||
|
||
if (Array.isArray(key)) { | ||
// args array | ||
args = key | ||
key = hash(key) | ||
} else { | ||
// convert null to '' | ||
key = String(key || '') | ||
} | ||
|
||
const errorKey = key ? 'err@' + key : '' | ||
|
||
return [key, args, errorKey] | ||
} | ||
|
||
subscribe(listener: cacheListener) { | ||
if (typeof listener !== 'function') { | ||
throw new Error('Expected the listener to be a function.') | ||
} | ||
|
||
let isSubscribed = true | ||
this.__listeners.push(listener) | ||
|
||
return () => { | ||
if (!isSubscribed) return | ||
isSubscribed = false | ||
const index = this.__listeners.indexOf(listener) | ||
if (index > -1) { | ||
this.__listeners[index] = this.__listeners[this.__listeners.length - 1] | ||
this.__listeners.length-- | ||
} | ||
} | ||
} | ||
|
||
// Notify Cache subscribers about a change in the cache | ||
private notify() { | ||
for (let listener of this.__listeners) { | ||
listener() | ||
} | ||
} | ||
} |
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 |
---|---|---|
@@ -1,11 +1,13 @@ | ||
export * from './use-swr' | ||
import { default as useSWR } from './use-swr' | ||
export { useSWRPages } from './use-swr-pages' | ||
export { cache } from './config' | ||
export { | ||
ConfigInterface, | ||
revalidateType, | ||
RevalidateOptionInterface, | ||
keyInterface, | ||
responseInterface | ||
responseInterface, | ||
CacheInterface | ||
} from './types' | ||
export default useSWR |
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.