easy-web-worker 1.0.0 → 1.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
CHANGED
|
@@ -108,4 +108,111 @@ await new EasyWebWorker((easyWorker, context) => {
|
|
|
108
108
|
|
|
109
109
|
```
|
|
110
110
|
|
|
111
|
-
This is a very simple example, but you could import a whole library into your worker, as *JQUERY*, *Bluebird* for example
|
|
111
|
+
This is a very simple example, but you could import a whole library into your worker, as *JQUERY*, *Bluebird* for example
|
|
112
|
+
|
|
113
|
+
## StaticEasyWebWorker
|
|
114
|
+
|
|
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.
|
|
116
|
+
|
|
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.
|
|
118
|
+
|
|
119
|
+
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
|
+
|
|
121
|
+
let's see how to use it:
|
|
122
|
+
|
|
123
|
+
// worker.js
|
|
124
|
+
// This is gonna be the content of your worker
|
|
125
|
+
// onMessage Callback is gonna receive all *send* method calls.
|
|
126
|
+
```
|
|
127
|
+
const onMessageCallback = (message: IEasyWebWorkerMessage<null, number>) => {
|
|
128
|
+
setTimeout(() => {
|
|
129
|
+
message.resolve(200);
|
|
130
|
+
}, 5000);
|
|
131
|
+
};
|
|
132
|
+
|
|
133
|
+
// this is gonna create the same message structure the runtime Workers
|
|
134
|
+
new StaticEasyWebWorker<null, number>(onMessageCallback);
|
|
135
|
+
```
|
|
136
|
+
|
|
137
|
+
and in your main thread:
|
|
138
|
+
```
|
|
139
|
+
const worker = new EasyWebWorker<null,number>('http://localhost:3000/worker.js');
|
|
140
|
+
await worker.send();
|
|
141
|
+
```
|
|
142
|
+
|
|
143
|
+
Super easy right?
|
|
144
|
+
|
|
145
|
+
## Want to see more?
|
|
146
|
+
|
|
147
|
+
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
|
+
|
|
149
|
+
```
|
|
150
|
+
interface FilterSource {
|
|
151
|
+
filter: string,
|
|
152
|
+
collection: any[],
|
|
153
|
+
reportProgress: boolean,
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
const worker = new EasyWebWorker<FilterSource, any[]>((easyWorker) => {
|
|
157
|
+
const containsValue = (item: any, filter: string): boolean => {
|
|
158
|
+
const itemKeys = Object.keys(item);
|
|
159
|
+
|
|
160
|
+
return itemKeys.some((key) => {
|
|
161
|
+
const prop = item[key] || null;
|
|
162
|
+
|
|
163
|
+
if (typeof prop !== 'string' && Object.keys(prop).length) return containsValue(prop, filter);
|
|
164
|
+
if (prop.toString().replace(/(\r\n|\n|\r)/gm, '').trim().toLowerCase()
|
|
165
|
+
.indexOf(filter) !== -1) return true;
|
|
166
|
+
|
|
167
|
+
return false;
|
|
168
|
+
});
|
|
169
|
+
};
|
|
170
|
+
|
|
171
|
+
easyWorker.onMessage((message: IEasyWebWorkerMessage<FilterSource, any[]>) => {
|
|
172
|
+
const { payload } = message;
|
|
173
|
+
const { collection, filter = '', reportProgress: countProgress } = payload;
|
|
174
|
+
const { length: collectionLength } = collection;
|
|
175
|
+
const result = filter === '' ? collection : [];
|
|
176
|
+
const progressPerItem = collectionLength ? 100 / collectionLength : 0;
|
|
177
|
+
|
|
178
|
+
let currentProgress = 0;
|
|
179
|
+
|
|
180
|
+
if (filter) {
|
|
181
|
+
for (let index = 0; index < collectionLength; index += 1) {
|
|
182
|
+
if (countProgress) {
|
|
183
|
+
currentProgress += progressPerItem;
|
|
184
|
+
message.reportProgress(currentProgress);
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
const item = collection[index];
|
|
188
|
+
|
|
189
|
+
if (containsValue(item, filter)) result.push(item);
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
message.resolve(result);
|
|
194
|
+
});
|
|
195
|
+
});
|
|
196
|
+
```
|
|
197
|
+
|
|
198
|
+
And how to use this?
|
|
199
|
+
|
|
200
|
+
```
|
|
201
|
+
worker.send({
|
|
202
|
+
collection: [{ name: 'julio perez' }, { name: 'carol starling' }, { name: 'goku' }, { name: { firstname: 'johnny' } }],
|
|
203
|
+
filter: 'johnny',
|
|
204
|
+
reportProgress: true,
|
|
205
|
+
}).onProgress((progressPercentage) => console.log(progressPercentage))
|
|
206
|
+
.then((filtered: any[]) => console.log(filtered));
|
|
207
|
+
```
|
|
208
|
+
|
|
209
|
+
the output should be:
|
|
210
|
+
=> 25
|
|
211
|
+
=> 50
|
|
212
|
+
=> 75
|
|
213
|
+
=> 100
|
|
214
|
+
=> [{ name: { firstname: 'johnny' } }]
|
|
215
|
+
|
|
216
|
+
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
|
+
|
|
218
|
+
*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,
|
|
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,QAAO,IA+CjC,CAAC;AAEF,eAAe,WAAW,CAAC"}
|
|
@@ -3,19 +3,19 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
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
|
-
const WorkerTemplate = (
|
|
6
|
+
const WorkerTemplate = () => {
|
|
7
7
|
const easyWorker = new (class {
|
|
8
8
|
constructor() {
|
|
9
9
|
this.onMessageCallback = () => {
|
|
10
10
|
// eslint-disable-next-line no-throw-literal
|
|
11
|
-
throw
|
|
11
|
+
throw "you didnt defined a message-callback, please assign a callback by calling IEasyWorkerInstance.onMessage";
|
|
12
12
|
};
|
|
13
13
|
this.onMessage = (callback) => {
|
|
14
14
|
this.onMessageCallback = callback;
|
|
15
15
|
};
|
|
16
16
|
}
|
|
17
17
|
})();
|
|
18
|
-
//
|
|
18
|
+
// @ts-ignore
|
|
19
19
|
self.onmessage = (event) => {
|
|
20
20
|
const { messageId, payload } = event.data;
|
|
21
21
|
// each message should have his own resolution methods
|
|
@@ -23,9 +23,13 @@ const WorkerTemplate = (self) => {
|
|
|
23
23
|
constructor() {
|
|
24
24
|
this.payload = payload;
|
|
25
25
|
this.messageId = messageId;
|
|
26
|
-
this.resolve = (...result) =>
|
|
26
|
+
this.resolve = (...result) =>
|
|
27
|
+
// @ts-ignore
|
|
28
|
+
self.postMessage({ messageId, payload: result });
|
|
27
29
|
this.reject = (error) => this.resolve({ error });
|
|
28
|
-
this.reportProgress = (progressPercentage) =>
|
|
30
|
+
this.reportProgress = (progressPercentage) =>
|
|
31
|
+
// @ts-ignore
|
|
32
|
+
self.postMessage({ messageId, progressPercentage });
|
|
29
33
|
}
|
|
30
34
|
})();
|
|
31
35
|
easyWorker.onMessageCallback(message, event);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "easy-web-worker",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.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/EasyWebWorker.js",
|
|
6
6
|
"files": [
|