function createTaskProcessorWorker(workerFunction) {
  async function onMessageHandler({ data }) {
    const transferableObjects = [];
    const responseMessage = {
      id: data.id,
      result: undefined,
      error: undefined,
    };

    self.CESIUM_BASE_URL = data.baseUrl;

    try {
      const result = await workerFunction(data.parameters, transferableObjects);
      responseMessage.result = result;
    } catch (error) {
      if (error instanceof Error) {
        responseMessage.error = {
          name: error.name,
          message: error.message,
          stack: error.stack,
        };
      } else {
        responseMessage.error = error;
      }
    }

    if (!data.canTransferArrayBuffer) {
      transferableObjects.length = 0;
    }

    try {
      postMessage(responseMessage, transferableObjects);
    } catch (error) {
      // something went wrong trying to post the message, post a simpler
      // error that we can be sure will be cloneable
      responseMessage.result = undefined;
      responseMessage.error = `postMessage failed with error: ${formatError(
        error,
      )}\n  with responseMessage: ${JSON.stringify(responseMessage)}`;
      postMessage(responseMessage);
    }
  }

  function onMessageErrorHandler(event) {
    postMessage({
      id: event.data?.id,
      error: `postMessage failed with error: ${JSON.stringify(event)}`,
    });
  }

  self.onmessage = onMessageHandler;
  self.onmessageerror = onMessageErrorHandler;
  return self;
}
Creates an adapter function to allow a calculation function to operate as a Web Worker,
paired with TaskProcessor, to receive tasks and return results.

@function createTaskProcessorWorker

@param {createTaskProcessorWorker.WorkerFunction} workerFunction The calculation function,
       which takes parameters and returns a result.
@returns {createTaskProcessorWorker.TaskProcessorWorkerFunction} A function that adapts the
         calculation function to work as a Web Worker onmessage listener with TaskProcessor.


@example
function doCalculation(parameters, transferableObjects) {
  // calculate some result using the inputs in parameters
  return result;
}

return Cesium.createTaskProcessorWorker(doCalculation);
// the resulting function is compatible with TaskProcessor

@see TaskProcessor
@see {@link http://www.w3.org/TR/workers/|Web Workers}
@see {@link http://www.w3.org/TR/html5/common-dom-interfaces.html#transferable-objects|Transferable objects}