easy-web-worker 6.2.0 → 6.2.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +41 -13
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -49,14 +49,42 @@ await worker.send(40);
|
|
|
49
49
|
|
|
50
50
|
And that's it! You now have a worker running heavy computation in a real separate thread, with real asynchronous programming in JavaScript.
|
|
51
51
|
|
|
52
|
-
You can also create an easy web worker from a static file, or from a native worker instance:
|
|
52
|
+
You can also create an easy web worker from a static file, or from a native worker instance, here various examples:
|
|
53
53
|
|
|
54
54
|
```ts
|
|
55
|
-
|
|
56
|
-
|
|
55
|
+
// on vite and typescript
|
|
56
|
+
import workerUrl from './worker?worker&url';
|
|
57
57
|
|
|
58
|
-
const
|
|
59
|
-
|
|
58
|
+
const isProduction = import.meta.env.MODE === 'production';
|
|
59
|
+
|
|
60
|
+
const workerSource = isProduction
|
|
61
|
+
? workerUrl
|
|
62
|
+
: new URL('./worker.ts', import.meta.url);
|
|
63
|
+
|
|
64
|
+
const worker = new EasyWebWorker(workerSource, {
|
|
65
|
+
workerOptions: {
|
|
66
|
+
type: 'module',
|
|
67
|
+
},
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
// or
|
|
71
|
+
const worker = createEasyWebWorker(workerSource, {
|
|
72
|
+
workerOptions: {
|
|
73
|
+
type: 'module',
|
|
74
|
+
},
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
// if you are using ESM - ECMAScript Modules
|
|
78
|
+
const worker = createEasyWebWorker([
|
|
79
|
+
new Worker(new URL('./worker.js', import.meta.url)),
|
|
80
|
+
]);
|
|
81
|
+
|
|
82
|
+
// other ways with vanilla js
|
|
83
|
+
const worker = createEasyWebWorker('./worker.js');
|
|
84
|
+
const worker = createEasyWebWorker(new Worker('./worker.js')); // ƒ Worker() { [native code] }
|
|
85
|
+
|
|
86
|
+
const worker = new EasyWebWorker('./worker.js');
|
|
87
|
+
const worker = new EasyWebWorker(new Worker('./worker.js')); // ƒ Worker() { [native code] }
|
|
60
88
|
```
|
|
61
89
|
|
|
62
90
|
When working with **static files**, which can offer substantial benefits with web workers, you simply need to create an instance of **StaticEasyWebWorker**.
|
|
@@ -84,15 +112,15 @@ onMessage((message) => {
|
|
|
84
112
|
|
|
85
113
|
/** You can reject the message and send back a reason,
|
|
86
114
|
* if no transfer is necessary just avoid the second parameter */
|
|
87
|
-
message.reject(new Error(
|
|
115
|
+
message.reject(new Error('something happened'));
|
|
88
116
|
|
|
89
|
-
const metadata = { message:
|
|
117
|
+
const metadata = { message: 'progress from inside the worker' };
|
|
90
118
|
|
|
91
119
|
/** You can report progress */
|
|
92
120
|
message.reportProgress(10, metadata, []); // all the methods allows you to send Transferable[]
|
|
93
121
|
|
|
94
122
|
/** You can cancel an operation from within the worker */
|
|
95
|
-
message.cancel(
|
|
123
|
+
message.cancel('the operating was canceled from the worker');
|
|
96
124
|
|
|
97
125
|
/** You can subscribe to the cancellation event of the message,
|
|
98
126
|
* regardless of whether this cancellation is internal or external to the worker.*/
|
|
@@ -117,7 +145,7 @@ onMessage((message) => {
|
|
|
117
145
|
/**
|
|
118
146
|
* For adding specific actions
|
|
119
147
|
*/
|
|
120
|
-
onMessage(
|
|
148
|
+
onMessage('readCSV', (message) => {
|
|
121
149
|
// do something
|
|
122
150
|
});
|
|
123
151
|
```
|
|
@@ -182,7 +210,7 @@ await createEasyWebWorker<null, string>(({ onMessage }) => {
|
|
|
182
210
|
If you need to pass a primitive parameter to the body of the worker, you can use the **primitiveParameters** configuration. This is an array of values that will be serialized and embedded into the worker's body.
|
|
183
211
|
|
|
184
212
|
```ts
|
|
185
|
-
const message =
|
|
213
|
+
const message = 'Hello';
|
|
186
214
|
|
|
187
215
|
await createEasyWebWorker<null, string>(
|
|
188
216
|
({ onMessage }, context) => {
|
|
@@ -193,7 +221,7 @@ await createEasyWebWorker<null, string>(
|
|
|
193
221
|
{
|
|
194
222
|
primitiveParameters: [message],
|
|
195
223
|
}
|
|
196
|
-
).send(
|
|
224
|
+
).send('hello!');
|
|
197
225
|
```
|
|
198
226
|
|
|
199
227
|
Take a look at Workers API if you don't know yet how they work: https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API,
|
|
@@ -460,9 +488,9 @@ const worker = createEasyWebWorker<string, string>(({ onMessage }) => {
|
|
|
460
488
|
});
|
|
461
489
|
});
|
|
462
490
|
|
|
463
|
-
const messagePromise = worker.send(
|
|
491
|
+
const messagePromise = worker.send('Hello!');
|
|
464
492
|
|
|
465
|
-
worker.reboot(
|
|
493
|
+
worker.reboot('Worker was restarted');
|
|
466
494
|
|
|
467
495
|
// The message promise will be rejected with the reason 'Worker was restarted'
|
|
468
496
|
```
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "easy-web-worker",
|
|
3
|
-
"version": "6.2.
|
|
3
|
+
"version": "6.2.2",
|
|
4
4
|
"description": "This is a library to easily handle and create web-workers, if you are a web developer and never tried this... do not wait any longer! Workers are an amazing option to improve user experience and make your webpages even greater",
|
|
5
5
|
"main": "lib/bundle.js",
|
|
6
6
|
"types": "lib/index.d.ts",
|