@xylabs/threads 4.13.4 → 4.13.6

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.
@@ -1,4 +1,4 @@
1
- import { F as FunctionThread, M as ModuleThread, S as StripAsync, W as Worker$1, B as BlobWorker$1, a as WorkerImplementation } from './master-D4MAqspp.js';
1
+ import { F as FunctionThread, M as ModuleThread, S as StripAsync, W as Worker$1, B as BlobWorker$1, a as WorkerImplementation } from './master-C8XAGDqb.js';
2
2
  import './master/pool-browser.js';
3
3
  import { W as WorkerFunction, a as WorkerModule } from './worker-04t9iwDh.js';
4
4
  import './master/implementation.browser.js';
@@ -8,13 +8,24 @@ type ArbitraryWorkerInterface = WorkerFunction & WorkerModule<string> & {
8
8
  };
9
9
  type ArbitraryThreadType = FunctionThread<any, any> & ModuleThread<any>;
10
10
  type ExposedToThreadType<Exposed extends WorkerFunction | WorkerModule<any>> = Exposed extends ArbitraryWorkerInterface ? ArbitraryThreadType : Exposed extends WorkerFunction ? FunctionThread<Parameters<Exposed>, StripAsync<ReturnType<Exposed>>> : Exposed extends WorkerModule<any> ? ModuleThread<Exposed> : never;
11
+ /**
12
+ * Spawn a new thread. Takes a fresh worker instance, wraps it in a thin
13
+ * abstraction layer to provide the transparent API and verifies that
14
+ * the worker has initialized successfully.
15
+ *
16
+ * @param worker Instance of `Worker`. Either a web worker, `worker_threads` worker or `tiny-worker` worker.
17
+ * @param [options]
18
+ * @param [options.timeout] Init message timeout. Default: 10000 or set by environment variable.
19
+ */
11
20
  declare function spawn<Exposed extends WorkerFunction | WorkerModule<any> = ArbitraryWorkerInterface>(worker: Worker$1, options?: {
12
21
  timeout?: number;
13
22
  }): Promise<ExposedToThreadType<Exposed>>;
14
23
 
15
24
  type BlobWorker = typeof BlobWorker$1;
25
+ /** Separate class to spawn workers from source code blobs or strings. */
16
26
  declare const BlobWorker: typeof BlobWorker$1;
17
27
  type Worker = Worker$1;
28
+ /** Worker implementation. Either web worker or a node.js Worker class. */
18
29
  declare const Worker: typeof WorkerImplementation;
19
30
 
20
31
  export { BlobWorker as B, type ExposedToThreadType as E, Worker as W, spawn as s };
@@ -1,8 +1,8 @@
1
1
  export { D as DefaultSerializer, J as JsonSerializable, S as Serializer, a as SerializerImplementation, r as registerSerializer } from './common-Cuiya5FG.js';
2
- export { B as BlobWorker, E as ExposedAs, W as Worker, s as spawn } from './index-browser-B8TCfn7g.js';
2
+ export { B as BlobWorker, E as ExposedAs, W as Worker, s as spawn } from './index-browser-C-9qksMi.js';
3
3
  export { Pool, QueuedTask, Thread } from './master/pool-browser.js';
4
- export { a as Transfer, T as TransferDescriptor } from './transferable-Cv9t618f.js';
5
- export { F as FunctionThread, M as ModuleThread } from './master-D4MAqspp.js';
4
+ export { a as Transfer, T as TransferDescriptor } from './transferable-Blu_CzPT.js';
5
+ export { F as FunctionThread, M as ModuleThread } from './master-C8XAGDqb.js';
6
6
  export { isWorkerRuntime } from './master/implementation.browser.js';
7
7
  import './worker-04t9iwDh.js';
8
8
  import 'observable-fns';
@@ -1,6 +1,6 @@
1
- import { I as ImplementationExport } from '../master-D4MAqspp.js';
1
+ import { I as ImplementationExport } from '../master-C8XAGDqb.js';
2
2
  import 'observable-fns';
3
- import '../transferable-Cv9t618f.js';
3
+ import '../transferable-Blu_CzPT.js';
4
4
 
5
5
  declare const defaultPoolSize: number;
6
6
  declare function getWorkerImplementation(): ImplementationExport;
@@ -1,7 +1,7 @@
1
- export { F as FunctionThread, M as ModuleThread } from '../master-D4MAqspp.js';
1
+ export { F as FunctionThread, M as ModuleThread } from '../master-C8XAGDqb.js';
2
2
  export { Pool, Thread } from './pool-browser.js';
3
- export { B as BlobWorker, W as Worker, s as spawn } from '../index-browser-B8TCfn7g.js';
3
+ export { B as BlobWorker, W as Worker, s as spawn } from '../index-browser-C-9qksMi.js';
4
4
  export { isWorkerRuntime } from './implementation.browser.js';
5
5
  import 'observable-fns';
6
- import '../transferable-Cv9t618f.js';
6
+ import '../transferable-Blu_CzPT.js';
7
7
  import '../worker-04t9iwDh.js';
@@ -1,14 +1,19 @@
1
1
  import { Observable } from 'observable-fns';
2
- import { T as Thread$1, b as WorkerEvent } from '../master-D4MAqspp.js';
3
- import '../transferable-Cv9t618f.js';
2
+ import { T as Thread$1, b as WorkerEvent } from '../master-C8XAGDqb.js';
3
+ import '../transferable-Blu_CzPT.js';
4
4
 
5
5
  type Thread = Thread$1;
6
+ /** Thread utility functions. Use them to manage or inspect a `spawn()`-ed thread. */
6
7
  declare const Thread: {
8
+ /** Return an observable that can be used to subscribe to all errors happening in the thread. */
7
9
  errors<ThreadT extends Thread$1>(thread: ThreadT): Observable<Error>;
10
+ /** Return an observable that can be used to subscribe to internal events happening in the thread. Useful for debugging. */
8
11
  events<ThreadT extends Thread$1>(thread: ThreadT): Observable<WorkerEvent>;
12
+ /** Terminate a thread. Remember to terminate every thread when you are done using it. */
9
13
  terminate<ThreadT extends Thread$1>(thread: ThreadT): Promise<void>;
10
14
  };
11
15
 
