@xylabs/threads 4.13.21 → 4.13.23

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 +857 -2
  2. package/package.json +4 -4
package/README.md CHANGED
@@ -15,12 +15,867 @@
15
15
 
16
16
  Web workers & worker threads as simple as a function call
17
17
 
18
- ## API Documentation
18
+ ## Reference
19
19
 
20
- **@xylabs/sdk-js**
20
+ **@xylabs/threads**
21
21
 
22
22
  ***
23
23
 
24
+ ## Modules
25
+
26
+ - [index-browser](#index-browser/README)
27
+ - [index-node](#index-node/README)
28
+
29
+ ### index-browser
30
+
31
+ ### functions
32
+
33
+ ### <a id="Transfer"></a>Transfer
34
+
35
+ [**@xylabs/threads**](#../../README)
36
+
37
+ ***
38
+
39
+ ## Call Signature
40
+
41
+ ```ts
42
+ function Transfer(transferable): TransferDescriptor;
43
+ ```
44
+
45
+ Mark a transferable object as such, so it will no be serialized and
46
+ deserialized on messaging with the main thread, but to transfer
47
+ ownership of it to the receiving thread.
48
+
49
+ Only works with array buffers, message ports and few more special
50
+ types of objects, but it's much faster than serializing and
51
+ deserializing them.
52
+
53
+ Note:
54
+ The transferable object cannot be accessed by this thread again
55
+ unless the receiving thread transfers it back again!
56
+
57
+ ### Parameters
58
+
59
+ ### transferable
60
+
61
+ `Transferable`
62
+
63
+ Array buffer, message port or similar.
64
+
65
+ ### Returns
66
+
67
+ [`TransferDescriptor`](#../interfaces/TransferDescriptor)
68
+
69
+ ### See
70
+
71
+ <https://developers.google.com/web/updates/2011/12/Transferable-Objects-Lightning-Fast>
72
+
73
+ ## Call Signature
74
+
75
+ ```ts
76
+ function Transfer<T>(payload, transferables): TransferDescriptor;
77
+ ```
78
+
79
+ Mark transferable objects within an arbitrary object or array as
80
+ being a transferable object. They will then not be serialized
81
+ and deserialized on messaging with the main thread, but ownership
82
+ of them will be tranferred to the receiving thread.
83
+
84
+ Only array buffers, message ports and few more special types of
85
+ objects can be transferred, but it's much faster than serializing and
86
+ deserializing them.
87
+
88
+ Note:
89
+ The transferable object cannot be accessed by this thread again
90
+ unless the receiving thread transfers it back again!
91
+
92
+ ### Type Parameters
93
+
94
+ ### T
95
+
96
+ `T`
97
+
98
+ ### Parameters
99
+
100
+ ### payload
101
+
102
+ `T`
103
+
104
+ ### transferables
105
+
106
+ `Transferable`[]
107
+
108
+ ### Returns
109
+
110
+ [`TransferDescriptor`](#../interfaces/TransferDescriptor)
111
+
112
+ ### See
113
+
114
+ <https://developers.google.com/web/updates/2011/12/Transferable-Objects-Lightning-Fast>
115
+
116
+ ### <a id="isWorkerRuntime"></a>isWorkerRuntime
117
+
118
+ [**@xylabs/threads**](#../../README)
119
+
120
+ ***
121
+
122
+ ```ts
123
+ function isWorkerRuntime(): boolean;
124
+ ```
125
+
126
+ ## Returns
127
+
128
+ `boolean`
129
+
130
+ ### <a id="registerSerializer"></a>registerSerializer
131
+
132
+ [**@xylabs/threads**](#../../README)
133
+
134
+ ***
135
+
136
+ ```ts
137
+ function registerSerializer(serializer): void;
138
+ ```
139
+
140
+ ## Parameters
141
+
142
+ ### serializer
143
+
144
+ [`SerializerImplementation`](#../interfaces/SerializerImplementation)\<[`JsonSerializable`](#../type-aliases/JsonSerializable)\>
145
+
146
+ ## Returns
147
+
148
+ `void`
149
+
150
+ ### <a id="spawn"></a>spawn
151
+
152
+ [**@xylabs/threads**](#../../README)
153
+
154
+ ***
155
+
156
+ ```ts
157
+ function spawn<Exposed>(worker, options?): Promise<ExposedAs<Exposed>>;
158
+ ```
159
+
160
+ Spawn a new thread. Takes a fresh worker instance, wraps it in a thin
161
+ abstraction layer to provide the transparent API and verifies that
162
+ the worker has initialized successfully.
163
+
164
+ ## Type Parameters
165
+
166
+ ### Exposed
167
+
168
+ `Exposed` *extends* `WorkerFunction` \| `WorkerModule`\<`any`\> = `ArbitraryWorkerInterface`
169
+
170
+ ## Parameters
171
+
172
+ ### worker
173
+
174
+ `Worker`
175
+
176
+ Instance of `Worker`. Either a web worker, `worker_threads` worker or `tiny-worker` worker.
177
+
178
+ ### options?
179
+
180
+ ### timeout?
181
+
182
+ `number`
183
+
184
+ Init message timeout. Default: 10000 or set by environment variable.
185
+
186
+ ## Returns
187
+
188
+ `Promise`\<[`ExposedAs`](#../type-aliases/ExposedAs)\<`Exposed`\>\>
189
+
190
+ ### interfaces
191
+
192
+ ### <a id="Pool"></a>Pool
193
+
194
+ [**@xylabs/threads**](#../../README)
195
+
196
+ ***
197
+
198
+ Thread pool managing a set of worker threads.
199
+ Use it to queue tasks that are run on those threads with limited
200
+ concurrency.
201
+
202
+ ## Type Parameters
203
+
204
+ ### ThreadType
205
+
206
+ `ThreadType` *extends* [`Thread`](#../type-aliases/Thread)
207
+
208
+ ## Methods
209
+
210
+ ### completed()
211
+
212
+ ```ts
213
+ completed(allowResolvingImmediately?): Promise<any>;
214
+ ```
215
+
216
+ Returns a promise that resolves once the task queue is emptied.
217
+ Promise will be rejected if any task fails.
218
+
219
+ ### Parameters
220
+
221
+ #### allowResolvingImmediately?
222
+
223
+ `boolean`
224
+
225
+ Set to `true` to resolve immediately if task queue is currently empty.
226
+
227
+ ### Returns
228
+
229
+ `Promise`\<`any`\>
230
+
231
+ ***
232
+
233
+ ### settled()
234
+
235
+ ```ts
236
+ settled(allowResolvingImmediately?): Promise<Error[]>;
237
+ ```
238
+
239
+ Returns a promise that resolves once the task queue is emptied.
240
+ Failing tasks will not cause the promise to be rejected.
241
+
242
+ ### Parameters
243
+
244
+ #### allowResolvingImmediately?
245
+
246
+ `boolean`
247
+
248
+ Set to `true` to resolve immediately if task queue is currently empty.
249
+
250
+ ### Returns
251
+
252
+ `Promise`\<`Error`[]\>
253
+
254
+ ***
255
+
256
+ ### events()
257
+
258
+ ```ts
259
+ events(): Observable<PoolEvent<ThreadType>>;
260
+ ```
261
+
262
+ Returns an observable that yields pool events.
263
+
264
+ ### Returns
265
+
266
+ `Observable`\<`PoolEvent`\<`ThreadType`\>\>
267
+
268
+ ***
269
+
270
+ ### queue()
271
+
272
+ ```ts
273
+ queue<Return>(task): QueuedTask<ThreadType, Return>;
274
+ ```
275
+
276
+ Queue a task and return a promise that resolves once the task has been dequeued,
277
+ started and finished.
278
+
279
+ ### Type Parameters
280
+
281
+ #### Return
282
+
283
+ `Return`
284
+
285
+ ### Parameters
286
+
287
+ #### task
288
+
289
+ `TaskRunFunction`\<`ThreadType`, `Return`\>
290
+
291
+ An async function that takes a thread instance and invokes it.
292
+
293
+ ### Returns
294
+
295
+ [`QueuedTask`](#QueuedTask)\<`ThreadType`, `Return`\>
296
+
297
+ ***
298
+
299
+ ### terminate()
300
+
301
+ ```ts
302
+ terminate(force?): Promise<void>;
303
+ ```
304
+
305
+ Terminate all pool threads.
306
+
307
+ ### Parameters
308
+
309
+ #### force?
310
+
311
+ `boolean`
312
+
313
+ Set to `true` to kill the thread even if it cannot be stopped gracefully.
314
+
315
+ ### Returns
316
+
317
+ `Promise`\<`void`\>
318
+
319
+ ### <a id="QueuedTask"></a>QueuedTask
320
+
321
+ [**@xylabs/threads**](#../../README)
322
+
323
+ ***
324
+
325
+ Task that has been `pool.queued()`-ed.
326
+
327
+ ## Type Parameters
328
+
329
+ ### ThreadType
330
+
331
+ `ThreadType` *extends* [`Thread`](#../type-aliases/Thread)
332
+
333
+ ### Return
334
+
335
+ `Return`
336
+
337
+ ## Properties
338
+
339
+ ### then()
340
+
341
+ ```ts
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
+ `null` | (`value`) => `TResult1` \| `PromiseLike`\<`TResult1`\>
367
+
368
+ #### onrejected?
369
+
370
+ The callback to execute when the Promise is rejected.
371
+
372
+ `null` | (`reason`) => `TResult2` \| `PromiseLike`\<`TResult2`\>
373
+
374
+ ### Returns
375
+
376
+ `Promise`\<`TResult1` \| `TResult2`\>
377
+
378
+ A Promise for the completion of which ever callback is executed.
379
+
380
+ ## Methods
381
+
382
+ ### cancel()
383
+
384
+ ```ts
385
+ cancel(): void;
386
+ ```
387
+
388
+ Queued tasks can be cancelled until the pool starts running them on a worker thread.
389
+
390
+ ### Returns
391
+
392
+ `void`
393
+
394
+ ### <a id="Serializer"></a>Serializer
395
+
396
+ [**@xylabs/threads**](#../../README)
397
+
398
+ ***
399
+
400
+ ## Type Parameters
401
+
402
+ ### Msg
403
+
404
+ `Msg` = [`JsonSerializable`](#../type-aliases/JsonSerializable)
405
+
406
+ ### Input
407
+
408
+ `Input` = `any`
409
+
410
+ ## Methods
411
+
412
+ ### deserialize()
413
+
414
+ ```ts
415
+ deserialize(message): Input;
416
+ ```
417
+
418
+ ### Parameters
419
+
420
+ #### message
421
+
422
+ `Msg`
423
+
424
+ ### Returns
425
+
426
+ `Input`
427
+
428
+ ***
429
+
430
+ ### serialize()
431
+
432
+ ```ts
433
+ serialize(input): Msg;
434
+ ```
435
+
436
+ ### Parameters
437
+
438
+ #### input
439
+
440
+ `Input`
441
+
442
+ ### Returns
443
+
444
+ `Msg`
445
+
446
+ ### <a id="SerializerImplementation"></a>SerializerImplementation
447
+
448
+ [**@xylabs/threads**](#../../README)
449
+
450
+ ***
451
+
452
+ ## Type Parameters
453
+
454
+ ### Msg
455
+
456
+ `Msg` = [`JsonSerializable`](#../type-aliases/JsonSerializable)
457
+
458
+ ### Input
459
+
460
+ `Input` = `any`
461
+
462
+ ## Methods
463
+
464
+ ### deserialize()
465
+
466
+ ```ts
467
+ deserialize(message, defaultDeserialize): Input;
468
+ ```
469
+
470
+ ### Parameters
471
+
472
+ #### message
473
+
474
+ `Msg`
475
+
476
+ #### defaultDeserialize
477
+
478
+ (`msg`) => `Input`
479
+
480
+ ### Returns
481
+
482
+ `Input`
483
+
484
+ ***
485
+
486
+ ### serialize()
487
+
488
+ ```ts
489
+ serialize(input, defaultSerialize): Msg;
490
+ ```
491
+
492
+ ### Parameters
493
+
494
+ #### input
495
+
496
+ `Input`
497
+
498
+ #### defaultSerialize
499
+
500
+ (`inp`) => `Msg`
501
+
502
+ ### Returns
503
+
504
+ `Msg`
505
+
506
+ ### <a id="TransferDescriptor"></a>TransferDescriptor
507
+
508
+ [**@xylabs/threads**](#../../README)
509
+
510
+ ***
511
+
512
+ ## Type Parameters
513
+
514
+ ### T
515
+
516
+ `T` = `any`
517
+
518
+ ## Properties
519
+
520
+ ### \[$transferable\]
521
+
522
+ ```ts
523
+ [$transferable]: true;
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
+ ```
541
+
542
+ ### namespaces
543
+
544
+ ### Pool
545
+
546
+ ### type-aliases
547
+
548
+ ### <a id="Event"></a>Event
549
+
550
+ [**@xylabs/threads**](#../../../../README)
551
+
552
+ ***
553
+
554
+ ```ts
555
+ type Event<ThreadType> = PoolEvent<ThreadType>;
556
+ ```
557
+
558
+ ## Type Parameters
559
+
560
+ ### ThreadType
561
+
562
+ `ThreadType` *extends* [`Thread`](#../../../type-aliases/Thread) = `any`
563
+
564
+ ### <a id="EventType"></a>EventType
565
+
566
+ [**@xylabs/threads**](#../../../../README)
567
+
568
+ ***
569
+
570
+ ```ts
571
+ type EventType = PoolEventType;
572
+ ```
573
+
574
+ ### type-aliases
575
+
576
+ ### <a id="BlobWorker"></a>BlobWorker
577
+
578
+ [**@xylabs/threads**](#../../README)
579
+
580
+ ***
581
+
582
+ ```ts
583
+ type BlobWorker = typeof BlobWorkerClass;
584
+ ```
585
+
586
+ ### <a id="ExposedAs"></a>ExposedAs
587
+
588
+ [**@xylabs/threads**](#../../README)
589
+
590
+ ***
591
+
592
+ ```ts
593
+ type ExposedAs<Exposed> = Exposed extends ArbitraryWorkerInterface ? ArbitraryThreadType : Exposed extends WorkerFunction ? FunctionThread<Parameters<Exposed>, StripAsync<ReturnType<Exposed>>> : Exposed extends WorkerModule<any> ? ModuleThread<Exposed> : never;
594
+ ```
595
+
596
+ ## Type Parameters
597
+
598
+ ### Exposed
599
+
600
+ `Exposed` *extends* `WorkerFunction` \| `WorkerModule`\<`any`\>
601
+
602
+ ### <a id="FunctionThread"></a>FunctionThread
603
+
604
+ [**@xylabs/threads**](#../../README)
605
+
606
+ ***
607
+
608
+ ```ts
609
+ type FunctionThread<Args, ReturnType> = ProxyableFunction<Args, ReturnType> & PrivateThreadProps;
610
+ ```
611
+
612
+ ## Type Parameters
613
+
614
+ ### Args
615
+
616
+ `Args` *extends* `any`[] = `any`[]
617
+
618
+ ### ReturnType
619
+
620
+ `ReturnType` = `any`
621
+
622
+ ### <a id="JsonSerializable"></a>JsonSerializable
623
+
624
+ [**@xylabs/threads**](#../../README)
625
+
626
+ ***
627
+
628
+ ```ts
629
+ type JsonSerializable =
630
+ | JsonSerializablePrimitive
631
+ | JsonSerializablePrimitive[]
632
+ | JsonSerializableObject
633
+ | JsonSerializableObject[];
634
+ ```
635
+
636
+ ### <a id="ModuleThread"></a>ModuleThread
637
+
638
+ [**@xylabs/threads**](#../../README)
639
+
640
+ ***
641
+
642
+ ```ts
643
+ type ModuleThread<Methods> = ModuleProxy<Methods> & PrivateThreadProps;
644
+ ```
645
+
646
+ ## Type Parameters
647
+
648
+ ### Methods
649
+
650
+ `Methods` *extends* `ModuleMethods` = `any`
651
+
652
+ ### <a id="Thread"></a>Thread
653
+
654
+ [**@xylabs/threads**](#../../README)
655
+
656
+ ***
657
+
658
+ ```ts
659
+ type Thread = ThreadType;
660
+ ```
661
+
662
+ ### <a id="Worker"></a>Worker
663
+
664
+ [**@xylabs/threads**](#../../README)
665
+
666
+ ***
667
+
668
+ ```ts
669
+ type Worker = WorkerType;
670
+ ```
671
+
672
+ ### variables
673
+
674
+ ### <a id="BlobWorker"></a>BlobWorker
675
+
676
+ [**@xylabs/threads**](#../../README)
677
+
678
+ ***
679
+
680
+ ```ts
681
+ BlobWorker: typeof BlobWorker;
682
+ ```
683
+
684
+ Separate class to spawn workers from source code blobs or strings.
685
+
686
+ ### <a id="DefaultSerializer"></a>DefaultSerializer
687
+
688
+ [**@xylabs/threads**](#../../README)
689
+
690
+ ***
691
+
692
+ ```ts
693
+ const DefaultSerializer: Serializer<JsonSerializable>;
694
+ ```
695
+
696
+ ### <a id="Pool"></a>Pool
697
+
698
+ [**@xylabs/threads**](#../../README)
699
+
700
+ ***
701
+
702
+ ```ts
703
+ Pool: <ThreadType>(spawnWorker, optionsOrSize?) => WorkerPool<ThreadType> & object;
704
+ ```
705
+
706
+ Thread pool constructor. Creates a new pool and spawns its worker threads.
707
+
708
+ ## Type declaration
709
+
710
+ ### EventType
711
+
712
+ ```ts
713
+ EventType: typeof PoolEventType;
714
+ ```
715
+
716
+ ### <a id="Thread"></a>Thread
717
+
718
+ [**@xylabs/threads**](#../../README)
719
+
720
+ ***
721
+
722
+ ```ts
723
+ Thread: object;
724
+ ```
725
+
726
+ Thread utility functions. Use them to manage or inspect a `spawn()`-ed thread.
727
+
728
+ ## Type declaration
729
+
730
+ ### errors()
731
+
732
+ ```ts
733
+ errors<ThreadT>(thread): Observable<Error>;
734
+ ```
735
+
736
+ Return an observable that can be used to subscribe to all errors happening in the thread.
737
+
738
+ ### Type Parameters
739
+
740
+ #### ThreadT
741
+
742
+ `ThreadT` *extends* `Thread`
743
+
744
+ ### Parameters
745
+
746
+ #### thread
747
+
748
+ `ThreadT`
749
+
750
+ ### Returns
751
+
752
+ `Observable`\<`Error`\>
753
+
754
+ ### events()
755
+
756
+ ```ts
757
+ events<ThreadT>(thread): Observable<WorkerEvent>;
758
+ ```
759
+
760
+ Return an observable that can be used to subscribe to internal events happening in the thread. Useful for debugging.
761
+
762
+ ### Type Parameters
763
+
764
+ #### ThreadT
765
+
766
+ `ThreadT` *extends* `Thread`
767
+
768
+ ### Parameters
769
+
770
+ #### thread
771
+
772
+ `ThreadT`
773
+
774
+ ### Returns
775
+
776
+ `Observable`\<`WorkerEvent`\>
777
+
778
+ ### terminate()
779
+
780
+ ```ts
781
+ terminate<ThreadT>(thread): Promise<void>;
782
+ ```
783
+
784
+ Terminate a thread. Remember to terminate every thread when you are done using it.
785
+
786
+ ### Type Parameters
787
+
788
+ #### ThreadT
789
+
790
+ `ThreadT` *extends* `Thread`
791
+
792
+ ### Parameters
793
+
794
+ #### thread
795
+
796
+ `ThreadT`
797
+
798
+ ### Returns
799
+
800
+ `Promise`\<`void`\>
801
+
802
+ ### <a id="Worker"></a>Worker
803
+
804
+ [**@xylabs/threads**](#../../README)
805
+
806
+ ***
807
+
808
+ ```ts
809
+ Worker: typeof WorkerImplementation;
810
+ ```
811
+
812
+ Worker implementation. Either web worker or a node.js Worker class.
813
+
814
+ ### index-node
815
+
816
+ ### functions
817
+
818
+ ### <a id="isWorkerRuntime"></a>isWorkerRuntime
819
+
820
+ [**@xylabs/threads**](#../../README)
821
+
822
+ ***
823
+
824
+ ```ts
825
+ function isWorkerRuntime(): boolean;
826
+ ```
827
+
828
+ ## Returns
829
+
830
+ `boolean`
831
+
832
+ ### type-aliases
833
+
834
+ ### <a id="BlobWorker"></a>BlobWorker
835
+
836
+ [**@xylabs/threads**](#../../README)
837
+
838
+ ***
839
+
840
+ ```ts
841
+ type BlobWorker = typeof BlobWorkerClass;
842
+ ```
843
+
844
+ ### <a id="Worker"></a>Worker
845
+
846
+ [**@xylabs/threads**](#../../README)
847
+
848
+ ***
849
+
850
+ ```ts
851
+ type Worker = WorkerType;
852
+ ```
853
+
854
+ ### variables
855
+
856
+ ### <a id="BlobWorker"></a>BlobWorker
857
+
858
+ [**@xylabs/threads**](#../../README)
859
+
860
+ ***
861
+
862
+ ```ts
863
+ BlobWorker: typeof BlobWorker;
864
+ ```
865
+
866
+ Separate class to spawn workers from source code blobs or strings.
867
+
868
+ ### <a id="Worker"></a>Worker
869
+
870
+ [**@xylabs/threads**](#../../README)
871
+
872
+ ***
873
+
874
+ ```ts
875
+ Worker: typeof WorkerImplementation;
876
+ ```
877
+
878
+ Worker implementation. Either web worker or a node.js Worker class.
24
879
 
25
880
 
26
881
  Part of [sdk-js](https://www.npmjs.com/package/@xyo-network/sdk-js)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@xylabs/threads",
3
- "version": "4.13.21",
3
+ "version": "4.13.23",
4
4
  "description": "Web workers & worker threads as simple as a function call",
5
5
  "keywords": [
6
6
  "thread",
@@ -113,15 +113,15 @@
113
113
  "package-compile-tsup": "tsup --config tsup.browser.config.ts && tsup --config tsup.node.config.ts && tsup --config tsup.neutral.config.ts"
114
114
  },
115
115
  "dependencies": {
116
- "@xylabs/assert": "^4.13.21",
116
+ "@xylabs/assert": "^4.13.23",
117
117
  "debug": "^4.4.1",
118
118
  "is-observable-2-1-0": "npm:is-observable@2.1.0",
119
119
  "observable-fns": "^0.6.1"
120
120
  },
121
121
  "devDependencies": {
122
- "@swc/core": "^1.12.14",
122
+ "@swc/core": "^1.13.1",
123
123
  "@types/debug": "^4.1.12",
124
- "@types/node": "^24.0.14",
124
+ "@types/node": "^24.0.15",
125
125
  "@xylabs/eslint-config-flat": "^7.0.0",
126
126
  "@xylabs/ts-scripts-yarn3": "^7.0.0",
127
127
  "eslint": "^9.31.0",