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(plugins): Add refetch interval plugin #153

Closed
wants to merge 1 commit into from

Conversation

vad1ym
Copy link

@vad1ym vad1ym commented Jan 7, 2025

Hi! Colada is awesome! I’m planning to migrate from TanStack, but the key feature I’m currently missing to start using the library is refetchInterval.

I’ve added a base plugin and some test cases, following the structure of other plugins. It works and behaves similarly to TanStack, but Colada’s plugin API feels a bit complicated to me, so I’d appreciate your help in finishing it properly.

In particular, I’m unsure about the Map keys I’m using. Should we use the query key (array?) itself as the Map key? And should it be a WeakMap instead?

Copy link

netlify bot commented Jan 7, 2025

Deploy Preview for funny-banoffee-0afb46 ready!

Name Link
🔨 Latest commit 036add9
🔍 Latest deploy log https://app.netlify.com/sites/funny-banoffee-0afb46/deploys/677d90d5479a6600082c72a3
😎 Deploy Preview https://deploy-preview-153--funny-banoffee-0afb46.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 configuration.

@posva
Copy link
Owner

posva commented Jan 31, 2025

Great! Does this overlap with #97 ? I yes, I might need to merge the other one first and then refactor the two PRs. We can change the API of plugins with breaking changes to improve them.

Edit: this is great but it feels off to have document visibility integrated into it.

The query key can be serialized but the method is not exported so I fixed that in 542e15e with toCacheKey.

Colada’s plugin API feels a bit complicated to me, so I’d appreciate your help in finishing it properly.

Sure 🙌 If you have any question, let me know. It would also help me out to point out details in the docs

},
"version": "0.0.1",
"description": "Refetch queries on a regular interval with Pinia Colada",
"author": {
Copy link
Owner

Choose a reason for hiding this comment

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

export interface RefetchIntervalOptions {
/**
* If set to a number, queries will continuously refetch at this frequency in milliseconds.
* If set to false, no automatic refetching will occur.
Copy link
Owner

Choose a reason for hiding this comment

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

What about setting it to true or 'auto' and have them auto refresh when stale? #97

timeoutId: setTimeout(() => {
// Only refetch if refetchIntervalInBackground is true or document is visible
if (options.refetchIntervalInBackground || isDocumentVisible) {
Promise.resolve(queryCache.fetch(queryEntry))
Copy link
Owner

Choose a reason for hiding this comment

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

Does it make sense to force a fetch? If the data is fresh, it should be fine to not fetch again 🤔

return ({ queryCache }) => {
const intervalMap = new Map<string, IntervalEntry>()

let isDocumentVisible = typeof document !== 'undefined'
Copy link
Owner

Choose a reason for hiding this comment

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

I'm wondering if this makes sense at this level. It could be set in the enabled option so it feels a bit redundant here, the overlap between options could prove difficult in the future to debug.

// setupNextInterval()

// If we can't refetch now, invalidate the query
queryCache.invalidate(queryEntry)
Copy link
Owner

Choose a reason for hiding this comment

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

FYI: invalidate also fetches if the query is active (displayed somewhere)

import type { PiniaColadaOptions, UseQueryOptions } from '@pinia/colada'
import { PiniaColadaRefetchInterval } from '.'

describe('Refetch Interval plugin', () => {
Copy link
Owner

Choose a reason for hiding this comment

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

Nice! A lot of tests 😄

@posva
Copy link
Owner

posva commented Jan 31, 2025

I release the plugin @pinia/colada-plugin-auto-refetch if you want to try it out! I personally feel like the smart refresh based on staleTime is better. It still allows to configure the interval

@vad1ym
Copy link
Author

vad1ym commented Feb 1, 2025

@posva Thanks for the review! As for the auto-refetch plugin, I didn’t realize right away that it basically does the same thing. I’ll give it a try, and it’ll probably be enough 👍

@vad1ym
Copy link
Author

vad1ym commented Feb 1, 2025

@posva Auto-refetch plugin does everything I need, but I'm currently really missing the ability to control autoRefetch options via a ref or getter—for example, to disable auto-refetch when the user leaves the tab.

EDIT: Sorry, it seems like it's working because the plugin uses toValue with this option, but there's a type mismatch. The plugin expects a boolean, but I guess it should be MaybeRefOrGetter<boolean>.

@posva
Copy link
Owner

posva commented Feb 1, 2025

Nice catch! I will release a patch in a second.

@posva
Copy link
Owner

posva commented Feb 1, 2025

I will close this for the moment, let me know if you find anything missing 🙂

@posva posva closed this Feb 1, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

2 participants