Reputation: 14595
I am copying a list of files from one place to another using electron and fs.copyFileSync (in a loop). In order to not block the whole UI, I am doing it in an invisible window running a worker.js. The main application sends the list of files I want to have copied to the worker via an IPC bus.
Now I got reports from users, saying that during the copying operation the worker is not able to handle any other IPC requests (from other parts of the main app).
So I wondered if the copying operation could be somehow delegated to the OS. Is maybe fs.copyFile less draining for the nodejs process than fs.copyFileSync?
Upvotes: 0
Views: 1187
Reputation: 552
fs.readFileSync
still blocks the main (or browser) thread where the electron application runs. You can take a look into the 'fs/promises' api and use something like:
import { copyFile } from 'fs/promises';
await Promise.all([copyFile("source-a", "dest-a"), copyFile("source-b", "dest-b"), ...]);
so the thread won't block completely. By making these calls async you allow the event loop to continue with other operations and not block it until all operations are done.
Upvotes: 1