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

View File

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