Skip to content

Commit

Permalink
refactor: remove cancel method as it no longer exists
Browse files Browse the repository at this point in the history
use signal in playground example instead of cancel fn
  • Loading branch information
TkDodo committed Dec 28, 2021
1 parent 28c9291 commit d1adaa0
Showing 1 changed file with 10 additions and 7 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

0 comments on commit d1adaa0

Please sign in to comment.