How to make node thread workers not to freeze my Electron Vite app?

I’m trying to implement file importing into my electron app using web workers to not block my main process. I already did everything they wrote in electron-vite docs (spoiler alert its not much) but app is freezing when calling worker. https://electron-vite.org/guide/dev#worker-threads

When I try to import files whole app is freezing but only visually. Copying itself works however after finish sometimes app unfreezes after some time and sometimes I need to restart it. I also tried to use 2nd method listed in docs to use workers in electron-vite app but then its not starting at all yelling at me that importWorker does not have default export, same error I got when trying to implement child process for this. Here is whole code for the app under branch workers https://github.com/UnderMan4/abManager/tree/workers.

This is how my code looks like now:

// main/index.ts

import { importFiles } from "./import";
// Other imports


function createWindow(): void {
   // Create the browser window.
   const mainWindow = new BrowserWindow({
      width: 1400,
      height: 900,
      minHeight: 600,
      minWidth: 1100,
      show: false,
      autoHideMenuBar: false,
      ...(process.platform === "linux" ? { icon } : {}),
      webPreferences: {
         preload: path.join(__dirname, "../preload/index.js"),
         sandbox: false,
         nodeIntegrationInWorker: true,
      },
   });

   [...]

   apiHandling();

   [...]
});

[...]

const apiHandling = () => {
   // more communication handling

   ipcMain.on("import-files", (_, data) => {
      importFiles(data);
   });
};

[...]
// main/import.ts
import { ipcMain } from "electron";
import { Worker } from "worker_threads";

import importWorker from "./workers/importWorker?nodeWorker";

const runningImports = new Map<string, { data: ImportData; worker: Worker }>();

// This method will allow my to start importing (copying into library folder). By design every call should create new worker, do all copying in background and send progress to renderer using ipcMain.emit() 
export const importFiles = (data: ImportData) => {
   if (runningImports.has(data.id)) return;

   const worker = importWorker({
      workerData: data,
   });

   runningImports.set(data.id, { data, worker });

   worker.on("message", (message: ImportWorkerMessage) => {
      const { type, ...data } = message;

      ipcMain.emit("import-message", type, data);

      if (type === "done") {
         worker.terminate();
      }
   });

   worker.on("error", (err) => {
      console.error(err);
   });

   worker.on("exit", (code) => {
      runningImports.delete(data.id);
   });
};
// main/workers/importWorker.ts
// The worker itself

import fs from "fs";
import p from "path";
import { parentPort, workerData } from "worker_threads";

if (!parentPort) {
   throw new Error("No parentPort found");
}

const { paths, options, id } = workerData as ImportData;

const fileQueue = [...paths];

const processFiles = () => {
   if (fileQueue.length === 0) {
      parentPort!.postMessage({ type: "done", id });
      return;
   }

   //Here I'm copying every file in paths array to desktop (for now)

   const path = fileQueue.shift()!;
   const fileName = p.basename(path);
   const readStream = fs.createReadStream(path);
   const writeStream = fs.createWriteStream(
      p.join("C:\Users\filip\Desktop", fileName)
   );
   parentPort!.postMessage({
      type: "fileStart",
      id,
      fileName,
   });

   readStream.on("data", (chunk) => {
      parentPort!.postMessage({
         type: "progress",
         id,
         fileName,
         chunkLength: chunk.length,
      });
   });

   readStream.on("end", () => {
      parentPort!.postMessage({
         type: "fileDone",
         id,
         fileName,
      });
      processFiles();
   });

   readStream.pipe(writeStream);

   writeStream.on("error", (error) => {
      parentPort!.postMessage({
         type: "error",
         id,
         fileName,
         error,
      });
   });
};

processFiles();
// defining communication methods for renderer in preload/index.ts
import: {
   importFiles: (path: string[], options: unknown) => {
      ipcRenderer.sendSync("import-files", path, options);
   },
   onMessage: (callback: (event: unknown, data: unknown) => void) => {
      ipcRenderer.addListener("import-message", callback);
      return () => ipcRenderer.removeListener("import-message", callback);
   },
},

New contributor

UnderMan4 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

1

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật