/** * Web Worker entry point for activity execution. * * Sets up `self.onmessage` to handle {@link ActivityExecutionRequest} and * posts back {@link ActivityExecutionResult} via `self.postMessage`. Uses * the existing `executeActivity` helper from `activity-runner.ts`. * * @module workers/activity-worker-entry */ /** * Function type that resolves an activity name to its handler function, or * returns `undefined` when the activity is not registered. * * Passed to `initializeActivityWorkerMessageLoop` to wire up the message * handler inside a Web Worker. Typically backed by a `Map` built at worker * startup from the activity registration object. * * @example * ```ts * import { type ActivityHandlerLookup } from '@lostgradient/weft'; * * const activities = new Map unknown>([ * ['double', (n: unknown) => (n as number) * 2], * ]); * * const lookup: ActivityHandlerLookup = (name) => activities.get(name); * console.log(lookup('double')); // [Function: double] * console.log(lookup('missing')); // undefined * ``` */ export type ActivityHandlerLookup = (name: string) => ((input: unknown) => unknown) | undefined; /** * Initialize the activity worker message loop. Call this from within a Web * Worker to wire up the activity execution protocol. * * @param getActivity - Resolves an activity name to its function. Typically * backed by a registration map built at worker creation time. * * @example * ```ts * import { initializeActivityWorkerMessageLoop } from '@lostgradient/weft'; * * const activities = new Map unknown>(); * activities.set('greet', (input: unknown) => { * if (typeof input !== 'object' || input === null || !('name' in input)) { * throw new Error('Expected greeting input'); * } * if (typeof input.name !== 'string') { * throw new Error('Expected greeting name'); * } * return `Hello, ${input.name}!`; * }); * * // Call inside a Worker file to start listening for tasks: * initializeActivityWorkerMessageLoop((name) => activities.get(name)); * ``` */ export declare function initializeActivityWorkerMessageLoop(getActivity: ActivityHandlerLookup): void; /** * Create a Blob URL that can be used to spawn an activity Web Worker with * the given activity registrations. * * @param registrations - Map of activity names to handler functions. The * handlers must be serializable (no closures over local state). * @returns A Blob URL string suitable for the `activityExecution.workerUrl` * option. Call {@link revokeActivityWorkerEntryUrl} when the URL is no * longer needed (e.g., during engine disposal) to free the registration. * @throws {Error} If any handler function cannot be safely serialized. * * @example * ```ts * import { createActivityWorkerEntryUrl, revokeActivityWorkerEntryUrl } from '@lostgradient/weft'; * * const registrations = new Map unknown>(); * registrations.set('double', (n: unknown) => (n as number) * 2); * * const url = createActivityWorkerEntryUrl(registrations); * // Pass url to Engine as activityExecution.workerUrl * revokeActivityWorkerEntryUrl(url); // cleanup when done * ``` */ export declare function createActivityWorkerEntryUrl(registrations: Map unknown>): string; /** * Revoke a Blob URL previously created by {@link createActivityWorkerEntryUrl}. * Call this once all workers that need the URL have been constructed (e.g., * during engine disposal) to free the URL registration and prevent leaks. * * @example * ```ts * import { createActivityWorkerEntryUrl, revokeActivityWorkerEntryUrl } from '@lostgradient/weft'; * * const registrations = new Map unknown>(); * registrations.set('greet', (input: unknown) => 'hello'); * const url = createActivityWorkerEntryUrl(registrations); * // After all workers using this URL have been created: * revokeActivityWorkerEntryUrl(url); * ``` */ export declare function revokeActivityWorkerEntryUrl(url: string): void;