Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: improve caching for KTableData #2545

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@
"focus-trap-vue": "^4.0.3",
"nanoid": "^5.0.9",
"sortablejs": "^1.15.3",
"swrv": "^1.0.4",
"swrv": "1.1.0-beta.2",
"v-calendar": "^3.1.2",
"vue-draggable-next": "^2.2.1"
},
Expand Down
14 changes: 11 additions & 3 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions sandbox/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ const sandboxAppLinks: SandboxNavigationItem[] = ([
{ name: 'KTextarea', to: { name: 'textarea' } },
{ name: 'KToaster', to: { name: 'toaster' } },
{ name: 'KTreeList', to: { name: 'treelist' } },
{ name: 'KTableData 2', to: { name: 'table-data-2' } },
])

// Provide the app links to the SandboxLayout components
Expand Down
148 changes: 148 additions & 0 deletions sandbox/pages/SandboxTableData2.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
<template>
<SandboxLayout
:links="inject('app-links', [])"
title="KTableData + SWRV beta"
>
<div class="horizontal-container">
<KInput v-model="query" />
<KButton @click="deleteZach()">
Delete Zach (revalidate)
</KButton>
<KButton @click="deleteZach(false)">
Delete Zach (no revalidate)
</KButton>
Comment on lines +11 to +13
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This deletion with no revalidate only takes effect after pagination, and a subsequent revalidation.

  1. Click button
  2. Nothing happens in UI
  3. Paginate to second page
  4. Paginate back to first page
  5. Zach is initially in the list
  6. List revalidates, and then Zach is removed

<KButton @click="show = !show">
Toggle Table
</KButton>
</div>
<div class="swrv-sandbox">
<KTableData
v-if="show"
cache-identifier="table"
:fetcher="fetcher"
:fetcher-cache-key="String(cacheKey)"
:headers="headers"
:search-input="query"
/>
</div>
</SandboxLayout>
</template>

<script setup lang="ts">
import { inject, ref } from 'vue'
import type { TableDataFetcherParams, TableDataHeader } from '@/types'

const headers: TableDataHeader[] = [
{ key: 'actions', label: 'Row actions' },
{ key: 'name', label: 'Full Name' },
{
key: 'username',
label: 'Username',
tooltip: 'Columns with a tooltip.',
sortable: true,
},
{ key: 'email', label: 'Email', hidable: true },
]

const cacheKey = ref(1)

const query = ref('')
const show = ref(true)

