///
import { Observable } from "observable-fns";
import { ObservablePromise } from "../observable-promise";
import { $errors, $events, $terminate, $worker } from "../symbols";
import { TransferDescriptor } from "../transferable";
interface ObservableLikeSubscription {
unsubscribe(): any;
}
interface ObservableLike {
subscribe(onNext: (value: T) => any, onError?: (error: any) => any, onComplete?: () => any): ObservableLikeSubscription;
subscribe(listeners: {
next?(value: T): any;
error?(error: any): any;
complete?(): any;
}): ObservableLikeSubscription;
}
export declare type StripAsync = Type extends Promise ? PromiseBaseType : Type extends ObservableLike ? ObservableBaseType : Type;
export declare type StripTransfer = Type extends TransferDescriptor ? BaseType : Type;
export declare type ModuleMethods = {
[methodName: string]: (...args: any) => any;
};
export declare type ProxyableArgs = Args extends [arg0: infer Arg0, ...rest: infer RestArgs] ? [Arg0 extends Transferable ? Arg0 | TransferDescriptor : Arg0, ...RestArgs] : Args;
export declare type ProxyableFunction = Args extends [] ? () => ObservablePromise>> : (...args: ProxyableArgs) => ObservablePromise>>;
export declare type ModuleProxy = {
[method in keyof Methods]: ProxyableFunction, ReturnType>;
};
export interface PrivateThreadProps {
[$errors]: Observable;
[$events]: Observable;
[$terminate]: () => Promise;
[$worker]: Worker;
}
export declare type FunctionThread = ProxyableFunction & PrivateThreadProps;
export declare type ModuleThread = ModuleProxy & PrivateThreadProps;
interface AnyFunctionThread extends PrivateThreadProps {
(...args: any[]): ObservablePromise;
}
interface AnyModuleThread extends PrivateThreadProps {
}
/** Worker thread. Either a `FunctionThread` or a `ModuleThread`. */
export declare type Thread = AnyFunctionThread | AnyModuleThread;
export declare type TransferList = Transferable[];
/** Worker instance. Either a web worker or a node.js Worker provided by `worker_threads` or `tiny-worker`. */
export interface Worker extends EventTarget {
postMessage(value: any, transferList?: TransferList): void;
/** In nodejs 10+ return type is Promise while with tiny-worker and in browser return type is void */
terminate(callback?: (error?: Error, exitCode?: number) => void): void | Promise;
}
export interface ThreadsWorkerOptions extends WorkerOptions {
/** Prefix for the path passed to the Worker constructor. Web worker only. */
_baseURL?: string;
/** Resource limits passed on to Node worker_threads */
resourceLimits?: {
/** The maximum size of the main heap in MB. */
maxOldGenerationSizeMb?: number;
/** The maximum size of a heap space for recently created objects. */
maxYoungGenerationSizeMb?: number;
/** The size of a pre-allocated memory range used for generated code. */
codeRangeSizeMb?: number;
};
/** Data passed on to node.js worker_threads. */
workerData?: any;
/** Whether to apply CORS protection workaround. Defaults to true. */
CORSWorkaround?: boolean;
}
/** Worker implementation. Either web worker or a node.js Worker class. */
export declare class WorkerImplementation extends EventTarget implements Worker {
constructor(path: string, options?: ThreadsWorkerOptions);
postMessage(value: any, transferList?: TransferList): void;
terminate(): void | Promise;
}
/** Class to spawn workers from a blob or source string. */
export declare class BlobWorker extends WorkerImplementation {
constructor(blob: Blob, options?: ThreadsWorkerOptions);
static fromText(source: string, options?: ThreadsWorkerOptions): WorkerImplementation;
}
export interface ImplementationExport {
blob: typeof BlobWorker;
default: typeof WorkerImplementation;
}
/** Event as emitted by worker thread. Subscribe to using `Thread.events(thread)`. */
export declare enum WorkerEventType {
internalError = "internalError",
message = "message",
termination = "termination"
}
export interface WorkerInternalErrorEvent {
type: WorkerEventType.internalError;
error: Error;
}
export interface WorkerMessageEvent {
type: WorkerEventType.message;
data: Data;
}
export interface WorkerTerminationEvent {
type: WorkerEventType.termination;
}
export declare type WorkerEvent = WorkerInternalErrorEvent | WorkerMessageEvent | WorkerTerminationEvent;
export {};