@loaders.gl/worker-utils 4.2.0-alpha.4 → 4.2.0-alpha.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (81) hide show
  1. package/dist/index.cjs +225 -86
  2. package/dist/index.cjs.map +7 -0
  3. package/dist/index.d.ts +18 -18
  4. package/dist/index.d.ts.map +1 -1
  5. package/dist/index.js +19 -8
  6. package/dist/lib/async-queue/async-queue.js +83 -68
  7. package/dist/lib/env-utils/assert.js +10 -4
  8. package/dist/lib/env-utils/globals.js +19 -7
  9. package/dist/lib/env-utils/version.js +20 -11
  10. package/dist/lib/library-utils/library-utils.js +148 -73
  11. package/dist/lib/node/require-utils.node.js +72 -43
  12. package/dist/lib/node/worker_threads-browser.js +9 -2
  13. package/dist/lib/node/worker_threads.js +4 -2
  14. package/dist/lib/process-utils/child-process-proxy.d.ts +1 -3
  15. package/dist/lib/process-utils/child-process-proxy.d.ts.map +1 -1
  16. package/dist/lib/process-utils/child-process-proxy.js +103 -93
  17. package/dist/lib/process-utils/process-utils.js +28 -23
  18. package/dist/lib/worker-api/create-worker.d.ts +1 -1
  19. package/dist/lib/worker-api/create-worker.d.ts.map +1 -1
  20. package/dist/lib/worker-api/create-worker.js +77 -74
  21. package/dist/lib/worker-api/get-worker-url.d.ts +1 -1
  22. package/dist/lib/worker-api/get-worker-url.d.ts.map +1 -1
  23. package/dist/lib/worker-api/get-worker-url.js +52 -26
  24. package/dist/lib/worker-api/process-on-worker.d.ts +1 -1
  25. package/dist/lib/worker-api/process-on-worker.d.ts.map +1 -1
  26. package/dist/lib/worker-api/process-on-worker.js +70 -67
  27. package/dist/lib/worker-api/validate-worker-version.d.ts +1 -1
  28. package/dist/lib/worker-api/validate-worker-version.d.ts.map +1 -1
  29. package/dist/lib/worker-api/validate-worker-version.js +29 -14
  30. package/dist/lib/worker-farm/worker-body.d.ts +1 -1
  31. package/dist/lib/worker-farm/worker-body.d.ts.map +1 -1
  32. package/dist/lib/worker-farm/worker-body.js +109 -68
  33. package/dist/lib/worker-farm/worker-farm.d.ts +1 -1
  34. package/dist/lib/worker-farm/worker-farm.d.ts.map +1 -1
  35. package/dist/lib/worker-farm/worker-farm.js +80 -62
  36. package/dist/lib/worker-farm/worker-job.d.ts +2 -2
  37. package/dist/lib/worker-farm/worker-job.d.ts.map +1 -1
  38. package/dist/lib/worker-farm/worker-job.js +48 -32
  39. package/dist/lib/worker-farm/worker-pool.d.ts +3 -3
  40. package/dist/lib/worker-farm/worker-pool.d.ts.map +1 -1
  41. package/dist/lib/worker-farm/worker-pool.js +153 -111
  42. package/dist/lib/worker-farm/worker-thread.d.ts +1 -1
  43. package/dist/lib/worker-farm/worker-thread.d.ts.map +1 -1
  44. package/dist/lib/worker-farm/worker-thread.js +126 -94
  45. package/dist/lib/worker-utils/get-loadable-worker-url.js +54 -24
  46. package/dist/lib/worker-utils/get-transfer-list.js +79 -44
  47. package/dist/lib/worker-utils/remove-nontransferable-options.js +23 -14
  48. package/dist/null-worker-node.js +6 -1
  49. package/dist/null-worker-node.js.map +2 -2
  50. package/dist/null-worker.js +3 -1
  51. package/dist/null-worker.js.map +2 -2
  52. package/dist/types.js +3 -1
  53. package/dist/workers/null-worker.js +6 -3
  54. package/package.json +11 -11
  55. package/src/lib/process-utils/child-process-proxy.ts +1 -1
  56. package/src/lib/worker-farm/worker-body.ts +7 -2
  57. package/dist/index.js.map +0 -1
  58. package/dist/lib/async-queue/async-queue.js.map +0 -1
  59. package/dist/lib/env-utils/assert.js.map +0 -1
  60. package/dist/lib/env-utils/globals.js.map +0 -1
  61. package/dist/lib/env-utils/version.js.map +0 -1
  62. package/dist/lib/library-utils/library-utils.js.map +0 -1
  63. package/dist/lib/node/require-utils.node.js.map +0 -1
  64. package/dist/lib/node/worker_threads-browser.js.map +0 -1
  65. package/dist/lib/node/worker_threads.js.map +0 -1
  66. package/dist/lib/process-utils/child-process-proxy.js.map +0 -1
  67. package/dist/lib/process-utils/process-utils.js.map +0 -1
  68. package/dist/lib/worker-api/create-worker.js.map +0 -1
  69. package/dist/lib/worker-api/get-worker-url.js.map +0 -1
  70. package/dist/lib/worker-api/process-on-worker.js.map +0 -1
  71. package/dist/lib/worker-api/validate-worker-version.js.map +0 -1
  72. package/dist/lib/worker-farm/worker-body.js.map +0 -1
  73. package/dist/lib/worker-farm/worker-farm.js.map +0 -1
  74. package/dist/lib/worker-farm/worker-job.js.map +0 -1
  75. package/dist/lib/worker-farm/worker-pool.js.map +0 -1
  76. package/dist/lib/worker-farm/worker-thread.js.map +0 -1
  77. package/dist/lib/worker-utils/get-loadable-worker-url.js.map +0 -1
  78. package/dist/lib/worker-utils/get-transfer-list.js.map +0 -1
  79. package/dist/lib/worker-utils/remove-nontransferable-options.js.map +0 -1
  80. package/dist/types.js.map +0 -1
  81. package/dist/workers/null-worker.js.map +0 -1
