import { AsyncIOResult, VoidIOResult } from "happy-rusty"; import { AttachSyncChannelOptions, ConnectSyncChannelOptions } from "./shared.mjs"; //#region src/sync/channel/connect.d.ts /** * Connects to a worker and establishes a sync channel for synchronous file system operations. * Must be called before using any sync API functions. * * @param worker - The worker to communicate with. Can be a `Worker` instance, a `URL`, or a URL string. * A URL is loaded as a classic worker unless `workerType: 'module'` is set. * @param options - Optional configuration options for the sync channel. * @returns A promise that resolves with an `AsyncIOResult` containing the `SharedArrayBuffer` when the worker is ready. * The returned buffer can be shared with other contexts (e.g., iframes) via `postMessage`. * @since 1.1.0 * @example * ```typescript * // Connect to worker and get SharedArrayBuffer * const result = await SyncChannel.connect( * new URL('./worker.js', import.meta.url), * { sharedBufferLength: 1024 * 1024, opTimeout: 5000, connectTimeout: 10000 } * ); * result.inspect(sharedBuffer => { * // Share with iframe * iframe.contentWindow.postMessage({ sharedBuffer }, '*'); * }); * ``` */ declare function connectSyncChannel(worker: Worker | URL | string, options?: ConnectSyncChannelOptions): AsyncIOResult; /** * Checks if the sync channel is ready to use. * * @returns `true` if ready, `false` otherwise. * @since 1.11.0 * @example * ```typescript * if (!SyncChannel.isReady()) { * await SyncChannel.connect(new URL('./worker.js', import.meta.url)); * } * ``` */ declare function isSyncChannelReady(): boolean; /** * Disconnects the sync channel and releases the resources owned by this context. * * A worker created by `connect` is terminated. A caller-supplied worker, or the * worker behind a buffer shared with `attach`, is left running so other contexts * that still use that buffer keep working. After disconnecting, `connect` or * `attach` can be called again — this is also the way out of a channel that got * stuck (worker crashed, response never arrived). * * Calling it while not connected is a no-op. * * @returns A `VoidIOResult` indicating success. * @since 2.3.0 * @see {@link connectSyncChannel} for establishing a connection * @example * ```typescript * SyncChannel.disconnect(); * * // A fresh connection can be established afterwards * await SyncChannel.connect(new URL('./worker.js', import.meta.url)); * ``` */ declare function disconnectSyncChannel(): VoidIOResult; /** * Attaches to an existing `SharedArrayBuffer` for synchronous file system operations. * Used to share a sync channel connection with other contexts (e.g., iframes). * * After calling this function, sync APIs (e.g., `readFileSync`, `writeFileSync`) * can be used in the current context without calling `connect`. * * @param sharedBuffer - The `SharedArrayBuffer` received from another context. * @param options - Optional configuration options. * @returns A `VoidIOResult` indicating success or failure. * @since 1.8.5 * @example * ```typescript * // In iframe: receive SharedArrayBuffer from main page * window.addEventListener('message', (event) => { * if (event.data.sharedBuffer) { * const result = SyncChannel.attach(event.data.sharedBuffer, { opTimeout: 5000 }); * if (result.isOk()) { * // Now sync APIs can be used * const fileResult = readTextFileSync('/data/file.txt'); * } * } * }); * ``` */ declare function attachSyncChannel(sharedBuffer: SharedArrayBuffer, options?: AttachSyncChannelOptions): VoidIOResult; //#endregion //#region src/sync/channel/listen.d.ts /** * Starts listening for sync channel requests in a Web Worker. * Waits for a SharedArrayBuffer from the main thread and begins processing requests. * * @returns A `VoidIOResult` indicating success or failure. * @since 1.1.0 * @example * ```typescript * // In worker.js * import { SyncChannel } from 'happy-opfs'; * const result = SyncChannel.listen(); * if (result.isErr()) { * console.error('Failed to start listening:', result.unwrapErr()); * } * ``` */ declare function listenSyncChannel(): VoidIOResult; //#endregion export { attachSyncChannel as attach, connectSyncChannel as connect, disconnectSyncChannel as disconnect, isSyncChannelReady as isReady, listenSyncChannel as listen }; //# sourceMappingURL=SyncChannel.d.mts.map