Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(mutation): add use mutating hook (#23)
* feat(mutation): add use mutating hook
- Loading branch information
1 parent
b98e737
commit 653c640
Showing
16 changed files
with
259 additions
and
11 deletions.
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
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,23 @@ | ||
--- | ||
id: useIsMutating | ||
title: useIsMutating | ||
--- | ||
|
||
`useIsMutating` is an optional hook that returns the `number` of mutations that your application is fetching (useful for app-wide loading indicators). | ||
|
||
```js | ||
import { useIsMutating } from '@sveltestack/svelte-query' | ||
// How many mutations are fetching? | ||
const isMutating = useIsMutating() | ||
// How many mutations matching the posts prefix are fetching? | ||
const isMutatingPosts = useIsMutating(['posts']) | ||
``` | ||
|
||
**Options** | ||
|
||
- `filters?: MutationFilters`: [Query Filters](../guides/filters#mutation-filters) | ||
|
||
**Returns** | ||
|
||
- `isMutating: number` | ||
- Will be the `number` of the mutations that your application is currently fetching. |
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
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,13 @@ | ||
<script lang="ts"> | ||
import type { MutationFilters } from '../queryCore/core/utils' | ||
import useIsMutating from './useIsMutating' | ||
export let filters: MutationFilters | ||
export let isMutating | ||
$: isMutatingResult = useIsMutating(filters) | ||
$: isMutating = $isMutatingResult | ||
</script> | ||
|
||
<slot name="isMutating" {isMutating} /> |
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,2 @@ | ||
export { default as IsMutating } from "./IsMutating.svelte"; | ||
export { default as useIsMutating } from "./useIsMutating"; |
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,27 @@ | ||
import { readable } from 'svelte/store' | ||
import type { Readable } from "svelte/store"; | ||
|
||
import { notifyManager, QueryClient } from '../queryCore/core' | ||
import { useQueryClient } from '../queryClientProvider' | ||
import type { MutationFilters } from '../queryCore/core/utils' | ||
|
||
export default function useIsMutating(filters?: MutationFilters): Readable<number> { | ||
const client: QueryClient = useQueryClient() | ||
const cache = client.getMutationCache() | ||
// isMutating is the prev value initialized on mount * | ||
let isMutating = client.isMutating(filters) | ||
|
||
const { subscribe } = readable(isMutating, set => { | ||
return cache.subscribe( | ||
notifyManager.batchCalls(() => { | ||
const newIisMutating = client.isMutating(filters) | ||
if (isMutating !== newIisMutating) { | ||
// * and update with each change | ||
isMutating = newIisMutating | ||
set(isMutating) | ||
} | ||
})) | ||
}) | ||
|
||
return { subscribe } | ||
} |
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
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
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,8 @@ | ||
<script lang="ts"> | ||
import { QueryClientProvider } from '../../src' | ||
import IsFetching from './IsMutating.svelte' | ||
</script> | ||
|
||
<QueryClientProvider> | ||
<IsFetching /> | ||
</QueryClientProvider> |
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,60 @@ | ||
<script lang="ts"> | ||
import { IsMutating, useMutation } from '../../src' | ||
import { useIsMutating } from '../../src/isMutating' | ||
const later = (delay, value) => | ||
new Promise(resolve => setTimeout(resolve, delay, value)) | ||
// the mutation | ||
const mutationFn = () => later(500, 'My response') | ||
const useMutationResult = useMutation(mutationFn) | ||
// the mutation 2 | ||
const mutationFn2 = () => later(500, 'My response 2') | ||
const useMutationResult2 = useMutation(mutationFn2) | ||
let isMutating = 0 | ||
let history = [] | ||
$: { | ||
history = [...history, isMutating] | ||
} | ||
// useIsMutating | ||
const isMutatingResult = useIsMutating() | ||
let useHistory = [] | ||
$: { | ||
useHistory = [...useHistory, $isMutatingResult] | ||
} | ||
</script> | ||
|
||
<main> | ||
<h3>IsFetching</h3> | ||
<IsMutating bind:isMutating> | ||
<div slot="isMutating"> | ||
isMutating change log: | ||
<span>{JSON.stringify(history)}</span> | ||
</div> | ||
</IsMutating> | ||
|
||
<h3>useIsMutating</h3> | ||
<div> | ||
useIsMutating change log: | ||
{JSON.stringify(useHistory)} | ||
<div> | ||
<button | ||
on:click={() => { | ||
$useMutationResult.mutate() | ||
$useMutationResult2.mutate() | ||
}}> | ||
Mutate All | ||
</button> | ||
|
||
<h3>Mutation 1</h3> | ||
<button on:click={() => $useMutationResult.mutate()}>mutate</button> | ||
{$useMutationResult.isLoading ? 'Mutation loading ...' : $useMutationResult.data || ''} | ||
|
||
<h3>Mutation 2</h3> | ||
<button on:click={() => $useMutationResult2.mutate()}>mutate 2</button> | ||
{$useMutationResult2.isLoading ? 'Mutation 2 loading ...' : $useMutationResult2.data || ''} | ||
</div> | ||
</div> | ||
</main> |
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,10 @@ | ||
import App from './App.svelte'; | ||
|
||
export default { | ||
title: 'Is mutating', | ||
component: App, | ||
}; | ||
|
||
export const History = () => ({ | ||
Component: App | ||
}); |
Oops, something went wrong.