Disable worker because it looks not need with async requests

This commit is contained in:
Arnaud 2024-08-23 17:04:45 +02:00
parent 252fb1b4f1
commit aa61eb1083
No known key found for this signature in database
GPG Key ID: 69D6CE281FCAE663
2 changed files with 36 additions and 35 deletions

View File

@ -66,7 +66,7 @@ type Props = {
* If true, the upload will run in a separate web worker.
* Default is !!window.Worker.
*/
useWorker?: boolean;
// useWorker?: boolean;
/**
* Apply custom css variables.
@ -106,7 +106,7 @@ export function Upload({
onDeleteItem,
onSuccess,
provider = defaultProvider,
useWorker = !!window.Worker,
// useWorker = !!window.Worker,
}: Props) {
const { deleteFile, files, uploadFiles, warning } = useUploadStategy(
multiple ? "multiple" : "single",
@ -189,7 +189,7 @@ export function Upload({
id={id}
onSuccess={onSuccess}
provider={provider}
useWorker={useWorker}
// useWorker={useWorker}
/>
))}
</>

View File

@ -30,7 +30,7 @@ type UploadFileProps = {
id: string;
onSuccess: ((cid: string) => void) | undefined;
provider: () => Promise<CodexData["upload"]>;
useWorker: boolean;
// useWorker: boolean;
};
type State = {
@ -128,7 +128,7 @@ export function UploadFile({
id,
onSuccess,
provider,
useWorker,
// useWorker,
}: UploadFileProps) {
const abort = useRef<(() => void) | null>(null);
const queryClient = useQueryClient();
@ -214,41 +214,42 @@ export function UploadFile({
reader.readAsDataURL(file);
}
if (useWorker) {
worker.current = new Worker(new URL("./worker", import.meta.url), {
type: "module",
});
provider().then(() => {
worker.current?.postMessage({ type: "init", upload: "" });
});
worker.current.onmessage = function (e) {
const data = e.data;
if (e.data.type === "progress") {
onProgress(data.loaded, data.total);
} else if (e.data.type === "completed") {
onInternalSuccess(e.data.value.data);
} else if (e.data.error) {
// TODO report with sentry
dispatch({ type: "error", error: e.data.error });
}
};
worker.current.onerror = function (e) {
// TODO report to sentry
console.error("Error in worker:", e);
dispatch({ type: "error", error: e.message });
worker.current?.terminate();
};
worker.current.postMessage({ type: "file", file });
} else {
console.info("running file !!");
mutateAsync(file);
}
}, [file, mutateAsync, onInternalSuccess, useWorker, provider]);
// if (useWorker) {
// worker.current = new Worker(new URL("./worker", import.meta.url), {
// type: "module",
// });
// provider().then(() => {
// worker.current?.postMessage({ type: "init", upload: "" });
// });
// worker.current.onmessage = function (e) {
// const data = e.data;
// if (e.data.type === "progress") {
// onProgress(data.loaded, data.total);
// } else if (e.data.type === "completed") {
// onInternalSuccess(e.data.value.data);
// } else if (e.data.error) {
// // TODO report with sentry
// dispatch({ type: "error", error: e.data.error });
// }
// };
// worker.current.onerror = function (e) {
// // TODO report to sentry
// console.error("Error in worker:", e);
// dispatch({ type: "error", error: e.message });
// worker.current?.terminate();
// };
// worker.current.postMessage({ type: "file", file });
// } else {
// mutateAsync(file);
// }
}, [file, mutateAsync, onInternalSuccess, provider]);
const onCancel = () => {
if (worker.current) {