createTaskProcessorWorker.js 4.1 KB

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