@@ -1,83 +1,124 @@
1
+ // loaders.gl
2
+ // SPDX-License-Identifier: MIT
3
+ // Copyright (c) vis.gl contributors
1
4
  import { getTransferList } from "../worker-utils/get-transfer-list.js";
5
+ // import type {TransferListItem} from '../node/worker_threads';
2
6
  import { parentPort } from "../node/worker_threads.js";
7
+ /** Vile hack to defeat over-zealous bundlers from stripping out the require */
3
8
  async function getParentPort() {
4
- return parentPort;
9
+ // const isNode = globalThis.process;
10
+ // let parentPort;
11
+ // try {
12
+ // // prettier-ignore
13
+ // eval('globalThis.parentPort = require(\'worker_threads\').parentPort'); // eslint-disable-line no-eval
14
+ // parentPort = globalThis.parentPort;
15
+ // } catch {
16
+ // try {
17
+ // // prettier-ignore
18
+ // eval('globalThis.workerThreadsPromise = import(\'worker_threads\')'); // eslint-disable-line no-eval
19
+ // const workerThreads = await globalThis.workerThreadsPromise;
20
+ // parentPort = workerThreads.parentPort;
21
+ // } catch (error) {
22
+ // console.error((error as Error).message); // eslint-disable-line no-console
23
+ // }
24
+ // }
25
+ return parentPort;
5
26
  }
6
27
  const onMessageWrapperMap = new Map();
