-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: aggregate key and isValidate cache data
- Loading branch information
Showing
5 changed files
with
87 additions
and
29 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 |
---|---|---|
@@ -1,5 +1,9 @@ | ||
// Next.js API route support: https://nextjs.org/docs/api-routes/introduction | ||
|
||
|
||
let counter = 1; | ||
|
||
export default (req, res) => { | ||
res.status(200).json({ name: 'John Doe' }) | ||
++counter; | ||
res.status(200).json({ name: `Hello World ${counter}`}) | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import { useState, useEffect } from "react"; | ||
import { cache } from "swr"; | ||
|
||
type CacheData = { | ||
key: string, | ||
data: any, | ||
isValidating: boolean, | ||
error: string, | ||
timestamp: Date, | ||
timestampString: string | ||
} | ||
|
||
const formatTime = (date: Date) => ( | ||
`${String(date.getHours()).padStart(2, '0')}:${String(date.getMinutes()).padStart(2, '0')}:${String(date.getSeconds()).padStart(2, '0')}` | ||
); | ||
|
||
const retrieveCache = (): CacheData[] => { | ||
const date = new Date(); | ||
return cache.keys() | ||
.filter(key => !key.startsWith("validating@") && !key.startsWith("err@")) | ||
.map(key => { | ||
const isValidating = cache.get(`validating@${key}`); | ||
const error = cache.get(`err@${key}`); | ||
const data = cache.get(key); | ||
return { | ||
key, | ||
data, | ||
isValidating, | ||
error, | ||
timestamp: date, | ||
timestampString: formatTime(date) | ||
} | ||
}) | ||
} | ||
|
||
export const useSWRCache = (): CacheData[] => { | ||
const [cacheData, setCacheData] = useState<CacheData[]>([]); | ||
useEffect(() => { | ||
const unsubscribe = cache.subscribe(() => { | ||
setCacheData(retrieveCache()) | ||
}) | ||
return () => unsubscribe(); | ||
}, [cache]); | ||
return cacheData; | ||
} | ||
|
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