@xylabs/threads 5.0.83 → 5.0.86
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 +152 -274
- package/dist/browser/{common-Cuiya5FG.d.ts → common-C73XWm6T.d.ts} +8 -0
- package/dist/browser/{index-browser-CZc9d-db.d.ts → index-browser-CaVWHLzx.d.ts} +3 -2
- package/dist/browser/index-browser.d.ts +5 -5
- package/dist/browser/index-browser.mjs.map +1 -1
- package/dist/browser/master/implementation.browser.d.ts +11 -2
- package/dist/browser/master/implementation.browser.mjs.map +1 -1
- package/dist/browser/master/index-browser.d.ts +4 -4
- package/dist/browser/master/index-browser.mjs.map +1 -1
- package/dist/browser/master/pool-browser.d.ts +4 -2
- package/dist/browser/master/pool-browser.mjs.map +1 -1
- package/dist/browser/{master-CFxtmoRJ.d.ts → master-tsvhUhUx.d.ts} +15 -1
- package/dist/{node/transferable-BjEBIWqm.d.ts → browser/transferable-sfmQXy9d.d.ts} +6 -0
- package/dist/browser/worker/worker.browser.d.ts +9 -3
- package/dist/browser/worker/worker.browser.mjs.map +1 -1
- package/dist/browser/{worker-04t9iwDh.d.ts → worker-DW4hSCsH.d.ts} +3 -0
- package/dist/neutral/master/register.mjs.map +1 -1
- package/dist/neutral/master/spawn.d.ts +4 -1
- package/dist/neutral/master/spawn.mjs.map +1 -1
- package/dist/neutral/master/thread.d.ts +2 -1
- package/dist/neutral/master/thread.mjs.map +1 -1
- package/dist/neutral/{master-BaKB0oWh.d.ts → master-4pyGz_8-.d.ts} +18 -0
- package/dist/neutral/observable.d.ts +3 -0
- package/dist/neutral/observable.mjs +3 -0
- package/dist/neutral/observable.mjs.map +1 -1
- package/dist/neutral/types/messages.d.ts +10 -0
- package/dist/neutral/types/messages.mjs.map +1 -1
- package/dist/node/{common-Cuiya5FG.d.ts → common-C73XWm6T.d.ts} +8 -0
- package/dist/node/{index-node-BjbCKNdn.d.ts → index-node-DqmfxCpi.d.ts} +4 -3
- package/dist/node/index-node.d.ts +6 -6
- package/dist/node/index-node.mjs.map +1 -1
- package/dist/node/master/implementation.node.d.ts +11 -2
- package/dist/node/master/implementation.node.mjs.map +1 -1
- package/dist/node/master/index-node.d.ts +5 -5
- package/dist/node/master/index-node.mjs.map +1 -1
- package/dist/node/master/pool-node.d.ts +3 -3
- package/dist/node/master/pool-node.mjs.map +1 -1
- package/dist/node/{master-CTbq2uzL.d.ts → master-CUiPpMI3.d.ts} +15 -1
- package/dist/node/{pool-types-CTBjm1B2.d.ts → pool-types-Ay463MW0.d.ts} +3 -1
- package/dist/{browser/transferable-BjEBIWqm.d.ts → node/transferable-sfmQXy9d.d.ts} +6 -0
- package/dist/node/worker/worker.node.d.ts +9 -3
- package/dist/node/worker/worker.node.mjs.map +1 -1
- package/dist/node/{worker-04t9iwDh.d.ts → worker-DW4hSCsH.d.ts} +3 -0
- package/package.json +6 -6
package/README.md
CHANGED
|
@@ -15,6 +15,8 @@
|
|
|
15
15
|
|
|
16
16
|
Web workers & worker threads as simple as a function call
|
|
17
17
|
|
|
18
|
+
|
|
19
|
+
|
|
18
20
|
## Reference
|
|
19
21
|
|
|
20
22
|
**@xylabs/threads**
|
|
@@ -23,8 +25,10 @@ Web workers & worker threads as simple as a function call
|
|
|
23
25
|
|
|
24
26
|
## Modules
|
|
25
27
|
|
|
26
|
-
|
|
27
|
-
|
|
28
|
+
| Module | Description |
|
|
29
|
+
| ------ | ------ |
|
|
30
|
+
| [index-browser](#index-browser/README) | - |
|
|
31
|
+
| [index-node](#index-node/README) | - |
|
|
28
32
|
|
|
29
33
|
### index-browser
|
|
30
34
|
|
|
@@ -39,7 +43,7 @@ Web workers & worker threads as simple as a function call
|
|
|
39
43
|
## Call Signature
|
|
40
44
|
|
|
41
45
|
```ts
|
|
42
|
-
function Transfer(transferable): TransferDescriptor;
|
|
46
|
+
function Transfer(transferable: Transferable): TransferDescriptor;
|
|
43
47
|
```
|
|
44
48
|
|
|
45
49
|
Mark a transferable object as such, so it will no be serialized and
|
|
@@ -56,11 +60,9 @@ unless the receiving thread transfers it back again!
|
|
|
56
60
|
|
|
57
61
|
### Parameters
|
|
58
62
|
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
`Transferable`
|
|
62
|
-
|
|
63
|
-
Array buffer, message port or similar.
|
|
63
|
+
| Parameter | Type | Description |
|
|
64
|
+
| ------ | ------ | ------ |
|
|
65
|
+
| `transferable` | `Transferable` | Array buffer, message port or similar. |
|
|
64
66
|
|
|
65
67
|
### Returns
|
|
66
68
|
|
|
@@ -73,7 +75,7 @@ Array buffer, message port or similar.
|
|
|
73
75
|
## Call Signature
|
|
74
76
|
|
|
75
77
|
```ts
|
|
76
|
-
function Transfer<T>(payload, transferables): TransferDescriptor;
|
|
78
|
+
function Transfer<T>(payload: T, transferables: Transferable[]): TransferDescriptor;
|
|
77
79
|
```
|
|
78
80
|
|
|
79
81
|
Mark transferable objects within an arbitrary object or array as
|
|
@@ -91,19 +93,16 @@ unless the receiving thread transfers it back again!
|
|
|
91
93
|
|
|
92
94
|
### Type Parameters
|
|
93
95
|
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
`T`
|
|
96
|
+
| Type Parameter |
|
|
97
|
+
| ------ |
|
|
98
|
+
| `T` |
|
|
97
99
|
|
|
98
100
|
### Parameters
|
|
99
101
|
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
`T`
|
|
103
|
-
|
|
104
|
-
### transferables
|
|
105
|
-
|
|
106
|
-
`Transferable`[]
|
|
102
|
+
| Parameter | Type |
|
|
103
|
+
| ------ | ------ |
|
|
104
|
+
| `payload` | `T` |
|
|
105
|
+
| `transferables` | `Transferable`[] |
|
|
107
106
|
|
|
108
107
|
### Returns
|
|
109
108
|
|
|
@@ -123,10 +122,14 @@ unless the receiving thread transfers it back again!
|
|
|
123
122
|
function isWorkerRuntime(): boolean;
|
|
124
123
|
```
|
|
125
124
|
|
|
125
|
+
Check whether the current code is running inside a web worker context.
|
|
126
|
+
|
|
126
127
|
## Returns
|
|
127
128
|
|
|
128
129
|
`boolean`
|
|
129
130
|
|
|
131
|
+
True if running in a worker, false otherwise.
|
|
132
|
+
|
|
130
133
|
### <a id="registerSerializer"></a>registerSerializer
|
|
131
134
|
|
|
132
135
|
[**@xylabs/threads**](#../../README)
|
|
@@ -134,14 +137,16 @@ function isWorkerRuntime(): boolean;
|
|
|
134
137
|
***
|
|
135
138
|
|
|
136
139
|
```ts
|
|
137
|
-
function registerSerializer(serializer): void;
|
|
140
|
+
function registerSerializer(serializer: SerializerImplementation<JsonSerializable>): void;
|
|
138
141
|
```
|
|
139
142
|
|
|
140
|
-
|
|
143
|
+
Register a custom serializer to extend the default serialization behavior for worker messages.
|
|
141
144
|
|
|
142
|
-
|
|
145
|
+
## Parameters
|
|
143
146
|
|
|
144
|
-
|
|
147
|
+
| Parameter | Type | Description |
|
|
148
|
+
| ------ | ------ | ------ |
|
|
149
|
+
| `serializer` | [`SerializerImplementation`](#../interfaces/SerializerImplementation)\<[`JsonSerializable`](#../type-aliases/JsonSerializable)\> | The serializer implementation to register. |
|
|
145
150
|
|
|
146
151
|
## Returns
|
|
147
152
|
|
|
@@ -154,7 +159,9 @@ function registerSerializer(serializer): void;
|
|
|
154
159
|
***
|
|
155
160
|
|
|
156
161
|
```ts
|
|
157
|
-
function spawn<Exposed>(worker, options
|
|
162
|
+
function spawn<Exposed>(worker: Worker, options?: {
|
|
163
|
+
timeout?: number;
|
|
164
|
+
}): Promise<ExposedAs<Exposed>>;
|
|
158
165
|
```
|
|
159
166
|
|
|
160
167
|
Spawn a new thread. Takes a fresh worker instance, wraps it in a thin
|
|
@@ -163,25 +170,17 @@ the worker has initialized successfully.
|
|
|
163
170
|
|
|
164
171
|
## Type Parameters
|
|
165
172
|
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
`Exposed` *extends* `WorkerFunction` \| `WorkerModule`\<`any`\>
|
|
173
|
+
| Type Parameter | Default type |
|
|
174
|
+
| ------ | ------ |
|
|
175
|
+
| `Exposed` *extends* `WorkerFunction` \| `WorkerModule`\<`any`\> | `ArbitraryWorkerInterface` |
|
|
169
176
|
|
|
170
177
|
## Parameters
|
|
171
178
|
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
`Worker`
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
### options?
|
|
179
|
-
|
|
180
|
-
### timeout?
|
|
181
|
-
|
|
182
|
-
`number`
|
|
183
|
-
|
|
184
|
-
Init message timeout. Default: 10000 or set by environment variable.
|
|
179
|
+
| Parameter | Type | Description |
|
|
180
|
+
| ------ | ------ | ------ |
|
|
181
|
+
| `worker` | `Worker` | Instance of `Worker`. Either a web worker or `worker_threads` worker. |
|
|
182
|
+
| `options?` | \{ `timeout?`: `number`; \} | - |
|
|
183
|
+
| `options.timeout?` | `number` | Init message timeout. Default: 10000 or set by environment variable. |
|
|
185
184
|
|
|
186
185
|
## Returns
|
|
187
186
|
|
|
@@ -201,16 +200,16 @@ concurrency.
|
|
|
201
200
|
|
|
202
201
|
## Type Parameters
|
|
203
202
|
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
`ThreadType` *extends* [`Thread`](#../type-aliases/Thread)
|
|
203
|
+
| Type Parameter |
|
|
204
|
+
| ------ |
|
|
205
|
+
| `ThreadType` *extends* [`Thread`](#../type-aliases/Thread) |
|
|
207
206
|
|
|
208
207
|
## Methods
|
|
209
208
|
|
|
210
209
|
### completed()
|
|
211
210
|
|
|
212
211
|
```ts
|
|
213
|
-
completed(allowResolvingImmediately
|
|
212
|
+
completed(allowResolvingImmediately?: boolean): Promise<any>;
|
|
214
213
|
```
|
|
215
214
|
|
|
216
215
|
Returns a promise that resolves once the task queue is emptied.
|
|
@@ -218,11 +217,9 @@ Promise will be rejected if any task fails.
|
|
|
218
217
|
|
|
219
218
|
### Parameters
|
|
220
219
|
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
`boolean`
|
|
224
|
-
|
|
225
|
-
Set to `true` to resolve immediately if task queue is currently empty.
|
|
220
|
+
| Parameter | Type | Description |
|
|
221
|
+
| ------ | ------ | ------ |
|
|
222
|
+
| `allowResolvingImmediately?` | `boolean` | Set to `true` to resolve immediately if task queue is currently empty. |
|
|
226
223
|
|
|
227
224
|
### Returns
|
|
228
225
|
|
|
@@ -233,7 +230,7 @@ Set to `true` to resolve immediately if task queue is currently empty.
|
|
|
233
230
|
### settled()
|
|
234
231
|
|
|
235
232
|
```ts
|
|
236
|
-
settled(allowResolvingImmediately
|
|
233
|
+
settled(allowResolvingImmediately?: boolean): Promise<Error[]>;
|
|
237
234
|
```
|
|
238
235
|
|
|
239
236
|
Returns a promise that resolves once the task queue is emptied.
|
|
@@ -241,11 +238,9 @@ Failing tasks will not cause the promise to be rejected.
|
|
|
241
238
|
|
|
242
239
|
### Parameters
|
|
243
240
|
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
`boolean`
|
|
247
|
-
|
|
248
|
-
Set to `true` to resolve immediately if task queue is currently empty.
|
|
241
|
+
| Parameter | Type | Description |
|
|
242
|
+
| ------ | ------ | ------ |
|
|
243
|
+
| `allowResolvingImmediately?` | `boolean` | Set to `true` to resolve immediately if task queue is currently empty. |
|
|
249
244
|
|
|
250
245
|
### Returns
|
|
251
246
|
|
|
@@ -270,7 +265,7 @@ Returns an observable that yields pool events.
|
|
|
270
265
|
### queue()
|
|
271
266
|
|
|
272
267
|
```ts
|
|
273
|
-
queue<Return>(task): QueuedTask<ThreadType, Return>;
|
|
268
|
+
queue<Return>(task: TaskRunFunction<ThreadType, Return>): QueuedTask<ThreadType, Return>;
|
|
274
269
|
```
|
|
275
270
|
|
|
276
271
|
Queue a task and return a promise that resolves once the task has been dequeued,
|
|
@@ -278,17 +273,15 @@ started and finished.
|
|
|
278
273
|
|
|
279
274
|
### Type Parameters
|
|
280
275
|
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
`Return`
|
|
276
|
+
| Type Parameter |
|
|
277
|
+
| ------ |
|
|
278
|
+
| `Return` |
|
|
284
279
|
|
|
285
280
|
### Parameters
|
|
286
281
|
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
`TaskRunFunction`\<`ThreadType`, `Return`\>
|
|
290
|
-
|
|
291
|
-
An async function that takes a thread instance and invokes it.
|
|
282
|
+
| Parameter | Type | Description |
|
|
283
|
+
| ------ | ------ | ------ |
|
|
284
|
+
| `task` | `TaskRunFunction`\<`ThreadType`, `Return`\> | An async function that takes a thread instance and invokes it. |
|
|
292
285
|
|
|
293
286
|
### Returns
|
|
294
287
|
|
|
@@ -299,18 +292,16 @@ An async function that takes a thread instance and invokes it.
|
|
|
299
292
|
### terminate()
|
|
300
293
|
|
|
301
294
|
```ts
|
|
302
|
-
terminate(force
|
|
295
|
+
terminate(force?: boolean): Promise<void>;
|
|
303
296
|
```
|
|
304
297
|
|
|
305
298
|
Terminate all pool threads.
|
|
306
299
|
|
|
307
300
|
### Parameters
|
|
308
301
|
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
`boolean`
|
|
312
|
-
|
|
313
|
-
Set to `true` to kill the thread even if it cannot be stopped gracefully.
|
|
302
|
+
| Parameter | Type | Description |
|
|
303
|
+
| ------ | ------ | ------ |
|
|
304
|
+
| `force?` | `boolean` | Set to `true` to kill the thread even if it cannot be stopped gracefully. |
|
|
314
305
|
|
|
315
306
|
### Returns
|
|
316
307
|
|
|
@@ -326,56 +317,16 @@ Task that has been `pool.queued()`-ed.
|
|
|
326
317
|
|
|
327
318
|
## Type Parameters
|
|
328
319
|
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
`ThreadType` *extends* [`Thread`](#../type-aliases/Thread)
|
|
332
|
-
|
|
333
|
-
### Return
|
|
334
|
-
|
|
335
|
-
`Return`
|
|
320
|
+
| Type Parameter |
|
|
321
|
+
| ------ |
|
|
322
|
+
| `ThreadType` *extends* [`Thread`](#../type-aliases/Thread) |
|
|
323
|
+
| `Return` |
|
|
336
324
|
|
|
337
325
|
## Properties
|
|
338
326
|
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
then: <TResult1, TResult2>(onfulfilled?, onrejected?) => Promise<TResult1 | TResult2>;
|
|
343
|
-
```
|
|
344
|
-
|
|
345
|
-
`QueuedTask` is thenable, so you can `await` it.
|
|
346
|
-
Resolves when the task has successfully been executed. Rejects if the task fails.
|
|
347
|
-
|
|
348
|
-
Attaches callbacks for the resolution and/or rejection of the Promise.
|
|
349
|
-
|
|
350
|
-
### Type Parameters
|
|
351
|
-
|
|
352
|
-
#### TResult1
|
|
353
|
-
|
|
354
|
-
`TResult1` = `Return`
|
|
355
|
-
|
|
356
|
-
#### TResult2
|
|
357
|
-
|
|
358
|
-
`TResult2` = `never`
|
|
359
|
-
|
|
360
|
-
### Parameters
|
|
361
|
-
|
|
362
|
-
#### onfulfilled?
|
|
363
|
-
|
|
364
|
-
The callback to execute when the Promise is resolved.
|
|
365
|
-
|
|
366
|
-
(`value`) => `TResult1` \| `PromiseLike`\<`TResult1`\> | `null`
|
|
367
|
-
|
|
368
|
-
#### onrejected?
|
|
369
|
-
|
|
370
|
-
The callback to execute when the Promise is rejected.
|
|
371
|
-
|
|
372
|
-
(`reason`) => `TResult2` \| `PromiseLike`\<`TResult2`\> | `null`
|
|
373
|
-
|
|
374
|
-
### Returns
|
|
375
|
-
|
|
376
|
-
`Promise`\<`TResult1` \| `TResult2`\>
|
|
377
|
-
|
|
378
|
-
A Promise for the completion of which ever callback is executed.
|
|
327
|
+
| Property | Type | Description |
|
|
328
|
+
| ------ | ------ | ------ |
|
|
329
|
+
| <a id="then"></a> `then` | \<`TResult1`, `TResult2`\>(`onfulfilled?`: (`value`: `Return`) => `TResult1` \| `PromiseLike`\<`TResult1`\> \| `null`, `onrejected?`: (`reason`: `any`) => `TResult2` \| `PromiseLike`\<`TResult2`\> \| `null`) => `Promise`\<`TResult1` \| `TResult2`\> | `QueuedTask` is thenable, so you can `await` it. Resolves when the task has successfully been executed. Rejects if the task fails. |
|
|
379
330
|
|
|
380
331
|
## Methods
|
|
381
332
|
|
|
@@ -397,29 +348,28 @@ Queued tasks can be cancelled until the pool starts running them on a worker thr
|
|
|
397
348
|
|
|
398
349
|
***
|
|
399
350
|
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
### Msg
|
|
403
|
-
|
|
404
|
-
`Msg` = [`JsonSerializable`](#../type-aliases/JsonSerializable)
|
|
351
|
+
A serializer that can convert between a message format and an input type.
|
|
405
352
|
|
|
406
|
-
|
|
353
|
+
## Type Parameters
|
|
407
354
|
|
|
408
|
-
|
|
355
|
+
| Type Parameter | Default type |
|
|
356
|
+
| ------ | ------ |
|
|
357
|
+
| `Msg` | [`JsonSerializable`](#../type-aliases/JsonSerializable) |
|
|
358
|
+
| `Input` | `any` |
|
|
409
359
|
|
|
410
360
|
## Methods
|
|
411
361
|
|
|
412
362
|
### deserialize()
|
|
413
363
|
|
|
414
364
|
```ts
|
|
415
|
-
deserialize(message): Input;
|
|
365
|
+
deserialize(message: Msg): Input;
|
|
416
366
|
```
|
|
417
367
|
|
|
418
368
|
### Parameters
|
|
419
369
|
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
`Msg`
|
|
370
|
+
| Parameter | Type |
|
|
371
|
+
| ------ | ------ |
|
|
372
|
+
| `message` | `Msg` |
|
|
423
373
|
|
|
424
374
|
### Returns
|
|
425
375
|
|
|
@@ -430,14 +380,14 @@ deserialize(message): Input;
|
|
|
430
380
|
### serialize()
|
|
431
381
|
|
|
432
382
|
```ts
|
|
433
|
-
serialize(input): Msg;
|
|
383
|
+
serialize(input: Input): Msg;
|
|
434
384
|
```
|
|
435
385
|
|
|
436
386
|
### Parameters
|
|
437
387
|
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
`Input`
|
|
388
|
+
| Parameter | Type |
|
|
389
|
+
| ------ | ------ |
|
|
390
|
+
| `input` | `Input` |
|
|
441
391
|
|
|
442
392
|
### Returns
|
|
443
393
|
|
|
@@ -449,33 +399,29 @@ serialize(input): Msg;
|
|
|
449
399
|
|
|
450
400
|
***
|
|
451
401
|
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
### Msg
|
|
455
|
-
|
|
456
|
-
`Msg` = [`JsonSerializable`](#../type-aliases/JsonSerializable)
|
|
402
|
+
A serializer implementation that receives a fallback (default) serializer for chaining.
|
|
457
403
|
|
|
458
|
-
|
|
404
|
+
## Type Parameters
|
|
459
405
|
|
|
460
|
-
|
|
406
|
+
| Type Parameter | Default type |
|
|
407
|
+
| ------ | ------ |
|
|
408
|
+
| `Msg` | [`JsonSerializable`](#../type-aliases/JsonSerializable) |
|
|
409
|
+
| `Input` | `any` |
|
|
461
410
|
|
|
462
411
|
## Methods
|
|
463
412
|
|
|
464
413
|
### deserialize()
|
|
465
414
|
|
|
466
415
|
```ts
|
|
467
|
-
deserialize(message, defaultDeserialize): Input;
|
|
416
|
+
deserialize(message: Msg, defaultDeserialize: (msg: Msg) => Input): Input;
|
|
468
417
|
```
|
|
469
418
|
|
|
470
419
|
### Parameters
|
|
471
420
|
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
`Msg`
|
|
475
|
-
|
|
476
|
-
#### defaultDeserialize
|
|
477
|
-
|
|
478
|
-
(`msg`) => `Input`
|
|
421
|
+
| Parameter | Type |
|
|
422
|
+
| ------ | ------ |
|
|
423
|
+
| `message` | `Msg` |
|
|
424
|
+
| `defaultDeserialize` | (`msg`: `Msg`) => `Input` |
|
|
479
425
|
|
|
480
426
|
### Returns
|
|
481
427
|
|
|
@@ -486,18 +432,15 @@ deserialize(message, defaultDeserialize): Input;
|
|
|
486
432
|
### serialize()
|
|
487
433
|
|
|
488
434
|
```ts
|
|
489
|
-
serialize(input, defaultSerialize): Msg;
|
|
435
|
+
serialize(input: Input, defaultSerialize: (inp: Input) => Msg): Msg;
|
|
490
436
|
```
|
|
491
437
|
|
|
492
438
|
### Parameters
|
|
493
439
|
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
`Input`
|
|
497
|
-
|
|
498
|
-
#### defaultSerialize
|
|
499
|
-
|
|
500
|
-
(`inp`) => `Msg`
|
|
440
|
+
| Parameter | Type |
|
|
441
|
+
| ------ | ------ |
|
|
442
|
+
| `input` | `Input` |
|
|
443
|
+
| `defaultSerialize` | (`inp`: `Input`) => `Msg` |
|
|
501
444
|
|
|
502
445
|
### Returns
|
|
503
446
|
|
|
@@ -509,35 +452,21 @@ serialize(input, defaultSerialize): Msg;
|
|
|
509
452
|
|
|
510
453
|
***
|
|
511
454
|
|
|
512
|
-
|
|
455
|
+
Descriptor wrapping a value with its associated transferable objects for zero-copy messaging.
|
|
513
456
|
|
|
514
|
-
|
|
457
|
+
## Type Parameters
|
|
515
458
|
|
|
516
|
-
|
|
459
|
+
| Type Parameter | Default type |
|
|
460
|
+
| ------ | ------ |
|
|
461
|
+
| `T` | `any` |
|
|
517
462
|
|
|
518
463
|
## Properties
|
|
519
464
|
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
***
|
|
527
|
-
|
|
528
|
-
### send
|
|
529
|
-
|
|
530
|
-
```ts
|
|
531
|
-
send: T;
|
|
532
|
-
```
|
|
533
|
-
|
|
534
|
-
***
|
|
535
|
-
|
|
536
|
-
### transferables
|
|
537
|
-
|
|
538
|
-
```ts
|
|
539
|
-
transferables: Transferable[];
|
|
540
|
-
```
|
|
465
|
+
| Property | Type |
|
|
466
|
+
| ------ | ------ |
|
|
467
|
+
| <a id="transferable"></a> `[$transferable]` | `true` |
|
|
468
|
+
| <a id="send"></a> `send` | `T` |
|
|
469
|
+
| <a id="transferables"></a> `transferables` | `Transferable`[] |
|
|
541
470
|
|
|
542
471
|
### namespaces
|
|
543
472
|
|
|
@@ -557,9 +486,9 @@ type Event<ThreadType> = PoolEvent<ThreadType>;
|
|
|
557
486
|
|
|
558
487
|
## Type Parameters
|
|
559
488
|
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
`ThreadType` *extends* [`Thread`](#../../../type-aliases/Thread)
|
|
489
|
+
| Type Parameter | Default type |
|
|
490
|
+
| ------ | ------ |
|
|
491
|
+
| `ThreadType` *extends* [`Thread`](#../../../type-aliases/Thread) | `any` |
|
|
563
492
|
|
|
564
493
|
### <a id="EventType"></a>EventType
|
|
565
494
|
|
|
@@ -595,11 +524,13 @@ Separate class to spawn workers from source code blobs or strings.
|
|
|
595
524
|
type ExposedAs<Exposed> = Exposed extends ArbitraryWorkerInterface ? ArbitraryThreadType : Exposed extends WorkerFunction ? FunctionThread<Parameters<Exposed>, StripAsync<ReturnType<Exposed>>> : Exposed extends WorkerModule<any> ? ModuleThread<Exposed> : never;
|
|
596
525
|
```
|
|
597
526
|
|
|
598
|
-
|
|
527
|
+
Maps a worker's exposed API type to its corresponding thread type (`FunctionThread` or `ModuleThread`).
|
|
599
528
|
|
|
600
|
-
|
|
529
|
+
## Type Parameters
|
|
601
530
|
|
|
602
|
-
|
|
531
|
+
| Type Parameter |
|
|
532
|
+
| ------ |
|
|
533
|
+
| `Exposed` *extends* `WorkerFunction` \| `WorkerModule`\<`any`\> |
|
|
603
534
|
|
|
604
535
|
### <a id="FunctionThread"></a>FunctionThread
|
|
605
536
|
|
|
@@ -611,15 +542,14 @@ type ExposedAs<Exposed> = Exposed extends ArbitraryWorkerInterface ? ArbitraryTh
|
|
|
611
542
|
type FunctionThread<Args, ReturnType> = ProxyableFunction<Args, ReturnType> & PrivateThreadProps;
|
|
612
543
|
```
|
|
613
544
|
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
### Args
|
|
545
|
+
A thread wrapping a single exposed function.
|
|
617
546
|
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
### ReturnType
|
|
547
|
+
## Type Parameters
|
|
621
548
|
|
|
622
|
-
|
|
549
|
+
| Type Parameter | Default type |
|
|
550
|
+
| ------ | ------ |
|
|
551
|
+
| `Args` *extends* `any`[] | `any`[] |
|
|
552
|
+
| `ReturnType` | `any` |
|
|
623
553
|
|
|
624
554
|
### <a id="JsonSerializable"></a>JsonSerializable
|
|
625
555
|
|
|
@@ -635,6 +565,8 @@ type JsonSerializable =
|
|
|
635
565
|
| JsonSerializableObject[];
|
|
636
566
|
```
|
|
637
567
|
|
|
568
|
+
A JSON-compatible value that can be serialized for worker message passing.
|
|
569
|
+
|
|
638
570
|
### <a id="ModuleThread"></a>ModuleThread
|
|
639
571
|
|
|
640
572
|
[**@xylabs/threads**](#../../README)
|
|
@@ -645,11 +577,13 @@ type JsonSerializable =
|
|
|
645
577
|
type ModuleThread<Methods> = ModuleProxy<Methods> & PrivateThreadProps;
|
|
646
578
|
```
|
|
647
579
|
|
|
648
|
-
|
|
580
|
+
A thread wrapping an exposed module of functions.
|
|
649
581
|
|
|
650
|
-
|
|
582
|
+
## Type Parameters
|
|
651
583
|
|
|
652
|
-
|
|
584
|
+
| Type Parameter | Default type |
|
|
585
|
+
| ------ | ------ |
|
|
586
|
+
| `Methods` *extends* `ModuleMethods` | `any` |
|
|
653
587
|
|
|
654
588
|
### <a id="Thread"></a>Thread
|
|
655
589
|
|
|
@@ -661,7 +595,7 @@ type ModuleThread<Methods> = ModuleProxy<Methods> & PrivateThreadProps;
|
|
|
661
595
|
type Thread = ThreadType;
|
|
662
596
|
```
|
|
663
597
|
|
|
664
|
-
Thread
|
|
598
|
+
Re-exported Thread type from the master types module.
|
|
665
599
|
|
|
666
600
|
### <a id="Worker"></a>Worker
|
|
667
601
|
|
|
@@ -699,6 +633,8 @@ Separate class to spawn workers from source code blobs or strings.
|
|
|
699
633
|
const DefaultSerializer: Serializer<JsonSerializable>;
|
|
700
634
|
```
|
|
701
635
|
|
|
636
|
+
Default serializer that handles Error instances and passes other values through.
|
|
637
|
+
|
|
702
638
|
### <a id="Pool"></a>Pool
|
|
703
639
|
|
|
704
640
|
[**@xylabs/threads**](#../../README)
|
|
@@ -706,18 +642,18 @@ const DefaultSerializer: Serializer<JsonSerializable>;
|
|
|
706
642
|
***
|
|
707
643
|
|
|
708
644
|
```ts
|
|
709
|
-
Pool: <ThreadType>(spawnWorker
|
|
645
|
+
Pool: <ThreadType>(spawnWorker: () => Promise<ThreadType>, optionsOrSize?: number | PoolOptions) => WorkerPool<ThreadType> & {
|
|
646
|
+
EventType: typeof PoolEventType;
|
|
647
|
+
};
|
|
710
648
|
```
|
|
711
649
|
|
|
712
650
|
Thread pool constructor. Creates a new pool and spawns its worker threads.
|
|
713
651
|
|
|
714
652
|
## Type Declaration
|
|
715
653
|
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
EventType: typeof PoolEventType;
|
|
720
|
-
```
|
|
654
|
+
| Name | Type |
|
|
655
|
+
| ------ | ------ |
|
|
656
|
+
| `EventType` | *typeof* `PoolEventType` |
|
|
721
657
|
|
|
722
658
|
### <a id="Thread"></a>Thread
|
|
723
659
|
|
|
@@ -726,84 +662,22 @@ EventType: typeof PoolEventType;
|
|
|
726
662
|
***
|
|
727
663
|
|
|
728
664
|
```ts
|
|
729
|
-
Thread:
|
|
665
|
+
Thread: {
|
|
666
|
+
errors: Observable<Error>;
|
|
667
|
+
events: Observable<WorkerEvent>;
|
|
668
|
+
terminate: Promise<void>;
|
|
669
|
+
};
|
|
730
670
|
```
|
|
731
671
|
|
|
732
672
|
Thread utility functions. Use them to manage or inspect a `spawn()`-ed thread.
|
|
733
673
|
|
|
734
674
|
## Type Declaration
|
|
735
675
|
|
|
736
|
-
|
|
737
|
-
|
|
738
|
-
|
|
739
|
-
|
|
740
|
-
|
|
741
|
-
|
|
742
|
-
Return an observable that can be used to subscribe to all errors happening in the thread.
|
|
743
|
-
|
|
744
|
-
### Type Parameters
|
|
745
|
-
|
|
746
|
-
#### ThreadT
|
|
747
|
-
|
|
748
|
-
`ThreadT` *extends* `Thread`
|
|
749
|
-
|
|
750
|
-
### Parameters
|
|
751
|
-
|
|
752
|
-
#### thread
|
|
753
|
-
|
|
754
|
-
`ThreadT`
|
|
755
|
-
|
|
756
|
-
### Returns
|
|
757
|
-
|
|
758
|
-
`Observable`\<`Error`\>
|
|
759
|
-
|
|
760
|
-
### events()
|
|
761
|
-
|
|
762
|
-
```ts
|
|
763
|
-
events<ThreadT>(thread): Observable<WorkerEvent>;
|
|
764
|
-
```
|
|
765
|
-
|
|
766
|
-
Return an observable that can be used to subscribe to internal events happening in the thread. Useful for debugging.
|
|
767
|
-
|
|
768
|
-
### Type Parameters
|
|
769
|
-
|
|
770
|
-
#### ThreadT
|
|
771
|
-
|
|
772
|
-
`ThreadT` *extends* `Thread`
|
|
773
|
-
|
|
774
|
-
### Parameters
|
|
775
|
-
|
|
776
|
-
#### thread
|
|
777
|
-
|
|
778
|
-
`ThreadT`
|
|
779
|
-
|
|
780
|
-
### Returns
|
|
781
|
-
|
|
782
|
-
`Observable`\<`WorkerEvent`\>
|
|
783
|
-
|
|
784
|
-
### terminate()
|
|
785
|
-
|
|
786
|
-
```ts
|
|
787
|
-
terminate<ThreadT>(thread): Promise<void>;
|
|
788
|
-
```
|
|
789
|
-
|
|
790
|
-
Terminate a thread. Remember to terminate every thread when you are done using it.
|
|
791
|
-
|
|
792
|
-
### Type Parameters
|
|
793
|
-
|
|
794
|
-
#### ThreadT
|
|
795
|
-
|
|
796
|
-
`ThreadT` *extends* `Thread`
|
|
797
|
-
|
|
798
|
-
### Parameters
|
|
799
|
-
|
|
800
|
-
#### thread
|
|
801
|
-
|
|
802
|
-
`ThreadT`
|
|
803
|
-
|
|
804
|
-
### Returns
|
|
805
|
-
|
|
806
|
-
`Promise`\<`void`\>
|
|
676
|
+
| Name | Type | Description |
|
|
677
|
+
| ------ | ------ | ------ |
|
|
678
|
+
| `errors()` | (`thread`: `ThreadT`) => `Observable`\<`Error`\> | Return an observable that can be used to subscribe to all errors happening in the thread. |
|
|
679
|
+
| `events()` | (`thread`: `ThreadT`) => `Observable`\<`WorkerEvent`\> | Return an observable that can be used to subscribe to internal events happening in the thread. Useful for debugging. |
|
|
680
|
+
| `terminate()` | (`thread`: `ThreadT`) => `Promise`\<`void`\> | Terminate a thread. Remember to terminate every thread when you are done using it. |
|
|
807
681
|
|
|
808
682
|
### <a id="Worker"></a>Worker
|
|
809
683
|
|
|
@@ -831,10 +705,14 @@ Worker implementation. Either web worker or a node.js Worker class.
|
|
|
831
705
|
function isWorkerRuntime(): boolean;
|
|
832
706
|
```
|
|
833
707
|
|
|
708
|
+
Check whether the current code is running inside a Node.js worker thread.
|
|
709
|
+
|
|
834
710
|
## Returns
|
|
835
711
|
|
|
836
712
|
`boolean`
|
|
837
713
|
|
|
714
|
+
True if running in a worker thread (not the main thread), false otherwise.
|
|
715
|
+
|
|
838
716
|
### type-aliases
|
|
839
717
|
|
|
840
718
|
### <a id="BlobWorker"></a>BlobWorker
|