-
-
Notifications
You must be signed in to change notification settings - Fork 163
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Updated Documentation for worker import URL #424
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,11 +15,11 @@ Uses web workers in the browser, `worker_threads` in node 12+ and [`tiny-worker` | |
|
||
### Features | ||
|
||
* First-class support for **async functions** & **observables** | ||
* Write code once, run it **on all platforms** | ||
* Manage bulk task executions with **thread pools** | ||
* Use **require()** and **import**/**export** in workers | ||
* Works great with **webpack** | ||
- First-class support for **async functions** & **observables** | ||
- Write code once, run it **on all platforms** | ||
- Manage bulk task executions with **thread pools** | ||
- Use **require()** and **import**/**export** in workers | ||
- Works great with **webpack** | ||
|
||
### Version 0.x | ||
|
||
|
@@ -31,7 +31,7 @@ You can find the old version 0.12 of threads.js on the [`v0` branch](https://git | |
npm install threads tiny-worker | ||
``` | ||
|
||
*You only need to install the `tiny-worker` package to support node.js < 12. It's an optional dependency and used as a fallback if `worker_threads` are not available.* | ||
_You only need to install the `tiny-worker` package to support node.js < 12. It's an optional dependency and used as a fallback if `worker_threads` are not available._ | ||
|
||
## Platform support | ||
|
||
|
@@ -42,7 +42,7 @@ npm install threads tiny-worker | |
|
||
Running code using threads.js in node works out of the box. | ||
|
||
Note that we wrap the native `Worker`, so `new Worker("./foo/bar")` will resolve the path relative to the module that calls it, not relative to the current working directory. | ||
Note that we wrap the native `Worker`, so `new Worker(new URL("./foo/bar", import.meta.url))` will resolve the path relative to the module that calls it, not relative to the current working directory. | ||
|
||
That aligns it with the behavior when bundling the code with webpack or parcel. | ||
|
||
|
@@ -147,26 +147,26 @@ Everything else should work out of the box. | |
|
||
```js | ||
// master.js | ||
import { spawn, Thread, Worker } from "threads" | ||
import { spawn, Thread, Worker } from "threads"; | ||
|
||
const auth = await spawn(new Worker("./workers/auth")) | ||
const hashed = await auth.hashPassword("Super secret password", "1234") | ||
const auth = await spawn(new Worker(new URL("./workers/auth", import.meta.url))); | ||
const hashed = await auth.hashPassword("Super secret password", "1234"); | ||
|
||
console.log("Hashed password:", hashed) | ||
console.log("Hashed password:", hashed); | ||
|
||
await Thread.terminate(auth) | ||
await Thread.terminate(auth); | ||
``` | ||
|
||
```js | ||
// workers/auth.js | ||
import sha256 from "js-sha256" | ||
import { expose } from "threads/worker" | ||
import sha256 from "js-sha256"; | ||
import { expose } from "threads/worker"; | ||
|
||
expose({ | ||
hashPassword(password, salt) { | ||
return sha256(password + salt) | ||
} | ||
}) | ||
return sha256(password + salt); | ||
}, | ||
}); | ||
Comment on lines
+168
to
+169
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. And the dangling comma? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. those are my prettifyjs settings... There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The rest of the code does not have semicolons, except for export { registerSerializer } from "./common"
export * from "./master/index"
export { expose } from "./worker/index"
export { DefaultSerializer, JsonSerializable, Serializer, SerializerImplementation } from "./serializers"
export { Transfer, TransferDescriptor } from "./transferable"
export { ExposedToThreadType as ExposedAs } from "./master/spawn";
export { QueuedTask } from "./master/pool"; |
||
``` | ||
|
||
### spawn() | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What's up with the semicolons?