forked from piscinajs/piscina
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: add electron to the examples section in docs (piscinajs#713)
- Loading branch information
1 parent
d973eac
commit 869d00f
Showing
1 changed file
with
46 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
--- | ||
id: Worker Options | ||
sidebar_position: 4 | ||
--- | ||
|
||
import { WorkerWrapperComponent } from "@site/src/components/WorkerWrapper.mdx"; | ||
import Tabs from "@theme/Tabs"; | ||
import TabItem from "@theme/TabItem"; | ||
|
||
Using workers and pooling within your application can greatly enhance performance and stability. The renderer is processed on the main thread, so any processor intensive tasks are going to cause hangups for users, either delivering a poor UX or in many cases, causing the dreaded `pthread_kill` and crashing the app. | ||
|
||
Implementing workers is relatively straight-forward in development, but you might run into some issues when packaging your build. | ||
|
||
## electron-vite | ||
NB: While Electron / electron-vite officially support ESM, with the way that rollup packages the worker, electron ends up not being able read the worker. You must be using CJS in electron to read the worker modules. Besides, the bytecode plugin for obfuscating your code only works with CJS too. | ||
|
||
In your main file, import the path to your worker (not the wrapper - no wrapper is needed because of rollup) by appending `\*?modulePath` to the end of the path. | ||
|
||
```typescript | ||
import workerPath from './worker?modulePath'; | ||
``` | ||
|
||
During compilation, rollup will now automatically create a separate file for the worker in your main folder output at the given path. The path above resolves, and we can now use this in Piscina at run time. | ||
|
||
Create a pool with Piscina, and use the imported path instead. | ||
|
||
For those using TypeScript, the filename export referenced in the guide is not needed. | ||
|
||
```typescript title="index.ts" | ||
const pool = new Piscina({ | ||
filename: workerPath, | ||
workerData: { | ||
userPath: path.join(app.getPath("userData")), | ||
}, | ||
}); | ||
``` | ||
|
||
Electron-specific modules are NOT accessible inside workers. | ||
|
||
If you need access to information that electron provides, (e.g. `app.getPath("userData")`), you'll need to pass in that data as workerData. | ||
|
||
That's it! You can now use pool like normal in the Piscina docs. | ||
|
||
For further help, reference the worker section of the docs from electron-vite. | ||
|
||
https://electron-vite.org/guide/dev#worker-threads |