Home Reference Source

src/worker/DataProcessor.js

  1. import { HermiteData } from "../volume/HermiteData";
  2.  
  3. /**
  4. * An empty set of Hermite data.
  5. *
  6. * @type {HermiteData}
  7. * @private
  8. * @final
  9. */
  10.  
  11. const data = new HermiteData(false);
  12.  
  13. /**
  14. * A volume data processor.
  15. *
  16. * @implements {TransferableContainer}
  17. */
  18.  
  19. export class DataProcessor {
  20.  
  21. /**
  22. * Constructs a new data processor.
  23. */
  24.  
  25. constructor() {
  26.  
  27. /**
  28. * A set of Hermite data that will be used during processing.
  29. *
  30. * @type {HermiteData}
  31. * @protected
  32. */
  33.  
  34. this.data = null;
  35.  
  36. /**
  37. * A container for the data that will be returned to the main thread.
  38. *
  39. * @type {DataMessage}
  40. * @protected
  41. */
  42.  
  43. this.response = null;
  44.  
  45. }
  46.  
  47. /**
  48. * Returns the data of this processor.
  49. *
  50. * @return {HermiteData} The data.
  51. */
  52.  
  53. getData() {
  54.  
  55. return this.data;
  56.  
  57. }
  58.  
  59. /**
  60. * Prepares a response that can be send back to the main thread.
  61. *
  62. * Should be used together with {@link DataProcessor#createTransferList}.
  63. *
  64. * @return {DataMessage} A response.
  65. */
  66.  
  67. respond() {
  68.  
  69. this.response.data = this.data.serialize();
  70.  
  71. return this.response;
  72.  
  73. }
  74.  
  75. /**
  76. * Creates a list of transferable items.
  77. *
  78. * @param {Array} [transferList] - An optional target list. The transferable items will be added to this list.
  79. * @return {Transferable[]} The transfer list.
  80. */
  81.  
  82. createTransferList(transferList = []) {
  83.  
  84. if(this.data !== null) {
  85.  
  86. this.data.createTransferList(transferList);
  87.  
  88. }
  89.  
  90. return transferList;
  91.  
  92. }
  93.  
  94. /**
  95. * Processes the given request.
  96. *
  97. * @param {DataMessage} request - A request.
  98. * @return {DataProcessor} This processor.
  99. */
  100.  
  101. process(request) {
  102.  
  103. this.data = data.deserialize(request.data);
  104.  
  105. return this;
  106.  
  107. }
  108.  
  109. }