You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
> This NPM package has had minimal testing under NodeJS + web-worker.
7
+
> **⚠️ Important**
8
+
>
9
+
> This package may not yet be production-ready. It does work in the browser and in NodeJS using a polyfill like the `web-worker` NPM package. Unit testing coverage currently covers synchronization objects, but not the asynchronous features. It is getting there, though.
9
10
10
11
## Objectives
11
12
@@ -20,7 +21,7 @@ These are the recommended steps to get things going:
20
21
to incoming messages.
21
22
2. Export the tasks worker object.
22
23
3. Create a new instance of `Worker` the way is recommended by your bundler, usually with the syntax
23
-
`new Worker("./myworker.js", impot.meta.url)`. However, this forces you to write the worker in JavaScript, at least
24
+
`new Worker("./my-worker.js", import.meta.url)`. However, this forces you to write the worker in JavaScript, at least
24
25
in Vite-powered projects.
25
26
4. Create a new instance of `AsyncWorker` (from this package) by passing the worker object and the tasks object from
26
27
the previous points.
@@ -81,6 +82,7 @@ This is a 3-step worker. The worker simply waits to be informed which step to r
81
82
This is what needs to be done in order to obtain an object that commands the worker:
82
83
83
84
> **⚡ Important**
85
+
>
84
86
> This example is using TypeScript and the following assumes a Vite-powered project. We are deviating from the
85
87
> recommended way of obtaining a worker because the recommended way requires the worker to be written in JavaScript
86
88
> while in serve mode (`npm run dev`).
@@ -144,17 +146,18 @@ self.onconnect = (ev) => {
144
146
## Bi-Directional Communication
145
147
146
148
> **🕓 TL;DR**
149
+
>
147
150
> It's OK for workers to transmit intermediate results like progress reports and partial results. It is not recommended for the main thread to have to send data to a paused task. Promises in work item objects resolve once the `QueueingOptions.processMessage()` function returns `true`.
148
151
149
152
The default functionality is fine for many cases: A worker task is started, the user interface waits for its
150
153
completion and when the task finishes, the work item's `promise` property spits out the resultant object when awaited.
151
154
152
-
There are also many cases where "interim" communcation between the worker and the UI thread is desired, most commonly:
155
+
There are also many cases where "interim" communication between the worker and the UI thread is desired, most commonly:
153
156
154
157
1. Progress reports
155
158
2. Partial results
156
159
157
-
How can a worker send data while the task is still in progress? By using the provied`post()` function.
160
+
How can a worker send data while the task is still in progress? By using the provided`post()` function.
158
161
159
162
In reality, the functions of the worker's tasks object in the quickstart are simplified. In reality we can re-write
160
163
the tasks object like this:
@@ -282,9 +285,10 @@ To learn about the implementation in the worker side, keep reading.
282
285
283
286
This package provides synchronization objects that use `Atomics` to cross the thread boundary safely.
284
287
285
-
> [!IMPORTANT]
288
+
> **⚡ Important**
289
+
>
286
290
> This implementation uses `Atomics` on `SharedArrayBuffer` objects which demands certain
[security requirements](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/SharedArrayBuffer#security_requirements) in browser environments.
288
292
289
293
### CancellationSource
290
294
@@ -299,7 +303,7 @@ import { CancellationSource, type Token } from '@wjfe/async-workers';
299
303
300
304
function computeSomeStuff(cancelToken?:Token) {
301
305
for (let i =0; i<Number.MAX_SAFE_INTEGER; ++i) {
302
-
// No thowing will be done if cancelToken is undefined.
306
+
// No throwing will be done if cancelToken is undefined.
303
307
CancellationSource.throwIfSignaled(cancelToken);
304
308
...
305
309
}
@@ -409,17 +413,19 @@ Generally speaking, terminated workers should be disposed. Do so as fast as pos
409
413
410
414
## Usage in Node.Js
411
415
412
-
This package, by design, is for use in the browser. However, there is this NPM package called [web-worker](https://www.npmjs.com/package/web-worker)
413
-
that claims to bring the browser API into Node. If this is indeed the case, using these 2 packages together should
414
-
work properly in Node.
416
+
> Since **v0.2.3**
417
+
418
+
This package, by design, is for use in the browser. However, there is this NPM package called [web-worker](https://www.npmjs.com/package/web-worker) that brings the browser API into Node. This collaboration works OK and enables the use of `@wjfe/async-workers` in Node.
419
+
420
+
Generally speaking, `AsyncWorker`, upon construction will duck-type test the provided worker. If it looks like a shared worker, it will initialize as a controller for a shared worker; otherwise, it will initialize as a controller for a dedicated worker. This should enable any `Worker` polyfill, not just `web-worker`.
0 commit comments