const data: Item[] = [
{ name: 'John Doe', username: 'johndoe', email: '[email protected]' },
{ name: 'Jane Smith', username: 'janesmith', email: '[email protected]' },
{ name: 'Alice Johnson', username: 'alicej', email: '[email protected]' },
{ name: 'Bob Brown', username: 'bobbrown', email: '[email protected]' },
{ name: 'Charlie Davis', username: 'charlied', email: '[email protected]' },
{ name: 'David Wilson', username: 'davidw', email: '[email protected]' },
{ name: 'Eva Green', username: 'evagreen', email: '[email protected]' },
{ name: 'Frank Harris', username: 'frankh', email: '[email protected]' },
{ name: 'Grace Lee', username: 'gracelee', email: '[email protected]' },
{ name: 'Henry King', username: 'henryk', email: '[email protected]' },
{ name: 'Ivy Scott', username: 'ivyscott', email: '[email protected]' },
{ name: 'Jack White', username: 'jackwhite', email: '[email protected]' },
{ name: 'Karen Young', username: 'kareny', email: '[email protected]' },
{ name: 'Leo Walker', username: 'leowalker', email: '[email protected]' },
{ name: 'Mia Hall', username: 'miahall', email: '[email protected]' },
{ name: 'Noah Allen', username: 'noahallen', email: '[email protected]' },
{ name: 'Olivia Wright', username: 'oliviawright', email: '[email protected]' },
{ name: 'Paul Adams', username: 'pauladams', email: '[email protected]' },
{ name: 'Quinn Baker', username: 'quinnb', email: '[email protected]' },
{ name: 'Rachel Clark', username: 'rachelc', email: '[email protected]' },
{ name: 'Sam Evans', username: 'samevans', email: '[email protected]' },
{ name: 'Tina Foster', username: 'tinaf', email: '[email protected]' },
{ name: 'Uma Garcia', username: 'umag', email: '[email protected]' },
{ name: 'Victor Hill', username: 'victorh', email: '[email protected]' },
{ name: 'Wendy Johnson', username: 'wendyj', email: '[email protected]' },
{ name: 'Xander Lee', username: 'xanderl', email: '[email protected]' },
{ name: 'Yara Martinez', username: 'yaram', email: '[email protected]' },
{ name: 'Zach Nelson', username: 'zachn', email: '[email protected]' },
{ name: 'Amy O\'Connor', username: 'amyo', email: '[email protected]' },
{ name: 'Brian Peterson', username: 'brianp', email: '[email protected]' },
{ name: 'Cathy Quinn', username: 'cathyq', email: '[email protected]' },
{ name: 'Derek Roberts', username: 'derekr', email: '[email protected]' },
{ name: 'Ella Stevens', username: 'ellas', email: '[email protected]' },
{ name: 'Felix Turner', username: 'felixt', email: '[email protected]' },
{ name: 'Gina Underwood', username: 'ginau', email: '[email protected]' },
{ name: 'Hank Vance', username: 'hankv', email: '[email protected]' },
{ name: 'Iris West', username: 'irisw', email: '[email protected]' },
{ name: 'Jake Young', username: 'jakey', email: '[email protected]' },
{ name: 'Kara Zane', username: 'karaz', email: '[email protected]' },
{ name: 'Liam Brown', username: 'liamb', email: '[email protected]' },
{ name: 'Mona Clark', username: 'monac', email: '[email protected]' },
{ name: 'Nina Davis', username: 'ninad', email: '[email protected]' },
{ name: 'Oscar Edwards', username: 'oscare', email: '[email protected]' },
{ name: 'Penny Foster', username: 'pennyf', email: '[email protected]' },
{ name: 'Quincy Green', username: 'quincyg', email: '[email protected]' },
{ name: 'Rita Harris', username: 'ritah', email: '[email protected]' },
{ name: 'Steve Irwin', username: 'stevei', email: '[email protected]' },
{ name: 'Tara Jones', username: 'taraj', email: '[email protected]' },
]

interface Item {
name: string
username: string
email: string
}

async function fetcher({
pageSize,
page,
query,
sortColumnOrder, // sort by `username`
}: TableDataFetcherParams) {
await new Promise((resolve) => setTimeout(resolve, 500))
const start = pageSize! * (page! - 1)
const end = start + pageSize!
const filteredData = data.filter(({ name, username, email }: Item) => {
const q = query?.toLowerCase() ?? ''
return [name, username, email].some(value => value.toLowerCase().includes(q))
})
const sortedData = filteredData.sort((a: Item, b: Item) => {
return sortColumnOrder === 'asc' ? a.username.localeCompare(b.username) : b.username.localeCompare(a.username)
})
return {
data: sortedData.slice(start, end),
total: sortedData.length,
}
}

function deleteZach(revalidate = true) {
const index = data.findIndex((item) => item.name === 'Zach Nelson')
if (index !== -1) {
data.splice(index, 1)
if (revalidate) {
cacheKey.value++
}
}
}
</script>

<style lang="scss" scoped>
.horizontal-container {
display: flex;
gap: $kui-space-40;
justify-content: space-between;
}
</style>
6 changes: 6 additions & 0 deletions sandbox/router/sandbox-routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,12 @@ const componentRoutes: RouteRecordRaw[] = [
meta: { title: 'Tree List Sandbox' },
component: () => import('../pages/SandboxTreeList.vue'),
},
{
path: '/table-data-2',
name: 'table-data-2',
meta: { title: 'Table Data 2 Sandbox' },
component: () => import('../pages/SandboxTableData2.vue'),
},
]

export default componentRoutes
Loading
Loading