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

fix(*): update useDebounce to fix delays in KTable after clearing the search #1220

Merged
merged 6 commits into from Mar 22, 2023

Conversation

sumimakito
Copy link
Member

Summary

After clearing the search input in a KTable, a perceptible delay is observed before KTable triggers the fetcher function.

This PR also refactored the useDebounce function to accept functions, which makes it more general.

Another option to fix this is to use the defineExpose to expose a function to clear the search input and trigger the revalidate function immediately.

KHCP-6651

PR Checklist

  • Does not introduce dependencies
  • Functional: all changes do not break existing APIs and if so, bump major version.
  • Tests pass: check the output of yarn test
  • Naming: the files and the method and prop variables use the same naming conventions as other Kongponents
  • Framework style: abides by the essential rules in Vue's style guide
  • Cleanliness: does not have formatting issues, unused code (e.g., console.logs, debugger), or leftover comments
  • Docs: includes a technically accurate README, uses JSDOC where appropriate

@sumimakito sumimakito requested a review from a team as a code owner March 16, 2023 09:20
@netlify
Copy link

netlify bot commented Mar 16, 2023

Deploy Preview for kongponents ready!

Name Link
🔨 Latest commit 5bdb813
🔍 Latest deploy log https://app.netlify.com/sites/kongponents/deploys/641a75577e1bee0008e48446
😎 Deploy Preview https://deploy-preview-1220--kongponents.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site settings.

src/components/KTable/KTable.vue Outdated Show resolved Hide resolved
@@ -486,7 +486,9 @@ export default defineComponent({
hasInitialized.value = true
}

const { query, search } = useDebounce('', 350)
const query = ref('')
const [search] = useDebounce((q: string) => { query.value = q })
Copy link
Collaborator

Choose a reason for hiding this comment

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

This appears to be a breaking change since the return format is now an array instead of an object?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes. We only have two components using this composable: KTable and KCatalog. I intended to make useDebounce return two functions: one is the denounced function, and the other one is a function that accepts a delay (other than the default one) and returns a new debounced function with that new delay (but which shares the same timeout handle). I tried to implement this composable to make it as general as possible for different circumstances.

This allows us to trigger the underlying function with a different delay. For example, if we want to call the function immediately and clear all pending calls, we can use the second function to generate a debounced function with no delay and trigger it.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Is it possible to refactor the composable to return an object instead of an array; one property for each function?

Copy link
Member Author

Choose a reason for hiding this comment

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

I refactored the useDebounce composable to make it returns an object instead of an array.

src/composables/useUtilities.ts Outdated Show resolved Hide resolved
src/composables/useUtilities.ts Outdated Show resolved Hide resolved
@@ -486,7 +486,9 @@ export default defineComponent({
hasInitialized.value = true
}

const { query, search } = useDebounce('', 350)
const query = ref('')
const [search] = useDebounce((q: string) => { query.value = q })
Copy link
Member Author

Choose a reason for hiding this comment

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

Yes. We only have two components using this composable: KTable and KCatalog. I intended to make useDebounce return two functions: one is the denounced function, and the other one is a function that accepts a delay (other than the default one) and returns a new debounced function with that new delay (but which shares the same timeout handle). I tried to implement this composable to make it as general as possible for different circumstances.

This allows us to trigger the underlying function with a different delay. For example, if we want to call the function immediately and clear all pending calls, we can use the second function to generate a debounced function with no delay and trigger it.

src/components/KTable/KTable.vue Outdated Show resolved Hide resolved
Comment on lines 856 to 864
if (newValue === '') {
search(newValue)
} else {
debouncedSearch(newValue)
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

suggestion: Can you add some comments here to explain why we are using two different methods?

Copy link
Member Author

Choose a reason for hiding this comment

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

Comments added

Comment on lines 863 to 877
watch(() => [query.value, page.value, pageSize.value], ([newQuery, , , oldQuery]) => {
if (newQuery === '' && newQuery !== oldQuery) {
revalidate()
} else {
debouncedRevalidate()
}
}, { deep: true, immediate: true })
Copy link
Collaborator

Choose a reason for hiding this comment

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

This is a bit confusing with the empty array in the callback; could this be refactored or utilize watchEffect instead?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Copy link
Member Author

Choose a reason for hiding this comment

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

Thanks. That's a good idea.

Copy link
Collaborator

@adamdehaven adamdehaven left a comment

Choose a reason for hiding this comment

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

@sumimakito I reviewed everything here and it appears to be solid.

After merge, can you pull it into Kong Manager on Wednesday and test/verify all of the table fetchers are still working as intended? It's hard to test every use-case here

@sumimakito sumimakito merged commit a550695 into main Mar 22, 2023
7 checks passed
@sumimakito sumimakito deleted the fix/debounce-clear-delays branch March 22, 2023 05:27
kongponents-bot pushed a commit that referenced this pull request Mar 22, 2023
## [8.40.7](v8.40.6...v8.40.7) (2023-03-22)

### Bug Fixes

* update useDebounce to fix delays in KTable after clearing the search ([#1220](#1220)) ([a550695](a550695))
@kongponents-bot
Copy link
Collaborator

🎉 This PR is included in version 8.40.7 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants