createTaskProcessorWorker.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /* This file is automatically rebuilt by the Cesium build process. */
  2. define(['./defaultValue-94c3e563'], (function (defaultValue) { 'use strict';
  3. /**
  4. * Formats an error object into a String. If available, uses name, message, and stack
  5. * properties, otherwise, falls back on toString().
  6. *
  7. * @function
  8. *
  9. * @param {*} object The item to find in the array.
  10. * @returns {String} A string containing the formatted error.
  11. */
  12. function formatError(object) {
  13. let result;
  14. const name = object.name;
  15. const message = object.message;
  16. if (defaultValue.defined(name) && defaultValue.defined(message)) {
  17. result = `${name}: ${message}`;
  18. } else {
  19. result = object.toString();
  20. }
  21. const stack = object.stack;
  22. if (defaultValue.defined(stack)) {
  23. result += `\n${stack}`;
  24. }
  25. return result;
  26. }
  27. // createXXXGeometry functions may return Geometry or a Promise that resolves to Geometry
  28. // if the function requires access to ApproximateTerrainHeights.
  29. // For fully synchronous functions, just wrapping the function call in a Promise doesn't
  30. // handle errors correctly, hence try-catch
  31. function callAndWrap(workerFunction, parameters, transferableObjects) {
  32. let resultOrPromise;
  33. try {
  34. resultOrPromise = workerFunction(parameters, transferableObjects);
  35. return resultOrPromise; // errors handled by Promise
  36. } catch (e) {
  37. return Promise.reject(e);
  38. }
  39. }
  40. /**
  41. * Creates an adapter function to allow a calculation function to operate as a Web Worker,
  42. * paired with TaskProcessor, to receive tasks and return results.
  43. *
  44. * @function createTaskProcessorWorker
  45. *
  46. * @param {createTaskProcessorWorker.WorkerFunction} workerFunction The calculation function,
  47. * which takes parameters and returns a result.
  48. * @returns {createTaskProcessorWorker.TaskProcessorWorkerFunction} A function that adapts the
  49. * calculation function to work as a Web Worker onmessage listener with TaskProcessor.
  50. *
  51. *
  52. * @example
  53. * function doCalculation(parameters, transferableObjects) {
  54. * // calculate some result using the inputs in parameters
  55. * return result;
  56. * }
  57. *
  58. * return Cesium.createTaskProcessorWorker(doCalculation);
  59. * // the resulting function is compatible with TaskProcessor
  60. *
  61. * @see TaskProcessor
  62. * @see {@link http://www.w3.org/TR/workers/|Web Workers}
  63. * @see {@link http://www.w3.org/TR/html5/common-dom-interfaces.html#transferable-objects|Transferable objects}
  64. */
  65. function createTaskProcessorWorker(workerFunction) {
  66. let postMessage;
  67. return function (event) {
  68. const data = event.data;
  69. const transferableObjects = [];
  70. const responseMessage = {
  71. id: data.id,
  72. result: undefined,
  73. error: undefined,
  74. };
  75. return Promise.resolve(
  76. callAndWrap(workerFunction, data.parameters, transferableObjects)
  77. )
  78. .then(function (result) {
  79. responseMessage.result = result;
  80. })
  81. .catch(function (e) {
  82. if (e instanceof Error) {
  83. // Errors can't be posted in a message, copy the properties
  84. responseMessage.error = {
  85. name: e.name,
  86. message: e.message,
  87. stack: e.stack,
  88. };
  89. } else {
  90. responseMessage.error = e;
  91. }
  92. })
  93. .finally(function () {
  94. if (!defaultValue.defined(postMessage)) {
  95. postMessage = defaultValue.defaultValue(self.webkitPostMessage, self.postMessage);
  96. }
  97. if (!data.canTransferArrayBuffer) {
  98. transferableObjects.length = 0;
  99. }
  100. try {
  101. postMessage(responseMessage, transferableObjects);
  102. } catch (e) {
  103. // something went wrong trying to post the message, post a simpler
  104. // error that we can be sure will be cloneable
  105. responseMessage.result = undefined;
  106. responseMessage.error = `postMessage failed with error: ${formatError(
  107. e
  108. )}\n with responseMessage: ${JSON.stringify(responseMessage)}`;
  109. postMessage(responseMessage);
  110. }
  111. });
  112. };
  113. }
  114. return createTaskProcessorWorker;
  115. }));