easy-web-worker 6.0.2 → 6.0.3
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 +62 -27
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -16,7 +16,7 @@ Check out the running example with **React** and **TypeScript** at [CODEPEN](htt
|
|
|
16
16
|
/**
|
|
17
17
|
* The callback parameter will be the body of the worker
|
|
18
18
|
*/
|
|
19
|
-
const worker = createEasyWebWorker((
|
|
19
|
+
const worker = createEasyWebWorker(({ onMessage }) => {
|
|
20
20
|
const fibonacci = (n) => {
|
|
21
21
|
if (n <= 1) return n;
|
|
22
22
|
return fibonacci(n - 1) + fibonacci(n - 2);
|
|
@@ -25,7 +25,7 @@ const worker = createEasyWebWorker((easyWorker) => {
|
|
|
25
25
|
/**
|
|
26
26
|
* Inside the worker we have to define an action when onMessage
|
|
27
27
|
*/
|
|
28
|
-
|
|
28
|
+
onMessage((message) => {
|
|
29
29
|
/**
|
|
30
30
|
* The payload includes whatever parameters are sent from the main thread
|
|
31
31
|
*/
|
|
@@ -66,12 +66,12 @@ The **StaticEasyWebWorker** provides an interface to continue working with [canc
|
|
|
66
66
|
From inside your worker, the message callbacks receive a message that includes multiple methods and functions. You can use these to communicate back with the main thread, or to subscribe to and react to the lifecycle of a worker.
|
|
67
67
|
|
|
68
68
|
```ts
|
|
69
|
-
const
|
|
69
|
+
const { onMessage } = new StaticEasyWebWorker();
|
|
70
70
|
|
|
71
71
|
/**
|
|
72
72
|
* For adding a default onMessage
|
|
73
73
|
*/
|
|
74
|
-
|
|
74
|
+
onMessage((message) => {
|
|
75
75
|
/** Your message receives a payload,
|
|
76
76
|
* which is any information sent from the main thread.*/
|
|
77
77
|
const { payload } = message;
|
|
@@ -117,7 +117,7 @@ easyWorker.onMessage((message) => {
|
|
|
117
117
|
/**
|
|
118
118
|
* For adding specific actions
|
|
119
119
|
*/
|
|
120
|
-
|
|
120
|
+
onMessage("readCSV", (message) => {
|
|
121
121
|
// do something
|
|
122
122
|
});
|
|
123
123
|
```
|
|
@@ -137,15 +137,15 @@ For a comprehensive understanding, watch our informative [introduction video](ht
|
|
|
137
137
|
Creating a new worker is as simple as
|
|
138
138
|
|
|
139
139
|
```TS
|
|
140
|
-
const backgroundWorker =
|
|
141
|
-
|
|
140
|
+
const backgroundWorker = createEasyWebWorker<string, string>(({ onMessage }) => {
|
|
141
|
+
onMessage((message) => {
|
|
142
142
|
const { payload } = message;
|
|
143
143
|
|
|
144
144
|
message.resolve(`this is a message from the worker: ${payload}`);
|
|
145
145
|
});
|
|
146
146
|
|
|
147
147
|
// you could also define and send specific methods which allow you to create a better structured API
|
|
148
|
-
|
|
148
|
+
onMessage<number, number>('doSomething', (message) => {
|
|
149
149
|
const { payload } = message;
|
|
150
150
|
|
|
151
151
|
message.resolve(payload + 2);
|
|
@@ -171,13 +171,29 @@ Take into consideration that the _workerBody_ is a template to create a worker i
|
|
|
171
171
|
```TS
|
|
172
172
|
const message = 'Hello';
|
|
173
173
|
|
|
174
|
-
await
|
|
175
|
-
|
|
174
|
+
await createEasyWebWorker<null, string>(({ onMessage }) => {
|
|
175
|
+
onMessage((message) => {
|
|
176
176
|
|
|
177
177
|
message.resolve(message); // THIS WILL PRODUCE AND ERROR!! the variable *message* will not exist in Worker-Scope.
|
|
178
178
|
});
|
|
179
179
|
}).send('hello!');
|
|
180
|
+
```
|
|
180
181
|
|
|
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
|
+
|
|
184
|
+
```ts
|
|
185
|
+
const message = "Hello";
|
|
186
|
+
|
|
187
|
+
await createEasyWebWorker<null, string>(
|
|
188
|
+
({ onMessage }, context) => {
|
|
189
|
+
const [message] = context.primitiveParameters;
|
|
190
|
+
|
|
191
|
+
console.log(message); // "hello!" // 👍 it works!
|
|
192
|
+
},
|
|
193
|
+
{
|
|
194
|
+
primitiveParameters: [message],
|
|
195
|
+
}
|
|
196
|
+
).send("hello!");
|
|
181
197
|
```
|
|
182
198
|
|
|
183
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,
|
|
@@ -186,7 +202,7 @@ You are just allowed to send information to Workers by messages, and vice versa
|
|
|
186
202
|
|
|
187
203
|
## IEasyWebWorkerMessage<IPayload = null, IResult = void>
|
|
188
204
|
|
|
189
|
-
When you defined an onMessage callback in your
|
|
205
|
+
When you defined an onMessage callback in your **Worker**, this will receive all messages from the **send** method:
|
|
190
206
|
|
|
191
207
|
```TS
|
|
192
208
|
easyWorker.onMessage((message) => {
|
|
@@ -228,15 +244,17 @@ onProgress Is gonna be executed every time you call _message.reportProgress_ ins
|
|
|
228
244
|
As _WorkerBody_ are just templates, you could reuse them on other _Workers_, or use them as plugins for your _Workers_. Let's see:
|
|
229
245
|
|
|
230
246
|
```TS
|
|
231
|
-
const
|
|
247
|
+
const reusableWorkerSegment: EasyWebWorkerBody = ({ onMessage, close, importScripts }, context) => {
|
|
232
248
|
context.doSomething = () => Promise.resolve('This is a plugin example');
|
|
233
249
|
};
|
|
234
250
|
|
|
235
|
-
const
|
|
251
|
+
const reusableWorkerSegment = await createEasyWebWorker([
|
|
252
|
+
reusableWorkerSegment,
|
|
253
|
+
({ onMessage }, context) => onMessage(async (message) => {
|
|
236
254
|
// context will have all stuff we added on other plugins
|
|
237
|
-
const
|
|
255
|
+
const result = await context.doSomething();
|
|
238
256
|
|
|
239
|
-
message.resolve(
|
|
257
|
+
message.resolve(result);
|
|
240
258
|
})]).send();
|
|
241
259
|
|
|
242
260
|
```
|
|
@@ -245,18 +263,18 @@ In this way, you could avoid having to create more than once the same template f
|
|
|
245
263
|
|
|
246
264
|
## Importing scripts into your _Workers_
|
|
247
265
|
|
|
248
|
-
Web Workers has this amazing method called importScripts, are you passed an array of strings in the
|
|
266
|
+
Web Workers has this amazing method called importScripts, are you passed an array of strings in the EasyWorker extra configuration, all those files are gonna be imported into your worker.
|
|
249
267
|
|
|
250
268
|
// test.js
|
|
251
269
|
|
|
252
270
|
```TS
|
|
253
271
|
self.message = 'Hello coders!';
|
|
254
|
-
|
|
272
|
+
self.doSomething = () => console.log(self.message);
|
|
255
273
|
```
|
|
256
274
|
|
|
257
275
|
```TS
|
|
258
|
-
await
|
|
259
|
-
|
|
276
|
+
await createEasyWebWorker(({ onMessage }, context) => {
|
|
277
|
+
onMessage((message) => context.doSomething());
|
|
260
278
|
}, {
|
|
261
279
|
scripts: ['http://localhost:3000/test.js'],
|
|
262
280
|
}).send();
|
|
@@ -277,17 +295,34 @@ let's see how to use it:
|
|
|
277
295
|
|
|
278
296
|
```TS
|
|
279
297
|
// this is gonna create the same message structure the runtime Workers
|
|
280
|
-
const
|
|
298
|
+
const { onMessage } = createStaticEasyWebWorker();
|
|
299
|
+
|
|
300
|
+
onMessage((message) => {
|
|
301
|
+
setTimeout(() => {
|
|
302
|
+
message.resolve(200);
|
|
303
|
+
}, 5000);
|
|
304
|
+
});
|
|
305
|
+
|
|
306
|
+
onMessage('action', (message) => {
|
|
281
307
|
setTimeout(() => {
|
|
282
308
|
message.resolve(200);
|
|
283
309
|
}, 5000);
|
|
284
310
|
});
|
|
285
311
|
```
|
|
286
312
|
|
|
313
|
+
By the way, if you're in need of a super simple static worker, just know that the first parameter of createStaticEasyWebWorker is a function which will be used as the default onmessage callback.
|
|
314
|
+
|
|
315
|
+
```ts
|
|
316
|
+
createStaticEasyWebWorker((message) => {
|
|
317
|
+
// this is the default onMessage
|
|
318
|
+
});
|
|
319
|
+
```
|
|
320
|
+
|
|
287
321
|
and in your main thread:
|
|
288
322
|
|
|
289
323
|
```TS
|
|
290
|
-
const worker =
|
|
324
|
+
const worker = createEasyWebWorker<null,number>('./worker.js');
|
|
325
|
+
|
|
291
326
|
await worker.send();
|
|
292
327
|
```
|
|
293
328
|
|
|
@@ -302,8 +337,8 @@ With EasyWebWorker, you can create operations that require heavy concurrency and
|
|
|
302
337
|
* Notice that the structure of the worker remains the same;
|
|
303
338
|
* the only changes are in the configuration parameters of the worker.
|
|
304
339
|
* Take a look below.*/
|
|
305
|
-
const worker = createEasyWebWorker((
|
|
306
|
-
|
|
340
|
+
const worker = createEasyWebWorker(({ onMessage }) => {
|
|
341
|
+
onMessage((message) => {
|
|
307
342
|
const { payload } = message;
|
|
308
343
|
|
|
309
344
|
// heavy computation like fibonacci
|
|
@@ -344,7 +379,7 @@ interface FilterSource {
|
|
|
344
379
|
reportProgress: boolean,
|
|
345
380
|
}
|
|
346
381
|
|
|
347
|
-
const worker =
|
|
382
|
+
const worker = createEasyWebWorker<FilterSource, any[]>(({ onMessage }) => {
|
|
348
383
|
const containsValue = (item: any, filter: string): boolean => {
|
|
349
384
|
const itemKeys = Object.keys(item);
|
|
350
385
|
|
|
@@ -359,7 +394,7 @@ const worker = new EasyWebWorker<FilterSource, any[]>((easyWorker) => {
|
|
|
359
394
|
});
|
|
360
395
|
};
|
|
361
396
|
|
|
362
|
-
|
|
397
|
+
onMessage((message: IEasyWebWorkerMessage<FilterSource, any[]>) => {
|
|
363
398
|
const { payload } = message;
|
|
364
399
|
const { collection, filter = '', reportProgress: countProgress } = payload;
|
|
365
400
|
const { length: collectionLength } = collection;
|
|
@@ -419,8 +454,8 @@ Returns an array of promises that are resolved with the rejection reason provide
|
|
|
419
454
|
Example usage:
|
|
420
455
|
|
|
421
456
|
```typescript
|
|
422
|
-
const worker =
|
|
423
|
-
|
|
457
|
+
const worker = createEasyWebWorker<string, string>(({ onMessage }) => {
|
|
458
|
+
onMessage((message) => {
|
|
424
459
|
message.resolve(`Received message: ${message.payload}`);
|
|
425
460
|
});
|
|
426
461
|
});
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "easy-web-worker",
|
|
3
|
-
"version": "6.0.
|
|
3
|
+
"version": "6.0.3",
|
|
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",
|