/** * This module is a utility framework that simplifies the use of * [Web Workers](https://developer.mozilla.org/docs/Web/API/Web_Workers_API/Using_web_workers) in the * ArcGIS Maps SDK for JavaScript. * * The workers framework takes advantage of multi-core CPUs to perform computationally expensive tasks in * a background thread without interfering with the user interface. This framework loads a script * and provides a calling function in the main thread using the [Connection class](https://developers.arcgis.com/javascript/latest/references/core/core/workers/Connection/). * The Connection can then be used to offload jobs asynchronously from the main thread to workers. * The workers framework returns a Promise once the job is completed. * * > [!WARNING] * > * > **Known Limitations** * > * > Workers do not have access to document, window and parent objects, and cannot directly affect the parent page; this includes * > DOM manipulation. * > * > The [esriRequest](https://developers.arcgis.com/javascript/latest/references/core/request/) module can be used within a worker script with the following exceptions: * > - [esriRequest.options.body](https://developers.arcgis.com/javascript/latest/references/core/request/types/#RequestOptions) `FormData` value is not supported. * > - [esriRequest.options.responseType](https://developers.arcgis.com/javascript/latest/references/core/request/types/#RequestOptions) supported values are: `array-buffer`, `blob`, `json`, `native`, `text` * > - [esriRequest.getHeader](https://developers.arcgis.com/javascript/latest/references/core/request/types/#GetHeader) is not supported. * * @since 4.2 * @see [ES modules custom workers sample](https://github.com/Esri/jsapi-resources/tree/main/templates/geometry-operator-worker) */ import type Connection from "./workers/Connection.js"; import type { AbortOptions } from "./promiseUtils.js"; /** * Indicates how to load the module. See the table below for a list of possible values. * * Possible Value | Description * ---------------|------------ * distributed | The module is loaded in each available worker. Each invocation is handled by an available worker. Use this strategy when the module is stateless and does not need to retain data between invocations. * dedicated | The module is loaded in a single worker, and all invocations are handled by that worker. Use this strategy when the module needs to retain state between invocations or maintain stateful communication with the main thread. * local | Deprecated since 5.2. The module runs on the main thread instead of in a worker. Use this strategy when worker execution is unavailable or not desired. This can affect UI responsiveness. To migrate, call the module’s functions directly on the main thread instead of using `workers.open()`. */ export type OpenStrategy = "distributed" | "dedicated" | "local"; export interface OpenOptions extends AbortOptions { /** * An object containing methods that the worker module can call on the main thread. * In a custom worker class, declare a `remoteClient` property and use * `this.remoteClient.invoke("methodName", data)` to invoke a method on this object. */ client?: any; /** * Indicates whether the module is an ES module. * Set to `true` when loading a custom ES module worker. * The default is `false` for backward compatibility with legacy AMD custom workers. Loading custom workers this way is deprecated. * * @deprecated since version 5.2. This option is temporary and will be removed at version 6.0 when custom worker modules default to ESM. Until then, migrate custom workers to ES modules and set this option to `true`. * @default false * @since 5.2 */ isESM?: boolean; /** * Indicates how to load the module. See [OpenStrategy](https://developers.arcgis.com/javascript/latest/references/core/core/workers/#OpenStrategy) for a list of possible values. * * @default distributed */ strategy?: OpenStrategy; } /** * Opens a connection to workers and loads a script with the workers framework. * * Custom ES module workers can load SDK modules using standard ES module imports * in npm-based applications or `$arcgis.import()` in CDN applications. * Only modules that do not depend on the DOM can be used within a worker. * * @param modulePath - A fully qualified URL to a script to execute with the workers framework. * @param options - Worker options. See properties below for object specifications. * @returns Resolves to an instance of [Connection](https://developers.arcgis.com/javascript/latest/references/core/core/workers/Connection/). * @see [Connection](https://developers.arcgis.com/javascript/latest/references/core/core/workers/Connection/) * @example * import { open } from "@arcgis/core/core/workers.js"; * * // Load an ESM worker module and invoke its "getMaxNumber" function. * const connection = await open(new URL("./workersFolder/Calculator.js", import.meta.url).href, { * isESM: true * }); * try { * // Invoke the worker function and receive its result on the main thread. * const result = await connection.invoke("getMaxNumber", [0, 1, 2, 3, 4]); * console.log(result); * } finally { * connection.close(); * } * * //********************************************************* * // module: workersFolder/Calculator.js * //********************************************************* * export function getMaxNumber(numbers) { * return Math.max(...numbers); * } * @example * // Load an ESM worker module that calls a function on the main thread. * const connection = await open(new URL("./workerScripts/TimeOfDay.js", import.meta.url).href, { * isESM: true, * client: { * getCurrentTime() { * return Date.now(); * } * } * }); * * try { * const result = await connection.invoke("timeOfTheDay"); * console.log(result); * } finally { * connection.close(); * } * * //********************************************************* * // module: workerScripts/TimeOfDay.js * //********************************************************* * export default class TimeOfTheDay { * remoteClient; * * async timeOfTheDay() { * const time = await this.remoteClient.invoke("getCurrentTime"); * return `The time is ${time}`; * } * } * @example * // CustomWorker.js - load an SDK module inside a custom ES module worker in a CDN application. * export async function createPoint(coordinates) { * const Point = await $arcgis.import("@arcgis/core/geometry/Point.js"); * * const point = new Point({ * longitude: coordinates.longitude, * latitude: coordinates.latitude * }); * * // Return serializable data to the main thread. * return point.toJSON(); * } */ export function open(modulePath: string, options?: OpenOptions): Promise;