1 |
|
2 | import { Observable } from "observable-fns";
|
3 | import { ObservablePromise } from "../observable-promise";
|
4 | import { $errors, $events, $terminate, $worker } from "../symbols";
|
5 | import { TransferDescriptor } from "../transferable";
|
6 | interface ObservableLikeSubscription {
|
7 | unsubscribe(): any;
|
8 | }
|
9 | interface ObservableLike<T> {
|
10 | subscribe(onNext: (value: T) => any, onError?: (error: any) => any, onComplete?: () => any): ObservableLikeSubscription;
|
11 | subscribe(listeners: {
|
12 | next?(value: T): any;
|
13 | error?(error: any): any;
|
14 | complete?(): any;
|
15 | }): ObservableLikeSubscription;
|
16 | }
|
17 | export declare type StripAsync<Type> = Type extends Promise<infer PromiseBaseType> ? PromiseBaseType : Type extends ObservableLike<infer ObservableBaseType> ? ObservableBaseType : Type;
|
18 | export declare type StripTransfer<Type> = Type extends TransferDescriptor<infer BaseType> ? BaseType : Type;
|
19 | export declare type ModuleMethods = {
|
20 | [methodName: string]: (...args: any) => any;
|
21 | };
|
22 | export declare type ProxyableArgs<Args extends any[]> = Args extends [arg0: infer Arg0, ...rest: infer RestArgs] ? [Arg0 extends Transferable ? Arg0 | TransferDescriptor<Arg0> : Arg0, ...RestArgs] : Args;
|
23 | export declare type ProxyableFunction<Args extends any[], ReturnType> = Args extends [] ? () => ObservablePromise<StripTransfer<StripAsync<ReturnType>>> : (...args: ProxyableArgs<Args>) => ObservablePromise<StripTransfer<StripAsync<ReturnType>>>;
|
24 | export declare type ModuleProxy<Methods extends ModuleMethods> = {
|
25 | [method in keyof Methods]: ProxyableFunction<Parameters<Methods[method]>, ReturnType<Methods[method]>>;
|
26 | };
|
27 | export interface PrivateThreadProps {
|
28 | [$errors]: Observable<Error>;
|
29 | [$events]: Observable<WorkerEvent>;
|
30 | [$terminate]: () => Promise<void>;
|
31 | [$worker]: Worker;
|
32 | }
|
33 | export declare type FunctionThread<Args extends any[] = any[], ReturnType = any> = ProxyableFunction<Args, ReturnType> & PrivateThreadProps;
|
34 | export declare type ModuleThread<Methods extends ModuleMethods = any> = ModuleProxy<Methods> & PrivateThreadProps;
|
35 | interface AnyFunctionThread extends PrivateThreadProps {
|
36 | (...args: any[]): ObservablePromise<any>;
|
37 | }
|
38 | interface AnyModuleThread extends PrivateThreadProps {
|
39 | }
|
40 |
|
41 | export declare type Thread = AnyFunctionThread | AnyModuleThread;
|
42 | export declare type TransferList = Transferable[];
|
43 |
|
44 | export interface Worker extends EventTarget {
|
45 | postMessage(value: any, transferList?: TransferList): void;
|
46 |
|
47 | terminate(callback?: (error?: Error, exitCode?: number) => void): void | Promise<number>;
|
48 | }
|
49 | export interface ThreadsWorkerOptions extends WorkerOptions {
|
50 |
|
51 | _baseURL?: string;
|
52 |
|
53 | resourceLimits?: {
|
54 |
|
55 | maxOldGenerationSizeMb?: number;
|
56 |
|
57 | maxYoungGenerationSizeMb?: number;
|
58 |
|
59 | codeRangeSizeMb?: number;
|
60 | };
|
61 |
|
62 | workerData?: any;
|
63 |
|
64 | CORSWorkaround?: boolean;
|
65 | }
|
66 |
|
67 | export declare class WorkerImplementation extends EventTarget implements Worker {
|
68 | constructor(path: string, options?: ThreadsWorkerOptions);
|
69 | postMessage(value: any, transferList?: TransferList): void;
|
70 | terminate(): void | Promise<number>;
|
71 | }
|
72 | /** Class to spawn workers from a blob or source string. */
|
73 | export declare class BlobWorker extends WorkerImplementation {
|
74 | constructor(blob: Blob, options?: ThreadsWorkerOptions);
|
75 | static fromText(source: string, options?: ThreadsWorkerOptions): WorkerImplementation;
|
76 | }
|
77 | export interface ImplementationExport {
|
78 | blob: typeof BlobWorker;
|
79 | default: typeof WorkerImplementation;
|
80 | }
|
81 |
|
82 | export declare enum WorkerEventType {
|
83 | internalError = "internalError",
|
84 | message = "message",
|
85 | termination = "termination"
|
86 | }
|
87 | export interface WorkerInternalErrorEvent {
|
88 | type: WorkerEventType.internalError;
|
89 | error: Error;
|
90 | }
|
91 | export interface WorkerMessageEvent<Data> {
|
92 | type: WorkerEventType.message;
|
93 | data: Data;
|
94 | }
|
95 | export interface WorkerTerminationEvent {
|
96 | type: WorkerEventType.termination;
|
97 | }
|
98 | export declare type WorkerEvent = WorkerInternalErrorEvent | WorkerMessageEvent<any> | WorkerTerminationEvent;
|
99 | export {};
|