Created
May 17, 2021 10:55
-
-
Save TCotton/b3043a768ee7ef12cc4e1cd9e7b30385 to your computer and use it in GitHub Desktop.
Stale data invalidation
This file contains hidden or 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
const swr = new Map; | |
const useSWR = (path, fetcher, cache) => { | |
let [data, update] = useState(null); | |
if (!swr.has(path) || swr.get(path) !== cache) { | |
fetcher(path).then(update, () => update(new Error(path))); | |
swr.set(path, cache); | |
} | |
const isError = data instanceof Error; | |
return { | |
data: isError ? null : data, | |
error: isError ? data : null | |
}; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment