From aa61eb108310c310303921dad7e0aacaa9be2165 Mon Sep 17 00:00:00 2001 From: Arnaud Date: Fri, 23 Aug 2024 17:04:45 +0200 Subject: [PATCH] Disable worker because it looks not need with async requests --- src/components/Upload/Upload.tsx | 6 +-- src/components/Upload/UploadFile.tsx | 65 ++++++++++++++-------------- 2 files changed, 36 insertions(+), 35 deletions(-) diff --git a/src/components/Upload/Upload.tsx b/src/components/Upload/Upload.tsx index cd333bd..0e4b19b 100644 --- a/src/components/Upload/Upload.tsx +++ b/src/components/Upload/Upload.tsx @@ -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} /> ))} diff --git a/src/components/Upload/UploadFile.tsx b/src/components/Upload/UploadFile.tsx index aad9116..f25015e 100644 --- a/src/components/Upload/UploadFile.tsx +++ b/src/components/Upload/UploadFile.tsx @@ -30,7 +30,7 @@ type UploadFileProps = { id: string; onSuccess: ((cid: string) => void) | undefined; provider: () => Promise; - 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", - }); + mutateAsync(file); - provider().then(() => { - worker.current?.postMessage({ type: "init", upload: "" }); - }); + // if (useWorker) { + // worker.current = new Worker(new URL("./worker", import.meta.url), { + // type: "module", + // }); - worker.current.onmessage = function (e) { - const data = e.data; + // provider().then(() => { + // worker.current?.postMessage({ type: "init", upload: "" }); + // }); - 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.onmessage = function (e) { + // const data = e.data; - worker.current.onerror = function (e) { - // TODO report to sentry - console.error("Error in worker:", e); - dispatch({ type: "error", error: e.message }); - worker.current?.terminate(); - }; + // 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.postMessage({ type: "file", file }); - } else { - console.info("running file !!"); - mutateAsync(file); - } - }, [file, mutateAsync, onInternalSuccess, useWorker, provider]); + // 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) {