Skip to content

Commit

Permalink
feat: add remove method and deprecate clear (#1022)
Browse files Browse the repository at this point in the history
  • Loading branch information
boschni committed Sep 16, 2020
1 parent 607425e commit 34a7c1c
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 6 deletions.
4 changes: 2 additions & 2 deletions docs/src/pages/docs/api.md
Expand Up @@ -7,7 +7,6 @@ title: API Reference

```js
const {
clear,
data,
error,
failureCount,
Expand All @@ -20,6 +19,7 @@ const {
isStale,
isSuccess,
refetch,
remove,
status,
} = useQuery(queryKey, queryFn?, {
cacheTime,
Expand Down Expand Up @@ -194,7 +194,7 @@ const queryInfo = useQuery({
- `refetch: Function({ throwOnError }) => Promise<TResult | undefined>`
- A function to manually refetch the query.
- If the query errors, the error will only be logged. If you want an error to be thrown, pass the `throwOnError: true` option
- `clear: Function() => void`
- `remove: Function() => void`
- A function to remove the query from the cache.
## `usePaginatedQuery`
Expand Down
12 changes: 11 additions & 1 deletion src/core/query.ts
Expand Up @@ -146,7 +146,7 @@ export class Query<TResult, TError> {
}

this.gcTimeout = setTimeout(() => {
this.clear()
this.remove()
}, this.cacheTime)
}

Expand Down Expand Up @@ -206,7 +206,17 @@ export class Query<TResult, TError> {
})
}

/**
* @deprecated
*/
clear(): void {
Console.warn(
'react-query: clear() has been deprecated, please use remove() instead'
)
this.remove()
}

remove(): void {
this.queryCache.removeQuery(this)
}

Expand Down
14 changes: 11 additions & 3 deletions src/core/queryObserver.ts
Expand Up @@ -30,7 +30,7 @@ export class QueryObserver<TResult, TError> {
this.initialUpdateCount = 0

// Bind exposed methods
this.clear = this.clear.bind(this)
this.remove = this.remove.bind(this)
this.refetch = this.refetch.bind(this)
this.fetchMore = this.fetchMore.bind(this)

Expand Down Expand Up @@ -110,8 +110,15 @@ export class QueryObserver<TResult, TError> {
return this.currentResult
}

/**
* @deprecated
*/
clear(): void {
return this.currentQuery.clear()
this.remove()
}

remove(): void {
this.currentQuery.remove()
}

refetch(options?: RefetchOptions): Promise<TResult | undefined> {
Expand Down Expand Up @@ -234,7 +241,7 @@ export class QueryObserver<TResult, TError> {
this.currentResult = {
...getStatusProps(status),
canFetchMore: state.canFetchMore,
clear: this.clear,
clear: this.remove,
data,
error: state.error,
failureCount: state.failureCount,
Expand All @@ -247,6 +254,7 @@ export class QueryObserver<TResult, TError> {
isPreviousData,
isStale: this.isStale,
refetch: this.refetch,
remove: this.remove,
updatedAt,
}
}
Expand Down
1 change: 1 addition & 0 deletions src/core/types.ts
Expand Up @@ -207,6 +207,7 @@ export interface QueryResultBase<TResult, TError = unknown> {
isStale: boolean
isSuccess: boolean
refetch: (options?: RefetchOptions) => Promise<TResult | undefined>
remove: () => void
status: QueryStatus
updatedAt: number
}
Expand Down
2 changes: 2 additions & 0 deletions src/react/tests/useInfiniteQuery.test.tsx
Expand Up @@ -78,6 +78,7 @@ describe('useInfiniteQuery', () => {
isStale: true,
isSuccess: false,
refetch: expect.any(Function),
remove: expect.any(Function),
status: 'loading',
updatedAt: expect.any(Number),
})
Expand Down Expand Up @@ -107,6 +108,7 @@ describe('useInfiniteQuery', () => {
isStale: true,
isSuccess: true,
refetch: expect.any(Function),
remove: expect.any(Function),
status: 'success',
updatedAt: expect.any(Number),
})
Expand Down
2 changes: 2 additions & 0 deletions src/react/tests/usePaginatedQuery.test.tsx
Expand Up @@ -50,6 +50,7 @@ describe('usePaginatedQuery', () => {
latestData: undefined,
resolvedData: undefined,
refetch: expect.any(Function),
remove: expect.any(Function),
status: 'loading',
updatedAt: expect.any(Number),
})
Expand All @@ -75,6 +76,7 @@ describe('usePaginatedQuery', () => {
latestData: 1,
resolvedData: 1,
refetch: expect.any(Function),
remove: expect.any(Function),
status: 'success',
updatedAt: expect.any(Number),
})
Expand Down
5 changes: 5 additions & 0 deletions src/react/tests/useQuery.test.tsx
Expand Up @@ -140,6 +140,7 @@ describe('useQuery', () => {
isStale: true,
isSuccess: false,
refetch: expect.any(Function),
remove: expect.any(Function),
status: 'loading',
updatedAt: expect.any(Number),
})
Expand All @@ -163,6 +164,7 @@ describe('useQuery', () => {
isStale: true,
isSuccess: true,
refetch: expect.any(Function),
remove: expect.any(Function),
status: 'success',
updatedAt: expect.any(Number),
})
Expand Down Expand Up @@ -216,6 +218,7 @@ describe('useQuery', () => {
isStale: true,
isSuccess: false,
refetch: expect.any(Function),
remove: expect.any(Function),
status: 'loading',
updatedAt: expect.any(Number),
})
Expand All @@ -239,6 +242,7 @@ describe('useQuery', () => {
isStale: true,
isSuccess: false,
refetch: expect.any(Function),
remove: expect.any(Function),
status: 'loading',
updatedAt: expect.any(Number),
})
Expand All @@ -262,6 +266,7 @@ describe('useQuery', () => {
isStale: true,
isSuccess: false,
refetch: expect.any(Function),
remove: expect.any(Function),
status: 'error',
updatedAt: expect.any(Number),
})
Expand Down

1 comment on commit 34a7c1c

@vercel
Copy link

@vercel vercel bot commented on 34a7c1c Sep 16, 2020

Choose a reason for hiding this comment

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

Please sign in to comment.