createTaskProcessorWorker.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /* This file is automatically rebuilt by the Cesium build process. */
  2. define(['./defined-26bd4a03', './freezeObject-2d83f591', './defaultValue-f2e68450', './when-ee12a2cb'], function (defined, freezeObject, defaultValue, when) { '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. * @exports formatError
  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. var result;
  14. var name = object.name;
  15. var message = object.message;
  16. if (defined.defined(name) && defined.defined(message)) {
  17. result = name + ': ' + message;
  18. } else {
  19. result = object.toString();
  20. }
  21. var stack = object.stack;
  22. if (defined.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 `when` Promise doesn't
  30. // handle errors correctly, hence try-catch
  31. function callAndWrap(workerFunction, parameters, transferableObjects) {
  32. var resultOrPromise;
  33. try {
  34. resultOrPromise = workerFunction(parameters, transferableObjects);
  35. return resultOrPromise; // errors handled by Promise
  36. } catch (e) {
  37. return when.when.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. * @exports 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. var postMessage;
  67. return function(event) {
  68. var data = event.data;
  69. var transferableObjects = [];
  70. var responseMessage = {
  71. id : data.id,
  72. result : undefined,
  73. error : undefined
  74. };
  75. return when.when(callAndWrap(workerFunction, data.parameters, transferableObjects))
  76. .then(function(result) {
  77. responseMessage.result = result;
  78. })
  79. .otherwise(function(e) {
  80. if (e instanceof Error) {
  81. // Errors can't be posted in a message, copy the properties
  82. responseMessage.error = {
  83. name : e.name,
  84. message : e.message,
  85. stack : e.stack
  86. };
  87. } else {
  88. responseMessage.error = e;
  89. }
  90. })
  91. .always(function() {
  92. if (!defined.defined(postMessage)) {
  93. postMessage = defaultValue.defaultValue(self.webkitPostMessage, self.postMessage);
  94. }
  95. if (!data.canTransferArrayBuffer) {
  96. transferableObjects.length = 0;
  97. }
  98. try {
  99. postMessage(responseMessage, transferableObjects);
  100. } catch (e) {
  101. // something went wrong trying to post the message, post a simpler
  102. // error that we can be sure will be cloneable
  103. responseMessage.result = undefined;
  104. responseMessage.error = 'postMessage failed with error: ' + formatError(e) + '\n with responseMessage: ' + JSON.stringify(responseMessage);
  105. postMessage(responseMessage);
  106. }
  107. });
  108. };
  109. }
  110. return createTaskProcessorWorker;
  111. });