UNPKG

javascript-kit-swift

Version:

A runtime library of JavaScriptKit which is Swift framework to interact with JavaScript through WebAssembly.

122 lines (121 loc) 4.42 kB
import { ImportedFunctions } from "./types.js"; declare type MainToWorkerMessage = { type: "wake"; }; declare type WorkerToMainMessage = { type: "job"; data: number; }; /** * A thread channel is a set of functions that are used to communicate between * the main thread and the worker thread. The main thread and the worker thread * can send messages to each other using these functions. * * @example * ```javascript * // worker.js * const runtime = new SwiftRuntime({ * threadChannel: { * postMessageToMainThread: postMessage, * listenMessageFromMainThread: (listener) => { * self.onmessage = (event) => { * listener(event.data); * }; * } * } * }); * * // main.js * const worker = new Worker("worker.js"); * const runtime = new SwiftRuntime({ * threadChannel: { * postMessageToWorkerThread: (tid, data) => { * worker.postMessage(data); * }, * listenMessageFromWorkerThread: (tid, listener) => { * worker.onmessage = (event) => { listener(event.data); * }; * } * } * }); * ``` */ export declare type SwiftRuntimeThreadChannel = { /** * This function is used to send messages from the worker thread to the main thread. * The message submitted by this function is expected to be listened by `listenMessageFromWorkerThread`. * @param message The message to be sent to the main thread. */ postMessageToMainThread: (message: WorkerToMainMessage) => void; /** * This function is expected to be set in the worker thread and should listen * to messages from the main thread sent by `postMessageToWorkerThread`. * @param listener The listener function to be called when a message is received from the main thread. */ listenMessageFromMainThread: (listener: (message: MainToWorkerMessage) => void) => void; } | { /** * This function is expected to be set in the main thread. * The message submitted by this function is expected to be listened by `listenMessageFromMainThread`. * @param tid The thread ID of the worker thread. * @param message The message to be sent to the worker thread. */ postMessageToWorkerThread: (tid: number, message: MainToWorkerMessage) => void; /** * This function is expected to be set in the main thread and shuold listen * to messsages sent by `postMessageToMainThread` from the worker thread. * @param tid The thread ID of the worker thread. * @param listener The listener function to be called when a message is received from the worker thread. */ listenMessageFromWorkerThread: (tid: number, listener: (message: WorkerToMainMessage) => void) => void; /** * This function is expected to be set in the main thread and called * when the worker thread is terminated. * @param tid The thread ID of the worker thread. */ terminateWorkerThread?: (tid: number) => void; }; export declare type SwiftRuntimeOptions = { /** * If `true`, the memory space of the WebAssembly instance can be shared * between the main thread and the worker thread. */ sharedMemory?: boolean; /** * The thread channel is a set of functions that are used to communicate * between the main thread and the worker thread. */ threadChannel?: SwiftRuntimeThreadChannel; }; export declare class SwiftRuntime { private _instance; private _memory; private _closureDeallocator; private options; private version; private textDecoder; private textEncoder; /** The thread ID of the current thread. */ private tid; constructor(options?: SwiftRuntimeOptions); setInstance(instance: WebAssembly.Instance): void; main(): void; /** * Start a new thread with the given `tid` and `startArg`, which * is forwarded to the `wasi_thread_start` function. * This function is expected to be called from the spawned Web Worker thread. */ startThread(tid: number, startArg: number): void; private get instance(); private get exports(); private get memory(); private get closureDeallocator(); private callHostFunction; /** @deprecated Use `wasmImports` instead */ importObjects: () => ImportedFunctions; get wasmImports(): ImportedFunctions; private postMessageToMainThread; private postMessageToWorkerThread; } export {};