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: remove cancel method as it no longer exists #3142

Merged
merged 2 commits into from
Dec 28, 2021
Merged
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
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