Skip to content

Commit

Permalink
feat: use Accessor utility type in hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
braden-w committed Jan 14, 2025
1 parent 32f7c6a commit 73fba28
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 6 deletions.
7 changes: 4 additions & 3 deletions packages/svelte-query/src/context.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { getContext, setContext } from 'svelte'
import type { QueryClient } from '@tanstack/query-core'
import type { Accessor } from './types.js'

const _contextKey = '$$_queryClient'

Expand All @@ -23,9 +24,9 @@ export const setQueryClientContext = (client: QueryClient): void => {
const _isRestoringContextKey = '$$_isRestoring'

/** Retrieves a `isRestoring` from Svelte's context */
export const getIsRestoringContext = (): (() => boolean) => {
export const getIsRestoringContext = (): Accessor<boolean> => {
try {
const isRestoring = getContext<(() => boolean) | undefined>(
const isRestoring = getContext<(Accessor<boolean>) | undefined>(
_isRestoringContextKey,
)
return isRestoring ?? (() => false)
Expand All @@ -35,6 +36,6 @@ export const getIsRestoringContext = (): (() => boolean) => {
}

/** Sets a `isRestoring` on Svelte's context */
export const setIsRestoringContext = (isRestoring: () => boolean): void => {
export const setIsRestoringContext = (isRestoring: Accessor<boolean>): void => {
setContext(_isRestoringContextKey, isRestoring)
}
3 changes: 2 additions & 1 deletion packages/svelte-query/src/useIsFetching.svelte.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { onDestroy } from 'svelte'
import { useQueryClient } from './useQueryClient.js'
import type { QueryClient, QueryFilters } from '@tanstack/query-core'
import type { Accessor } from './types.js'

export function useIsFetching(
filters?: QueryFilters,
queryClient?: QueryClient,
): () => number {
): Accessor<number> {
const client = useQueryClient(queryClient)
const queryCache = client.getQueryCache()

Expand Down
3 changes: 2 additions & 1 deletion packages/svelte-query/src/useIsMutating.svelte.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { notifyManager } from '@tanstack/query-core'
import { useQueryClient } from './useQueryClient.js'
import type { MutationFilters, QueryClient } from '@tanstack/query-core'
import type { Accessor } from './types.js'

export function useIsMutating(
filters?: MutationFilters,
queryClient?: QueryClient,
): () => number {
): Accessor<number> {
const client = useQueryClient(queryClient)
const cache = client.getMutationCache()
// isMutating is the prev value initialized on mount *
Expand Down
3 changes: 2 additions & 1 deletion packages/svelte-query/src/useIsRestoring.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { getIsRestoringContext } from './context.js'
import type { Accessor } from './types.js'

export function useIsRestoring(): () => boolean {
export function useIsRestoring(): Accessor<boolean> {
return getIsRestoringContext()
}

0 comments on commit 73fba28

Please sign in to comment.