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

refactor(svelte-query): rename FunctionedParams<T> to Accessor<T> for improved semantics and consistency, use in function return types #8534

Open
wants to merge 2 commits into
base: svelte-5-adapter
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
}
4 changes: 2 additions & 2 deletions packages/svelte-query/src/createBaseQuery.svelte.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { useQueryClient } from './useQueryClient.js'
import type {
CreateBaseQueryOptions,
CreateBaseQueryResult,
FunctionedParams,
Accessor,
} from './types.js'
import type {
QueryClient,
Expand All @@ -20,7 +20,7 @@ export function createBaseQuery<
TQueryData,
TQueryKey extends QueryKey,
>(
options: FunctionedParams<
options: Accessor<
CreateBaseQueryOptions<TQueryFnData, TError, TData, TQueryData, TQueryKey>
>,
Observer: typeof QueryObserver,
Expand Down
4 changes: 2 additions & 2 deletions packages/svelte-query/src/createInfiniteQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import type {
import type {
CreateInfiniteQueryOptions,
CreateInfiniteQueryResult,
FunctionedParams,
Accessor,
} from './types.js'

export function createInfiniteQuery<
Expand All @@ -20,7 +20,7 @@ export function createInfiniteQuery<
TQueryKey extends QueryKey = QueryKey,
TPageParam = unknown,
>(
options: FunctionedParams<
options: Accessor<
CreateInfiniteQueryOptions<
TQueryFnData,
TError,
Expand Down
4 changes: 2 additions & 2 deletions packages/svelte-query/src/createMutation.svelte.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import type {
CreateMutateFunction,
CreateMutationOptions,
CreateMutationResult,
FunctionedParams,
Accessor,
} from './types.js'

import type { DefaultError, QueryClient } from '@tanstack/query-core'
Expand All @@ -17,7 +17,7 @@ export function createMutation<
TVariables = void,
TContext = unknown,
>(
options: FunctionedParams<
options: Accessor<
CreateMutationOptions<TData, TError, TVariables, TContext>
>,
queryClient?: QueryClient,
Expand Down
4 changes: 2 additions & 2 deletions packages/svelte-query/src/createQueries.svelte.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { untrack } from 'svelte'
import { QueriesObserver, notifyManager } from '@tanstack/query-core'
import { useIsRestoring } from './useIsRestoring.js'
import { useQueryClient } from './useQueryClient.js'
import type { FunctionedParams } from './types.js'
import type { Accessor } from './types.js'
import type {
DefaultError,
DefinedQueryObserverResult,
Expand Down Expand Up @@ -208,7 +208,7 @@ export function createQueries<
queries,
...options
}: {
queries: FunctionedParams<[...QueriesOptions<T>]>
queries: Accessor<[...QueriesOptions<T>]>
combine?: (result: QueriesResults<T>) => TCombinedResult
},
queryClient?: QueryClient,
Expand Down
10 changes: 5 additions & 5 deletions packages/svelte-query/src/createQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import type {
CreateQueryOptions,
CreateQueryResult,
DefinedCreateQueryResult,
FunctionedParams,
Accessor,
} from './types.js'
import type {
DefinedInitialDataOptions,
Expand All @@ -18,7 +18,7 @@ export function createQuery<
TData = TQueryFnData,
TQueryKey extends QueryKey = QueryKey,
>(
options: FunctionedParams<
options: Accessor<
DefinedInitialDataOptions<TQueryFnData, TError, TData, TQueryKey>
>,
queryClient?: QueryClient,
Expand All @@ -30,7 +30,7 @@ export function createQuery<
TData = TQueryFnData,
TQueryKey extends QueryKey = QueryKey,
>(
options: FunctionedParams<
options: Accessor<
UndefinedInitialDataOptions<TQueryFnData, TError, TData, TQueryKey>
>,
queryClient?: QueryClient,
Expand All @@ -42,14 +42,14 @@ export function createQuery<
TData = TQueryFnData,
TQueryKey extends QueryKey = QueryKey,
>(
options: FunctionedParams<
options: Accessor<
CreateQueryOptions<TQueryFnData, TError, TData, TQueryKey>
>,
queryClient?: QueryClient,
): CreateQueryResult<TData, TError>

export function createQuery(
options: FunctionedParams<CreateQueryOptions>,
options: Accessor<CreateQueryOptions>,
queryClient?: QueryClient,
) {
return createBaseQuery(options, QueryObserver, queryClient)
Expand Down
2 changes: 1 addition & 1 deletion packages/svelte-query/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import type {
QueryObserverResult,
} from '@tanstack/query-core'

export type FunctionedParams<T> = () => T
export type Accessor<T> = () => T

/** Options for createBaseQuery */
export type CreateBaseQueryOptions<
Expand Down
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()
}
4 changes: 2 additions & 2 deletions packages/svelte-query/tests/createQuery/BaseExample.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
import { createQuery } from '../../src/index.js'
import type { QueryClient, QueryObserverResult } from '@tanstack/query-core'
import type { CreateQueryOptions, FunctionedParams } from '../../src/index.js'

Accessor
let {
options,
queryClient,
queryClient,Accessor
states,
}: {
options: FunctionedParams<CreateQueryOptions<any>>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@
import type {
CreateMutationOptions,
FunctionedParams,
MutationStateOptions,
AccessorteOptions,
} from '../../src/index.js'

let {
successMutationOpts,
errorMutationOpts,
mutationStateOpts,
mutationAccessor,
}: {
Accessor
successMutationOpts: FunctionedParams<CreateMutationOptions>
errorMutationOpts: FunctionedParams<CreateMutationOptions>
mutationStateOpts?: MutationStateOptions | undefined
Expand Down
Loading