easy-web-worker 2.0.0 → 2.0.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 +64 -15
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -2,43 +2,92 @@
|
|
|
2
2
|
|
|
3
3
|

|
|
4
4
|
|
|
5
|
-
Hello and
|
|
5
|
+
Hello and welcome to **easy-web-worker** with [cancelable-promise-jq](https://www.npmjs.com/package/cancelable-promise-jq) – your go-to solution for seamless **Web Workers** integration, now enhanced with cancelable promises! 🚀
|
|
6
6
|
|
|
7
|
-
|
|
7
|
+
[Web Workers](https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API/Using_web_workers) are a native tool provided by **JavaScript**, allowing you to use them within your favorite framework like **React**, **Angular**, **Vue**, and others, or with pure **JavaScript** and **TypeScript**.
|
|
8
|
+
|
|
9
|
+
Check out the running example with **React** and **TypeScript** at [CODEPEN](https://codepen.io/johnnynabetes/pen/wvOvygW?editors=0010); let's explore the capabilities of JavaScript's concurrent processing with Web Workers!"
|
|
10
|
+
|
|
11
|
+
### Creating a web worker never was easier!
|
|
8
12
|
|
|
9
13
|
```ts
|
|
14
|
+
/**
|
|
15
|
+
* The callback parameter will be the body of the worker
|
|
16
|
+
*/
|
|
10
17
|
const worker = new EasyWebWorker((easyWorker) => {
|
|
11
|
-
|
|
18
|
+
/**
|
|
19
|
+
* Inside the worker we have to define an action when onMessage
|
|
20
|
+
*/
|
|
12
21
|
easyWorker.onMessage((message) => {
|
|
22
|
+
/**
|
|
23
|
+
* The payload includes whatever parameters are sent from the main thread
|
|
24
|
+
*/
|
|
13
25
|
const { payload } = message;
|
|
14
26
|
|
|
15
|
-
console.log(payload); // Hello Worker!
|
|
16
|
-
|
|
17
|
-
// do something heavy...
|
|
18
|
-
|
|
19
|
-
message.reportProgress(50); // report progress
|
|
20
|
-
|
|
21
27
|
message.resolve();
|
|
22
28
|
//message.reject(); // or reject
|
|
23
29
|
});
|
|
24
30
|
});
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
Then, for sending a message to the worker:
|
|
25
34
|
|
|
26
|
-
|
|
35
|
+
```ts
|
|
36
|
+
/**
|
|
37
|
+
* This returns a CancelablePromise
|
|
38
|
+
*/
|
|
27
39
|
await worker.send('Hello Worker!');
|
|
28
40
|
```
|
|
29
41
|
|
|
30
|
-
|
|
42
|
+
And that's it! You now have a worker running heavy computation in a real separate thread, with real asynchronous programming in JavaScript.
|
|
43
|
+
|
|
44
|
+
You can also create an easy web worker from a static file, or from a native worker instance.
|
|
45
|
+
|
|
46
|
+
```ts
|
|
47
|
+
// static file
|
|
48
|
+
const worker = new EasyWebWorker('./worker.js');
|
|
49
|
+
|
|
50
|
+
const worker = new EasyWebWorker(new Worker('./worker.js')); // ƒ Worker() { [native code] }
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
When working with **static files**, which can offer substantial benefits with web workers, you simply need to create an instance of **StaticEasyWebWorker**.
|
|
54
|
+
|
|
55
|
+
The **StaticEasyWebWorker** provides an interface to continue working with [cancelable-promise-jq](https://www.npmjs.com/package/cancelable-promise-jq) and build more complex APIs within your worker.
|
|
31
56
|
|
|
32
57
|
```ts
|
|
33
|
-
|
|
34
|
-
|
|
58
|
+
const easyWorker = new StaticEasyWebWorker();
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* For adding a default onMessage
|
|
62
|
+
*/
|
|
63
|
+
easyWorker.onMessage((message) => {
|
|
64
|
+
// do something
|
|
65
|
+
|
|
66
|
+
message.reportProgress(10); // report progress
|
|
67
|
+
|
|
68
|
+
/** Take action when the message is canceled by the main thread*/
|
|
69
|
+
message.onCancel(() => {});
|
|
70
|
+
|
|
71
|
+
message.resolve();
|
|
72
|
+
});
|
|
35
73
|
|
|
36
|
-
|
|
37
|
-
|
|
74
|
+
/**
|
|
75
|
+
* For adding specific actions
|
|
76
|
+
*/
|
|
77
|
+
easyWorker.onMessage('readCSV', (message) => {
|
|
38
78
|
// do something
|
|
79
|
+
|
|
80
|
+
message.reportProgress(20); // report progress
|
|
81
|
+
|
|
82
|
+
/** Take action when the message is canceled by the main thread*/
|
|
83
|
+
message.onCancel(() => {});
|
|
84
|
+
|
|
85
|
+
message.resolve();
|
|
39
86
|
});
|
|
40
87
|
```
|
|
41
88
|
|
|
89
|
+
**easy-web-worker** is designed to enhance the capabilities of the **Worker** class by integrating a pattern of cancelable promises from the [cancelable-promise-jq](https://www.npmjs.com/package/cancelable-promise-jq) library. For straightforward tasks, it simplifies the process by eliminating the need to configure webpack or other bundlers. And for more complex requirements, the **StaticEasyWebWorker** class allows the integration of easy worker and cancelable promises capabilities into your static workers.
|
|
90
|
+
|
|
42
91
|
Start enhancing your applications with robust, cancelable promises and easy web worker integration today! 🌐
|
|
43
92
|
|
|
44
93
|
Experience it in action with a [Live Example featuring text-diff](https://johnny-quesada-developer.github.io/easy-web-workers-example/) 📘.
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "easy-web-worker",
|
|
3
|
-
"version": "2.0.
|
|
3
|
+
"version": "2.0.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",
|