-
Notifications
You must be signed in to change notification settings - Fork 75
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
Optimistic local mutations without setting dedupingInterval to 0 #152
Comments
@davidblnc sounds good, we should add the https://github.com/vercel/swr/blob/master/src/types.ts#L68-L72 |
Unfortunately I haven't been able to get the tests to pass. it('mutate respects "shouldRevalidate" option', async done => {
const loadData = key => new Promise(res => setTimeout(() => res(key), 100))
let firstFetch = true;
const vm = new Vue(defineComponent({
template: `<div>hello, {{data}}</div>`,
setup() {
const { data, mutate: revalidate } = useSWRV("mutate-should-not-revalidate", () => {
if (firstFetch) {
firstFetch = false
return 'initial-state'
}
return 'revalidated-state'
});
setTimeout(() => {
mutate("mutate-should-not-revalidate", loadData('optimistic-update-state'), undefined, undefined, false);
}, 100);
setTimeout(() => {
revalidate()
}, 300);
return { data };
}
})).$mount();
expect(vm.$el.textContent).toBe('hello, initial-state')
timeout(200)
await tick(2)
expect(vm.$el.textContent).toBe('hello, optimistic-update-state')
timeout(100)
await tick(2)
expect(vm.$el.textContent).toBe('hello, revalidated-state')
done()
}) |
@davidblnc this test passes for me on type MutatorOptions = {
shouldRevalidate: boolean
cache: SWRVCache<Omit<IResponse, 'mutate'>>
ttl: number
}
export type Mutator<Data = any> = (
key: IKey,
data?: Data | Promise<Data>,
options?: MutatorOptions
) => Promise<Data | undefined>
const mutate: Mutator = async (key, res, options) => {
// ...
}
// e.g.
mutate("mutate-should-not-revalidate", loadData('optimistic-update-state'), {
shouldRevalidate: false
}) which will give us the ability to fallback to default cache and ttls. |
After testing a bit, I think the library actually doesn't revalidate after calling mutate. In this example, I would've expected to see:
|
@davidblnc whoops you're right... the mutate actually needs to be added as a feature #92 (comment) |
Hello guys, for those who are looking for workaround I've implemented tracking mechanism for
Then u can use it as:
And to make it work instead of useSWRV directly, use
|
Hi! I was looking for alternatives to SWR in Vue and I found this library, which is great!
I found how to do optimistic local mutations in this issue (#83), but I'm kind of worried about having to set dedupingInterval to 0, because it means we can't really use deduping intervals and optimistic mutations at the same time.
Looking through the code, I think putting these lines inside an if could work.
Is there a way to do this I haven't found? Otherwise, would you be willing to accept a PR to add this functionality?
The text was updated successfully, but these errors were encountered: