{"version":3,"file":"createTaskProcessorWorker.js","sources":["../../../../Source/Core/formatError.js","../../../../Source/WorkersES6/createTaskProcessorWorker.js"],"sourcesContent":["import defined from \"./defined.js\";\n\n/**\n * Formats an error object into a String. If available, uses name, message, and stack\n * properties, otherwise, falls back on toString().\n *\n * @function\n *\n * @param {*} object The item to find in the array.\n * @returns {String} A string containing the formatted error.\n */\nfunction formatError(object) {\n let result;\n\n const name = object.name;\n const message = object.message;\n if (defined(name) && defined(message)) {\n result = `${name}: ${message}`;\n } else {\n result = object.toString();\n }\n\n const stack = object.stack;\n if (defined(stack)) {\n result += `\\n${stack}`;\n }\n\n return result;\n}\nexport default formatError;\n","import defaultValue from \"../Core/defaultValue.js\";\nimport defined from \"../Core/defined.js\";\nimport formatError from \"../Core/formatError.js\";\nimport when from \"../ThirdParty/when.js\";\n\n// createXXXGeometry functions may return Geometry or a Promise that resolves to Geometry\n// if the function requires access to ApproximateTerrainHeights.\n// For fully synchronous functions, just wrapping the function call in a `when` Promise doesn't\n// handle errors correctly, hence try-catch\nfunction callAndWrap(workerFunction, parameters, transferableObjects) {\n let resultOrPromise;\n try {\n resultOrPromise = workerFunction(parameters, transferableObjects);\n return resultOrPromise; // errors handled by Promise\n } catch (e) {\n return when.reject(e);\n }\n}\n\n/**\n * Creates an adapter function to allow a calculation function to operate as a Web Worker,\n * paired with TaskProcessor, to receive tasks and return results.\n *\n * @function createTaskProcessorWorker\n *\n * @param {createTaskProcessorWorker.WorkerFunction} workerFunction The calculation function,\n * which takes parameters and returns a result.\n * @returns {createTaskProcessorWorker.TaskProcessorWorkerFunction} A function that adapts the\n * calculation function to work as a Web Worker onmessage listener with TaskProcessor.\n *\n *\n * @example\n * function doCalculation(parameters, transferableObjects) {\n * // calculate some result using the inputs in parameters\n * return result;\n * }\n *\n * return Cesium.createTaskProcessorWorker(doCalculation);\n * // the resulting function is compatible with TaskProcessor\n *\n * @see TaskProcessor\n * @see {@link http://www.w3.org/TR/workers/|Web Workers}\n * @see {@link http://www.w3.org/TR/html5/common-dom-interfaces.html#transferable-objects|Transferable objects}\n */\nfunction createTaskProcessorWorker(workerFunction) {\n let postMessage;\n\n return function (event) {\n const data = event.data;\n\n const transferableObjects = [];\n const responseMessage = {\n id: data.id,\n result: undefined,\n error: undefined,\n };\n\n return when(\n callAndWrap(workerFunction, data.parameters, transferableObjects)\n )\n .then(function (result) {\n responseMessage.result = result;\n })\n .otherwise(function (e) {\n if (e instanceof Error) {\n // Errors can't be posted in a message, copy the properties\n responseMessage.error = {\n name: e.name,\n message: e.message,\n stack: e.stack,\n };\n } else {\n responseMessage.error = e;\n }\n })\n .always(function () {\n if (!defined(postMessage)) {\n postMessage = defaultValue(self.webkitPostMessage, self.postMessage);\n }\n\n if (!data.canTransferArrayBuffer) {\n transferableObjects.length = 0;\n }\n\n try {\n postMessage(responseMessage, transferableObjects);\n } catch (e) {\n // something went wrong trying to post the message, post a simpler\n // error that we can be sure will be cloneable\n responseMessage.result = undefined;\n responseMessage.error = `postMessage failed with error: ${formatError(\n e\n )}\\n with responseMessage: ${JSON.stringify(responseMessage)}`;\n postMessage(responseMessage);\n }\n });\n };\n}\n\n/**\n * A function that performs a calculation in a Web Worker.\n * @callback createTaskProcessorWorker.WorkerFunction\n *\n * @param {Object} parameters Parameters to the calculation.\n * @param {Array} transferableObjects An array that should be filled with references to objects inside\n * the result that should be transferred back to the main document instead of copied.\n * @returns {Object} The result of the calculation.\n *\n * @example\n * function calculate(parameters, transferableObjects) {\n * // perform whatever calculation is necessary.\n * const typedArray = new Float32Array(0);\n *\n * // typed arrays are transferable\n * transferableObjects.push(typedArray)\n *\n * return {\n * typedArray : typedArray\n * };\n * }\n */\n\n/**\n * A Web Worker message event handler function that handles the interaction with TaskProcessor,\n * specifically, task ID management and posting a response message containing the result.\n * @callback createTaskProcessorWorker.TaskProcessorWorkerFunction\n *\n * @param {Object} event The onmessage event object.\n */\nexport default createTaskProcessorWorker;\n"],"names":["defined","when","defaultValue"],"mappings":";;;;;;;;;;;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