createTaskProcessorWorker.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /**
  2. * Cesium - https://github.com/CesiumGS/cesium
  3. *
  4. * Copyright 2011-2020 Cesium Contributors
  5. *
  6. * Licensed under the Apache License, Version 2.0 (the "License");
  7. * you may not use this file except in compliance with the License.
  8. * You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. *
  18. * Columbus View (Pat. Pend.)
  19. *
  20. * Portions licensed separately.
  21. * See https://github.com/CesiumGS/cesium/blob/main/LICENSE.md for full licensing details.
  22. */
  23. define(['./defaultValue-81eec7ed'], (function (defaultValue) { 'use strict';
  24. /**
  25. * Formats an error object into a String. If available, uses name, message, and stack
  26. * properties, otherwise, falls back on toString().
  27. *
  28. * @function
  29. *
  30. * @param {*} object The item to find in the array.
  31. * @returns {String} A string containing the formatted error.
  32. */
  33. function formatError(object) {
  34. let result;
  35. const name = object.name;
  36. const message = object.message;
  37. if (defaultValue.defined(name) && defaultValue.defined(message)) {
  38. result = `${name}: ${message}`;
  39. } else {
  40. result = object.toString();
  41. }
  42. const stack = object.stack;
  43. if (defaultValue.defined(stack)) {
  44. result += `\n${stack}`;
  45. }
  46. return result;
  47. }
  48. // createXXXGeometry functions may return Geometry or a Promise that resolves to Geometry
  49. // if the function requires access to ApproximateTerrainHeights.
  50. // For fully synchronous functions, just wrapping the function call in a Promise doesn't
  51. // handle errors correctly, hence try-catch
  52. function callAndWrap(workerFunction, parameters, transferableObjects) {
  53. let resultOrPromise;
  54. try {
  55. resultOrPromise = workerFunction(parameters, transferableObjects);
  56. return resultOrPromise; // errors handled by Promise
  57. } catch (e) {
  58. return Promise.reject(e);
  59. }
  60. }
  61. /**
  62. * Creates an adapter function to allow a calculation function to operate as a Web Worker,
  63. * paired with TaskProcessor, to receive tasks and return results.
  64. *
  65. * @function createTaskProcessorWorker
  66. *
  67. * @param {createTaskProcessorWorker.WorkerFunction} workerFunction The calculation function,
  68. * which takes parameters and returns a result.
  69. * @returns {createTaskProcessorWorker.TaskProcessorWorkerFunction} A function that adapts the
  70. * calculation function to work as a Web Worker onmessage listener with TaskProcessor.
  71. *
  72. *
  73. * @example
  74. * function doCalculation(parameters, transferableObjects) {
  75. * // calculate some result using the inputs in parameters
  76. * return result;
  77. * }
  78. *
  79. * return Cesium.createTaskProcessorWorker(doCalculation);
  80. * // the resulting function is compatible with TaskProcessor
  81. *
  82. * @see TaskProcessor
  83. * @see {@link http://www.w3.org/TR/workers/|Web Workers}
  84. * @see {@link http://www.w3.org/TR/html5/common-dom-interfaces.html#transferable-objects|Transferable objects}
  85. */
  86. function createTaskProcessorWorker(workerFunction) {
  87. let postMessage;
  88. return function (event) {
  89. const data = event.data;
  90. const transferableObjects = [];
  91. const responseMessage = {
  92. id: data.id,
  93. result: undefined,
  94. error: undefined,
  95. };
  96. return Promise.resolve(
  97. callAndWrap(workerFunction, data.parameters, transferableObjects)
  98. )
  99. .then(function (result) {
  100. responseMessage.result = result;
  101. })
  102. .catch(function (e) {
  103. if (e instanceof Error) {
  104. // Errors can't be posted in a message, copy the properties
  105. responseMessage.error = {
  106. name: e.name,
  107. message: e.message,
  108. stack: e.stack,
  109. };
  110. } else {
  111. responseMessage.error = e;
  112. }
  113. })
  114. .finally(function () {
  115. if (!defaultValue.defined(postMessage)) {
  116. postMessage = defaultValue.defaultValue(self.webkitPostMessage, self.postMessage);
  117. }
  118. if (!data.canTransferArrayBuffer) {
  119. transferableObjects.length = 0;
  120. }
  121. try {
  122. postMessage(responseMessage, transferableObjects);
  123. } catch (e) {
  124. // something went wrong trying to post the message, post a simpler
  125. // error that we can be sure will be cloneable
  126. responseMessage.result = undefined;
  127. responseMessage.error = `postMessage failed with error: ${formatError(
  128. e
  129. )}\n with responseMessage: ${JSON.stringify(responseMessage)}`;
  130. postMessage(responseMessage);
  131. }
  132. });
  133. };
  134. }
  135. return createTaskProcessorWorker;
  136. }));
  137. //# sourceMappingURL=createTaskProcessorWorker.js.map