-
Notifications
You must be signed in to change notification settings - Fork 23
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
Justineo
wants to merge
3
commits into
main
Choose a base branch
from
ktabledata-cache
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,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> | ||
<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> |
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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.