16
+ /** Pool event type. Specifies the type of each `PoolEvent`. */
12
17
  declare enum PoolEventType {
13
18
  initialized = "initialized",
14
19
  taskCanceled = "taskCanceled",
@@ -20,6 +25,7 @@ declare enum PoolEventType {
20
25
  terminated = "terminated"
21
26
  }
22
27
  type TaskRunFunction<ThreadType extends Thread, Return> = (worker: ThreadType) => Promise<Return>;
28
+ /** Pool event. Subscribe to those events using `pool.events()`. Useful for debugging. */
23
29
  type PoolEvent<ThreadType extends Thread> = {
24
30
  type: PoolEventType.initialized;
25
31
  size: number;
@@ -49,17 +55,33 @@ type PoolEvent<ThreadType extends Thread> = {
49
55
  type: PoolEventType.terminated;
50
56
  remainingQueue: Array<QueuedTask<ThreadType, any>>;
51
57
  };
58
+ /**
59
+ * Task that has been `pool.queued()`-ed.
60
+ */
52
61
  interface QueuedTask<ThreadType extends Thread, Return> {
62
+ /** @private */
53
63
  id: number;
64
+ /** @private */
54
65
  run: TaskRunFunction<ThreadType, Return>;
66
+ /**
67
+ * Queued tasks can be cancelled until the pool starts running them on a worker thread.
68
+ */
55
69
  cancel(): void;
70
+ /**
71
+ * `QueuedTask` is thenable, so you can `await` it.
72
+ * Resolves when the task has successfully been executed. Rejects if the task fails.
73
+ */
56
74
  then: Promise<Return>['then'];
57
75
  }
58
76
 
59
77
  interface PoolOptions {
78
+ /** Maximum no. of tasks to run on one worker thread at a time. Defaults to one. */
60
79
  concurrency?: number;
80
+ /** Maximum no. of jobs to be queued for execution before throwing an error. */
61
81
  maxQueuedJobs?: number;
82
+ /** Gives that pool a name to be used for debug logging, letting you distinguish between log output of different pools. */
62
83
  name?: string;
84
+ /** No. of worker threads to spawn and to be managed by the pool. */
63
85
  size?: number;
64
86
  }
65
87
  declare class WorkerPool<ThreadType extends Thread> implements Pool<ThreadType> {
@@ -85,18 +107,55 @@ declare class WorkerPool<ThreadType extends Thread> implements Pool<ThreadType>
85
107
  queue(taskFunction: TaskRunFunction<ThreadType, any>): QueuedTask<ThreadType, any>;
86
108
  terminate(force?: boolean): Promise<void>;
87
109
  }
110
+ /**
111
+ * Thread pool constructor. Creates a new pool and spawns its worker threads.
112
+ */
88
113
  declare function PoolConstructor<ThreadType extends Thread>(spawnWorker: () => Promise<ThreadType>, optionsOrSize?: number | PoolOptions): WorkerPool<ThreadType>;
89
114
  declare namespace Pool {
90
115
  type Event<ThreadType extends Thread = any> = PoolEvent<ThreadType>;
91
116
  type EventType = PoolEventType;
92
117
  }
118
+ /**
119
+ * Thread pool managing a set of worker threads.
120
+ * Use it to queue tasks that are run on those threads with limited
121
+ * concurrency.
122
+ */
93
123
  interface Pool<ThreadType extends Thread> {
124
+ /**
125
+ * Returns a promise that resolves once the task queue is emptied.
126
+ * Promise will be rejected if any task fails.
127
+ *
128
+ * @param allowResolvingImmediately Set to `true` to resolve immediately if task queue is currently empty.
129
+ */
94
130
  completed(allowResolvingImmediately?: boolean): Promise<any>;
131
+ /**
132
+ * Returns a promise that resolves once the task queue is emptied.
133
+ * Failing tasks will not cause the promise to be rejected.
134
+ *
135
+ * @param allowResolvingImmediately Set to `true` to resolve immediately if task queue is currently empty.
136
+ */
95
137
  settled(allowResolvingImmediately?: boolean): Promise<Error[]>;
138
+ /**
139
+ * Returns an observable that yields pool events.
140
+ */
96
141
  events(): Observable<PoolEvent<ThreadType>>;
142
+ /**
143
+ * Queue a task and return a promise that resolves once the task has been dequeued,
144
+ * started and finished.
145
+ *
146
+ * @param task An async function that takes a thread instance and invokes it.
147
+ */
97
148
  queue<Return>(task: TaskRunFunction<ThreadType, Return>): QueuedTask<ThreadType, Return>;
149
+ /**
150
+ * Terminate all pool threads.
151
+ *
152
+ * @param force Set to `true` to kill the thread even if it cannot be stopped gracefully.
153
+ */
98
154
  terminate(force?: boolean): Promise<void>;
99
155
  }
156
+ /**
157
+ * Thread pool constructor. Creates a new pool and spawns its worker threads.
158
+ */
100
159
  declare const Pool: typeof PoolConstructor & {
101
160
  EventType: typeof PoolEventType;
102
161
  };
@@ -1,11 +1,24 @@
1
1
  import { Observable, SubscriptionObserver, ObservableLike as ObservableLike$1 } from 'observable-fns';
2
- import { $ as $errors, b as $events, c as $terminate, d as $worker, T as TransferDescriptor } from './transferable-Cv9t618f.js';
2
+ import { $ as $errors, b as $events, c as $terminate, d as $worker, T as TransferDescriptor } from './transferable-Blu_CzPT.js';
3
3
 
4
4
  type Initializer<T> = (observer: SubscriptionObserver<T>) => UnsubscribeFn | void;
5
5
  type Thenable<T> = {
6
6
  then: (onFulfilled?: (value: T) => any, onRejected?: (error: any) => any) => any;
7
7
  };
8
8
  type UnsubscribeFn = () => void;
9
+ /**
10
+ * Creates a hybrid, combining the APIs of an Observable and a Promise.
11
+ *
12
+ * It is used to proxy async process states when we are initially not sure
13
+ * if that async process will yield values once (-> Promise) or multiple
14
+ * times (-> Observable).
15
+ *
16
+ * Note that the observable promise inherits some of the observable's characteristics:
17
+ * The `init` function will be called *once for every time anyone subscribes to it*.
18
+ *
19
+ * If this is undesired, derive a hot observable from it using `makeHot()` and
20
+ * subscribe to that.
21
+ */
9
22
  declare class ObservablePromise<T> extends Observable<T> implements Promise<T> {
10
23
  readonly [Symbol.toStringTag] = "[object ObservablePromise]";
11
24
  private initHasRun;
@@ -59,27 +72,39 @@ interface AnyFunctionThread extends PrivateThreadProps {
59
72
  }
60
73
  interface AnyModuleThread extends PrivateThreadProps {
61
74
  }
75
+ /** Worker thread. Either a `FunctionThread` or a `ModuleThread`. */
62
76
  type Thread = AnyFunctionThread | AnyModuleThread;
63
77
  type TransferList = Transferable[];
78
+ /** Worker instance. Either a web worker or a node.js Worker provided by `worker_threads` or `tiny-worker`. */
64
79
  interface Worker extends EventTarget {
65
80
  postMessage(value: any, transferList?: TransferList): void;
81
+ /** In nodejs 10+ return type is Promise while with tiny-worker and in browser return type is void */
66
82
  terminate(callback?: (error?: Error, exitCode?: number) => void): void | Promise<number>;
67
83
  }
68
84
  interface ThreadsWorkerOptions extends WorkerOptions {
85
+ /** Whether to apply CORS protection workaround. Defaults to true. */
69
86
  CORSWorkaround?: boolean;
87
+ /** Prefix for the path passed to the Worker constructor. Web worker only. */
70
88
  _baseURL?: string;
89
+ /** Resource limits passed on to Node worker_threads */
71
90
  resourceLimits?: {
91
+ /** The size of a pre-allocated memory range used for generated code. */
72
92
  codeRangeSizeMb?: number;
93
+ /** The maximum size of the main heap in MB. */
73
94
  maxOldGenerationSizeMb?: number;
95
+ /** The maximum size of a heap space for recently created objects. */
74
96
  maxYoungGenerationSizeMb?: number;
75
97
  };
98
+ /** Data passed on to node.js worker_threads. */
76
99
  workerData?: any;
77
100
  }
101
+ /** Worker implementation. Either web worker or a node.js Worker class. */
78
102
  declare class WorkerImplementation extends EventTarget implements Worker {
79
103
  constructor(path: string, options?: ThreadsWorkerOptions);
80
104
  postMessage(value: any, transferList?: TransferList): void;
81
105
  terminate(): void | Promise<number>;
82
106
  }
107
+ /** Class to spawn workers from a blob or source string. */
83
108
  declare class BlobWorker extends WorkerImplementation {
84
109
  constructor(blob: Blob, options?: ThreadsWorkerOptions);
85
110
  static fromText(source: string, options?: ThreadsWorkerOptions): WorkerImplementation;
@@ -88,6 +113,7 @@ interface ImplementationExport {
88
113
  blob: typeof BlobWorker;
89
114
  default: typeof WorkerImplementation;
90
115
  }
116
+ /** Event as emitted by worker thread. Subscribe to using `Thread.events(thread)`. */
91
117
  declare enum WorkerEventType {
92
118
  internalError = "internalError",
93
119
  message = "message",
@@ -0,0 +1,48 @@
1
+ declare const $errors: unique symbol;
2
+ declare const $events: unique symbol;
3
+ declare const $terminate: unique symbol;
4
+ declare const $transferable: unique symbol;
5
+ declare const $worker: unique symbol;
6
+
7
+ interface TransferDescriptor<T = any> {
8
+ [$transferable]: true;
9
+ send: T;
10
+ transferables: Transferable[];
11
+ }
12
+ /**
13
+ * Mark a transferable object as such, so it will no be serialized and
14
+ * deserialized on messaging with the main thread, but to transfer
15
+ * ownership of it to the receiving thread.
16
+ *
17
+ * Only works with array buffers, message ports and few more special
18
+ * types of objects, but it's much faster than serializing and
19
+ * deserializing them.
20
+ *
21
+ * Note:
22
+ * The transferable object cannot be accessed by this thread again
23
+ * unless the receiving thread transfers it back again!
24
+ *
25
+ * @param transferable Array buffer, message port or similar.
26
+ * @see <https://developers.google.com/web/updates/2011/12/Transferable-Objects-Lightning-Fast>
27
+ */
28
+ declare function Transfer(transferable: Transferable): TransferDescriptor;
29
+ /**
30
+ * Mark transferable objects within an arbitrary object or array as
31
+ * being a transferable object. They will then not be serialized
32
+ * and deserialized on messaging with the main thread, but ownership
33
+ * of them will be tranferred to the receiving thread.
34
+ *
35
+ * Only array buffers, message ports and few more special types of
36
+ * objects can be transferred, but it's much faster than serializing and
37
+ * deserializing them.
38
+ *
39
+ * Note:
40
+ * The transferable object cannot be accessed by this thread again
41
+ * unless the receiving thread transfers it back again!
42
+ *
43
+ * @param transferable Array buffer, message port or similar.
44
+ * @see <https://developers.google.com/web/updates/2011/12/Transferable-Objects-Lightning-Fast>
45
+ */
46
+ declare function Transfer<T>(payload: T, transferables: Transferable[]): TransferDescriptor;
47
+
48
+ export { $errors as $, type TransferDescriptor as T, Transfer as a, $events as b, $terminate as c, $worker as d };
@@ -1,6 +1,6 @@
1
1
  import { A as AbstractedWorkerAPI, W as WorkerFunction, a as WorkerModule } from '../worker-04t9iwDh.js';
2
2
  export { r as registerSerializer } from '../common-Cuiya5FG.js';
3
- export { a as Transfer } from '../transferable-Cv9t618f.js';
3
+ export { a as Transfer } from '../transferable-Blu_CzPT.js';
4
4
 
5
5
  declare const isWorkerRuntime: AbstractedWorkerAPI['isWorkerRuntime'];
6
6
  declare const postMessageToMaster: AbstractedWorkerAPI['postMessageToMaster'];
@@ -1,4 +1,4 @@
1
- import { F as FunctionThread, M as ModuleThread, S as StripAsync, a as Worker } from '../master-DDdg1BKb.js';
1
+ import { F as FunctionThread, M as ModuleThread, S as StripAsync, a as Worker } from '../master-zgnSDvrP.js';
2
2
  import 'observable-fns';
3
3
  import '../observable-promise.js';
4
4
 
@@ -12,6 +12,15 @@ type ArbitraryWorkerInterface = WorkerFunction & WorkerModule<string> & {
12
12
  };
13
13
  type ArbitraryThreadType = FunctionThread<any, any> & ModuleThread<any>;
14
14
  type ExposedToThreadType<Exposed extends WorkerFunction | WorkerModule<any>> = Exposed extends ArbitraryWorkerInterface ? ArbitraryThreadType : Exposed extends WorkerFunction ? FunctionThread<Parameters<Exposed>, StripAsync<ReturnType<Exposed>>> : Exposed extends WorkerModule<any> ? ModuleThread<Exposed> : never;
15
+ /**
16
+ * Spawn a new thread. Takes a fresh worker instance, wraps it in a thin
17
+ * abstraction layer to provide the transparent API and verifies that
18
+ * the worker has initialized successfully.
19
+ *
20
+ * @param worker Instance of `Worker`. Either a web worker, `worker_threads` worker or `tiny-worker` worker.
21
+ * @param [options]
22
+ * @param [options.timeout] Init message timeout. Default: 10000 or set by environment variable.
23
+ */
15
24
  declare function spawn<Exposed extends WorkerFunction | WorkerModule<any> = ArbitraryWorkerInterface>(worker: Worker, options?: {
16
25
  timeout?: number;
17
26
  }): Promise<ExposedToThreadType<Exposed>>;
@@ -1,11 +1,15 @@
1
1
  import { Observable } from 'observable-fns';
2
- import { T as Thread$1, W as WorkerEvent } from '../master-DDdg1BKb.js';
2
+ import { T as Thread$1, W as WorkerEvent } from '../master-zgnSDvrP.js';
3
3
  import '../observable-promise.js';
4
4
 
5
5
  type Thread = Thread$1;
6
+ /** Thread utility functions. Use them to manage or inspect a `spawn()`-ed thread. */
6
7
  declare const Thread: {
8
+ /** Return an observable that can be used to subscribe to all errors happening in the thread. */
7
9
  errors<ThreadT extends Thread$1>(thread: ThreadT): Observable<Error>;
10
+ /** Return an observable that can be used to subscribe to internal events happening in the thread. Useful for debugging. */
8
11
  events<ThreadT extends Thread$1>(thread: ThreadT): Observable<WorkerEvent>;
12
+ /** Terminate a thread. Remember to terminate every thread when you are done using it. */
9
13
  terminate<ThreadT extends Thread$1>(thread: ThreadT): Promise<void>;
10
14
  };
11
15
 
@@ -47,12 +47,16 @@ interface AnyFunctionThread extends PrivateThreadProps {
47
47
  }
48
48
  interface AnyModuleThread extends PrivateThreadProps {
49
49
  }
50
+ /** Worker thread. Either a `FunctionThread` or a `ModuleThread`. */
50
51
  type Thread = AnyFunctionThread | AnyModuleThread;
51
52
  type TransferList = Transferable[];
53
+ /** Worker instance. Either a web worker or a node.js Worker provided by `worker_threads` or `tiny-worker`. */
52
54
  interface Worker extends EventTarget {
53
55
  postMessage(value: any, transferList?: TransferList): void;
56
+ /** In nodejs 10+ return type is Promise while with tiny-worker and in browser return type is void */
54
57
  terminate(callback?: (error?: Error, exitCode?: number) => void): void | Promise<number>;
55
58
  }
59
+ /** Event as emitted by worker thread. Subscribe to using `Thread.events(thread)`. */
56
60
  declare enum WorkerEventType {
57
61
  internalError = "internalError",
58
62
  message = "message",
@@ -5,6 +5,19 @@ type Thenable<T> = {
5
5
  then: (onFulfilled?: (value: T) => any, onRejected?: (error: any) => any) => any;
6
6
  };
7
7
  type UnsubscribeFn = () => void;
8
+ /**
9
+ * Creates a hybrid, combining the APIs of an Observable and a Promise.
10
+ *
11
+ * It is used to proxy async process states when we are initially not sure
12
+ * if that async process will yield values once (-> Promise) or multiple
13
+ * times (-> Observable).
14
+ *
15
+ * Note that the observable promise inherits some of the observable's characteristics:
16
+ * The `init` function will be called *once for every time anyone subscribes to it*.
17
+ *
18
+ * If this is undesired, derive a hot observable from it using `makeHot()` and
19
+ * subscribe to that.
20
+ */
8
21
  declare class ObservablePromise<T> extends Observable<T> implements Promise<T> {
9
22
  readonly [Symbol.toStringTag] = "[object ObservablePromise]";
10
23
  private initHasRun;
@@ -2,6 +2,15 @@ import { Observable, ObservableLike } from 'observable-fns';
2
2
  export { Observable } from 'observable-fns';
3
3
 
4
4
  declare const $observers: unique symbol;
5
+ /**
6
+ * Observable subject. Implements the Observable interface, but also exposes
7
+ * the `next()`, `error()`, `complete()` methods to initiate observable
8
+ * updates "from the outside".
9
+ *
10
+ * Use `Observable.from(subject)` to derive an observable that proxies all
11
+ * values, errors and the completion raised on this subject, but does not
12
+ * expose the `next()`, `error()`, `complete()` methods.
13
+ */
5
14
  declare class Subject<T> extends Observable<T> implements ObservableLike<T> {
6
15
  private [$observers];
7
16
  constructor();
@@ -1,13 +1,17 @@
1
- import { F as FunctionThread, M as ModuleThread, S as StripAsync, a as Worker$1, B as BlobWorker$1, b as WorkerImplementation } from './master-BjjSaJAj.js';
1
+ import { F as FunctionThread, M as ModuleThread, S as StripAsync, a as Worker$1, B as BlobWorker$1, b as WorkerImplementation } from './master-DBU-F6eB.js';
2
2
  import { Observable } from 'observable-fns';
3
- import { T as Thread, P as PoolEvent, a as PoolEventType, b as TaskRunFunction, Q as QueuedTask } from './pool-types-Bzei07Nj.js';
3
+ import { T as Thread, P as PoolEvent, a as PoolEventType, b as TaskRunFunction, Q as QueuedTask } from './pool-types-BnrcRDgC.js';
4
4
  import { W as WorkerFunction, a as WorkerModule } from './worker-04t9iwDh.js';
5
5
  import './master/implementation.node.js';
6
6
 
7
7
  interface PoolOptions {
8
+ /** Maximum no. of tasks to run on one worker thread at a time. Defaults to one. */
8
9
  concurrency?: number;
10
+ /** Maximum no. of jobs to be queued for execution before throwing an error. */
9
11
  maxQueuedJobs?: number;
12
+ /** Gives that pool a name to be used for debug logging, letting you distinguish between log output of different pools. */
10
13
  name?: string;
14
+ /** No. of worker threads to spawn and to be managed by the pool. */
11
15
  size?: number;
12
16
  }
13
17
  declare class WorkerPool<ThreadType extends Thread> implements Pool<ThreadType> {
@@ -33,18 +37,55 @@ declare class WorkerPool<ThreadType extends Thread> implements Pool<ThreadType>
33
37
  queue(taskFunction: TaskRunFunction<ThreadType, any>): QueuedTask<ThreadType, any>;
34
38
  terminate(force?: boolean): Promise<void>;
35
39
  }
40
+ /**
41
+ * Thread pool constructor. Creates a new pool and spawns its worker threads.
42
+ */
36
43
  declare function PoolConstructor<ThreadType extends Thread>(spawnWorker: () => Promise<ThreadType>, optionsOrSize?: number | PoolOptions): WorkerPool<ThreadType>;
37
44
  declare namespace Pool {
38
45
  type Event<ThreadType extends Thread = any> = PoolEvent<ThreadType>;
39
46
  type EventType = PoolEventType;
40
47
  }
48
+ /**
49
+ * Thread pool managing a set of worker threads.
50
+ * Use it to queue tasks that are run on those threads with limited
51
+ * concurrency.
52
+ */
41
53
  interface Pool<ThreadType extends Thread> {
54
+ /**
55
+ * Returns a promise that resolves once the task queue is emptied.
56
+ * Promise will be rejected if any task fails.
57
+ *
58
+ * @param allowResolvingImmediately Set to `true` to resolve immediately if task queue is currently empty.
59
+ */
42
60
  completed(allowResolvingImmediately?: boolean): Promise<any>;
61
+ /**
62
+ * Returns a promise that resolves once the task queue is emptied.
63
+ * Failing tasks will not cause the promise to be rejected.
64
+ *
65
+ * @param allowResolvingImmediately Set to `true` to resolve immediately if task queue is currently empty.
66
+ */
43
67
  settled(allowResolvingImmediately?: boolean): Promise<Error[]>;
68
+ /**
69
+ * Returns an observable that yields pool events.
70
+ */
44
71
  events(): Observable<PoolEvent<ThreadType>>;
72
+ /**
73
+ * Queue a task and return a promise that resolves once the task has been dequeued,
74
+ * started and finished.
75
+ *
76
+ * @param task An async function that takes a thread instance and invokes it.
77
+ */
45
78
  queue<Return>(task: TaskRunFunction<ThreadType, Return>): QueuedTask<ThreadType, Return>;
79
+ /**
80
+ * Terminate all pool threads.
81
+ *
82
+ * @param force Set to `true` to kill the thread even if it cannot be stopped gracefully.
83
+ */
46
84
  terminate(force?: boolean): Promise<void>;
47
85
  }
86
+ /**
87
+ * Thread pool constructor. Creates a new pool and spawns its worker threads.
88
+ */
48
89
  declare const Pool: typeof PoolConstructor & {
49
90
  EventType: typeof PoolEventType;
50
91
  };
@@ -54,13 +95,24 @@ type ArbitraryWorkerInterface = WorkerFunction & WorkerModule<string> & {
54
95
  };
55
96
  type ArbitraryThreadType = FunctionThread<any, any> & ModuleThread<any>;
56
97
  type ExposedToThreadType<Exposed extends WorkerFunction | WorkerModule<any>> = Exposed extends ArbitraryWorkerInterface ? ArbitraryThreadType : Exposed extends WorkerFunction ? FunctionThread<Parameters<Exposed>, StripAsync<ReturnType<Exposed>>> : Exposed extends WorkerModule<any> ? ModuleThread<Exposed> : never;
98
+ /**
99
+ * Spawn a new thread. Takes a fresh worker instance, wraps it in a thin
100
+ * abstraction layer to provide the transparent API and verifies that
101
+ * the worker has initialized successfully.
102
+ *
103
+ * @param worker Instance of `Worker`. Either a web worker, `worker_threads` worker or `tiny-worker` worker.
104
+ * @param [options]
105
+ * @param [options.timeout] Init message timeout. Default: 10000 or set by environment variable.
106
+ */
57
107
  declare function spawn<Exposed extends WorkerFunction | WorkerModule<any> = ArbitraryWorkerInterface>(worker: Worker$1, options?: {
58
108
  timeout?: number;
59
109
  }): Promise<ExposedToThreadType<Exposed>>;
60
110
 
61
111
  type BlobWorker = typeof BlobWorker$1;
112
+ /** Separate class to spawn workers from source code blobs or strings. */
62
113
  declare const BlobWorker: typeof BlobWorker$1;
63
114
  type Worker = Worker$1;
115
+ /** Worker implementation. Either web worker or a node.js Worker class. */
64
116
  declare const Worker: typeof WorkerImplementation;
65
117
 
66
118
  export { BlobWorker as B, type ExposedToThreadType as E, Pool as P, Worker as W, spawn as s };
@@ -1,8 +1,8 @@
1
1
  export { D as DefaultSerializer, J as JsonSerializable, S as Serializer, a as SerializerImplementation, r as registerSerializer } from './common-Cuiya5FG.js';
2
- export { B as BlobWorker, E as ExposedAs, P as Pool, W as Worker, s as spawn } from './index-node-DB1sNl0d.js';
3
- export { Q as QueuedTask, T as Thread } from './pool-types-Bzei07Nj.js';
4
- export { a as Transfer, T as TransferDescriptor } from './transferable-Cv9t618f.js';
5
- export { F as FunctionThread, M as ModuleThread } from './master-BjjSaJAj.js';
2
+ export { B as BlobWorker, E as ExposedAs, P as Pool, W as Worker, s as spawn } from './index-node-COoRYkbY.js';
3
+ export { Q as QueuedTask, T as Thread } from './pool-types-BnrcRDgC.js';
4
+ export { a as Transfer, T as TransferDescriptor } from './transferable-Blu_CzPT.js';
5
+ export { F as FunctionThread, M as ModuleThread } from './master-DBU-F6eB.js';
6
6
  export { isWorkerRuntime } from './master/implementation.node.js';
7
7
  import 'observable-fns';
8
8
  import './worker-04t9iwDh.js';
@@ -1,6 +1,6 @@
1
- import { I as ImplementationExport } from '../master-BjjSaJAj.js';
1
+ import { I as ImplementationExport } from '../master-DBU-F6eB.js';
2
2
  import 'observable-fns';
3
- import '../transferable-Cv9t618f.js';
3
+ import '../transferable-Blu_CzPT.js';
4
4
 
5
5
  declare const defaultPoolSize: number;
6
6
  declare function getWorkerImplementation(): ImplementationExport;
@@ -1,7 +1,7 @@
1
- export { F as FunctionThread, M as ModuleThread } from '../master-BjjSaJAj.js';
2
- export { B as BlobWorker, P as Pool, W as Worker, s as spawn } from '../index-node-DB1sNl0d.js';
3
- export { T as Thread } from '../pool-types-Bzei07Nj.js';
1
+ export { F as FunctionThread, M as ModuleThread } from '../master-DBU-F6eB.js';
2
+ export { B as BlobWorker, P as Pool, W as Worker, s as spawn } from '../index-node-COoRYkbY.js';
3
+ export { T as Thread } from '../pool-types-BnrcRDgC.js';
4
4
  export { isWorkerRuntime } from './implementation.node.js';
5
5
  import 'observable-fns';
6
- import '../transferable-Cv9t618f.js';
6
+ import '../transferable-Blu_CzPT.js';
7
7
  import '../worker-04t9iwDh.js';
@@ -1,12 +1,16 @@
1
1
  import { Observable } from 'observable-fns';
2
- import { T as Thread, P as PoolEvent, a as PoolEventType, b as TaskRunFunction, Q as QueuedTask } from '../pool-types-Bzei07Nj.js';
3
- import '../master-BjjSaJAj.js';
4
- import '../transferable-Cv9t618f.js';
2
+ import { T as Thread, P as PoolEvent, a as PoolEventType, b as TaskRunFunction, Q as QueuedTask } from '../pool-types-BnrcRDgC.js';
3
+ import '../master-DBU-F6eB.js';
4
+ import '../transferable-Blu_CzPT.js';
5
5
 
6
6
  interface PoolOptions {
7
+ /** Maximum no. of tasks to run on one worker thread at a time. Defaults to one. */
7
8
  concurrency?: number;
9
+ /** Maximum no. of jobs to be queued for execution before throwing an error. */
8
10
  maxQueuedJobs?: number;
11
+ /** Gives that pool a name to be used for debug logging, letting you distinguish between log output of different pools. */
9
12
  name?: string;
13
+ /** No. of worker threads to spawn and to be managed by the pool. */
10
14
  size?: number;
11
15
  }
12
16
  declare class WorkerPool<ThreadType extends Thread> implements Pool<ThreadType> {
@@ -32,18 +36,55 @@ declare class WorkerPool<ThreadType extends Thread> implements Pool<ThreadType>
32
36
  queue(taskFunction: TaskRunFunction<ThreadType, any>): QueuedTask<ThreadType, any>;
33
37
  terminate(force?: boolean): Promise<void>;
34
38
  }
39
+ /**
40
+ * Thread pool constructor. Creates a new pool and spawns its worker threads.
41
+ */
35
42
  declare function PoolConstructor<ThreadType extends Thread>(spawnWorker: () => Promise<ThreadType>, optionsOrSize?: number | PoolOptions): WorkerPool<ThreadType>;
36
43
  declare namespace Pool {
37
44
  type Event<ThreadType extends Thread = any> = PoolEvent<ThreadType>;
38
45
  type EventType = PoolEventType;
39
46
  }
47
+ /**
48
+ * Thread pool managing a set of worker threads.
49
+ * Use it to queue tasks that are run on those threads with limited
50
+ * concurrency.
51
+ */
40
52
  interface Pool<ThreadType extends Thread> {
53
+ /**
54
+ * Returns a promise that resolves once the task queue is emptied.
55
+ * Promise will be rejected if any task fails.
56
+ *
57
+ * @param allowResolvingImmediately Set to `true` to resolve immediately if task queue is currently empty.
58
+ */
41
59
  completed(allowResolvingImmediately?: boolean): Promise<any>;
60
+ /**
61
+ * Returns a promise that resolves once the task queue is emptied.
62
+ * Failing tasks will not cause the promise to be rejected.
63
+ *
64
+ * @param allowResolvingImmediately Set to `true` to resolve immediately if task queue is currently empty.
65
+ */
42
66
  settled(allowResolvingImmediately?: boolean): Promise<Error[]>;
67
+ /**
68
+ * Returns an observable that yields pool events.
69
+ */
43
70
  events(): Observable<PoolEvent<ThreadType>>;
71
+ /**
72
+ * Queue a task and return a promise that resolves once the task has been dequeued,
73
+ * started and finished.
74
+ *
75
+ * @param task An async function that takes a thread instance and invokes it.
76
+ */
44
77
  queue<Return>(task: TaskRunFunction<ThreadType, Return>): QueuedTask<ThreadType, Return>;
78
+ /**
79
+ * Terminate all pool threads.
80
+ *
81
+ * @param force Set to `true` to kill the thread even if it cannot be stopped gracefully.
82
+ */
45
83
  terminate(force?: boolean): Promise<void>;
46
84
  }
85
+ /**
86
+ * Thread pool constructor. Creates a new pool and spawns its worker threads.
87
+ */
47
88
  declare const Pool: typeof PoolConstructor & {
48
89
  EventType: typeof PoolEventType;
49
90
  };
@@ -1,11 +1,24 @@
1
1
  import { Observable, SubscriptionObserver, ObservableLike as ObservableLike$1 } from 'observable-fns';
2
- import { $ as $errors, b as $events, c as $terminate, d as $worker, T as TransferDescriptor } from './transferable-Cv9t618f.js';
2
+ import { $ as $errors, b as $events, c as $terminate, d as $worker, T as TransferDescriptor } from './transferable-Blu_CzPT.js';
3
3
 
4
4
  type Initializer<T> = (observer: SubscriptionObserver<T>) => UnsubscribeFn | void;
5
5
  type Thenable<T> = {
6
6
  then: (onFulfilled?: (value: T) => any, onRejected?: (error: any) => any) => any;
7
7
  };
8
8
  type UnsubscribeFn = () => void;
9
+ /**
10
+ * Creates a hybrid, combining the APIs of an Observable and a Promise.
11
+ *
12
+ * It is used to proxy async process states when we are initially not sure
13
+ * if that async process will yield values once (-> Promise) or multiple
14
+ * times (-> Observable).
15
+ *
16
+ * Note that the observable promise inherits some of the observable's characteristics:
17
+ * The `init` function will be called *once for every time anyone subscribes to it*.
18
+ *
19
+ * If this is undesired, derive a hot observable from it using `makeHot()` and
20
+ * subscribe to that.
21
+ */
9
22
  declare class ObservablePromise<T> extends Observable<T> implements Promise<T> {
10
23
  readonly [Symbol.toStringTag] = "[object ObservablePromise]";
11
24
  private initHasRun;
@@ -59,27 +72,39 @@ interface AnyFunctionThread extends PrivateThreadProps {
59
72
  }
60
73
  interface AnyModuleThread extends PrivateThreadProps {
61
74
  }
75
+ /** Worker thread. Either a `FunctionThread` or a `ModuleThread`. */
62
76
  type Thread = AnyFunctionThread | AnyModuleThread;
63
77
  type TransferList = Transferable[];
78
+ /** Worker instance. Either a web worker or a node.js Worker provided by `worker_threads` or `tiny-worker`. */
64
79
  interface Worker extends EventTarget {
65
80
  postMessage(value: any, transferList?: TransferList): void;
81
+ /** In nodejs 10+ return type is Promise while with tiny-worker and in browser return type is void */
66
82
  terminate(callback?: (error?: Error, exitCode?: number) => void): void | Promise<number>;
67
83
  }
68
84
  interface ThreadsWorkerOptions extends WorkerOptions {
85
+ /** Whether to apply CORS protection workaround. Defaults to true. */
69
86
  CORSWorkaround?: boolean;
87
+ /** Prefix for the path passed to the Worker constructor. Web worker only. */
70
88
  _baseURL?: string;
89
+ /** Resource limits passed on to Node worker_threads */
71
90
  resourceLimits?: {
91
+ /** The size of a pre-allocated memory range used for generated code. */
72
92
  codeRangeSizeMb?: number;
93
+ /** The maximum size of the main heap in MB. */
73
94
  maxOldGenerationSizeMb?: number;
95
+ /** The maximum size of a heap space for recently created objects. */
74
96
  maxYoungGenerationSizeMb?: number;
75
97
  };
98
+ /** Data passed on to node.js worker_threads. */
76
99
  workerData?: any;
77
100
  }
101
+ /** Worker implementation. Either web worker or a node.js Worker class. */
78
102
  declare class WorkerImplementation extends EventTarget implements Worker {
79
103
  constructor(path: string, options?: ThreadsWorkerOptions);
80
104
  postMessage(value: any, transferList?: TransferList): void;
81
105
  terminate(): void | Promise<number>;
82
106
  }
107
+ /** Class to spawn workers from a blob or source string. */
83
108
  declare class BlobWorker extends WorkerImplementation {
84
109
  constructor(blob: Blob, options?: ThreadsWorkerOptions);
85
110
  static fromText(source: string, options?: ThreadsWorkerOptions): WorkerImplementation;
@@ -88,6 +113,7 @@ interface ImplementationExport {
88
113
  blob: typeof BlobWorker;
89
114
  default: typeof WorkerImplementation;
90
115
  }
116
+ /** Event as emitted by worker thread. Subscribe to using `Thread.events(thread)`. */
91
117
  declare enum WorkerEventType {
92
118
  internalError = "internalError",
93
119
  message = "message",
@@ -1,13 +1,18 @@
1
1
  import { Observable } from 'observable-fns';
2
- import { T as Thread$1, W as WorkerEvent } from './master-BjjSaJAj.js';
2
+ import { T as Thread$1, W as WorkerEvent } from './master-DBU-F6eB.js';
3
3
 
4
4
  type Thread = Thread$1;
5
+ /** Thread utility functions. Use them to manage or inspect a `spawn()`-ed thread. */
5
6
  declare const Thread: {
7
+ /** Return an observable that can be used to subscribe to all errors happening in the thread. */
6
8
  errors<ThreadT extends Thread$1>(thread: ThreadT): Observable<Error>;
9
+ /** Return an observable that can be used to subscribe to internal events happening in the thread. Useful for debugging. */
7
10
  events<ThreadT extends Thread$1>(thread: ThreadT): Observable<WorkerEvent>;
11
+ /** Terminate a thread. Remember to terminate every thread when you are done using it. */
8
12
  terminate<ThreadT extends Thread$1>(thread: ThreadT): Promise<void>;
9
13
  };
10
14
 
15
+ /** Pool event type. Specifies the type of each `PoolEvent`. */
11
16
  declare enum PoolEventType {
12
17
  initialized = "initialized",
13
18
  taskCanceled = "taskCanceled",
@@ -19,6 +24,7 @@ declare enum PoolEventType {
19
24
  terminated = "terminated"
20
25
  }
21
26
  type TaskRunFunction<ThreadType extends Thread, Return> = (worker: ThreadType) => Promise<Return>;
27
+ /** Pool event. Subscribe to those events using `pool.events()`. Useful for debugging. */
22
28
  type PoolEvent<ThreadType extends Thread> = {
23
29
  type: PoolEventType.initialized;
24
30
  size: number;
@@ -48,10 +54,22 @@ type PoolEvent<ThreadType extends Thread> = {
48
54
  type: PoolEventType.terminated;
49
55
  remainingQueue: Array<QueuedTask<ThreadType, any>>;
50
56
  };
57
+ /**
58
+ * Task that has been `pool.queued()`-ed.
59
+ */
51
60
  interface QueuedTask<ThreadType extends Thread, Return> {
61
+ /** @private */
52
62
  id: number;
63
+ /** @private */
53
64
  run: TaskRunFunction<ThreadType, Return>;
65
+ /**
66
+ * Queued tasks can be cancelled until the pool starts running them on a worker thread.
67
+ */
54
68
  cancel(): void;
69
+ /**
70
+ * `QueuedTask` is thenable, so you can `await` it.
71
+ * Resolves when the task has successfully been executed. Rejects if the task fails.
72
+ */
55
73
  then: Promise<Return>['then'];
56
74
  }
57
75
 
@@ -0,0 +1,48 @@
1
+ declare const $errors: unique symbol;
2
+ declare const $events: unique symbol;
3
+ declare const $terminate: unique symbol;
4
+ declare const $transferable: unique symbol;
5
+ declare const $worker: unique symbol;
6
+
7
+ interface TransferDescriptor<T = any> {
8
+ [$transferable]: true;
9
+ send: T;
10
+ transferables: Transferable[];
11
+ }
12
+ /**
13
+ * Mark a transferable object as such, so it will no be serialized and
14
+ * deserialized on messaging with the main thread, but to transfer
15
+ * ownership of it to the receiving thread.
16
+ *
17
+ * Only works with array buffers, message ports and few more special
18
+ * types of objects, but it's much faster than serializing and
19
+ * deserializing them.
20
+ *
21
+ * Note:
22
+ * The transferable object cannot be accessed by this thread again
23
+ * unless the receiving thread transfers it back again!
24
+ *
25
+ * @param transferable Array buffer, message port or similar.
26
+ * @see <https://developers.google.com/web/updates/2011/12/Transferable-Objects-Lightning-Fast>
27
+ */
28
+ declare function Transfer(transferable: Transferable): TransferDescriptor;
29
+ /**
30
+ * Mark transferable objects within an arbitrary object or array as
31
+ * being a transferable object. They will then not be serialized
32
+ * and deserialized on messaging with the main thread, but ownership
33
+ * of them will be tranferred to the receiving thread.
34
+ *
35
+ * Only array buffers, message ports and few more special types of
36
+ * objects can be transferred, but it's much faster than serializing and
37
+ * deserializing them.
38
+ *
39
+ * Note:
40
+ * The transferable object cannot be accessed by this thread again
41
+ * unless the receiving thread transfers it back again!
42
+ *
43
+ * @param transferable Array buffer, message port or similar.
44
+ * @see <https://developers.google.com/web/updates/2011/12/Transferable-Objects-Lightning-Fast>
45
+ */
46
+ declare function Transfer<T>(payload: T, transferables: Transferable[]): TransferDescriptor;
47
+
48
+ export { $errors as $, type TransferDescriptor as T, Transfer as a, $events as b, $terminate as c, $worker as d };
@@ -2,7 +2,7 @@ import { A as AbstractedWorkerAPI, W as WorkerFunction, a as WorkerModule } from
2
2
  import * as worker_threads from 'worker_threads';
3
3
  import { MessagePort } from 'node:worker_threads';
4
4
  export { r as registerSerializer } from '../common-Cuiya5FG.js';
5
- export { a as Transfer } from '../transferable-Cv9t618f.js';
5
+ export { a as Transfer } from '../transferable-Blu_CzPT.js';
6
6
 
7
7
  declare const isWorkerRuntime: AbstractedWorkerAPI['isWorkerRuntime'];
8
8
  declare const postMessageToMaster: AbstractedWorkerAPI['postMessageToMaster'];
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@xylabs/threads",
3
- "version": "4.13.4",
3
+ "version": "4.13.6",
4
4
  "description": "Web workers & worker threads as simple as a function call",
5
5
  "keywords": [
6
6
  "thread",
@@ -113,7 +113,7 @@
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.4",
116
+ "@xylabs/assert": "^4.13.6",
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"
@@ -122,8 +122,8 @@
122
122
  "@swc/core": "^1.12.11",
123
123
  "@types/debug": "^4.1.12",
124
124
  "@types/node": "^24.0.13",
125
- "@xylabs/eslint-config-flat": "^7.0.0-rc.7",
126
- "@xylabs/ts-scripts-yarn3": "^7.0.0-rc.7",
125
+ "@xylabs/eslint-config-flat": "^7.0.0-rc.8",
126
+ "@xylabs/ts-scripts-yarn3": "^7.0.0-rc.8",
127
127
  "tiny-worker": "^2.3.0",
128
128
  "tsup": "^8.5.0",
129
129
  "typescript": "^5.8.3"
@@ -1,15 +0,0 @@
1
- declare const $errors: unique symbol;
2
- declare const $events: unique symbol;
3
- declare const $terminate: unique symbol;
4
- declare const $transferable: unique symbol;
5
- declare const $worker: unique symbol;
6
-
7
- interface TransferDescriptor<T = any> {
8
- [$transferable]: true;
9
- send: T;
10
- transferables: Transferable[];
11
- }
12
- declare function Transfer(transferable: Transferable): TransferDescriptor;
13
- declare function Transfer<T>(payload: T, transferables: Transferable[]): TransferDescriptor;
14
-
15
- export { $errors as $, type TransferDescriptor as T, Transfer as a, $events as b, $terminate as c, $worker as d };
@@ -1,15 +0,0 @@
1
- declare const $errors: unique symbol;
2
- declare const $events: unique symbol;
3
- declare const $terminate: unique symbol;
4
- declare const $transferable: unique symbol;
5
- declare const $worker: unique symbol;
6
-
7
- interface TransferDescriptor<T = any> {
8
- [$transferable]: true;
9
- send: T;
10
- transferables: Transferable[];
11
- }
12
- declare function Transfer(transferable: Transferable): TransferDescriptor;
13
- declare function Transfer<T>(payload: T, transferables: Transferable[]): TransferDescriptor;
14
-
15
- export { $errors as $, type TransferDescriptor as T, Transfer as a, $events as b, $terminate as c, $worker as d };