easy-web-worker 6.1.0 → 6.1.1
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 +14 -14
- package/lib/EasyWebWorkerFixtures.d.ts +1 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -30,7 +30,7 @@ const worker = createEasyWebWorker(({ onMessage }) => {
|
|
|
30
30
|
* The payload includes whatever parameters are sent from the main thread
|
|
31
31
|
*/
|
|
32
32
|
const { payload } = message;
|
|
33
|
-
const result = fibonacci(payload.
|
|
33
|
+
const result = fibonacci(payload.index);
|
|
34
34
|
|
|
35
35
|
message.resolve(result);
|
|
36
36
|
//message.reject(); // or reject
|
|
@@ -44,7 +44,7 @@ Then, for sending a message to the worker:
|
|
|
44
44
|
/**
|
|
45
45
|
* This returns a CancelablePromise
|
|
46
46
|
*/
|
|
47
|
-
await worker.send(40);
|
|
47
|
+
await worker.send({ index: 40 });
|
|
48
48
|
```
|
|
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.
|
|
@@ -52,11 +52,11 @@ And that's it! You now have a worker running heavy computation in a real separat
|
|
|
52
52
|
You can also create an easy web worker from a static file, or from a native worker instance:
|
|
53
53
|
|
|
54
54
|
```ts
|
|
55
|
-
const worker = createEasyWebWorker(
|
|
56
|
-
const worker = createEasyWebWorker(new Worker(
|
|
55
|
+
const worker = createEasyWebWorker('./worker.js');
|
|
56
|
+
const worker = createEasyWebWorker(new Worker('./worker.js')); // ƒ Worker() { [native code] }
|
|
57
57
|
|
|
58
|
-
const worker = new EasyWebWorker(
|
|
59
|
-
const worker = new EasyWebWorker(new Worker(
|
|
58
|
+
const worker = new EasyWebWorker('./worker.js');
|
|
59
|
+
const worker = new EasyWebWorker(new Worker('./worker.js')); // ƒ Worker() { [native code] }
|
|
60
60
|
```
|
|
61
61
|
|
|
62
62
|
When working with **static files**, which can offer substantial benefits with web workers, you simply need to create an instance of **StaticEasyWebWorker**.
|
|
@@ -84,15 +84,15 @@ onMessage((message) => {
|
|
|
84
84
|
|
|
85
85
|
/** You can reject the message and send back a reason,
|
|
86
86
|
* if no transfer is necessary just avoid the second parameter */
|
|
87
|
-
message.reject(new Error(
|
|
87
|
+
message.reject(new Error('something happened'));
|
|
88
88
|
|
|
89
|
-
const metadata = { message:
|
|
89
|
+
const metadata = { message: 'progress from inside the worker' };
|
|
90
90
|
|
|
91
91
|
/** You can report progress */
|
|
92
92
|
message.reportProgress(10, metadata, []); // all the methods allows you to send Transferable[]
|
|
93
93
|
|
|
94
94
|
/** You can cancel an operation from within the worker */
|
|
95
|
-
message.cancel(
|
|
95
|
+
message.cancel('the operating was canceled from the worker');
|
|
96
96
|
|
|
97
97
|
/** You can subscribe to the cancellation event of the message,
|
|
98
98
|
* regardless of whether this cancellation is internal or external to the worker.*/
|
|
@@ -117,7 +117,7 @@ onMessage((message) => {
|
|
|
117
117
|
/**
|
|
118
118
|
* For adding specific actions
|
|
119
119
|
*/
|
|
120
|
-
onMessage(
|
|
120
|
+
onMessage('readCSV', (message) => {
|
|
121
121
|
// do something
|
|
122
122
|
});
|
|
123
123
|
```
|
|
@@ -182,7 +182,7 @@ await createEasyWebWorker<null, string>(({ onMessage }) => {
|
|
|
182
182
|
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
183
|
|
|
184
184
|
```ts
|
|
185
|
-
const message =
|
|
185
|
+
const message = 'Hello';
|
|
186
186
|
|
|
187
187
|
await createEasyWebWorker<null, string>(
|
|
188
188
|
({ onMessage }, context) => {
|
|
@@ -193,7 +193,7 @@ await createEasyWebWorker<null, string>(
|
|
|
193
193
|
{
|
|
194
194
|
primitiveParameters: [message],
|
|
195
195
|
}
|
|
196
|
-
).send(
|
|
196
|
+
).send('hello!');
|
|
197
197
|
```
|
|
198
198
|
|
|
199
199
|
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 +460,9 @@ const worker = createEasyWebWorker<string, string>(({ onMessage }) => {
|
|
|
460
460
|
});
|
|
461
461
|
});
|
|
462
462
|
|
|
463
|
-
const messagePromise = worker.send(
|
|
463
|
+
const messagePromise = worker.send('Hello!');
|
|
464
464
|
|
|
465
|
-
worker.reboot(
|
|
465
|
+
worker.reboot('Worker was restarted');
|
|
466
466
|
|
|
467
467
|
// The message promise will be rejected with the reason 'Worker was restarted'
|
|
468
468
|
```
|
|
@@ -4,6 +4,7 @@ export declare const generatedId: () => string;
|
|
|
4
4
|
* this template is used to create dynamic workers from strings templates
|
|
5
5
|
* PLEASE KEEP THIS FUNCTION UPDATED WITH THE LATEST VERSION OF THE StaticEasyWebWorker CLASS
|
|
6
6
|
* [IMPORTANT] Avoid ";" at the end of the template
|
|
7
|
+
* for transpiling you can use https://johnny-quesada-developer.github.io/easy-web-workers-example/?example=typescript
|
|
7
8
|
* for minifying the template, please use the following options:
|
|
8
9
|
* - remove comments
|
|
9
10
|
* - remove types
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "easy-web-worker",
|
|
3
|
-
"version": "6.1.
|
|
3
|
+
"version": "6.1.1",
|
|
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",
|