easy-web-worker 1.0.5 → 1.0.7

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,57 +1,95 @@
1
- import * as IEasyWebWorker from './EasyWebWorkerTypes';
1
+ import { EasyWebWorkerBody, IWorkerConfig } from './EasyWebWorkerTypes';
2
+ import { CancelablePromise } from 'cancelable-promise-jq';
2
3
  /**
3
- * This is a class to create global-store objects
4
- * @template IPayload - Indicates if your WORKERS messages requires a parameter to be provided, NULL indicates they doesn't
5
- * @template IResult - Indicates if your WORKERS messages has a result... NULL indicates all you messages are Promise<void>
6
- * @param {IEasyWebWorker.EasyWebWorkerBody<IPayload, IResult> | IEasyWebWorker.EasyWebWorkerBody<IPayload, IResult>[]} workerBody -
7
- * this parameter should be a function or set of functions that will become the body of your Web-Worker
8
- * IMPORTANT!! all WORKERS content is gonna be transpiled on run time, so you can not use any variable, method of resource that weren't included into the WORKER.
9
- * the above the reason of why we are injecting all worker context into the MessageBody Callbacks, so,
10
- * you could easily identify what is on the context of your Worker.
11
- * @param {Partial<IEasyWebWorker.IWorkerConfig>} WorkerConfig - You could add extra configuration to your worker,
12
- * consult IWorkerConfig description to have more information
13
- * */
14
- declare class EasyWebWorker<IPayload = null, IResult = void> implements IEasyWebWorker.IEasyWebWorker<IPayload, IResult> {
15
- protected workerBody: IEasyWebWorker.EasyWebWorkerBody<IPayload, IResult> | IEasyWebWorker.EasyWebWorkerBody<IPayload, IResult>[] | string;
4
+ * This is a class to create global-store objects
5
+ * @template IPayload - Indicates if your WORKERS messages requires a parameter to be provided, NULL indicates they doesn't
6
+ * @template IResult - Indicates if your WORKERS messages has a result... NULL indicates all you messages are Promise<void>
7
+ * @param {EasyWebWorkerBody<IPayload, IResult> | EasyWebWorkerBody<IPayload, IResult>[]} workerBody -
8
+ * this parameter should be a function or set of functions that will become the body of your Web-Worker
9
+ * IMPORTANT!! all WORKERS content is gonna be transpiled on run time, so you can not use any variable, method of resource that weren't included into the WORKER.
10
+ * the above the reason of why we are injecting all worker context into the MessageBody Callbacks, so,
11
+ * you could easily identify what is on the context of your Worker.
12
+ * @param {Partial<IWorkerConfig>} WorkerConfig - You could add extra configuration to your worker,
13
+ * consult IWorkerConfig description to have more information
14
+ * */
15
+ export declare class EasyWebWorker<IPayload = null, IResult = void> {
16
+ /**
17
+ * this parameter should be a function or set of functions that will become the body of your Web-Worker
18
+ * IMPORTANT!! all WORKERS content is gonna be transpiled on run time, so you can not use any variable, method of resource that weren't included into the WORKER.
19
+ * the above the reason of why we are injecting all worker context into the MessageBody Callbacks, so,
20
+ * you could easily identify what is on the context of your Worker.
21
+ */
22
+ protected workerBody: EasyWebWorkerBody<IPayload, IResult> | EasyWebWorkerBody<IPayload, IResult>[] | string;
16
23
  name: string;
17
- private worker;
24
+ /**
25
+ * @deprecated Directly modifying the worker may lead to unexpected behavior. Use it only if you know what you are doing.
26
+ */
27
+ worker: Worker;
28
+ /**
29
+ * These where send to the worker but not yet resolved
30
+ */
18
31
  private messagesQueue;
32
+ /**
33
+ * This is the URL of the worker file
34
+ */
19
35
  workerUrl: string;
20
- protected scripts: string[];
36
+ /**
37
+ * This is the list of scripts that will be imported into the worker
38
+ */
39
+ scripts: string[];
40
+ /**
41
+ * This is the callback that will be executed when the worker throws an error
42
+ */
43
+ onWorkerError: (error: ErrorEvent) => void;
21
44
  protected get isExternalWorkerFile(): boolean;
22
- constructor(workerBody: IEasyWebWorker.EasyWebWorkerBody<IPayload, IResult> | IEasyWebWorker.EasyWebWorkerBody<IPayload, IResult>[] | string, { scripts, name, }?: Partial<IEasyWebWorker.IWorkerConfig>);
45
+ constructor(
46
+ /**
47
+ * this parameter should be a function or set of functions that will become the body of your Web-Worker
48
+ * IMPORTANT!! all WORKERS content is gonna be transpiled on run time, so you can not use any variable, method of resource that weren't included into the WORKER.
49
+ * the above the reason of why we are injecting all worker context into the MessageBody Callbacks, so,
50
+ * you could easily identify what is on the context of your Worker.
51
+ */
52
+ workerBody: EasyWebWorkerBody<IPayload, IResult> | EasyWebWorkerBody<IPayload, IResult>[] | string,
53
+ /**
54
+ * You could import scripts into your worker, this is useful if you want to use external libraries
55
+ */
56
+ { scripts, name, onWorkerError }?: Partial<IWorkerConfig>);
23
57
  private RemoveMessageFromQueue;
58
+ /**
59
+ * Categorizes the worker response and executes the corresponding callback
60
+ */
24
61
  private executeMessageCallback;
25
62
  protected getWorkerUrl(): string;
26
63
  protected createWorker(): Worker;
27
64
  /**
28
- * Disable the resolve of all current WebWorkers messages, no any of the current messages gonna call onProgress callback, neither promise.resolve
29
- */
30
- cancelAll(): void;
31
- /**
32
- * Send a message to the worker queue
33
- * @param {IPayload} payload - whatever json data you want to send to the worker
34
- * @returns {IMessagePromise<IResult>} generated defer that will be resolved when the message completed
35
- */
36
- send(...payload: IPayload extends null ? [null?] : [IPayload]): IEasyWebWorker.IMessagePromise<IResult>;
37
- /**
38
- * Web Workers works as a QUEUE, sometimes a new message actually would be the only message that you'll want to resolve...
39
- * you could use OVERRIDE to that purpose.
40
- * @param {IPayload} payload - whatever json data you want to send to the worker
41
- * @returns {IMessagePromise<IResult>} generated defer that will be resolved when the message completed
42
- */
43
- override(...payload: IPayload extends null ? [null?] : [IPayload]): IEasyWebWorker.IMessagePromise<IResult>;
44
- /**
45
- * Web Workers works as a QUEUE, sometimes a new message actually would be the only message that you'll want to resolve...
46
- * you could use OVERRIDE to that purpose.
47
- * @param {IPayload} payload - whatever json data you want to send to the worker
48
- * @returns {IMessagePromise<IResult>} generated defer that will be resolved when the message completed
49
- */
50
- overrideAfterCurrent(...payload: IPayload extends null ? [null?] : [IPayload]): IEasyWebWorker.IMessagePromise<IResult>;
51
- /**
52
- * This method will remove the WebWorker and the BlobUrl
53
- */
65
+ * Terminates the worker and remove all messages from the queue
66
+ * Execute the cancel callback of each message in the queue if provided
67
+ * @param {unknown} reason - reason why the worker was terminated
68
+ */
69
+ cancelAll(reason?: unknown): void;
70
+ /**
71
+ * Send a message to the worker queue
72
+ * @param {IPayload} payload - whatever json data you want to send to the worker
73
+ * @returns {IMessagePromise<IResult>} generated defer that will be resolved when the message completed
74
+ */
75
+ send: IPayload extends null ? () => CancelablePromise<IResult> : (payload: IPayload) => CancelablePromise<IResult>;
76
+ /**
77
+ * This method terminate all current messages and send a new one to the worker queue
78
+ * @param {IPayload} payload - whatever json data you want to send to the worker, should be serializable
79
+ * @param {unknown} reason - reason why the worker was terminated
80
+ * @returns {IMessagePromise<IResult>} generated defer that will be resolved when the message completed
81
+ */
82
+ override: IPayload extends null ? (reason?: unknown) => CancelablePromise<IResult> : (payload: IPayload, reason?: unknown) => CancelablePromise<IResult>;
83
+ /**
84
+ * This method will alow the current message to be completed and send a new one to the worker queue after it, all the messages after the current one will be canceled
85
+ * @param {IPayload} payload - whatever json data you want to send to the worker should be serializable
86
+ * @param {unknown} reason - reason why the worker was terminated
87
+ * @returns {IMessagePromise<IResult>} generated defer that will be resolved when the message completed
88
+ */
89
+ overrideAfterCurrent: IPayload extends null ? (reason?: unknown) => CancelablePromise<IResult> : (payload: IPayload, reason?: unknown) => CancelablePromise<IResult>;
90
+ /**
91
+ * This method will remove the WebWorker and the BlobUrl
92
+ */
54
93
  dispose(): void;
55
94
  }
56
95
  export default EasyWebWorker;
57
- //# s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