TagSpaces is an offline, open source, document manager with tagging support
Cancel loading kanban S3 folders on location-folder change (#2115)
Stanimirddf929dec104
diff --git a/src/renderer/hooks/DirectoryContentContextProvider.tsx b/src/renderer/hooks/DirectoryContentContextProvider.tsxindex 72f936cbb..1f10d545a 100644--- a/src/renderer/hooks/DirectoryContentContextProvider.tsx+++ b/src/renderer/hooks/DirectoryContentContextProvider.tsx@@ -60,6 +60,7 @@ import { useEditedEntryContext } from '-/hooks/useEditedEntryContext';import { useEditedEntryMetaContext } from '-/hooks/useEditedEntryMetaContext';import { useEditedKanBanMetaContext } from '-/hooks/useEditedKanBanMetaContext';import { CommonLocation } from '-/utils/CommonLocation';+import { useCancelable } from '-/utils/useCancelable';type DirectoryContentContextData = {currentLocationPath: string;@@ -198,6 +199,7 @@ export const DirectoryContentContextProvider = ({const { kanbanActions } = useEditedKanBanMetaContext();const { showNotification, hideNotifications } = useNotificationContext();const { selectedEntries, setSelectedEntries } = useSelectedEntriesContext();+ const { signal, abort } = useCancelable();const currentLocationPath = useRef<string>('');//const useGenerateThumbnails = useSelector(getUseGenerateThumbnails);@@ -222,6 +224,7 @@ export const DirectoryContentContextProvider = ({* undefined means no .ts folder exist*/const isMetaLoaded = useRef<boolean>(undefined);+ const isLoading = useRef<boolean>(false);//const isMetaFolderExist = useRef<boolean>(undefined);const currentDirectoryPath = useRef<string>(undefined);const currentDirectoryFiles = useRef<TS.OrderVisibilitySettings[]>([]);@@ -235,7 +238,16 @@ export const DirectoryContentContextProvider = ({getLocationPath(currentLocation).then((locationPath) => {currentLocationPath.current = locationPath;if (!skipInitialDirList) {- return openDirectory(locationPath);+ if (isLoading.current) {+ abort();+ }+ isLoading.current = true;+ openDirectory(locationPath)+ .then((success) => {+ isLoading.current = false;+ return success;+ })+ .catch((ex) => console.log(ex));}});manualPerspective.current = 'unspecified';@@ -680,6 +692,9 @@ export const DirectoryContentContextProvider = ({} else {directoryMeta.current = getDefaultDirMeta();}+ if (signal.aborted) {+ return [];+ }return loadDirectoryContentInt(directoryPath,currentLocation,@@ -702,6 +717,10 @@ export const DirectoryContentContextProvider = ({location: CommonLocation,showHiddenEntries = undefined,): Promise<TS.FileSystemEntry[]> {+ const uploadCancelled = new Promise((_resolve, reject) => {+ signal.addEventListener('abort', () => reject());+ });+showNotification(t('core:loading'), 'info', false);const resultsLimit = {maxLoops:@@ -710,7 +729,7 @@ export const DirectoryContentContextProvider = ({: AppConfig.maxLoops,IsTruncated: false,};- return location+ const promise = location.listDirectoryPromise(directoryPath,[],@@ -718,6 +737,9 @@ export const DirectoryContentContextProvider = ({resultsLimit,).then((results) => {+ if (signal.aborted) {+ return [];+ }if (resultsLimit.IsTruncated) {//OPEN ISTRUNCATED dialogdispatch(AppActions.toggleTruncatedConfirmDialog());@@ -737,6 +759,12 @@ export const DirectoryContentContextProvider = ({// console.timeEnd('listDirectoryPromise');return loadDirectoryFailure(error);});+ return Promise.race([promise, uploadCancelled]).then(() => {+ // useCancelable will call abort when unmounted. After listDirectoryPromise succeeded,+ // we no longer care about that method of cancellation. Catch here to avoid an unhandled promise rejection.+ uploadCancelled.catch(() => {});+ return promise;+ });}function clearDirectoryContent() {diff --git a/src/renderer/utils/useCancelable.ts b/src/renderer/utils/useCancelable.tsnew file mode 100644index 000000000..c715d7723--- /dev/null+++ b/src/renderer/utils/useCancelable.ts@@ -0,0 +1,42 @@+/**+ * TagSpaces - universal file and folder organizer+ * Copyright (C) 2024-present TagSpaces GmbH+ *+ * This program is free software: you can redistribute it and/or modify+ * it under the terms of the GNU Affero General Public License (version 3) as+ * published by the Free Software Foundation.+ *+ * This program is distributed in the hope that it will be useful,+ * but WITHOUT ANY WARRANTY; without even the implied warranty of+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the+ * GNU Affero General Public License for more details.+ *+ * You should have received a copy of the GNU Affero General Public License+ * along with this program. If not, see <https://www.gnu.org/licenses/>.+ *+ * https://usehooks-typescript.com/use-event-listener/+ * eslint-disable @typescript-eslint/ban-types+ */+import { useEffect, useState } from 'react';++type UseCancelableResult = {+ signal: AbortSignal;+ abort: () => void;+};++export const useCancelable = (): UseCancelableResult => {+ const [controller, setController] = useState(new AbortController());++ // Abort it automatically in the destructor+ useEffect(() => {+ return () => controller.abort();+ }, [controller]);++ return {+ signal: controller.signal,+ abort: () => {+ controller.abort();+ setController(new AbortController());+ },+ };+};