28
+ /**
29
+ * Type safe wrapper for worker code
30
+ */
7
31
  export default class WorkerBody {
8
- static async inWorkerThread() {
9
- return typeof self !== 'undefined' || Boolean(await getParentPort());
10
- }
11
- static set onmessage(onMessage) {
12
- async function handleMessage(message) {
13
- const parentPort = await getParentPort();
14
- const {
15
- type,
16
- payload
17
- } = parentPort ? message : message.data;
18
- onMessage(type, payload);
32
+ /** Check that we are actually in a worker thread */
33
+ static async inWorkerThread() {
34
+ return typeof self !== 'undefined' || Boolean(await getParentPort());
19
35
  }
20
- getParentPort().then(parentPort => {
21
- if (parentPort) {
22
- parentPort.on('message', handleMessage);
23
- parentPort.on('exit', () => console.debug('Node worker closing'));
24
- } else {
25
- globalThis.onmessage = handleMessage;
26
- }
27
- });
28
- }
29
- static async addEventListener(onMessage) {
30
- let onMessageWrapper = onMessageWrapperMap.get(onMessage);
31
- if (!onMessageWrapper) {
32
- onMessageWrapper = async message => {
33
- if (!isKnownMessage(message)) {
34
- return;
36
+ /*
37
+ * (type: WorkerMessageType, payload: WorkerMessagePayload) => any
38
+ */
39
+ static set onmessage(onMessage) {
40
+ async function handleMessage(message) {
41
+ const parentPort = await getParentPort();
42
+ // Confusingly the message itself also has a 'type' field which is always set to 'message'
43
+ const { type, payload } = parentPort ? message : message.data;
44
+ // if (!isKnownMessage(message)) {
45
+ // return;
46
+ // }
47
+ onMessage(type, payload);
35
48
  }
36
- const parentPort = await getParentPort();
37
- const {
38
- type,
39
- payload
40
- } = parentPort ? message : message.data;
41
- onMessage(type, payload);
42
- };
49
+ getParentPort().then((parentPort) => {
50
+ if (parentPort) {
51
+ parentPort.on('message', (message) => {
52
+ handleMessage(message);
53
+ });
54
+ // if (message == 'exit') { parentPort.unref(); }
55
+ // eslint-disable-next-line
56
+ parentPort.on('exit', () => console.debug('Node worker closing'));
57
+ }
58
+ else {
59
+ // eslint-disable-next-line no-restricted-globals
60
+ globalThis.onmessage = handleMessage;
61
+ }
62
+ });
43
63
  }
44
- const parentPort = await getParentPort();
45
- if (parentPort) {
46
- console.error('not implemented');
47
- } else {
48
- globalThis.addEventListener('message', onMessageWrapper);
64
+ static async addEventListener(onMessage) {
65
+ let onMessageWrapper = onMessageWrapperMap.get(onMessage);
66
+ if (!onMessageWrapper) {
67
+ onMessageWrapper = async (message) => {
68
+ if (!isKnownMessage(message)) {
69
+ return;
70
+ }
71
+ const parentPort = await getParentPort();
72
+ // Confusingly in the browser, the message itself also has a 'type' field which is always set to 'message'
73
+ const { type, payload } = parentPort ? message : message.data;
74
+ onMessage(type, payload);
75
+ };
76
+ }
77
+ const parentPort = await getParentPort();
78
+ if (parentPort) {
79
+ console.error('not implemented'); // eslint-disable-line
80
+ }
81
+ else {
82
+ globalThis.addEventListener('message', onMessageWrapper);
83
+ }
49
84
  }
50
- }
51
- static async removeEventListener(onMessage) {
52
- const onMessageWrapper = onMessageWrapperMap.get(onMessage);
53
- onMessageWrapperMap.delete(onMessage);
54
- const parentPort = await getParentPort();
55
- if (parentPort) {
56
- console.error('not implemented');
57
- } else {
58
- globalThis.removeEventListener('message', onMessageWrapper);
85
+ static async removeEventListener(onMessage) {
86
+ const onMessageWrapper = onMessageWrapperMap.get(onMessage);
87
+ onMessageWrapperMap.delete(onMessage);
88
+ const parentPort = await getParentPort();
89
+ if (parentPort) {
90
+ console.error('not implemented'); // eslint-disable-line
91
+ }
92
+ else {
93
+ globalThis.removeEventListener('message', onMessageWrapper);
94
+ }
59
95
  }
60
- }
61
- static async postMessage(type, payload) {
62
- const data = {
63
- source: 'loaders.gl',
64
- type,
65
- payload
66
- };
67
- const transferList = getTransferList(payload);
68
- const parentPort = await getParentPort();
69
- if (parentPort) {
70
- parentPort.postMessage(data, transferList);
71
- } else {
72
- globalThis.postMessage(data, transferList);
96
+ /**
97
+ * Send a message from a worker to creating thread (main thread)
98
+ * @param type
99
+ * @param payload
100
+ */
101
+ static async postMessage(type, payload) {
102
+ const data = { source: 'loaders.gl', type, payload };
103
+ // console.log('posting message', data);
104
+ // Cast to Node compatible transfer list
105
+ const transferList = getTransferList(payload);
106
+ const parentPort = await getParentPort();
107
+ if (parentPort) {
108
+ parentPort.postMessage(data, transferList);
109
+ // console.log('posted message', data);
110
+ }
111
+ else {
112
+ // @ts-expect-error Outside of worker scopes this call has a third parameter
113
+ globalThis.postMessage(data, transferList);
114
+ }
73
115
  }
74
- }
75
116
  }
117
+ // Filter out noise messages sent to workers
76
118
  function isKnownMessage(message) {
77
- const {
78
- type,
79
- data
80
- } = message;
81
- return type === 'message' && data && typeof data.source === 'string' && data.source.startsWith('loaders.gl');
119
+ const { type, data } = message;
120
+ return (type === 'message' &&
121
+ data &&
122
+ typeof data.source === 'string' &&
123
+ data.source.startsWith('loaders.gl'));
82
124
  }
83
- //# sourceMappingURL=worker-body.js.map
@@ -1,4 +1,4 @@
1
- import WorkerPool from './worker-pool';
1
+ import WorkerPool from "./worker-pool.js";
2
2
  /**
3
3
  * @param maxConcurrency - max count of workers
4
4
  * @param maxMobileConcurrency - max count of workers on mobile
@@ -1 +1 @@
1
- {"version":3,"file":"worker-farm.d.ts","sourceRoot":"","sources":["../../../src/lib/worker-farm/worker-farm.ts"],"names":[],"mappings":"AAIA,OAAO,UAAU,MAAM,eAAe,CAAC;AAGvC;;;;;;GAMG;AACH,MAAM,MAAM,eAAe,GAAG;IAC5B,cAAc,CAAC,EAAE,MAAM,CAAC;IACxB,oBAAoB,CAAC,EAAE,MAAM,CAAC;IAC9B,YAAY,CAAC,EAAE,OAAO,CAAC;IACvB,OAAO,CAAC,EAAE,MAAM,IAAI,CAAC;CACtB,CAAC;AASF;;GAEG;AACH,MAAM,CAAC,OAAO,OAAO,UAAU;IAC7B,OAAO,CAAC,KAAK,CAAkB;IAC/B,OAAO,CAAC,WAAW,CAAiC;IAEpD,OAAO,CAAC,MAAM,CAAC,WAAW,CAAC,CAAa;IAExC,uDAAuD;IACvD,MAAM,CAAC,WAAW,IAAI,OAAO;IAI7B,2DAA2D;IAC3D,MAAM,CAAC,aAAa,CAAC,KAAK,GAAE,eAAoB,GAAG,UAAU;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AAC,GAAG,UAAU;IAejF,mBAAmB;;;;;;CAQpB"}
@@ -1,71 +1,89 @@
1
+ // loaders.gl
2
+ // SPDX-License-Identifier: MIT
3
+ // Copyright (c) vis.gl contributors
1
4
  import WorkerPool from "./worker-pool.js";
2
5
  import WorkerThread from "./worker-thread.js";
3
6
  const DEFAULT_PROPS = {
4
- maxConcurrency: 3,
5
- maxMobileConcurrency: 1,
6
- reuseWorkers: true,
7
- onDebug: () => {}
7
+ maxConcurrency: 3,
8
+ maxMobileConcurrency: 1,
9
+ reuseWorkers: true,
10
+ onDebug: () => { }
8
11
  };
12
+ /**
13
+ * Process multiple jobs with a "farm" of different workers in worker pools.
14
+ */
9
15
  export default class WorkerFarm {
10
- static isSupported() {
11
- return WorkerThread.isSupported();
12
- }
13
- static getWorkerFarm() {
14
- let props = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
15
- WorkerFarm._workerFarm = WorkerFarm._workerFarm || new WorkerFarm({});
16
- WorkerFarm._workerFarm.setProps(props);
17
- return WorkerFarm._workerFarm;
18
- }
19
- constructor(props) {
20
- this.props = void 0;
21
- this.workerPools = new Map();
22
- this.props = {
23
- ...DEFAULT_PROPS
24
- };
25
- this.setProps(props);
26
- this.workerPools = new Map();
27
- }
28
- destroy() {
29
- for (const workerPool of this.workerPools.values()) {
30
- workerPool.destroy();
16
+ props;
17
+ workerPools = new Map();
18
+ // singleton
19
+ static _workerFarm;
20
+ /** Checks if workers are supported on this platform */
21
+ static isSupported() {
22
+ return WorkerThread.isSupported();
31
23
  }
32
- this.workerPools = new Map();
33
- }
34
- setProps(props) {
35
- this.props = {
36
- ...this.props,
37
- ...props
38
- };
39
- for (const workerPool of this.workerPools.values()) {
40
- workerPool.setProps(this._getWorkerPoolProps());
24
+ /** Get the singleton instance of the global worker farm */
25
+ static getWorkerFarm(props = {}) {
26
+ WorkerFarm._workerFarm = WorkerFarm._workerFarm || new WorkerFarm({});
27
+ WorkerFarm._workerFarm.setProps(props);
28
+ return WorkerFarm._workerFarm;
41
29
  }
42
- }
43
- getWorkerPool(options) {
44
- const {
45
- name,
46
- source,
47
- url
48
- } = options;
49
- let workerPool = this.workerPools.get(name);
50
- if (!workerPool) {
51
- workerPool = new WorkerPool({
52
- name,
53
- source,
54
- url
55
- });
56
- workerPool.setProps(this._getWorkerPoolProps());
57
- this.workerPools.set(name, workerPool);
30
+ /** get global instance with WorkerFarm.getWorkerFarm() */
31
+ constructor(props) {
32
+ this.props = { ...DEFAULT_PROPS };
33
+ this.setProps(props);
34
+ /** @type Map<string, WorkerPool>} */
35
+ this.workerPools = new Map();
36
+ }
37
+ /**
38
+ * Terminate all workers in the farm
39
+ * @note Can free up significant memory
40
+ */
41
+ destroy() {
42
+ for (const workerPool of this.workerPools.values()) {
43
+ workerPool.destroy();
44
+ }
45
+ this.workerPools = new Map();
46
+ }
47
+ /**
48
+ * Set props used when initializing worker pools
49
+ * @param props
50
+ */
51
+ setProps(props) {
52
+ this.props = { ...this.props, ...props };
53
+ // Update worker pool props
54
+ for (const workerPool of this.workerPools.values()) {
55
+ workerPool.setProps(this._getWorkerPoolProps());
56
+ }
57
+ }
58
+ /**
59
+ * Returns a worker pool for the specified worker
60
+ * @param options - only used first time for a specific worker name
61
+ * @param options.name - the name of the worker - used to identify worker pool
62
+ * @param options.url -
63
+ * @param options.source -
64
+ * @example
65
+ * const job = WorkerFarm.getWorkerFarm().getWorkerPool({name, url}).startJob(...);
66
+ */
67
+ getWorkerPool(options) {
68
+ const { name, source, url } = options;
69
+ let workerPool = this.workerPools.get(name);
70
+ if (!workerPool) {
71
+ workerPool = new WorkerPool({
72
+ name,
73
+ source,
74
+ url
75
+ });
76
+ workerPool.setProps(this._getWorkerPoolProps());
77
+ this.workerPools.set(name, workerPool);
78
+ }
79
+ return workerPool;
80
+ }
81
+ _getWorkerPoolProps() {
82
+ return {
83
+ maxConcurrency: this.props.maxConcurrency,
84
+ maxMobileConcurrency: this.props.maxMobileConcurrency,
85
+ reuseWorkers: this.props.reuseWorkers,
86
+ onDebug: this.props.onDebug
87
+ };
58
88
  }
59
- return workerPool;
60
- }
61
- _getWorkerPoolProps() {
62
- return {
63
- maxConcurrency: this.props.maxConcurrency,
64
- maxMobileConcurrency: this.props.maxMobileConcurrency,
65
- reuseWorkers: this.props.reuseWorkers,
66
- onDebug: this.props.onDebug
67
- };
68
- }
69
89
  }
70
- WorkerFarm._workerFarm = void 0;
71
- //# sourceMappingURL=worker-farm.js.map
@@ -1,5 +1,5 @@
1
- import type { WorkerMessageType, WorkerMessagePayload } from '../../types';
2
- import WorkerThread from './worker-thread';
1
+ import type { WorkerMessageType, WorkerMessagePayload } from "../../types.js";
2
+ import WorkerThread from "./worker-thread.js";
3
3
  /**
4
4
  * Represents one Job handled by a WorkerPool or WorkerFarm
5
5
  */
@@ -1 +1 @@
1
- {"version":3,"file":"worker-job.d.ts","sourceRoot":"","sources":["../../../src/lib/worker-farm/worker-job.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAAC,iBAAiB,EAAE,oBAAoB,EAAC,MAAM,aAAa,CAAC;AACzE,OAAO,YAAY,MAAM,iBAAiB,CAAC;AAG3C;;GAEG;AACH,MAAM,CAAC,OAAO,OAAO,SAAS;IAC5B,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IACtB,QAAQ,CAAC,YAAY,EAAE,YAAY,CAAC;IACpC,SAAS,EAAE,OAAO,CAAQ;IAC1B,6CAA6C;IAC7C,QAAQ,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,CAAC,CAAC;IAE9B,OAAO,CAAC,QAAQ,CAAkC;IAClD,OAAO,CAAC,OAAO,CAAoC;gBAEvC,OAAO,EAAE,MAAM,EAAE,YAAY,EAAE,YAAY;IASvD;;;OAGG;IACH,WAAW,CAAC,IAAI,EAAE,iBAAiB,EAAE,OAAO,EAAE,oBAAoB,GAAG,IAAI;IAQzE;;OAEG;IACH,IAAI,CAAC,KAAK,EAAE,GAAG,GAAG,IAAI;IAMtB;;OAEG;IACH,KAAK,CAAC,KAAK,EAAE,KAAK,GAAG,IAAI;CAK1B"}
1
+ {"version":3,"file":"worker-job.d.ts","sourceRoot":"","sources":["../../../src/lib/worker-farm/worker-job.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAAC,iBAAiB,EAAE,oBAAoB,EAAC,uBAAoB;AACzE,OAAO,YAAY,2BAAwB;AAG3C;;GAEG;AACH,MAAM,CAAC,OAAO,OAAO,SAAS;IAC5B,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IACtB,QAAQ,CAAC,YAAY,EAAE,YAAY,CAAC;IACpC,SAAS,EAAE,OAAO,CAAQ;IAC1B,6CAA6C;IAC7C,QAAQ,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,CAAC,CAAC;IAE9B,OAAO,CAAC,QAAQ,CAAkC;IAClD,OAAO,CAAC,OAAO,CAAoC;gBAEvC,OAAO,EAAE,MAAM,EAAE,YAAY,EAAE,YAAY;IASvD;;;OAGG;IACH,WAAW,CAAC,IAAI,EAAE,iBAAiB,EAAE,OAAO,EAAE,oBAAoB,GAAG,IAAI;IAQzE;;OAEG;IACH,IAAI,CAAC,KAAK,EAAE,GAAG,GAAG,IAAI;IAMtB;;OAEG;IACH,KAAK,CAAC,KAAK,EAAE,KAAK,GAAG,IAAI;CAK1B"}
@@ -1,35 +1,51 @@
1
+ // loaders.gl
2
+ // SPDX-License-Identifier: MIT
3
+ // Copyright (c) vis.gl contributors
1
4
  import { assert } from "../env-utils/assert.js";
5
+ /**
6
+ * Represents one Job handled by a WorkerPool or WorkerFarm
7
+ */
2
8
  export default class WorkerJob {
3
- constructor(jobName, workerThread) {
4
- this.name = void 0;
5
- this.workerThread = void 0;
6
- this.isRunning = true;
7
- this.result = void 0;
8
- this._resolve = () => {};
9
- this._reject = () => {};
10
- this.name = jobName;
11
- this.workerThread = workerThread;
12
- this.result = new Promise((resolve, reject) => {
13
- this._resolve = resolve;
14
- this._reject = reject;
15
- });
16
- }
17
- postMessage(type, payload) {
18
- this.workerThread.postMessage({
19
- source: 'loaders.gl',
20
- type,
21
- payload
22
- });
23
- }
24
- done(value) {
25
- assert(this.isRunning);
26
- this.isRunning = false;
27
- this._resolve(value);
28
- }
29
- error(error) {
30
- assert(this.isRunning);
31
- this.isRunning = false;
32
- this._reject(error);
33
- }
9
+ name;
10
+ workerThread;
11
+ isRunning = true;
12
+ /** Promise that resolves when Job is done */
13
+ result;
14
+ _resolve = () => { };
15
+ _reject = () => { };
16
+ constructor(jobName, workerThread) {
17
+ this.name = jobName;
18
+ this.workerThread = workerThread;
19
+ this.result = new Promise((resolve, reject) => {
20
+ this._resolve = resolve;
21
+ this._reject = reject;
22
+ });
23
+ }
24
+ /**
25
+ * Send a message to the job's worker thread
26
+ * @param data any data structure, ideally consisting mostly of transferrable objects
27
+ */
28
+ postMessage(type, payload) {
29
+ this.workerThread.postMessage({
30
+ source: 'loaders.gl', // Lets worker ignore unrelated messages
31
+ type,
32
+ payload
33
+ });
34
+ }
35
+ /**
36
+ * Call to resolve the `result` Promise with the supplied value
37
+ */
38
+ done(value) {
39
+ assert(this.isRunning);
40
+ this.isRunning = false;
41
+ this._resolve(value);
42
+ }
43
+ /**
44
+ * Call to reject the `result` Promise with the supplied error
45
+ */
46
+ error(error) {
47
+ assert(this.isRunning);
48
+ this.isRunning = false;
49
+ this._reject(error);
50
+ }
34
51
  }
35
- //# sourceMappingURL=worker-job.js.map
@@ -1,6 +1,6 @@
1
- import type { WorkerMessageType, WorkerMessagePayload } from '../../types';
2
- import WorkerThread from './worker-thread';
3
- import WorkerJob from './worker-job';
1
+ import type { WorkerMessageType, WorkerMessagePayload } from "../../types.js";
2
+ import WorkerThread from "./worker-thread.js";
3
+ import WorkerJob from "./worker-job.js";
4
4
  /** WorkerPool onDebug Callback Parameters */
5
5
  type OnDebugParameters = {
6
6
  message: string;
@@ -1 +1 @@
1
- {"version":3,"file":"worker-pool.d.ts","sourceRoot":"","sources":["../../../src/lib/worker-farm/worker-pool.ts"],"names":[],"mappings":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