@xylabs/threads 5.0.84 → 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.
Files changed (2) hide show
  1. package/README.md +125 -275
  2. 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
- - [index-browser](#index-browser/README)
27
- - [index-node](#index-node/README)
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
- ### transferable
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
- ### T
95
-
96
- `T`
96
+ | Type Parameter |
97
+ | ------ |
98
+ | `T` |
97
99
 
98
100
  ### Parameters
99
101
 
100
- ### payload
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
 
@@ -138,18 +137,16 @@ True if running in a worker, false otherwise.
138
137
  ***
139
138
 
140
139
  ```ts
141
- function registerSerializer(serializer): void;
140
+ function registerSerializer(serializer: SerializerImplementation<JsonSerializable>): void;
142
141
  ```
143
142
 
144
143
  Register a custom serializer to extend the default serialization behavior for worker messages.
145
144
 
146
145
  ## Parameters
147
146
 
148
- ### serializer
149
-
150
- [`SerializerImplementation`](#../interfaces/SerializerImplementation)\<[`JsonSerializable`](#../type-aliases/JsonSerializable)\>
151
-
152
- The serializer implementation to register.
147
+ | Parameter | Type | Description |
148
+ | ------ | ------ | ------ |
149
+ | `serializer` | [`SerializerImplementation`](#../interfaces/SerializerImplementation)\<[`JsonSerializable`](#../type-aliases/JsonSerializable)\> | The serializer implementation to register. |
153
150
 
154
151
  ## Returns
155
152
 
@@ -162,7 +159,9 @@ The serializer implementation to register.
162
159
  ***
163
160
 
164
161
  ```ts
165
- function spawn<Exposed>(worker, options?): Promise<ExposedAs<Exposed>>;
162
+ function spawn<Exposed>(worker: Worker, options?: {
163
+ timeout?: number;
164
+ }): Promise<ExposedAs<Exposed>>;
166
165
  ```
167
166
 
168
167
  Spawn a new thread. Takes a fresh worker instance, wraps it in a thin
@@ -171,25 +170,17 @@ the worker has initialized successfully.
171
170
 
172
171
  ## Type Parameters
173
172
 
174
- ### Exposed
175
-
176
- `Exposed` *extends* `WorkerFunction` \| `WorkerModule`\<`any`\> = `ArbitraryWorkerInterface`
173
+ | Type Parameter | Default type |
174
+ | ------ | ------ |
175
+ | `Exposed` *extends* `WorkerFunction` \| `WorkerModule`\<`any`\> | `ArbitraryWorkerInterface` |
177
176
 
178
177
  ## Parameters
179
178
 
180
- ### worker
181
-
182
- `Worker`
183
-
184
- Instance of `Worker`. Either a web worker or `worker_threads` worker.
185
-
186
- ### options?
187
-
188
- ### timeout?
189
-
190
- `number`
191
-
192
- 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. |
193
184
 
194
185
  ## Returns
195
186
 
@@ -209,16 +200,16 @@ concurrency.
209
200
 
210
201
  ## Type Parameters
211
202
 
212
- ### ThreadType
213
-
214
- `ThreadType` *extends* [`Thread`](#../type-aliases/Thread)
203
+ | Type Parameter |
204
+ | ------ |
205
+ | `ThreadType` *extends* [`Thread`](#../type-aliases/Thread) |
215
206
 
216
207
  ## Methods
217
208
 
218
209
  ### completed()
219
210
 
220
211
  ```ts
221
- completed(allowResolvingImmediately?): Promise<any>;
212
+ completed(allowResolvingImmediately?: boolean): Promise<any>;
222
213
  ```
223
214
 
224
215
  Returns a promise that resolves once the task queue is emptied.
@@ -226,11 +217,9 @@ Promise will be rejected if any task fails.
226
217
 
227
218
  ### Parameters
228
219
 
229
- #### allowResolvingImmediately?
230
-
231
- `boolean`
232
-
233
- 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. |
234
223
 
235
224
  ### Returns
236
225
 
@@ -241,7 +230,7 @@ Set to `true` to resolve immediately if task queue is currently empty.
241
230
  ### settled()
242
231
 
243
232
  ```ts
244
- settled(allowResolvingImmediately?): Promise<Error[]>;
233
+ settled(allowResolvingImmediately?: boolean): Promise<Error[]>;
245
234
  ```
246
235
 
247
236
  Returns a promise that resolves once the task queue is emptied.
@@ -249,11 +238,9 @@ Failing tasks will not cause the promise to be rejected.
249
238
 
250
239
  ### Parameters
251
240
 
252
- #### allowResolvingImmediately?
253
-
254
- `boolean`
255
-
256
- 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. |
257
244
 
258
245
  ### Returns
259
246
 
@@ -278,7 +265,7 @@ Returns an observable that yields pool events.
278
265
  ### queue()
279
266
 
280
267
  ```ts
281
- queue<Return>(task): QueuedTask<ThreadType, Return>;
268
+ queue<Return>(task: TaskRunFunction<ThreadType, Return>): QueuedTask<ThreadType, Return>;
282
269
  ```
283
270
 
284
271
  Queue a task and return a promise that resolves once the task has been dequeued,
@@ -286,17 +273,15 @@ started and finished.
286
273
 
287
274
  ### Type Parameters
288
275
 
289
- #### Return
290
-
291
- `Return`
276
+ | Type Parameter |
277
+ | ------ |
278
+ | `Return` |
292
279
 
293
280
  ### Parameters
294
281
 
295
- #### task
296
-
297
- `TaskRunFunction`\<`ThreadType`, `Return`\>
298
-
299
- 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. |
300
285
 
301
286
  ### Returns
302
287
 
@@ -307,18 +292,16 @@ An async function that takes a thread instance and invokes it.
307
292
  ### terminate()
308
293
 
309
294
  ```ts
310
- terminate(force?): Promise<void>;
295
+ terminate(force?: boolean): Promise<void>;
311
296
  ```
312
297
 
313
298
  Terminate all pool threads.
314
299
 
315
300
  ### Parameters
316
301
 
317
- #### force?
318
-
319
- `boolean`
320
-
321
- 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. |
322
305
 
323
306
  ### Returns
324
307
 
@@ -334,56 +317,16 @@ Task that has been `pool.queued()`-ed.
334
317
 
335
318
  ## Type Parameters
336
319
 
337
- ### ThreadType
338
-
339
- `ThreadType` *extends* [`Thread`](#../type-aliases/Thread)
340
-
341
- ### Return
342
-
343
- `Return`
320
+ | Type Parameter |
321
+ | ------ |
322
+ | `ThreadType` *extends* [`Thread`](#../type-aliases/Thread) |
323
+ | `Return` |
344
324
 
345
325
  ## Properties
346
326
 
347
- ### then()
348
-
349
- ```ts
350
- then: <TResult1, TResult2>(onfulfilled?, onrejected?) => Promise<TResult1 | TResult2>;
351
- ```
352
-
353
- `QueuedTask` is thenable, so you can `await` it.
354
- Resolves when the task has successfully been executed. Rejects if the task fails.
355
-
356
- Attaches callbacks for the resolution and/or rejection of the Promise.
357
-
358
- ### Type Parameters
359
-
360
- #### TResult1
361
-
362
- `TResult1` = `Return`
363
-
364
- #### TResult2
365
-
366
- `TResult2` = `never`
367
-
368
- ### Parameters
369
-
370
- #### onfulfilled?
371
-
372
- The callback to execute when the Promise is resolved.
373
-
374
- (`value`) => `TResult1` \| `PromiseLike`\<`TResult1`\> | `null`
375
-
376
- #### onrejected?
377
-
378
- The callback to execute when the Promise is rejected.
379
-
380
- (`reason`) => `TResult2` \| `PromiseLike`\<`TResult2`\> | `null`
381
-
382
- ### Returns
383
-
384
- `Promise`\<`TResult1` \| `TResult2`\>
385
-
386
- 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. |
387
330
 
388
331
  ## Methods
389
332
 
@@ -409,27 +352,24 @@ A serializer that can convert between a message format and an input type.
409
352
 
410
353
  ## Type Parameters
411
354
 
412
- ### Msg
413
-
414
- `Msg` = [`JsonSerializable`](#../type-aliases/JsonSerializable)
415
-
416
- ### Input
417
-
418
- `Input` = `any`
355
+ | Type Parameter | Default type |
356
+ | ------ | ------ |
357
+ | `Msg` | [`JsonSerializable`](#../type-aliases/JsonSerializable) |
358
+ | `Input` | `any` |
419
359
 
420
360
  ## Methods
421
361
 
422
362
  ### deserialize()
423
363
 
424
364
  ```ts
425
- deserialize(message): Input;
365
+ deserialize(message: Msg): Input;
426
366
  ```
427
367
 
428
368
  ### Parameters
429
369
 
430
- #### message
431
-
432
- `Msg`
370
+ | Parameter | Type |
371
+ | ------ | ------ |
372
+ | `message` | `Msg` |
433
373
 
434
374
  ### Returns
435
375
 
@@ -440,14 +380,14 @@ deserialize(message): Input;
440
380
  ### serialize()
441
381
 
442
382
  ```ts
443
- serialize(input): Msg;
383
+ serialize(input: Input): Msg;
444
384
  ```
445
385
 
446
386
  ### Parameters
447
387
 
448
- #### input
449
-
450
- `Input`
388
+ | Parameter | Type |
389
+ | ------ | ------ |
390
+ | `input` | `Input` |
451
391
 
452
392
  ### Returns
453
393
 
@@ -463,31 +403,25 @@ A serializer implementation that receives a fallback (default) serializer for ch
463
403
 
464
404
  ## Type Parameters
465
405
 
466
- ### Msg
467
-
468
- `Msg` = [`JsonSerializable`](#../type-aliases/JsonSerializable)
469
-
470
- ### Input
471
-
472
- `Input` = `any`
406
+ | Type Parameter | Default type |
407
+ | ------ | ------ |
408
+ | `Msg` | [`JsonSerializable`](#../type-aliases/JsonSerializable) |
409
+ | `Input` | `any` |
473
410
 
474
411
  ## Methods
475
412
 
476
413
  ### deserialize()
477
414
 
478
415
  ```ts
479
- deserialize(message, defaultDeserialize): Input;
416
+ deserialize(message: Msg, defaultDeserialize: (msg: Msg) => Input): Input;
480
417
  ```
481
418
 
482
419
  ### Parameters
483
420
 
484
- #### message
485
-
486
- `Msg`
487
-
488
- #### defaultDeserialize
489
-
490
- (`msg`) => `Input`
421
+ | Parameter | Type |
422
+ | ------ | ------ |
423
+ | `message` | `Msg` |
424
+ | `defaultDeserialize` | (`msg`: `Msg`) => `Input` |
491
425
 
492
426
  ### Returns
493
427
 
@@ -498,18 +432,15 @@ deserialize(message, defaultDeserialize): Input;
498
432
  ### serialize()
499
433
 
500
434
  ```ts
501
- serialize(input, defaultSerialize): Msg;
435
+ serialize(input: Input, defaultSerialize: (inp: Input) => Msg): Msg;
502
436
  ```
503
437
 
504
438
  ### Parameters
505
439
 
506
- #### input
507
-
508
- `Input`
509
-
510
- #### defaultSerialize
511
-
512
- (`inp`) => `Msg`
440
+ | Parameter | Type |
441
+ | ------ | ------ |
442
+ | `input` | `Input` |
443
+ | `defaultSerialize` | (`inp`: `Input`) => `Msg` |
513
444
 
514
445
  ### Returns
515
446
 
@@ -525,33 +456,17 @@ Descriptor wrapping a value with its associated transferable objects for zero-co
525
456
 
526
457
  ## Type Parameters
527
458
 
528
- ### T
529
-
530
- `T` = `any`
459
+ | Type Parameter | Default type |
460
+ | ------ | ------ |
461
+ | `T` | `any` |
531
462
 
532
463
  ## Properties
533
464
 
534
- ### \[$transferable\]
535
-
536
- ```ts
537
- [$transferable]: true;
538
- ```
539
-
540
- ***
541
-
542
- ### send
543
-
544
- ```ts
545
- send: T;
546
- ```
547
-
548
- ***
549
-
550
- ### transferables
551
-
552
- ```ts
553
- transferables: Transferable[];
554
- ```
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`[] |
555
470
 
556
471
  ### namespaces
557
472
 
@@ -571,9 +486,9 @@ type Event<ThreadType> = PoolEvent<ThreadType>;
571
486
 
572
487
  ## Type Parameters
573
488
 
574
- ### ThreadType
575
-
576
- `ThreadType` *extends* [`Thread`](#../../../type-aliases/Thread) = `any`
489
+ | Type Parameter | Default type |
490
+ | ------ | ------ |
491
+ | `ThreadType` *extends* [`Thread`](#../../../type-aliases/Thread) | `any` |
577
492
 
578
493
  ### <a id="EventType"></a>EventType
579
494
 
@@ -613,9 +528,9 @@ Maps a worker's exposed API type to its corresponding thread type (`FunctionThre
613
528
 
614
529
  ## Type Parameters
615
530
 
616
- ### Exposed
617
-
618
- `Exposed` *extends* `WorkerFunction` \| `WorkerModule`\<`any`\>
531
+ | Type Parameter |
532
+ | ------ |
533
+ | `Exposed` *extends* `WorkerFunction` \| `WorkerModule`\<`any`\> |
619
534
 
620
535
  ### <a id="FunctionThread"></a>FunctionThread
621
536
 
@@ -631,13 +546,10 @@ A thread wrapping a single exposed function.
631
546
 
632
547
  ## Type Parameters
633
548
 
634
- ### Args
635
-
636
- `Args` *extends* `any`[] = `any`[]
637
-
638
- ### ReturnType
639
-
640
- `ReturnType` = `any`
549
+ | Type Parameter | Default type |
550
+ | ------ | ------ |
551
+ | `Args` *extends* `any`[] | `any`[] |
552
+ | `ReturnType` | `any` |
641
553
 
642
554
  ### <a id="JsonSerializable"></a>JsonSerializable
643
555
 
@@ -669,9 +581,9 @@ A thread wrapping an exposed module of functions.
669
581
 
670
582
  ## Type Parameters
671
583
 
672
- ### Methods
673
-
674
- `Methods` *extends* `ModuleMethods` = `any`
584
+ | Type Parameter | Default type |
585
+ | ------ | ------ |
586
+ | `Methods` *extends* `ModuleMethods` | `any` |
675
587
 
676
588
  ### <a id="Thread"></a>Thread
677
589
 
@@ -730,18 +642,18 @@ Default serializer that handles Error instances and passes other values through.
730
642
  ***
731
643
 
732
644
  ```ts
733
- Pool: <ThreadType>(spawnWorker, optionsOrSize?) => WorkerPool<ThreadType> & object;
645
+ Pool: <ThreadType>(spawnWorker: () => Promise<ThreadType>, optionsOrSize?: number | PoolOptions) => WorkerPool<ThreadType> & {
646
+ EventType: typeof PoolEventType;
647
+ };
734
648
  ```
735
649
 
736
650
  Thread pool constructor. Creates a new pool and spawns its worker threads.
737
651
 
738
652
  ## Type Declaration
739
653
 
740
- ### EventType
741
-
742
- ```ts
743
- EventType: typeof PoolEventType;
744
- ```
654
+ | Name | Type |
655
+ | ------ | ------ |
656
+ | `EventType` | *typeof* `PoolEventType` |
745
657
 
746
658
  ### <a id="Thread"></a>Thread
747
659
 
@@ -750,84 +662,22 @@ EventType: typeof PoolEventType;
750
662
  ***
751
663
 
752
664
  ```ts
753
- Thread: object;
665
+ Thread: {
666
+ errors: Observable<Error>;
667
+ events: Observable<WorkerEvent>;
668
+ terminate: Promise<void>;
669
+ };
754
670
  ```
755
671
 
756
672
  Thread utility functions. Use them to manage or inspect a `spawn()`-ed thread.
757
673
 
758
674
  ## Type Declaration
759
675
 
760
- ### errors()
761
-
762
- ```ts
763
- errors<ThreadT>(thread): Observable<Error>;
764
- ```
765
-
766
- Return an observable that can be used to subscribe to all errors happening in the thread.
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`\<`Error`\>
783
-
784
- ### events()
785
-
786
- ```ts
787
- events<ThreadT>(thread): Observable<WorkerEvent>;
788
- ```
789
-
790
- Return an observable that can be used to subscribe to internal events happening in the thread. Useful for debugging.
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
- `Observable`\<`WorkerEvent`\>
807
-
808
- ### terminate()
809
-
810
- ```ts
811
- terminate<ThreadT>(thread): Promise<void>;
812
- ```
813
-
814
- Terminate a thread. Remember to terminate every thread when you are done using it.
815
-
816
- ### Type Parameters
817
-
818
- #### ThreadT
819
-
820
- `ThreadT` *extends* `Thread`
821
-
822
- ### Parameters
823
-
824
- #### thread
825
-
826
- `ThreadT`
827
-
828
- ### Returns
829
-
830
- `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. |
831
681
 
832
682
  ### <a id="Worker"></a>Worker
833
683
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@xylabs/threads",
3
- "version": "5.0.84",
3
+ "version": "5.0.86",
4
4
  "description": "Web workers & worker threads as simple as a function call",
5
5
  "keywords": [
6
6
  "thread",
@@ -112,17 +112,17 @@
112
112
  "package-compile-tsup": "tsup --config tsup.browser.config.ts && tsup --config tsup.node.config.ts && tsup --config tsup.neutral.config.ts"
113
113
  },
114
114
  "dependencies": {
115
- "@xylabs/assert": "~5.0.84",
115
+ "@xylabs/assert": "~5.0.86",
116
116
  "debug": "~4.4.3",
117
117
  "is-observable-2-1-0": "npm:is-observable@2.1.0",
118
118
  "observable-fns": "~0.6.1"
119
119
  },
120
120
  "devDependencies": {
121
121
  "@types/debug": "~4.1.12",
122
- "@xylabs/eslint-config-flat": "~7.4.13",
123
- "@xylabs/ts-scripts-yarn3": "~7.4.13",
124
- "@xylabs/tsconfig": "~7.4.13",
125
- "eslint": "~9.39.4",
122
+ "@xylabs/eslint-config-flat": "~7.4.16",
123
+ "@xylabs/ts-scripts-yarn3": "~7.4.16",
124
+ "@xylabs/tsconfig": "~7.4.16",
125
+ "eslint": "~10.0.3",
126
126
  "tsup": "~8.5.1",
127
127
  "typescript": "~5.9.3",
128
128
  "vitest": "^4.0.18"