Skip to content

Commit db0de73

Browse files
committed
feat(devtools): show cache writes
1 parent 21ced4a commit db0de73

File tree

5 files changed

+32
-7
lines changed

5 files changed

+32
-7
lines changed

packages/nuxt/client/components/devtools/Devtools.vue

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ const tab = useLocalStorage('rstore-devtools-tab', '0')
2626
const currentTab = computed(() => tabs[Number.parseInt(tab.value)]!)
2727
2828
const cache = useStoreCache()
29+
30+
const showCacheOps = useLocalStorage('rstore-devtools-show-cache-ops', false)
2931
</script>
3032

3133
<template>
@@ -44,8 +46,15 @@ const cache = useStoreCache()
4446
}"
4547
/>
4648

47-
<div class="flex-1 flex justify-end items-center gap-1">
49+
<div class="flex-1 flex justify-end items-center gap-2">
4850
<template v-if="currentTab.slot === 'history'">
51+
<USwitch
52+
v-model="showCacheOps"
53+
size="xs"
54+
label="Cache"
55+
class="text-xs"
56+
/>
57+
4958
<UTooltip text="Clear history">
5059
<UButton
5160
icon="lucide:trash"
@@ -70,7 +79,7 @@ const cache = useStoreCache()
7079
<template v-if="currentTab.slot === 'history'">
7180
<div class="flex flex-col-reverse p-1 gap-1">
7281
<DevtoolsHistoryItem
73-
v-for="(item, index) in stats.store"
82+
v-for="(item, index) in showCacheOps ? stats.store : stats.store.filter((item) => !item.operation.startsWith('cache'))"
7483
:key="index"
7584
:item
7685
/>

packages/nuxt/client/components/devtools/HistoryItem.vue

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,11 @@ const props = defineProps<{
33
item: StoreHistoryItem
44
}>()
55
6-
const startedAgo = useTimeAgo(() => props.item.started)
6+
const startedAgo = useTimeAgo(() => props.item.started ?? props.item.ended)
77
const duration = computed(() => {
8+
if (!props.item.started) {
9+
return null
10+
}
811
const diff = props.item.ended.getTime() - props.item.started.getTime()
912
return diff < 1000 ? `${diff}ms` : `${(diff / 1000).toFixed(2)}s`
1013
})
@@ -15,6 +18,7 @@ const icons = {
1518
create: 'lucide:plus',
1619
update: 'lucide:pen',
1720
delete: 'lucide:trash',
21+
cacheWrite: 'lucide:database',
1822
}
1923
</script>
2024

@@ -95,7 +99,7 @@ const icons = {
9599
</div>
96100
<div class="flex items-baseline gap-2 pt-0.5 pr-0.5">
97101
<span class="opacity-50 font-sans">{{ startedAgo }}</span>
98-
<span>{{ duration }}</span>
102+
<span v-if="duration">{{ duration }}</span>
99103
</div>
100104
</div>
101105
</div>

packages/nuxt/client/utils/stats.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { useDevtoolsClient } from '@nuxt/devtools-kit/iframe-client'
33
export function useStoreStats() {
44
const client = useDevtoolsClient()
55

6-
const stats = computed(() => client.value?.host.nuxt.$rstoreDevtoolsStats())
6+
const stats = computed(() => client.value?.host.nuxt.$rstoreDevtoolsStats() as { store: StoreHistoryItem[] })
77

88
client.value?.host.nuxt.$rstoreHistoryUpdated?.on(() => {
99
triggerRef(stats)
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
import type { FindOptions } from '@rstore/shared'
22

33
export interface StoreHistoryItem {
4-
operation: 'fetchFirst' | 'fetchMany' | 'create' | 'update' | 'delete'
4+
operation: 'fetchFirst' | 'fetchMany' | 'create' | 'update' | 'delete' | 'cacheWrite'
55
model: string
66
key?: string
77
findOptions?: FindOptions<any, any, any>
88
item?: any
99
result: any
10-
started: Date
10+
started?: Date | undefined
1111
ended: Date
1212
server?: boolean
1313
}

packages/nuxt/src/runtime/devtools.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,18 @@ export const devtoolsPlugin = definePlugin({
9999
historyUpdated.trigger()
100100
}
101101
})
102+
103+
hook('afterCacheWrite', (payload) => {
104+
storeStats.value.store.push({
105+
operation: 'cacheWrite',
106+
model: payload.model.name,
107+
ended: new Date(),
108+
result: payload.result,
109+
key: payload.key,
110+
server: import.meta.server,
111+
})
112+
historyUpdated.trigger()
113+
})
102114
},
103115
})
104116

0 commit comments

Comments
 (0)