From d1adaa0758a772c0b3078e6eda09ea7fa0f43e05 Mon Sep 17 00:00:00 2001 From: Dominik Dorfmeister Date: Tue, 28 Dec 2021 22:33:18 +0100 Subject: [PATCH] refactor: remove cancel method as it no longer exists use signal in playground example instead of cancel fn --- examples/playground/src/index.js | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/examples/playground/src/index.js b/examples/playground/src/index.js index 475e1ca0c7..886d24c182 100644 --- a/examples/playground/src/index.js +++ b/examples/playground/src/index.js @@ -181,7 +181,7 @@ function Todos({ initialFilter = "", setEditingIndex }) { const { status, data, isFetching, error, failureCount, refetch } = useQuery( ["todos", { filter }], - () => fetchTodos({ filter }) + fetchTodos ); return ( @@ -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( @@ -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 }) {