Skip to content

Commit

Permalink
refactor: remove cancel method as it no longer exists (#3142)
Browse files Browse the repository at this point in the history
* refactor: remove cancel method as it no longer exists

it wasn't doing anything in that test

* refactor: remove cancel method as it no longer exists

use signal in playground example instead of cancel fn
  • Loading branch information
TkDodo authored Dec 28, 2021
1 parent 250c654 commit e21ef6c
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 18 deletions.
17 changes: 10 additions & 7 deletions examples/playground/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ function Todos({ initialFilter = "", setEditingIndex }) {

const { status, data, isFetching, error, failureCount, refetch } = useQuery(
["todos", { filter }],
() => fetchTodos({ filter })
fetchTodos
);

return (
Expand Down Expand Up @@ -370,9 +370,16 @@ function AddTodo() {
);
}

function fetchTodos({ filter } = {}) {
function fetchTodos({ signal, queryKey: [, { filter }] }) {
console.info("fetchTodos", { filter });
const promise = new Promise((resolve, reject) => {

if (signal) {
signal.addEventListener("abort", () => {
console.info("cancelled", filter);
});
}

return new Promise((resolve, reject) => {
setTimeout(() => {
if (Math.random() < errorRate) {
return reject(
Expand All @@ -382,10 +389,6 @@ function fetchTodos({ filter } = {}) {
resolve(list.filter((d) => d.name.includes(filter)));
}, queryTimeMin + Math.random() * (queryTimeMax - queryTimeMin));
});

promise.cancel = () => console.info("cancelled", filter);

return promise;
}

function fetchTodoById({ id }) {
Expand Down
14 changes: 3 additions & 11 deletions src/reactjs/tests/useQuery.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4268,17 +4268,9 @@ describe('useQuery', () => {
const key = queryKey()
const states: UseQueryResult<string>[] = []

const queryFn = () => {
let cancelFn = jest.fn()

const promise = new Promise<string>((resolve, reject) => {
cancelFn = jest.fn(() => reject('Cancelled'))
sleep(50).then(() => resolve('OK'))
})

;(promise as any).cancel = cancelFn

return promise
const queryFn = async () => {
await sleep(50)
return 'OK'
}

function Page() {
Expand Down

0 comments on commit e21ef6c

Please sign in to comment.