easy-web-worker 6.2.0 → 6.2.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.
Files changed (2) hide show
  1. package/README.md +34 -13
  2. package/package.json +1 -1
package/README.md CHANGED
@@ -49,14 +49,35 @@ 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
- const worker = createEasyWebWorker("./worker.js");
56
- const worker = createEasyWebWorker(new Worker("./worker.js")); // ƒ Worker() { [native code] }
55
+ // on vite and typescript
56
+ import workerUrl from './worker?worker&url';
57
57
 
58
- const worker = new EasyWebWorker("./worker.js");
59
- const worker = new EasyWebWorker(new Worker("./worker.js")); // ƒ Worker() { [native code] }
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
+ // if you are using ESM - ECMAScript Modules
71
+ const worker = createEasyWebWorker([
72
+ new Worker(new URL('./worker.js', import.meta.url)),
73
+ ]);
74
+
75
+ // other ways with vanilla js
76
+ const worker = createEasyWebWorker('./worker.js');
77
+ const worker = createEasyWebWorker(new Worker('./worker.js')); // ƒ Worker() { [native code] }
78
+
79
+ const worker = new EasyWebWorker('./worker.js');
80
+ const worker = new EasyWebWorker(new Worker('./worker.js')); // ƒ Worker() { [native code] }
60
81
  ```
61
82
 
62
83
  When working with **static files**, which can offer substantial benefits with web workers, you simply need to create an instance of **StaticEasyWebWorker**.
@@ -84,15 +105,15 @@ onMessage((message) => {
84
105
 
85
106
  /** You can reject the message and send back a reason,
86
107
  * if no transfer is necessary just avoid the second parameter */
87
- message.reject(new Error("something happened"));
108
+ message.reject(new Error('something happened'));
88
109
 
89
- const metadata = { message: "progress from inside the worker" };
110
+ const metadata = { message: 'progress from inside the worker' };
90
111
 
91
112
  /** You can report progress */
92
113
  message.reportProgress(10, metadata, []); // all the methods allows you to send Transferable[]
93
114
 
94
115
  /** You can cancel an operation from within the worker */
95
- message.cancel("the operating was canceled from the worker");
116
+ message.cancel('the operating was canceled from the worker');
96
117
 
97
118
  /** You can subscribe to the cancellation event of the message,
98
119
  * regardless of whether this cancellation is internal or external to the worker.*/
@@ -117,7 +138,7 @@ onMessage((message) => {
117
138
  /**
118
139
  * For adding specific actions
119
140
  */
120
- onMessage("readCSV", (message) => {
141
+ onMessage('readCSV', (message) => {
121
142
  // do something
122
143
  });
123
144
  ```
@@ -182,7 +203,7 @@ await createEasyWebWorker<null, string>(({ onMessage }) => {
182
203
  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
204
 
184
205
  ```ts
185
- const message = "Hello";
206
+ const message = 'Hello';
186
207
 
187
208
  await createEasyWebWorker<null, string>(
188
209
  ({ onMessage }, context) => {
@@ -193,7 +214,7 @@ await createEasyWebWorker<null, string>(
193
214
  {
194
215
  primitiveParameters: [message],
195
216
  }
196
- ).send("hello!");
217
+ ).send('hello!');
197
218
  ```
198
219
 
199
220
  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 +481,9 @@ const worker = createEasyWebWorker<string, string>(({ onMessage }) => {
460
481
  });
461
482
  });
462
483
 
463
- const messagePromise = worker.send("Hello!");
484
+ const messagePromise = worker.send('Hello!');
464
485
 
465
- worker.reboot("Worker was restarted");
486
+ worker.reboot('Worker was restarted');
466
487
 
467
488
  // The message promise will be rejected with the reason 'Worker was restarted'
468
489
  ```
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "easy-web-worker",
3
- "version": "6.2.0",
3
+ "version": "6.2.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",