pool.js 3.5 KB

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