createTaskProcessorWorker.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /**
  2. * Cesium - https://github.com/AnalyticalGraphicsInc/cesium
  3. *
  4. * Copyright 2011-2017 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/AnalyticalGraphicsInc/cesium/blob/master/LICENSE.md for full licensing details.
  22. */
  23. define(['./when-8d13db60'], function (when) { '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. * @exports formatError
  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. var result;
  35. var name = object.name;
  36. var message = object.message;
  37. if (when.defined(name) && when.defined(message)) {
  38. result = name + ': ' + message;
  39. } else {
  40. result = object.toString();
  41. }
  42. var stack = object.stack;
  43. if (when.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 `when` Promise doesn't
  51. // handle errors correctly, hence try-catch
  52. function callAndWrap(workerFunction, parameters, transferableObjects) {
  53. var resultOrPromise;
  54. try {
  55. resultOrPromise = workerFunction(parameters, transferableObjects);
  56. return resultOrPromise; // errors handled by Promise
  57. } catch (e) {
  58. return when.when.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. * @exports 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. var postMessage;
  88. return function(event) {
  89. var data = event.data;
  90. var transferableObjects = [];
  91. var responseMessage = {
  92. id : data.id,
  93. result : undefined,
  94. error : undefined
  95. };
  96. return when.when(callAndWrap(workerFunction, data.parameters, transferableObjects))
  97. .then(function(result) {
  98. responseMessage.result = result;
  99. })
  100. .otherwise(function(e) {
  101. if (e instanceof Error) {
  102. // Errors can't be posted in a message, copy the properties
  103. responseMessage.error = {
  104. name : e.name,
  105. message : e.message,
  106. stack : e.stack
  107. };
  108. } else {
  109. responseMessage.error = e;
  110. }
  111. })
  112. .always(function() {
  113. if (!when.defined(postMessage)) {
  114. postMessage = when.defaultValue(self.webkitPostMessage, self.postMessage);
  115. }
  116. if (!data.canTransferArrayBuffer) {
  117. transferableObjects.length = 0;
  118. }
  119. try {
  120. postMessage(responseMessage, transferableObjects);
  121. } catch (e) {
  122. // something went wrong trying to post the message, post a simpler
  123. // error that we can be sure will be cloneable
  124. responseMessage.result = undefined;
  125. responseMessage.error = 'postMessage failed with error: ' + formatError(e) + '\n with responseMessage: ' + JSON.stringify(responseMessage);
  126. postMessage(responseMessage);
  127. }
  128. });
  129. };
  130. }
  131. return createTaskProcessorWorker;
  132. });