pool.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. "use strict";
  2. var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
  3. if (k2 === undefined) k2 = k;
  4. Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
  5. }) : (function(o, m, k, k2) {
  6. if (k2 === undefined) k2 = k;
  7. o[k2] = m[k];
  8. }));
  9. var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
  10. Object.defineProperty(o, "default", { enumerable: true, value: v });
  11. }) : function(o, v) {
  12. o["default"] = v;
  13. });
  14. var __importStar = (this && this.__importStar) || function (mod) {
  15. if (mod && mod.__esModule) return mod;
  16. var result = {};
  17. if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
  18. __setModuleDefault(result, mod);
  19. return result;
  20. };
  21. Object.defineProperty(exports, "__esModule", { value: true });
  22. const index_js_1 = require("./compression/index.js");
  23. const defaultPoolSize = typeof navigator !== 'undefined' ? (navigator.hardwareConcurrency || 2) : 2;
  24. /**
  25. * @module pool
  26. */
  27. /**
  28. * Pool for workers to decode chunks of the images.
  29. */
  30. class Pool {
  31. /**
  32. * @constructor
  33. * @param {Number} [size] The size of the pool. Defaults to the number of CPUs
  34. * available. When this parameter is `null` or 0, then the
  35. * decoding will be done in the main thread.
  36. * @param {function(): Worker} [createWorker] A function that creates the decoder worker.
  37. * Defaults to a worker with all decoders that ship with geotiff.js. The `createWorker()`
  38. * function is expected to return a `Worker` compatible with Web Workers. For code that
  39. * runs in Node, [web-worker](https://www.npmjs.com/package/web-worker) is a good choice.
  40. *
  41. * A worker that uses a custom lzw decoder would look like this `my-custom-worker.js` file:
  42. * ```js
  43. * import { addDecoder, getDecoder } from 'geotiff';
  44. * addDecoder(5, () => import ('./my-custom-lzw').then((m) => m.default));
  45. * self.addEventListener('message', async (e) => {
  46. * const { id, fileDirectory, buffer } = e.data;
  47. * const decoder = await getDecoder(fileDirectory);
  48. * const decoded = await decoder.decode(fileDirectory, buffer);
  49. * self.postMessage({ decoded, id }, [decoded]);
  50. * });
  51. * ```
  52. * The way the above code is built into a worker by the `createWorker()` function
  53. * depends on the used bundler. For most bundlers, something like this will work:
  54. * ```js
  55. * function createWorker() {
  56. * return new Worker(new URL('./my-custom-worker.js', import.meta.url));
  57. * }
  58. * ```
  59. */
  60. constructor(size = defaultPoolSize, createWorker) {
  61. this.workers = null;
  62. this._awaitingDecoder = null;
  63. this.size = size;
  64. this.messageId = 0;
  65. if (size) {
  66. this._awaitingDecoder = createWorker ? Promise.resolve(createWorker) : new Promise((resolve) => {
  67. Promise.resolve().then(() => __importStar(require('./worker/decoder.js'))).then((module) => {
  68. resolve(module.create);
  69. });
  70. });
  71. this._awaitingDecoder.then((create) => {
  72. this._awaitingDecoder = null;
  73. this.workers = [];
  74. for (let i = 0; i < size; i++) {
  75. this.workers.push({ worker: create(), idle: true });
  76. }
  77. });
  78. }
  79. }
  80. /**
  81. * Decode the given block of bytes with the set compression method.
  82. * @param {ArrayBuffer} buffer the array buffer of bytes to decode.
  83. * @returns {Promise<ArrayBuffer>} the decoded result as a `Promise`
  84. */
  85. async decode(fileDirectory, buffer) {
  86. if (this._awaitingDecoder) {
  87. await this._awaitingDecoder;
  88. }
  89. return this.size === 0
  90. ? (0, index_js_1.getDecoder)(fileDirectory).then((decoder) => decoder.decode(fileDirectory, buffer))
  91. : new Promise((resolve) => {
  92. const worker = this.workers.find((candidate) => candidate.idle)
  93. || this.workers[Math.floor(Math.random() * this.size)];
  94. worker.idle = false;
  95. const id = this.messageId++;
  96. const onMessage = (e) => {
  97. if (e.data.id === id) {
  98. worker.idle = true;
  99. resolve(e.data.decoded);
  100. worker.worker.removeEventListener('message', onMessage);
  101. }
  102. };
  103. worker.worker.addEventListener('message', onMessage);
  104. worker.worker.postMessage({ fileDirectory, buffer, id }, [buffer]);
  105. });
  106. }
  107. destroy() {
  108. if (this.workers) {
  109. this.workers.forEach((worker) => {
  110. worker.worker.terminate();
  111. });
  112. this.workers = null;
  113. }
  114. }
  115. }
  116. exports.default = Pool;
  117. //# sourceMappingURL=pool.js.map