Skip to content

Commit

Permalink
fix: filter duplicated cache data and fix layouts
Browse files Browse the repository at this point in the history
  • Loading branch information
koba04 committed Aug 9, 2021
1 parent 70e5a14 commit 501a389
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 22 deletions.
24 changes: 17 additions & 7 deletions packages/swr-devtools-extensions/src/runtime.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,26 @@
import type { Cache } from "swr";

const proccessedCacheData = new Set<Cache>();

// @ts-expect-error
window.__SWR_DEVTOOLS__ = {
launch(cache: Cache) {
cache.subscribe(() => {
const cacheData = cache.keys().reduce(
(acc, key) => ({
...acc,
[key]: cache.get(key),
}),
{}
);
const cacheData = cache
.keys()
.map((key) => [key, cache.get(key)])
.filter(([_, value]) => !proccessedCacheData.has(value))
.map(([key, value]) => {
proccessedCacheData.add(value);
return { [key]: value };
})
.reduce(
(acc, data) => ({
...acc,
...data,
}),
{}
);
// console.log(data);
window.postMessage({ cacheData }, "*");
});
Expand Down
22 changes: 12 additions & 10 deletions packages/swr-devtools/src/cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,11 @@ const formatTime = (date: Date) =>
date.getMinutes()
).padStart(2, "0")}:${String(date.getSeconds()).padStart(2, "0")}`;

const cacheLogsStore = new Set<any>();
const latestCacheStore = new Map<any, any>();
const cacheHistory = new Map<string, any>();
const proccessedCacheData = new Map<any, any>();

const getCacheHistoryKey = (key: string, timestamp: Date) =>
`${key}__${timestamp.getTime()}`;

let id = 1;

Expand All @@ -30,15 +33,11 @@ const retrieveCache = (
.filter((key) => !key.startsWith("validating@") && !key.startsWith("err@"))
.map((key) => {
const data = cache.get(key);
const devToolsCache = latestCacheStore.get(data);
if (devToolsCache) {
return devToolsCache;
}

const isValidating = cache.get(`validating@${key}`);
const error = cache.get(`err@${key}`);
++id;
const cacheData = {
const cacheData = proccessedCacheData.get(data) || {
id,
key,
data,
Expand All @@ -47,11 +46,14 @@ const retrieveCache = (
timestamp: date,
timestampString: formatTime(date),
};
latestCacheStore.set(data, cacheData);
cacheLogsStore.add(cacheData);
proccessedCacheData.set(data, cacheData);
const cacheHistoryKey = getCacheHistoryKey(key, cacheData.timestamp);
if (!cacheHistory.get(cacheHistoryKey)) {
cacheHistory.set(cacheHistoryKey, cacheData);
}
return cacheData;
});
return [retrieveCacheData, Array.from(cacheLogsStore).reverse()];
return [retrieveCacheData, Array.from(cacheHistory.values()).reverse()];
};

export const useSWRCache = (
Expand Down
4 changes: 1 addition & 3 deletions packages/swr-devtools/src/components/Panel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,10 @@ const PanelWrapper = styled.section`

const PanelItem = styled.div`
flex: 1;
overflow: scroll;
`;

const CacheItems = styled.ul`
overflow: scroll;
overflow-y: scroll;
height: 100%;
margin: 0;
list-style: none;
padding-inline-start: 0;
Expand Down
5 changes: 3 additions & 2 deletions packages/swr-devtools/src/components/SWRDevTool.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ const DevToolWindow = styled.div`

const Header = styled.header`
display: flex;
gap: 10;
/* TODO: stop using the fixed size */
height: 40px;
`;

const HeaderTitle = styled.h3`
Expand All @@ -68,6 +69,6 @@ const HeaderTitle = styled.h3`

const PanelWrapper = styled.div`
position: relative;
flex-grow: 1;
height: calc(100% - 40px);
width: 100%;
`;

0 comments on commit 501a389

Please sign in to comment.