Skip to content

Commit

Permalink
fix(request): abort obj request after going to new path (#227)
Browse files Browse the repository at this point in the history
  • Loading branch information
Lanfei authored Jan 10, 2025
1 parent ef56a90 commit 2c17889
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 6 deletions.
14 changes: 12 additions & 2 deletions src/hooks/usePath.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import { useRouter } from "./useRouter"

let first_fetch = true

let cancelObj: Canceler
let cancelList: Canceler
export function addOrUpdateQuery(
key: string,
Expand Down Expand Up @@ -79,7 +80,15 @@ export const resetGlobalPage = () => {
}
export const usePath = () => {
const { pathname, to } = useRouter()
const [, getObj] = useFetch((path: string) => fsGet(path, password()))
const [, getObj] = useFetch((path: string) =>
fsGet(
path,
password(),
new axios.CancelToken((c) => {
cancelObj = c
}),
),
)
const pagination = getPagination()
if (pagination.type === "pagination" && getQueryVariable("page")) {
globalPage = parseInt(getQueryVariable("page"))
Expand All @@ -102,7 +111,7 @@ export const usePath = () => {
page.index,
page.size,
arg?.force,
new axios.CancelToken(function executor(c) {
new axios.CancelToken((c) => {
cancelList = c
}),
)
Expand All @@ -127,6 +136,7 @@ export const usePath = () => {
// if not, fetch get then determine if it is dir or file
const handlePathChange = (path: string, rp?: boolean, force?: boolean) => {
log(`handle [${path}] change`)
cancelObj?.()
cancelList?.()
retry_pass = rp ?? false
handleErr("")
Expand Down
15 changes: 11 additions & 4 deletions src/utils/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,18 @@ import { r } from "."
export const fsGet = (
path: string = "/",
password = "",
cancelToken?: CancelToken,
): Promise<FsGetResp> => {
return r.post("/fs/get", {
path: path,
password: password,
})
return r.post(
"/fs/get",
{
path: path,
password: password,
},
{
cancelToken: cancelToken,
},
)
}
export const fsList = (
path: string = "/",
Expand Down

0 comments on commit 2c17889

Please sign in to comment.