easy-web-worker 1.0.4 → 1.0.6

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 CHANGED
@@ -1,11 +1,12 @@
1
1
  # easy-web-worker
2
+
2
3
  This is a package to easily create and handle Workers, both run time and static .js workers files
3
4
 
4
5
  ## Creating a simple Web Worker
5
6
 
6
7
  Creating a new worker is as simple as
7
8
 
8
- ```
9
+ ```TS
9
10
  const backgroundWorker = new EasyWebWorker<string, string>((easyWorker) => {
10
11
  easyWorker.onMessage((message) => {
11
12
  const { payload } = message;
@@ -17,15 +18,16 @@ const backgroundWorker = new EasyWebWorker<string, string>((easyWorker) => {
17
18
  const messsageResult = await backgroundWorker.send('hello!');
18
19
  ```
19
20
 
20
- ### Important notes:
21
+ ### Important notes:
21
22
 
22
23
  EasyWebWorker<IPayload, IResult> has two generic parameters... They will affect the typing of the send() and response() methods.
23
- * If IResult is null, the *resolve* method will not require parameters
24
- * If IPayload is null, the *send* method will not require parameters
25
24
 
26
- Take into consideration that the *workerBody* is a template to create a worker in run time, so you'll not be able to use anything outside of the Worker-Scope
25
+ - If IResult is null, the _resolve_ method will not require parameters
26
+ - If IPayload is null, the _send_ method will not require parameters
27
27
 
28
- ```
28
+ Take into consideration that the _workerBody_ is a template to create a worker in run time, so you'll not be able to use anything outside of the Worker-Scope
29
+
30
+ ```TS
29
31
  const message = 'Hello';
30
32
 
31
33
  await new EasyWebWorker<null, string>((easyWorker) => {
@@ -38,12 +40,14 @@ await new EasyWebWorker<null, string>((easyWorker) => {
38
40
  ```
39
41
 
40
42
  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,
41
- If you need t to send data to the worker, please define IPayload while creating a worker. *new EasyWebWorker<IPayload>(*
43
+ If you need t to send data to the worker, please define IPayload while creating a worker. _new EasyWebWorker<IPayload>(_
42
44
  You are just allowed to send information to Workers by messages, and vice versa
43
45
 
44
46
  ## IEasyWebWorkerMessage<IPayload = null, IResult = void>
45
- When you defined an onMessage callback in your *Worker*, this will receive all messages from the *send* method:
46
- ```
47
+
48
+ When you defined an onMessage callback in your _Worker_, this will receive all messages from the _send_ method:
49
+
50
+ ```TS
47
51
  easyWorker.onMessage((message) => {
48
52
  // the *message* will be strongly typed with TS
49
53
 
@@ -59,21 +63,22 @@ easyWorker.onMessage((message) => {
59
63
  ```
60
64
 
61
65
  ## onProgress
66
+
62
67
  Let say you are performing some heavy process in your worker, but you still wanted to implement some kind of progress bar in the main thread... you could add an onProgress callback.
63
68
 
64
- ```
69
+ ```TS
65
70
  await worker.send().onProgress((progress: number) => {
66
71
  // change some progress bar percentage
67
72
  }).then(doSomething);
68
73
  ```
69
74
 
70
- onProgress Is gonna be executed every time you call *message.reportProgress* inside the worker... the cool part here is that the *reportProgress* is not gonna finish the main promise returned by the *send* method.
75
+ onProgress Is gonna be executed every time you call _message.reportProgress_ inside the worker... the cool part here is that the _reportProgress_ is not gonna finish the main promise returned by the _send_ method.
71
76
 
72
77
  ## Having multiple Worker-Templates
73
78
 
74
- As *WorkerBody* are just templates, you could reuse them on other *Workers*, or use them as plugins for your *Workers*. Let's see:
79
+ As _WorkerBody_ are just templates, you could reuse them on other _Workers_, or use them as plugins for your _Workers_. Let's see:
75
80
 
76
- ```
81
+ ```TS
77
82
  const WorkerPluggin: EasyWebWorkerBody = (_easyWorker, context) => {
78
83
  context.doSomething = () => Promise.resolve('This is a plugin example');
79
84
  };
@@ -89,17 +94,18 @@ const plugginMessage = await new EasyWebWorker([WorkerPluggin, (easyWorker, cont
89
94
 
90
95
  In this way, you could avoid having to create more than once the same template for your worker.
91
96
 
92
- ## Importing scripts into your *Workers*
97
+ ## Importing scripts into your _Workers_
93
98
 
94
99
  Web Workers has this amazing method called importScripts, are you passed an array of strings in the EeasyWorker extra configuration, all those files are gonna be imported into your worker.
95
100
 
96
101
  // test.js
97
- ```
102
+
103
+ ```TS
98
104
  self.message = 'Hello coders!';
99
105
  selft.doSomething = () => console.log(self.message);
100
106
  ```
101
107
 
102
- ```
108
+ ```TS
103
109
  await new EasyWebWorker((easyWorker, context) => {
104
110
  easyWorker.onMessage((message) => context.doSomething());
105
111
  }, {
@@ -108,22 +114,23 @@ await new EasyWebWorker((easyWorker, context) => {
108
114
 
109
115
  ```
110
116
 
111
- This is a very simple example, but you could import a whole library into your worker, as *JQUERY*, *Bluebird* for example
117
+ This is a very simple example, but you could import a whole library into your worker, as _JQUERY_, _Bluebird_ for example
112
118
 
113
119
  ## StaticEasyWebWorker
114
120
 
115
- If you want to create a *Worker* with a static .js file and don't want to lose the structure of messages and promises and the onProgress callback from the library... you could use *StaticEasyWebWorker<IPayload = null, IResult = void>* directly in your Worker.
121
+ If you want to create a _Worker_ with a static .js file and don't want to lose the structure of messages and promises and the onProgress callback from the library... you could use _StaticEasyWebWorker<IPayload = null, IResult = void>_ directly in your Worker.
116
122
 
117
- trust me, talking about performance it's going to be the same. but may if you are trying to create something very complex and huge into a *Worker*... OK, a static js file could be a good option.
123
+ trust me, talking about performance it's going to be the same. but may if you are trying to create something very complex and huge into a _Worker_... OK, a static js file could be a good option.
118
124
 
119
125
  Workers are gonna work just as the javascript you have in your main thread, but into another thread, so, the user experience could improve!!
120
126
 
121
- let's see how to use it:
127
+ let's see how to use it:
122
128
 
123
129
  // worker.js
124
130
  // This is gonna be the content of your worker
125
- // onMessage Callback is gonna receive all *send* method calls.
126
- ```
131
+ // onMessage Callback is gonna receive all _send_ method calls.
132
+
133
+ ```TS
127
134
  const onMessageCallback = (message: IEasyWebWorkerMessage<null, number>) => {
128
135
  setTimeout(() => {
129
136
  message.resolve(200);
@@ -135,18 +142,19 @@ new StaticEasyWebWorker<null, number>(onMessageCallback);
135
142
  ```
136
143
 
137
144
  and in your main thread:
138
- ```
145
+
146
+ ```TS
139
147
  const worker = new EasyWebWorker<null,number>('http://localhost:3000/worker.js');
140
148
  await worker.send();
141
149
  ```
142
150
 
143
- Super easy right?
151
+ Super easy right?
144
152
 
145
- ## Want to see more?
153
+ ## Want to see more?
146
154
 
147
155
  Here is an example of how you could easily create data filter into a Worker, to avoid performing loops process into the main thread that could end affecting user experience.
148
156
 
149
- ```
157
+ ```TS
150
158
  interface FilterSource {
151
159
  filter: string,
152
160
  collection: any[],
@@ -195,9 +203,9 @@ const worker = new EasyWebWorker<FilterSource, any[]>((easyWorker) => {
195
203
  });
196
204
  ```
197
205
 
198
- And how to use this?
206
+ And how to use this?
199
207
 
200
- ```
208
+ ```TS
201
209
  worker.send({
202
210
  collection: [{ name: 'julio perez' }, { name: 'carol starling' }, { name: 'goku' }, { name: { firstname: 'johnny' } }],
203
211
  filter: 'johnny',
@@ -215,4 +223,4 @@ the output should be:
215
223
 
216
224
  Of course this is a very tiny array, but is just to give you and idea, actually you also could make fetch requests into workers... give it a try.
217
225
 
218
- *Thanks for reading, hope this help someone*
226
+ _Thanks for reading, hope this help someone_
@@ -1 +1 @@
1
- {"version":3,"file":"EasyWebWorkerFixtures.d.ts","sourceRoot":"","sources":["../src/EasyWebWorkerFixtures.ts"],"names":[],"mappings":"AAGA,eAAO,MAAM,WAAW,QAAO,MACsC,CAAC;AAEtE,eAAO,MAAM,cAAc,cA6C1B,CAAC;AAEF,eAAe,WAAW,CAAC"}
1
+ {"version":3,"file":"EasyWebWorkerFixtures.d.ts","sourceRoot":"","sources":["../src/EasyWebWorkerFixtures.ts"],"names":[],"mappings":"AAGA,eAAO,MAAM,WAAW,QAAO,MACsC,CAAC;AAEtE,eAAO,MAAM,cAAc,cAgC1B,CAAC;AAEF,eAAe,WAAW,CAAC"}
@@ -4,45 +4,32 @@ exports.WorkerTemplate = exports.generatedId = void 0;
4
4
  const generatedId = () => `${new Date().getTime()}${Math.random().toString(36).substr(2, 9)}`;
5
5
  exports.generatedId = generatedId;
6
6
  const WorkerTemplate = () => `
7
- // this code was auto-generated
8
- const easyWorker = new (class
9
- implements IEasyWebWorker.IEasyWorkerInstance<any, any>
10
- {
11
- public onMessageCallback: (
12
- message: IEasyWebWorker.IEasyWebWorkerMessage<any>,
13
- event: MessageEvent<IEasyWebWorker.IMessageData<any>>
14
- ) => void = () => {
7
+ // this code was auto-generated
8
+ const easyWorker = new (class {
9
+ onMessageCallback = () => {
15
10
  // eslint-disable-next-line no-throw-literal
16
11
  throw "you didnt defined a message-callback, please assign a callback by calling IEasyWorkerInstance.onMessage";
17
12
  };
18
13
 
19
- public onMessage = (
20
- callback: (
21
- message: IEasyWebWorker.IEasyWebWorkerMessage<any, any>,
22
- event: MessageEvent<IEasyWebWorker.IMessageData<any>>
23
- ) => void
24
- ): void => {
14
+ onMessage = (callback) => {
25
15
  this.onMessageCallback = callback;
26
16
  };
27
17
  })();
28
18
 
29
- self.onmessage = (event: MessageEvent<any>) => {
19
+ self.onmessage = (event) => {
30
20
  const { messageId, payload } = event.data;
31
21
 
32
22
  // each message should have his own resolution methods
33
- const message = new (class
34
- implements IEasyWebWorker.IEasyWebWorkerMessage<any, any>
35
- {
36
- payload: any = payload;
23
+ const message = new (class {
24
+ payload = payload;
37
25
 
38
- messageId: string = messageId;
26
+ messageId = messageId;
39
27
 
40
- public resolve = (...result: [null?] | [any]) =>
41
- self.postMessage({ messageId, payload: result });
28
+ resolve = (...result) => self.postMessage({ messageId, payload: result });
42
29
 
43
- public reject = (error: string | Error) => this.resolve({ error });
30
+ reject = (error) => this.resolve({ error });
44
31
 
45
- public reportProgress = (progressPercentage: number) =>
32
+ reportProgress = (progressPercentage) =>
46
33
  self.postMessage({ messageId, progressPercentage });
47
34
  })();
48
35
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "easy-web-worker",
3
- "version": "1.0.4",
3
+ "version": "1.0.6",
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/EasyWebWorker.js",
6
6
  "files": [