@xylabs/threads 5.0.94 → 5.0.96
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 +348 -214
- package/dist/browser/{index-browser-CaVWHLzx.d.ts → index-browser-PIBA6s8X.d.ts} +2 -2
- package/dist/browser/index-browser.d.ts +3 -3
- package/dist/browser/index-browser.mjs +18 -18
- package/dist/browser/index-browser.mjs.map +1 -1
- package/dist/browser/master/implementation.browser.d.ts +1 -1
- package/dist/browser/master/implementation.browser.mjs +5 -5
- package/dist/browser/master/implementation.browser.mjs.map +1 -1
- package/dist/browser/master/index-browser.d.ts +3 -3
- package/dist/browser/master/index-browser.mjs +18 -18
- package/dist/browser/master/index-browser.mjs.map +1 -1
- package/dist/browser/master/pool-browser.d.ts +2 -2
- package/dist/browser/master/pool-browser.mjs +4 -4
- package/dist/browser/master/pool-browser.mjs.map +1 -1
- package/dist/browser/{master-tsvhUhUx.d.ts → master-Cbd5sMft.d.ts} +1 -3
- package/dist/browser/worker/worker.browser.d.ts +2 -2
- package/dist/browser/worker/worker.browser.mjs +7 -7
- package/dist/browser/worker/worker.browser.mjs.map +1 -1
- package/dist/{node/worker-DW4hSCsH.d.ts → browser/worker-Bk-ckoOM.d.ts} +1 -3
- package/dist/neutral/master/register.mjs +5 -5
- package/dist/neutral/master/register.mjs.map +1 -1
- package/dist/neutral/master/spawn.d.ts +2 -4
- package/dist/neutral/master/spawn.mjs +10 -10
- package/dist/neutral/master/spawn.mjs.map +1 -1
- package/dist/neutral/master/thread.d.ts +1 -1
- package/dist/neutral/master/thread.mjs +2 -2
- package/dist/neutral/master/thread.mjs.map +1 -1
- package/dist/neutral/{master-4pyGz_8-.d.ts → master-C26rzf58.d.ts} +1 -3
- package/dist/neutral/observable-promise.mjs.map +1 -1
- package/dist/neutral/observable.mjs +1 -1
- package/dist/neutral/observable.mjs.map +1 -1
- package/dist/node/{index-node-DqmfxCpi.d.ts → index-node-BjdYwJiq.d.ts} +3 -3
- package/dist/node/index-node.d.ts +4 -4
- package/dist/node/index-node.mjs +19 -19
- package/dist/node/index-node.mjs.map +1 -1
- package/dist/node/master/implementation.node.d.ts +1 -1
- package/dist/node/master/implementation.node.mjs +5 -5
- package/dist/node/master/implementation.node.mjs.map +1 -1
- package/dist/node/master/index-node.d.ts +4 -4
- package/dist/node/master/index-node.mjs +19 -19
- package/dist/node/master/index-node.mjs.map +1 -1
- package/dist/node/master/pool-node.d.ts +2 -2
- package/dist/node/master/pool-node.mjs +3 -3
- package/dist/node/master/pool-node.mjs.map +1 -1
- package/dist/node/{master-CUiPpMI3.d.ts → master-w3q_qDzT.d.ts} +1 -3
- package/dist/node/{pool-types-Ay463MW0.d.ts → pool-types-CV3A06rk.d.ts} +2 -2
- package/dist/node/worker/worker.node.d.ts +3 -3
- package/dist/node/worker/worker.node.mjs +7 -7
- package/dist/node/worker/worker.node.mjs.map +1 -1
- package/dist/{browser/worker-DW4hSCsH.d.ts → node/worker-Bk-ckoOM.d.ts} +1 -3
- package/package.json +10 -13
package/README.md
CHANGED
|
@@ -1,40 +1,54 @@
|
|
|
1
1
|
# @xylabs/threads
|
|
2
2
|
|
|
3
|
-
[![
|
|
3
|
+
[![npm][npm-badge]][npm-link]
|
|
4
|
+
[![license][license-badge]][license-link]
|
|
4
5
|
|
|
5
|
-
|
|
6
|
-
[![npm-badge][]][npm-link]
|
|
7
|
-
[![npm-downloads-badge][]][npm-link]
|
|
8
|
-
[![jsdelivr-badge][]][jsdelivr-link]
|
|
9
|
-
[![npm-license-badge][]](LICENSE)
|
|
10
|
-
[![codacy-badge][]][codacy-link]
|
|
11
|
-
[![codeclimate-badge][]][codeclimate-link]
|
|
12
|
-
[![snyk-badge][]][snyk-link]
|
|
13
|
-
[![socket-badge][]][socket-link]
|
|
6
|
+
> Web workers & worker threads as simple as a function call
|
|
14
7
|
|
|
8
|
+
## Install
|
|
15
9
|
|
|
16
|
-
|
|
10
|
+
Using npm:
|
|
17
11
|
|
|
12
|
+
```sh
|
|
13
|
+
npm install {{name}}
|
|
14
|
+
```
|
|
18
15
|
|
|
16
|
+
Using yarn:
|
|
19
17
|
|
|
20
|
-
|
|
18
|
+
```sh
|
|
19
|
+
yarn add {{name}}
|
|
20
|
+
```
|
|
21
21
|
|
|
22
|
-
|
|
22
|
+
Using pnpm:
|
|
23
23
|
|
|
24
|
-
|
|
24
|
+
```sh
|
|
25
|
+
pnpm add {{name}}
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
Using bun:
|
|
29
|
+
|
|
30
|
+
```sh
|
|
31
|
+
bun add {{name}}
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
## License
|
|
25
36
|
|
|
26
|
-
|
|
37
|
+
See the [LICENSE](LICENSE) file for license rights and limitations (LGPL-3.0-only).
|
|
27
38
|
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
39
|
+
## Reference
|
|
40
|
+
|
|
41
|
+
### packages
|
|
42
|
+
|
|
43
|
+
### threads
|
|
32
44
|
|
|
33
|
-
###
|
|
45
|
+
### .temp-typedoc
|
|
34
46
|
|
|
35
|
-
|
|
47
|
+
### index-browser
|
|
36
48
|
|
|
37
|
-
|
|
49
|
+
### functions
|
|
50
|
+
|
|
51
|
+
### <a id="Transfer"></a>Transfer
|
|
38
52
|
|
|
39
53
|
[**@xylabs/threads**](#../../README)
|
|
40
54
|
|
|
@@ -43,7 +57,7 @@ Web workers & worker threads as simple as a function call
|
|
|
43
57
|
## Call Signature
|
|
44
58
|
|
|
45
59
|
```ts
|
|
46
|
-
function Transfer(transferable
|
|
60
|
+
function Transfer(transferable): TransferDescriptor;
|
|
47
61
|
```
|
|
48
62
|
|
|
49
63
|
Mark a transferable object as such, so it will no be serialized and
|
|
@@ -60,9 +74,11 @@ unless the receiving thread transfers it back again!
|
|
|
60
74
|
|
|
61
75
|
### Parameters
|
|
62
76
|
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
77
|
+
### transferable
|
|
78
|
+
|
|
79
|
+
`Transferable`
|
|
80
|
+
|
|
81
|
+
Array buffer, message port or similar.
|
|
66
82
|
|
|
67
83
|
### Returns
|
|
68
84
|
|
|
@@ -75,7 +91,7 @@ unless the receiving thread transfers it back again!
|
|
|
75
91
|
## Call Signature
|
|
76
92
|
|
|
77
93
|
```ts
|
|
78
|
-
function Transfer<T>(payload
|
|
94
|
+
function Transfer<T>(payload, transferables): TransferDescriptor;
|
|
79
95
|
```
|
|
80
96
|
|
|
81
97
|
Mark transferable objects within an arbitrary object or array as
|
|
@@ -93,16 +109,19 @@ unless the receiving thread transfers it back again!
|
|
|
93
109
|
|
|
94
110
|
### Type Parameters
|
|
95
111
|
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
112
|
+
### T
|
|
113
|
+
|
|
114
|
+
`T`
|
|
99
115
|
|
|
100
116
|
### Parameters
|
|
101
117
|
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
118
|
+
### payload
|
|
119
|
+
|
|
120
|
+
`T`
|
|
121
|
+
|
|
122
|
+
### transferables
|
|
123
|
+
|
|
124
|
+
`Transferable`[]
|
|
106
125
|
|
|
107
126
|
### Returns
|
|
108
127
|
|
|
@@ -112,7 +131,7 @@ unless the receiving thread transfers it back again!
|
|
|
112
131
|
|
|
113
132
|
<https://developers.google.com/web/updates/2011/12/Transferable-Objects-Lightning-Fast>
|
|
114
133
|
|
|
115
|
-
|
|
134
|
+
### <a id="isWorkerRuntime"></a>isWorkerRuntime
|
|
116
135
|
|
|
117
136
|
[**@xylabs/threads**](#../../README)
|
|
118
137
|
|
|
@@ -130,38 +149,38 @@ Check whether the current code is running inside a web worker context.
|
|
|
130
149
|
|
|
131
150
|
True if running in a worker, false otherwise.
|
|
132
151
|
|
|
133
|
-
|
|
152
|
+
### <a id="registerSerializer"></a>registerSerializer
|
|
134
153
|
|
|
135
154
|
[**@xylabs/threads**](#../../README)
|
|
136
155
|
|
|
137
156
|
***
|
|
138
157
|
|
|
139
158
|
```ts
|
|
140
|
-
function registerSerializer(serializer
|
|
159
|
+
function registerSerializer(serializer): void;
|
|
141
160
|
```
|
|
142
161
|
|
|
143
162
|
Register a custom serializer to extend the default serialization behavior for worker messages.
|
|
144
163
|
|
|
145
164
|
## Parameters
|
|
146
165
|
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
166
|
+
### serializer
|
|
167
|
+
|
|
168
|
+
[`SerializerImplementation`](#../interfaces/SerializerImplementation)\<[`JsonSerializable`](#../type-aliases/JsonSerializable)\>
|
|
169
|
+
|
|
170
|
+
The serializer implementation to register.
|
|
150
171
|
|
|
151
172
|
## Returns
|
|
152
173
|
|
|
153
174
|
`void`
|
|
154
175
|
|
|
155
|
-
|
|
176
|
+
### <a id="spawn"></a>spawn
|
|
156
177
|
|
|
157
178
|
[**@xylabs/threads**](#../../README)
|
|
158
179
|
|
|
159
180
|
***
|
|
160
181
|
|
|
161
182
|
```ts
|
|
162
|
-
function spawn<Exposed>(worker
|
|
163
|
-
timeout?: number;
|
|
164
|
-
}): Promise<ExposedAs<Exposed>>;
|
|
183
|
+
function spawn<Exposed>(worker, options?): Promise<ExposedAs<Exposed>>;
|
|
165
184
|
```
|
|
166
185
|
|
|
167
186
|
Spawn a new thread. Takes a fresh worker instance, wraps it in a thin
|
|
@@ -170,25 +189,33 @@ the worker has initialized successfully.
|
|
|
170
189
|
|
|
171
190
|
## Type Parameters
|
|
172
191
|
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
192
|
+
### Exposed
|
|
193
|
+
|
|
194
|
+
`Exposed` *extends* `WorkerFunction` \| `WorkerModule`\<`any`\> = `ArbitraryWorkerInterface`
|
|
176
195
|
|
|
177
196
|
## Parameters
|
|
178
197
|
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
198
|
+
### worker
|
|
199
|
+
|
|
200
|
+
`Worker`
|
|
201
|
+
|
|
202
|
+
Instance of `Worker`. Either a web worker or `worker_threads` worker.
|
|
203
|
+
|
|
204
|
+
### options?
|
|
205
|
+
|
|
206
|
+
### timeout?
|
|
207
|
+
|
|
208
|
+
`number`
|
|
209
|
+
|
|
210
|
+
Init message timeout. Default: 10000 or set by environment variable.
|
|
184
211
|
|
|
185
212
|
## Returns
|
|
186
213
|
|
|
187
214
|
`Promise`\<[`ExposedAs`](#../type-aliases/ExposedAs)\<`Exposed`\>\>
|
|
188
215
|
|
|
189
|
-
|
|
216
|
+
### interfaces
|
|
190
217
|
|
|
191
|
-
|
|
218
|
+
### <a id="Pool"></a>Pool
|
|
192
219
|
|
|
193
220
|
[**@xylabs/threads**](#../../README)
|
|
194
221
|
|
|
@@ -200,16 +227,16 @@ concurrency.
|
|
|
200
227
|
|
|
201
228
|
## Type Parameters
|
|
202
229
|
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
230
|
+
### ThreadType
|
|
231
|
+
|
|
232
|
+
`ThreadType` *extends* [`Thread`](#../type-aliases/Thread)
|
|
206
233
|
|
|
207
234
|
## Methods
|
|
208
235
|
|
|
209
236
|
### completed()
|
|
210
237
|
|
|
211
238
|
```ts
|
|
212
|
-
completed(allowResolvingImmediately
|
|
239
|
+
completed(allowResolvingImmediately?): Promise<any>;
|
|
213
240
|
```
|
|
214
241
|
|
|
215
242
|
Returns a promise that resolves once the task queue is emptied.
|
|
@@ -217,9 +244,11 @@ Promise will be rejected if any task fails.
|
|
|
217
244
|
|
|
218
245
|
### Parameters
|
|
219
246
|
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
247
|
+
#### allowResolvingImmediately?
|
|
248
|
+
|
|
249
|
+
`boolean`
|
|
250
|
+
|
|
251
|
+
Set to `true` to resolve immediately if task queue is currently empty.
|
|
223
252
|
|
|
224
253
|
### Returns
|
|
225
254
|
|
|
@@ -230,7 +259,7 @@ Promise will be rejected if any task fails.
|
|
|
230
259
|
### settled()
|
|
231
260
|
|
|
232
261
|
```ts
|
|
233
|
-
settled(allowResolvingImmediately
|
|
262
|
+
settled(allowResolvingImmediately?): Promise<Error[]>;
|
|
234
263
|
```
|
|
235
264
|
|
|
236
265
|
Returns a promise that resolves once the task queue is emptied.
|
|
@@ -238,9 +267,11 @@ Failing tasks will not cause the promise to be rejected.
|
|
|
238
267
|
|
|
239
268
|
### Parameters
|
|
240
269
|
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
270
|
+
#### allowResolvingImmediately?
|
|
271
|
+
|
|
272
|
+
`boolean`
|
|
273
|
+
|
|
274
|
+
Set to `true` to resolve immediately if task queue is currently empty.
|
|
244
275
|
|
|
245
276
|
### Returns
|
|
246
277
|
|
|
@@ -265,7 +296,7 @@ Returns an observable that yields pool events.
|
|
|
265
296
|
### queue()
|
|
266
297
|
|
|
267
298
|
```ts
|
|
268
|
-
queue<Return>(task
|
|
299
|
+
queue<Return>(task): QueuedTask<ThreadType, Return>;
|
|
269
300
|
```
|
|
270
301
|
|
|
271
302
|
Queue a task and return a promise that resolves once the task has been dequeued,
|
|
@@ -273,15 +304,17 @@ started and finished.
|
|
|
273
304
|
|
|
274
305
|
### Type Parameters
|
|
275
306
|
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
307
|
+
#### Return
|
|
308
|
+
|
|
309
|
+
`Return`
|
|
279
310
|
|
|
280
311
|
### Parameters
|
|
281
312
|
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
313
|
+
#### task
|
|
314
|
+
|
|
315
|
+
`TaskRunFunction`\<`ThreadType`, `Return`\>
|
|
316
|
+
|
|
317
|
+
An async function that takes a thread instance and invokes it.
|
|
285
318
|
|
|
286
319
|
### Returns
|
|
287
320
|
|
|
@@ -292,22 +325,24 @@ started and finished.
|
|
|
292
325
|
### terminate()
|
|
293
326
|
|
|
294
327
|
```ts
|
|
295
|
-
terminate(force
|
|
328
|
+
terminate(force?): Promise<void>;
|
|
296
329
|
```
|
|
297
330
|
|
|
298
331
|
Terminate all pool threads.
|
|
299
332
|
|
|
300
333
|
### Parameters
|
|
301
334
|
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
335
|
+
#### force?
|
|
336
|
+
|
|
337
|
+
`boolean`
|
|
338
|
+
|
|
339
|
+
Set to `true` to kill the thread even if it cannot be stopped gracefully.
|
|
305
340
|
|
|
306
341
|
### Returns
|
|
307
342
|
|
|
308
343
|
`Promise`\<`void`\>
|
|
309
344
|
|
|
310
|
-
|
|
345
|
+
### <a id="QueuedTask"></a>QueuedTask
|
|
311
346
|
|
|
312
347
|
[**@xylabs/threads**](#../../README)
|
|
313
348
|
|
|
@@ -317,16 +352,56 @@ Task that has been `pool.queued()`-ed.
|
|
|
317
352
|
|
|
318
353
|
## Type Parameters
|
|
319
354
|
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
355
|
+
### ThreadType
|
|
356
|
+
|
|
357
|
+
`ThreadType` *extends* [`Thread`](#../type-aliases/Thread)
|
|
358
|
+
|
|
359
|
+
### Return
|
|
360
|
+
|
|
361
|
+
`Return`
|
|
324
362
|
|
|
325
363
|
## Properties
|
|
326
364
|
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
365
|
+
### then
|
|
366
|
+
|
|
367
|
+
```ts
|
|
368
|
+
then: <TResult1, TResult2>(onfulfilled?, onrejected?) => Promise<TResult1 | TResult2>;
|
|
369
|
+
```
|
|
370
|
+
|
|
371
|
+
`QueuedTask` is thenable, so you can `await` it.
|
|
372
|
+
Resolves when the task has successfully been executed. Rejects if the task fails.
|
|
373
|
+
|
|
374
|
+
Attaches callbacks for the resolution and/or rejection of the Promise.
|
|
375
|
+
|
|
376
|
+
### Type Parameters
|
|
377
|
+
|
|
378
|
+
#### TResult1
|
|
379
|
+
|
|
380
|
+
`TResult1` = `Return`
|
|
381
|
+
|
|
382
|
+
#### TResult2
|
|
383
|
+
|
|
384
|
+
`TResult2` = `never`
|
|
385
|
+
|
|
386
|
+
### Parameters
|
|
387
|
+
|
|
388
|
+
#### onfulfilled?
|
|
389
|
+
|
|
390
|
+
((`value`) => `TResult1` \| `PromiseLike`\<`TResult1`\>) \| `null`
|
|
391
|
+
|
|
392
|
+
The callback to execute when the Promise is resolved.
|
|
393
|
+
|
|
394
|
+
#### onrejected?
|
|
395
|
+
|
|
396
|
+
((`reason`) => `TResult2` \| `PromiseLike`\<`TResult2`\>) \| `null`
|
|
397
|
+
|
|
398
|
+
The callback to execute when the Promise is rejected.
|
|
399
|
+
|
|
400
|
+
### Returns
|
|
401
|
+
|
|
402
|
+
`Promise`\<`TResult1` \| `TResult2`\>
|
|
403
|
+
|
|
404
|
+
A Promise for the completion of which ever callback is executed.
|
|
330
405
|
|
|
331
406
|
## Methods
|
|
332
407
|
|
|
@@ -342,7 +417,7 @@ Queued tasks can be cancelled until the pool starts running them on a worker thr
|
|
|
342
417
|
|
|
343
418
|
`void`
|
|
344
419
|
|
|
345
|
-
|
|
420
|
+
### <a id="Serializer"></a>Serializer
|
|
346
421
|
|
|
347
422
|
[**@xylabs/threads**](#../../README)
|
|
348
423
|
|
|
@@ -352,24 +427,27 @@ A serializer that can convert between a message format and an input type.
|
|
|
352
427
|
|
|
353
428
|
## Type Parameters
|
|
354
429
|
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
430
|
+
### Msg
|
|
431
|
+
|
|
432
|
+
`Msg` = [`JsonSerializable`](#../type-aliases/JsonSerializable)
|
|
433
|
+
|
|
434
|
+
### Input
|
|
435
|
+
|
|
436
|
+
`Input` = `any`
|
|
359
437
|
|
|
360
438
|
## Methods
|
|
361
439
|
|
|
362
440
|
### deserialize()
|
|
363
441
|
|
|
364
442
|
```ts
|
|
365
|
-
deserialize(message
|
|
443
|
+
deserialize(message): Input;
|
|
366
444
|
```
|
|
367
445
|
|
|
368
446
|
### Parameters
|
|
369
447
|
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
448
|
+
#### message
|
|
449
|
+
|
|
450
|
+
`Msg`
|
|
373
451
|
|
|
374
452
|
### Returns
|
|
375
453
|
|
|
@@ -380,20 +458,20 @@ deserialize(message: Msg): Input;
|
|
|
380
458
|
### serialize()
|
|
381
459
|
|
|
382
460
|
```ts
|
|
383
|
-
serialize(input
|
|
461
|
+
serialize(input): Msg;
|
|
384
462
|
```
|
|
385
463
|
|
|
386
464
|
### Parameters
|
|
387
465
|
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
466
|
+
#### input
|
|
467
|
+
|
|
468
|
+
`Input`
|
|
391
469
|
|
|
392
470
|
### Returns
|
|
393
471
|
|
|
394
472
|
`Msg`
|
|
395
473
|
|
|
396
|
-
|
|
474
|
+
### <a id="SerializerImplementation"></a>SerializerImplementation
|
|
397
475
|
|
|
398
476
|
[**@xylabs/threads**](#../../README)
|
|
399
477
|
|
|
@@ -403,25 +481,31 @@ A serializer implementation that receives a fallback (default) serializer for ch
|
|
|
403
481
|
|
|
404
482
|
## Type Parameters
|
|
405
483
|
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
484
|
+
### Msg
|
|
485
|
+
|
|
486
|
+
`Msg` = [`JsonSerializable`](#../type-aliases/JsonSerializable)
|
|
487
|
+
|
|
488
|
+
### Input
|
|
489
|
+
|
|
490
|
+
`Input` = `any`
|
|
410
491
|
|
|
411
492
|
## Methods
|
|
412
493
|
|
|
413
494
|
### deserialize()
|
|
414
495
|
|
|
415
496
|
```ts
|
|
416
|
-
deserialize(message
|
|
497
|
+
deserialize(message, defaultDeserialize): Input;
|
|
417
498
|
```
|
|
418
499
|
|
|
419
500
|
### Parameters
|
|
420
501
|
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
502
|
+
#### message
|
|
503
|
+
|
|
504
|
+
`Msg`
|
|
505
|
+
|
|
506
|
+
#### defaultDeserialize
|
|
507
|
+
|
|
508
|
+
(`msg`) => `Input`
|
|
425
509
|
|
|
426
510
|
### Returns
|
|
427
511
|
|
|
@@ -432,21 +516,24 @@ deserialize(message: Msg, defaultDeserialize: (msg: Msg) => Input): Input;
|
|
|
432
516
|
### serialize()
|
|
433
517
|
|
|
434
518
|
```ts
|
|
435
|
-
serialize(input
|
|
519
|
+
serialize(input, defaultSerialize): Msg;
|
|
436
520
|
```
|
|
437
521
|
|
|
438
522
|
### Parameters
|
|
439
523
|
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
524
|
+
#### input
|
|
525
|
+
|
|
526
|
+
`Input`
|
|
527
|
+
|
|
528
|
+
#### defaultSerialize
|
|
529
|
+
|
|
530
|
+
(`inp`) => `Msg`
|
|
444
531
|
|
|
445
532
|
### Returns
|
|
446
533
|
|
|
447
534
|
`Msg`
|
|
448
535
|
|
|
449
|
-
|
|
536
|
+
### <a id="TransferDescriptor"></a>TransferDescriptor
|
|
450
537
|
|
|
451
538
|
[**@xylabs/threads**](#../../README)
|
|
452
539
|
|
|
@@ -456,25 +543,41 @@ Descriptor wrapping a value with its associated transferable objects for zero-co
|
|
|
456
543
|
|
|
457
544
|
## Type Parameters
|
|
458
545
|
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
546
|
+
### T
|
|
547
|
+
|
|
548
|
+
`T` = `any`
|
|
462
549
|
|
|
463
550
|
## Properties
|
|
464
551
|
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
552
|
+
### \[$transferable\]
|
|
553
|
+
|
|
554
|
+
```ts
|
|
555
|
+
[$transferable]: true;
|
|
556
|
+
```
|
|
557
|
+
|
|
558
|
+
***
|
|
559
|
+
|
|
560
|
+
### send
|
|
561
|
+
|
|
562
|
+
```ts
|
|
563
|
+
send: T;
|
|
564
|
+
```
|
|
565
|
+
|
|
566
|
+
***
|
|
567
|
+
|
|
568
|
+
### transferables
|
|
569
|
+
|
|
570
|
+
```ts
|
|
571
|
+
transferables: Transferable[];
|
|
572
|
+
```
|
|
470
573
|
|
|
471
|
-
|
|
574
|
+
### namespaces
|
|
472
575
|
|
|
473
|
-
|
|
576
|
+
### Pool
|
|
474
577
|
|
|
475
|
-
|
|
578
|
+
### type-aliases
|
|
476
579
|
|
|
477
|
-
|
|
580
|
+
### <a id="Event"></a>Event
|
|
478
581
|
|
|
479
582
|
[**@xylabs/threads**](#../../../../README)
|
|
480
583
|
|
|
@@ -486,11 +589,11 @@ type Event<ThreadType> = PoolEvent<ThreadType>;
|
|
|
486
589
|
|
|
487
590
|
## Type Parameters
|
|
488
591
|
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
592
|
+
### ThreadType
|
|
593
|
+
|
|
594
|
+
`ThreadType` *extends* [`Thread`](#../../../type-aliases/Thread) = `any`
|
|
492
595
|
|
|
493
|
-
|
|
596
|
+
### <a id="EventType"></a>EventType
|
|
494
597
|
|
|
495
598
|
[**@xylabs/threads**](#../../../../README)
|
|
496
599
|
|
|
@@ -500,9 +603,9 @@ type Event<ThreadType> = PoolEvent<ThreadType>;
|
|
|
500
603
|
type EventType = PoolEventType;
|
|
501
604
|
```
|
|
502
605
|
|
|
503
|
-
|
|
606
|
+
### type-aliases
|
|
504
607
|
|
|
505
|
-
|
|
608
|
+
### <a id="BlobWorker"></a>BlobWorker
|
|
506
609
|
|
|
507
610
|
[**@xylabs/threads**](#../../README)
|
|
508
611
|
|
|
@@ -514,7 +617,7 @@ type BlobWorker = typeof BlobWorkerClass;
|
|
|
514
617
|
|
|
515
618
|
Separate class to spawn workers from source code blobs or strings.
|
|
516
619
|
|
|
517
|
-
|
|
620
|
+
### <a id="ExposedAs"></a>ExposedAs
|
|
518
621
|
|
|
519
622
|
[**@xylabs/threads**](#../../README)
|
|
520
623
|
|
|
@@ -528,11 +631,11 @@ Maps a worker's exposed API type to its corresponding thread type (`FunctionThre
|
|
|
528
631
|
|
|
529
632
|
## Type Parameters
|
|
530
633
|
|
|
531
|
-
|
|
532
|
-
| ------ |
|
|
533
|
-
| `Exposed` *extends* `WorkerFunction` \| `WorkerModule`\<`any`\> |
|
|
634
|
+
### Exposed
|
|
534
635
|
|
|
535
|
-
|
|
636
|
+
`Exposed` *extends* `WorkerFunction` \| `WorkerModule`\<`any`\>
|
|
637
|
+
|
|
638
|
+
### <a id="FunctionThread"></a>FunctionThread
|
|
536
639
|
|
|
537
640
|
[**@xylabs/threads**](#../../README)
|
|
538
641
|
|
|
@@ -546,12 +649,15 @@ A thread wrapping a single exposed function.
|
|
|
546
649
|
|
|
547
650
|
## Type Parameters
|
|
548
651
|
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
652
|
+
### Args
|
|
653
|
+
|
|
654
|
+
`Args` *extends* `any`[] = `any`[]
|
|
655
|
+
|
|
656
|
+
### ReturnType
|
|
553
657
|
|
|
554
|
-
|
|
658
|
+
`ReturnType` = `any`
|
|
659
|
+
|
|
660
|
+
### <a id="JsonSerializable"></a>JsonSerializable
|
|
555
661
|
|
|
556
662
|
[**@xylabs/threads**](#../../README)
|
|
557
663
|
|
|
@@ -567,7 +673,7 @@ type JsonSerializable =
|
|
|
567
673
|
|
|
568
674
|
A JSON-compatible value that can be serialized for worker message passing.
|
|
569
675
|
|
|
570
|
-
|
|
676
|
+
### <a id="ModuleThread"></a>ModuleThread
|
|
571
677
|
|
|
572
678
|
[**@xylabs/threads**](#../../README)
|
|
573
679
|
|
|
@@ -581,11 +687,11 @@ A thread wrapping an exposed module of functions.
|
|
|
581
687
|
|
|
582
688
|
## Type Parameters
|
|
583
689
|
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
690
|
+
### Methods
|
|
691
|
+
|
|
692
|
+
`Methods` *extends* `ModuleMethods` = `any`
|
|
587
693
|
|
|
588
|
-
|
|
694
|
+
### <a id="Thread"></a>Thread
|
|
589
695
|
|
|
590
696
|
[**@xylabs/threads**](#../../README)
|
|
591
697
|
|
|
@@ -597,7 +703,7 @@ type Thread = ThreadType;
|
|
|
597
703
|
|
|
598
704
|
Re-exported Thread type from the master types module.
|
|
599
705
|
|
|
600
|
-
|
|
706
|
+
### <a id="Worker"></a>Worker
|
|
601
707
|
|
|
602
708
|
[**@xylabs/threads**](#../../README)
|
|
603
709
|
|
|
@@ -609,9 +715,9 @@ type Worker = WorkerType;
|
|
|
609
715
|
|
|
610
716
|
Worker implementation. Either web worker or a node.js Worker class.
|
|
611
717
|
|
|
612
|
-
|
|
718
|
+
### variables
|
|
613
719
|
|
|
614
|
-
|
|
720
|
+
### <a id="BlobWorker"></a>BlobWorker
|
|
615
721
|
|
|
616
722
|
[**@xylabs/threads**](#../../README)
|
|
617
723
|
|
|
@@ -623,7 +729,7 @@ BlobWorker: typeof BlobWorker;
|
|
|
623
729
|
|
|
624
730
|
Separate class to spawn workers from source code blobs or strings.
|
|
625
731
|
|
|
626
|
-
|
|
732
|
+
### <a id="DefaultSerializer"></a>DefaultSerializer
|
|
627
733
|
|
|
628
734
|
[**@xylabs/threads**](#../../README)
|
|
629
735
|
|
|
@@ -635,51 +741,113 @@ const DefaultSerializer: Serializer<JsonSerializable>;
|
|
|
635
741
|
|
|
636
742
|
Default serializer that handles Error instances and passes other values through.
|
|
637
743
|
|
|
638
|
-
|
|
744
|
+
### <a id="Pool"></a>Pool
|
|
639
745
|
|
|
640
746
|
[**@xylabs/threads**](#../../README)
|
|
641
747
|
|
|
642
748
|
***
|
|
643
749
|
|
|
644
750
|
```ts
|
|
645
|
-
Pool: <ThreadType>(spawnWorker
|
|
646
|
-
EventType: typeof PoolEventType;
|
|
647
|
-
};
|
|
751
|
+
Pool: <ThreadType>(spawnWorker, optionsOrSize?) => WorkerPool<ThreadType> & object;
|
|
648
752
|
```
|
|
649
753
|
|
|
650
754
|
Thread pool constructor. Creates a new pool and spawns its worker threads.
|
|
651
755
|
|
|
652
756
|
## Type Declaration
|
|
653
757
|
|
|
654
|
-
|
|
655
|
-
| ------ | ------ |
|
|
656
|
-
| `EventType` | *typeof* `PoolEventType` |
|
|
758
|
+
### EventType
|
|
657
759
|
|
|
658
|
-
|
|
760
|
+
```ts
|
|
761
|
+
EventType: typeof PoolEventType;
|
|
762
|
+
```
|
|
763
|
+
|
|
764
|
+
### <a id="Thread"></a>Thread
|
|
659
765
|
|
|
660
766
|
[**@xylabs/threads**](#../../README)
|
|
661
767
|
|
|
662
768
|
***
|
|
663
769
|
|
|
664
770
|
```ts
|
|
665
|
-
Thread:
|
|
666
|
-
errors: Observable<Error>;
|
|
667
|
-
events: Observable<WorkerEvent>;
|
|
668
|
-
terminate: Promise<void>;
|
|
669
|
-
};
|
|
771
|
+
Thread: object;
|
|
670
772
|
```
|
|
671
773
|
|
|
672
774
|
Thread utility functions. Use them to manage or inspect a `spawn()`-ed thread.
|
|
673
775
|
|
|
674
776
|
## Type Declaration
|
|
675
777
|
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
778
|
+
### errors()
|
|
779
|
+
|
|
780
|
+
```ts
|
|
781
|
+
errors<ThreadT>(thread): Observable<Error>;
|
|
782
|
+
```
|
|
783
|
+
|
|
784
|
+
Return an observable that can be used to subscribe to all errors happening in the thread.
|
|
785
|
+
|
|
786
|
+
### Type Parameters
|
|
787
|
+
|
|
788
|
+
#### ThreadT
|
|
789
|
+
|
|
790
|
+
`ThreadT` *extends* `Thread`
|
|
681
791
|
|
|
682
|
-
|
|
792
|
+
### Parameters
|
|
793
|
+
|
|
794
|
+
#### thread
|
|
795
|
+
|
|
796
|
+
`ThreadT`
|
|
797
|
+
|
|
798
|
+
### Returns
|
|
799
|
+
|
|
800
|
+
`Observable`\<`Error`\>
|
|
801
|
+
|
|
802
|
+
### events()
|
|
803
|
+
|
|
804
|
+
```ts
|
|
805
|
+
events<ThreadT>(thread): Observable<WorkerEvent>;
|
|
806
|
+
```
|
|
807
|
+
|
|
808
|
+
Return an observable that can be used to subscribe to internal events happening in the thread. Useful for debugging.
|
|
809
|
+
|
|
810
|
+
### Type Parameters
|
|
811
|
+
|
|
812
|
+
#### ThreadT
|
|
813
|
+
|
|
814
|
+
`ThreadT` *extends* `Thread`
|
|
815
|
+
|
|
816
|
+
### Parameters
|
|
817
|
+
|
|
818
|
+
#### thread
|
|
819
|
+
|
|
820
|
+
`ThreadT`
|
|
821
|
+
|
|
822
|
+
### Returns
|
|
823
|
+
|
|
824
|
+
`Observable`\<`WorkerEvent`\>
|
|
825
|
+
|
|
826
|
+
### terminate()
|
|
827
|
+
|
|
828
|
+
```ts
|
|
829
|
+
terminate<ThreadT>(thread): Promise<void>;
|
|
830
|
+
```
|
|
831
|
+
|
|
832
|
+
Terminate a thread. Remember to terminate every thread when you are done using it.
|
|
833
|
+
|
|
834
|
+
### Type Parameters
|
|
835
|
+
|
|
836
|
+
#### ThreadT
|
|
837
|
+
|
|
838
|
+
`ThreadT` *extends* `Thread`
|
|
839
|
+
|
|
840
|
+
### Parameters
|
|
841
|
+
|
|
842
|
+
#### thread
|
|
843
|
+
|
|
844
|
+
`ThreadT`
|
|
845
|
+
|
|
846
|
+
### Returns
|
|
847
|
+
|
|
848
|
+
`Promise`\<`void`\>
|
|
849
|
+
|
|
850
|
+
### <a id="Worker"></a>Worker
|
|
683
851
|
|
|
684
852
|
[**@xylabs/threads**](#../../README)
|
|
685
853
|
|
|
@@ -691,11 +859,11 @@ Worker: typeof WorkerImplementation;
|
|
|
691
859
|
|
|
692
860
|
Worker implementation. Either web worker or a node.js Worker class.
|
|
693
861
|
|
|
694
|
-
### index-node
|
|
862
|
+
### index-node
|
|
695
863
|
|
|
696
|
-
|
|
864
|
+
### functions
|
|
697
865
|
|
|
698
|
-
|
|
866
|
+
### <a id="isWorkerRuntime"></a>isWorkerRuntime
|
|
699
867
|
|
|
700
868
|
[**@xylabs/threads**](#../../README)
|
|
701
869
|
|
|
@@ -713,9 +881,9 @@ Check whether the current code is running inside a Node.js worker thread.
|
|
|
713
881
|
|
|
714
882
|
True if running in a worker thread (not the main thread), false otherwise.
|
|
715
883
|
|
|
716
|
-
|
|
884
|
+
### type-aliases
|
|
717
885
|
|
|
718
|
-
|
|
886
|
+
### <a id="BlobWorker"></a>BlobWorker
|
|
719
887
|
|
|
720
888
|
[**@xylabs/threads**](#../../README)
|
|
721
889
|
|
|
@@ -727,7 +895,7 @@ type BlobWorker = typeof BlobWorkerClass;
|
|
|
727
895
|
|
|
728
896
|
Separate class to spawn workers from source code blobs or strings.
|
|
729
897
|
|
|
730
|
-
|
|
898
|
+
### <a id="Worker"></a>Worker
|
|
731
899
|
|
|
732
900
|
[**@xylabs/threads**](#../../README)
|
|
733
901
|
|
|
@@ -739,9 +907,9 @@ type Worker = WorkerType;
|
|
|
739
907
|
|
|
740
908
|
Worker implementation. Either web worker or a node.js Worker class.
|
|
741
909
|
|
|
742
|
-
|
|
910
|
+
### variables
|
|
743
911
|
|
|
744
|
-
|
|
912
|
+
### <a id="BlobWorker"></a>BlobWorker
|
|
745
913
|
|
|
746
914
|
[**@xylabs/threads**](#../../README)
|
|
747
915
|
|
|
@@ -753,7 +921,7 @@ BlobWorker: typeof BlobWorker;
|
|
|
753
921
|
|
|
754
922
|
Separate class to spawn workers from source code blobs or strings.
|
|
755
923
|
|
|
756
|
-
|
|
924
|
+
### <a id="Worker"></a>Worker
|
|
757
925
|
|
|
758
926
|
[**@xylabs/threads**](#../../README)
|
|
759
927
|
|
|
@@ -766,41 +934,7 @@ Worker: typeof WorkerImplementation;
|
|
|
766
934
|
Worker implementation. Either web worker or a node.js Worker class.
|
|
767
935
|
|
|
768
936
|
|
|
769
|
-
Part of [sdk-js](https://www.npmjs.com/package/@xyo-network/sdk-js)
|
|
770
|
-
|
|
771
|
-
## Maintainers
|
|
772
|
-
|
|
773
|
-
- [Arie Trouw](https://github.com/arietrouw) ([arietrouw.com](https://arietrouw.com))
|
|
774
|
-
- [Matt Jones](https://github.com/jonesmac)
|
|
775
|
-
- [Joel Carter](https://github.com/JoelBCarter)
|
|
776
|
-
- [Jordan Trouw](https://github.com/jordantrouw)
|
|
777
|
-
|
|
778
|
-
## License
|
|
779
|
-
|
|
780
|
-
> See the [LICENSE](LICENSE) file for license details
|
|
781
|
-
|
|
782
|
-
## Credits
|
|
783
|
-
|
|
784
|
-
[Made with 🔥 and ❄️ by XYLabs](https://xylabs.com)
|
|
785
|
-
|
|
786
|
-
[logo]: https://cdn.xy.company/img/brand/XYPersistentCompany_Logo_Icon_Colored.svg
|
|
787
|
-
|
|
788
|
-
[main-build]: https://github.com/xylabs/sdk-js/actions/workflows/build.yml/badge.svg
|
|
789
|
-
[main-build-link]: https://github.com/xylabs/sdk-js/actions/workflows/build.yml
|
|
790
937
|
[npm-badge]: https://img.shields.io/npm/v/@xylabs/threads.svg
|
|
791
938
|
[npm-link]: https://www.npmjs.com/package/@xylabs/threads
|
|
792
|
-
[
|
|
793
|
-
[
|
|
794
|
-
[codeclimate-badge]: https://api.codeclimate.com/v1/badges/c5eb068f806f0b047ea7/maintainability
|
|
795
|
-
[codeclimate-link]: https://codeclimate.com/github/xylabs/sdk-js/maintainability
|
|
796
|
-
[snyk-badge]: https://snyk.io/test/github/xylabs/sdk-js/badge.svg?targetFile=package.json
|
|
797
|
-
[snyk-link]: https://snyk.io/test/github/xylabs/sdk-js?targetFile=package.json
|
|
798
|
-
|
|
799
|
-
[npm-downloads-badge]: https://img.shields.io/npm/dw/@xylabs/threads
|
|
800
|
-
[npm-license-badge]: https://img.shields.io/npm/l/@xylabs/threads
|
|
801
|
-
|
|
802
|
-
[jsdelivr-badge]: https://data.jsdelivr.com/v1/package/npm/@xylabs/threads/badge
|
|
803
|
-
[jsdelivr-link]: https://www.jsdelivr.com/package/npm/@xylabs/threads
|
|
804
|
-
|
|
805
|
-
[socket-badge]: https://socket.dev/api/badge/npm/package/@xylabs/threads
|
|
806
|
-
[socket-link]: https://socket.dev/npm/package/@xylabs/threads
|
|
939
|
+
[license-badge]: https://img.shields.io/npm/l/@xylabs/threads.svg
|
|
940
|
+
[license-link]: https://github.com/xylabs/sdk-js/blob/main/LICENSE
|