@math.gl/tasks 0.0.0-stage → 5.0.0-beta.10
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.
- package/LICENSE +147 -0
- package/LOADERS-GL-LICENSE.txt +52 -0
- package/README.md +26 -2
- package/THIRD-PARTY-NOTICES.md +5 -0
- package/dist/index.cjs +3454 -0
- package/dist/index.cjs.map +6 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +5 -0
- package/dist/index.js.map +1 -0
- package/dist/tasks/index.d.ts +9 -0
- package/dist/tasks/index.d.ts.map +1 -0
- package/dist/tasks/index.js +9 -0
- package/dist/tasks/index.js.map +1 -0
- package/dist/tasks/task-channel.d.ts +48 -0
- package/dist/tasks/task-channel.d.ts.map +1 -0
- package/dist/tasks/task-channel.js +166 -0
- package/dist/tasks/task-channel.js.map +1 -0
- package/dist/tasks/task-operation-queue.d.ts +26 -0
- package/dist/tasks/task-operation-queue.d.ts.map +1 -0
- package/dist/tasks/task-operation-queue.js +106 -0
- package/dist/tasks/task-operation-queue.js.map +1 -0
- package/dist/tasks/task-preload.d.ts +13 -0
- package/dist/tasks/task-preload.d.ts.map +1 -0
- package/dist/tasks/task-preload.js +96 -0
- package/dist/tasks/task-preload.js.map +1 -0
- package/dist/tasks/task-runner.d.ts +74 -0
- package/dist/tasks/task-runner.d.ts.map +1 -0
- package/dist/tasks/task-runner.js +811 -0
- package/dist/tasks/task-runner.js.map +1 -0
- package/dist/tasks/task-runtime.d.ts +27 -0
- package/dist/tasks/task-runtime.d.ts.map +1 -0
- package/dist/tasks/task-runtime.js +216 -0
- package/dist/tasks/task-runtime.js.map +1 -0
- package/dist/tasks/task-sync.d.ts +4 -0
- package/dist/tasks/task-sync.d.ts.map +1 -0
- package/dist/tasks/task-sync.js +95 -0
- package/dist/tasks/task-sync.js.map +1 -0
- package/dist/tasks/task-worker-channel.d.ts +11 -0
- package/dist/tasks/task-worker-channel.d.ts.map +1 -0
- package/dist/tasks/task-worker-channel.js +34 -0
- package/dist/tasks/task-worker-channel.js.map +1 -0
- package/dist/tasks/task.d.ts +132 -0
- package/dist/tasks/task.d.ts.map +1 -0
- package/dist/tasks/task.js +5 -0
- package/dist/tasks/task.js.map +1 -0
- package/dist/tasks/typed-task-dispatch.d.ts +30 -0
- package/dist/tasks/typed-task-dispatch.d.ts.map +1 -0
- package/dist/tasks/typed-task-dispatch.js +12 -0
- package/dist/tasks/typed-task-dispatch.js.map +1 -0
- package/dist/worker-utils/index.d.ts +25 -0
- package/dist/worker-utils/index.d.ts.map +1 -0
- package/dist/worker-utils/index.js +46 -0
- package/dist/worker-utils/index.js.map +1 -0
- package/dist/worker-utils/lib/async-queue/async-queue.d.ts +32 -0
- package/dist/worker-utils/lib/async-queue/async-queue.d.ts.map +1 -0
- package/dist/worker-utils/lib/async-queue/async-queue.js +88 -0
- package/dist/worker-utils/lib/async-queue/async-queue.js.map +1 -0
- package/dist/worker-utils/lib/env-utils/assert.d.ts +3 -0
- package/dist/worker-utils/lib/env-utils/assert.d.ts.map +1 -0
- package/dist/worker-utils/lib/env-utils/assert.js +13 -0
- package/dist/worker-utils/lib/env-utils/assert.js.map +1 -0
- package/dist/worker-utils/lib/env-utils/globals.d.ts +22 -0
- package/dist/worker-utils/lib/env-utils/globals.d.ts.map +1 -0
- package/dist/worker-utils/lib/env-utils/globals.js +30 -0
- package/dist/worker-utils/lib/env-utils/globals.js.map +1 -0
- package/dist/worker-utils/lib/env-utils/version.d.ts +3 -0
- package/dist/worker-utils/lib/env-utils/version.d.ts.map +1 -0
- package/dist/worker-utils/lib/env-utils/version.js +11 -0
- package/dist/worker-utils/lib/env-utils/version.js.map +1 -0
- package/dist/worker-utils/lib/library-utils/library-utils.d.ts +42 -0
- package/dist/worker-utils/lib/library-utils/library-utils.d.ts.map +1 -0
- package/dist/worker-utils/lib/library-utils/library-utils.js +251 -0
- package/dist/worker-utils/lib/library-utils/library-utils.js.map +1 -0
- package/dist/worker-utils/lib/node/worker_threads-browser.d.ts +15 -0
- package/dist/worker-utils/lib/node/worker_threads-browser.d.ts.map +1 -0
- package/dist/worker-utils/lib/node/worker_threads-browser.js +23 -0
- package/dist/worker-utils/lib/node/worker_threads-browser.js.map +1 -0
- package/dist/worker-utils/lib/node/worker_threads.d.ts +16 -0
- package/dist/worker-utils/lib/node/worker_threads.d.ts.map +1 -0
- package/dist/worker-utils/lib/node/worker_threads.js +7 -0
- package/dist/worker-utils/lib/node/worker_threads.js.map +1 -0
- package/dist/worker-utils/lib/npm-tag.d.ts +2 -0
- package/dist/worker-utils/lib/npm-tag.d.ts.map +1 -0
- package/dist/worker-utils/lib/npm-tag.js +5 -0
- package/dist/worker-utils/lib/npm-tag.js.map +1 -0
- package/dist/worker-utils/lib/process-utils/child-process-proxy.browser.d.ts +8 -0
- package/dist/worker-utils/lib/process-utils/child-process-proxy.browser.d.ts.map +1 -0
- package/dist/worker-utils/lib/process-utils/child-process-proxy.browser.js +18 -0
- package/dist/worker-utils/lib/process-utils/child-process-proxy.browser.js.map +1 -0
- package/dist/worker-utils/lib/process-utils/child-process-proxy.d.ts +45 -0
- package/dist/worker-utils/lib/process-utils/child-process-proxy.d.ts.map +1 -0
- package/dist/worker-utils/lib/process-utils/child-process-proxy.js +128 -0
- package/dist/worker-utils/lib/process-utils/child-process-proxy.js.map +1 -0
- package/dist/worker-utils/lib/process-utils/process-utils.d.ts +2 -0
- package/dist/worker-utils/lib/process-utils/process-utils.d.ts.map +1 -0
- package/dist/worker-utils/lib/process-utils/process-utils.js +32 -0
- package/dist/worker-utils/lib/process-utils/process-utils.js.map +1 -0
- package/dist/worker-utils/lib/worker-api/create-worker.d.ts +9 -0
- package/dist/worker-utils/lib/worker-api/create-worker.d.ts.map +1 -0
- package/dist/worker-utils/lib/worker-api/create-worker.js +229 -0
- package/dist/worker-utils/lib/worker-api/create-worker.js.map +1 -0
- package/dist/worker-utils/lib/worker-api/get-worker-url.d.ts +29 -0
- package/dist/worker-utils/lib/worker-api/get-worker-url.d.ts.map +1 -0
- package/dist/worker-utils/lib/worker-api/get-worker-url.js +101 -0
- package/dist/worker-utils/lib/worker-api/get-worker-url.js.map +1 -0
- package/dist/worker-utils/lib/worker-api/process-on-worker.d.ts +67 -0
- package/dist/worker-utils/lib/worker-api/process-on-worker.d.ts.map +1 -0
- package/dist/worker-utils/lib/worker-api/process-on-worker.js +429 -0
- package/dist/worker-utils/lib/worker-api/process-on-worker.js.map +1 -0
- package/dist/worker-utils/lib/worker-api/shared-state.d.ts +34 -0
- package/dist/worker-utils/lib/worker-api/shared-state.d.ts.map +1 -0
- package/dist/worker-utils/lib/worker-api/shared-state.js +105 -0
- package/dist/worker-utils/lib/worker-api/shared-state.js.map +1 -0
- package/dist/worker-utils/lib/worker-api/validate-output-ownership.d.ts +3 -0
- package/dist/worker-utils/lib/worker-api/validate-output-ownership.d.ts.map +1 -0
- package/dist/worker-utils/lib/worker-api/validate-output-ownership.js +10 -0
- package/dist/worker-utils/lib/worker-api/validate-output-ownership.js.map +1 -0
- package/dist/worker-utils/lib/worker-api/validate-worker-version.d.ts +9 -0
- package/dist/worker-utils/lib/worker-api/validate-worker-version.d.ts.map +1 -0
- package/dist/worker-utils/lib/worker-api/validate-worker-version.js +35 -0
- package/dist/worker-utils/lib/worker-api/validate-worker-version.js.map +1 -0
- package/dist/worker-utils/lib/worker-api/worker-resource.d.ts +33 -0
- package/dist/worker-utils/lib/worker-api/worker-resource.d.ts.map +1 -0
- package/dist/worker-utils/lib/worker-api/worker-resource.js +156 -0
- package/dist/worker-utils/lib/worker-api/worker-resource.js.map +1 -0
- package/dist/worker-utils/lib/worker-farm/worker-body.d.ts +20 -0
- package/dist/worker-utils/lib/worker-farm/worker-body.d.ts.map +1 -0
- package/dist/worker-utils/lib/worker-farm/worker-body.js +134 -0
- package/dist/worker-utils/lib/worker-farm/worker-body.js.map +1 -0
- package/dist/worker-utils/lib/worker-farm/worker-farm.d.ts +73 -0
- package/dist/worker-utils/lib/worker-farm/worker-farm.d.ts.map +1 -0
- package/dist/worker-utils/lib/worker-farm/worker-farm.js +135 -0
- package/dist/worker-utils/lib/worker-farm/worker-farm.js.map +1 -0
- package/dist/worker-utils/lib/worker-farm/worker-job.d.ts +47 -0
- package/dist/worker-utils/lib/worker-farm/worker-job.d.ts.map +1 -0
- package/dist/worker-utils/lib/worker-farm/worker-job.js +124 -0
- package/dist/worker-utils/lib/worker-farm/worker-job.js.map +1 -0
- package/dist/worker-utils/lib/worker-farm/worker-pool.d.ts +152 -0
- package/dist/worker-utils/lib/worker-farm/worker-pool.d.ts.map +1 -0
- package/dist/worker-utils/lib/worker-farm/worker-pool.js +584 -0
- package/dist/worker-utils/lib/worker-farm/worker-pool.js.map +1 -0
- package/dist/worker-utils/lib/worker-farm/worker-thread.d.ts +77 -0
- package/dist/worker-utils/lib/worker-farm/worker-thread.d.ts.map +1 -0
- package/dist/worker-utils/lib/worker-farm/worker-thread.js +215 -0
- package/dist/worker-utils/lib/worker-farm/worker-thread.js.map +1 -0
- package/dist/worker-utils/lib/worker-utils/get-input-byte-length.d.ts +5 -0
- package/dist/worker-utils/lib/worker-utils/get-input-byte-length.d.ts.map +1 -0
- package/dist/worker-utils/lib/worker-utils/get-input-byte-length.js +39 -0
- package/dist/worker-utils/lib/worker-utils/get-input-byte-length.js.map +1 -0
- package/dist/worker-utils/lib/worker-utils/get-loadable-worker-url.d.ts +15 -0
- package/dist/worker-utils/lib/worker-utils/get-loadable-worker-url.d.ts.map +1 -0
- package/dist/worker-utils/lib/worker-utils/get-loadable-worker-url.js +84 -0
- package/dist/worker-utils/lib/worker-utils/get-loadable-worker-url.js.map +1 -0
- package/dist/worker-utils/lib/worker-utils/get-transfer-list.d.ts +16 -0
- package/dist/worker-utils/lib/worker-utils/get-transfer-list.d.ts.map +1 -0
- package/dist/worker-utils/lib/worker-utils/get-transfer-list.js +127 -0
- package/dist/worker-utils/lib/worker-utils/get-transfer-list.js.map +1 -0
- package/dist/worker-utils/lib/worker-utils/remove-nontransferable-options.d.ts +6 -0
- package/dist/worker-utils/lib/worker-utils/remove-nontransferable-options.d.ts.map +1 -0
- package/dist/worker-utils/lib/worker-utils/remove-nontransferable-options.js +27 -0
- package/dist/worker-utils/lib/worker-utils/remove-nontransferable-options.js.map +1 -0
- package/dist/worker-utils/types.d.ts +165 -0
- package/dist/worker-utils/types.d.ts.map +1 -0
- package/dist/worker-utils/types.js +5 -0
- package/dist/worker-utils/types.js.map +1 -0
- package/dist/worker-utils/workers/null-worker.d.ts +2 -0
- package/dist/worker-utils/workers/null-worker.d.ts.map +1 -0
- package/dist/worker-utils/workers/null-worker.js +17 -0
- package/dist/worker-utils/workers/null-worker.js.map +1 -0
- package/package.json +43 -4
- package/src/index.ts +5 -0
- package/src/tasks/index.ts +33 -0
- package/src/tasks/task-channel.ts +191 -0
- package/src/tasks/task-operation-queue.ts +109 -0
- package/src/tasks/task-preload.ts +107 -0
- package/src/tasks/task-runner.ts +933 -0
- package/src/tasks/task-runtime.ts +244 -0
- package/src/tasks/task-sync.ts +100 -0
- package/src/tasks/task-worker-channel.ts +44 -0
- package/src/tasks/task.ts +168 -0
- package/src/tasks/typed-task-dispatch.ts +44 -0
- package/src/worker-utils/index.ts +105 -0
- package/src/worker-utils/lib/async-queue/async-queue.ts +100 -0
- package/src/worker-utils/lib/env-utils/assert.ts +14 -0
- package/src/worker-utils/lib/env-utils/globals.ts +40 -0
- package/src/worker-utils/lib/env-utils/version.ts +15 -0
- package/src/worker-utils/lib/library-utils/library-utils.ts +305 -0
- package/src/worker-utils/lib/node/worker_threads-browser.ts +44 -0
- package/src/worker-utils/lib/node/worker_threads.ts +21 -0
- package/src/worker-utils/lib/npm-tag.ts +5 -0
- package/src/worker-utils/lib/process-utils/child-process-proxy.browser.ts +23 -0
- package/src/worker-utils/lib/process-utils/child-process-proxy.ts +172 -0
- package/src/worker-utils/lib/process-utils/process-utils.ts +34 -0
- package/src/worker-utils/lib/worker-api/create-worker.ts +271 -0
- package/src/worker-utils/lib/worker-api/get-worker-url.ts +130 -0
- package/src/worker-utils/lib/worker-api/process-on-worker.ts +596 -0
- package/src/worker-utils/lib/worker-api/shared-state.ts +135 -0
- package/src/worker-utils/lib/worker-api/validate-output-ownership.ts +10 -0
- package/src/worker-utils/lib/worker-api/validate-worker-version.ts +44 -0
- package/src/worker-utils/lib/worker-api/worker-resource.ts +203 -0
- package/src/worker-utils/lib/worker-farm/worker-body.ts +154 -0
- package/src/worker-utils/lib/worker-farm/worker-farm.ts +172 -0
- package/src/worker-utils/lib/worker-farm/worker-job.ts +148 -0
- package/src/worker-utils/lib/worker-farm/worker-pool.ts +719 -0
- package/src/worker-utils/lib/worker-farm/worker-thread.ts +260 -0
- package/src/worker-utils/lib/worker-utils/get-input-byte-length.ts +34 -0
- package/src/worker-utils/lib/worker-utils/get-loadable-worker-url.ts +96 -0
- package/src/worker-utils/lib/worker-utils/get-transfer-list.ts +125 -0
- package/src/worker-utils/lib/worker-utils/remove-nontransferable-options.ts +27 -0
- package/src/worker-utils/types.ts +224 -0
- package/src/worker-utils/workers/null-worker.ts +20 -0
|
@@ -0,0 +1,172 @@
|
|
|
1
|
+
// loaders.gl
|
|
2
|
+
// SPDX-License-Identifier: MIT
|
|
3
|
+
// SPDX-FileCopyrightText: Copyright (c) vis.gl contributors
|
|
4
|
+
|
|
5
|
+
import WorkerPool from './worker-pool';
|
|
6
|
+
import type {WorkerPoolTarget} from './worker-pool';
|
|
7
|
+
import WorkerThread from './worker-thread';
|
|
8
|
+
import type {LoadWorker, WorkerJobMetrics} from '../../types';
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* @param maxConcurrency - max count of workers
|
|
12
|
+
* @param maxMobileConcurrency - max count of workers on mobile
|
|
13
|
+
* @param maxConcurrency - max count of workers
|
|
14
|
+
* @param reuseWorkers - if false, destroys workers when task is completed
|
|
15
|
+
* @param onDebug - callback intended to allow application to log worker pool activity
|
|
16
|
+
*/
|
|
17
|
+
export type WorkerFarmProps = {
|
|
18
|
+
maxQueuedJobs?: number;
|
|
19
|
+
maxPendingAdmissions?: number;
|
|
20
|
+
priorityAgingMs?: number;
|
|
21
|
+
onMetrics?: (metrics: WorkerJobMetrics) => void;
|
|
22
|
+
performanceMarks?: boolean;
|
|
23
|
+
maxQueuedBytes?: number;
|
|
24
|
+
maxInFlightBytes?: number;
|
|
25
|
+
maxConcurrency?: number;
|
|
26
|
+
maxMobileConcurrency?: number;
|
|
27
|
+
reuseWorkers?: boolean;
|
|
28
|
+
onDebug?: () => void;
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
const DEFAULT_PROPS: Required<WorkerFarmProps> = {
|
|
32
|
+
maxQueuedJobs: Infinity,
|
|
33
|
+
maxPendingAdmissions: 128,
|
|
34
|
+
priorityAgingMs: 1000,
|
|
35
|
+
onMetrics: () => {},
|
|
36
|
+
performanceMarks: false,
|
|
37
|
+
maxQueuedBytes: Infinity,
|
|
38
|
+
maxInFlightBytes: Infinity,
|
|
39
|
+
maxConcurrency: 3,
|
|
40
|
+
maxMobileConcurrency: 1,
|
|
41
|
+
reuseWorkers: true,
|
|
42
|
+
onDebug: () => {}
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
/** Stable ids used to distinguish worker factories in the pool cache. */
|
|
46
|
+
const loadWorkerIds = new WeakMap<LoadWorker, number>();
|
|
47
|
+
/** Next stable worker-factory id. */
|
|
48
|
+
let nextLoadWorkerId = 1;
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* Process multiple jobs with a "farm" of different workers in worker pools.
|
|
52
|
+
*/
|
|
53
|
+
export default class WorkerFarm {
|
|
54
|
+
private props: WorkerFarmProps;
|
|
55
|
+
private workerPools = new Map<string, WorkerPool>();
|
|
56
|
+
// singleton
|
|
57
|
+
private static _workerFarm?: WorkerFarm;
|
|
58
|
+
|
|
59
|
+
/** Checks if workers are supported on this platform */
|
|
60
|
+
static isSupported(): boolean {
|
|
61
|
+
return WorkerThread.isSupported();
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/** Get the singleton instance of the global worker farm */
|
|
65
|
+
static getWorkerFarm(props: WorkerFarmProps = {}): WorkerFarm {
|
|
66
|
+
WorkerFarm._workerFarm = WorkerFarm._workerFarm || new WorkerFarm({});
|
|
67
|
+
WorkerFarm._workerFarm.setProps(props);
|
|
68
|
+
return WorkerFarm._workerFarm;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/** get global instance with WorkerFarm.getWorkerFarm() */
|
|
72
|
+
private constructor(props: WorkerFarmProps) {
|
|
73
|
+
this.props = {...DEFAULT_PROPS};
|
|
74
|
+
this.setProps(props);
|
|
75
|
+
/** @type Map<string, WorkerPool>} */
|
|
76
|
+
this.workerPools = new Map();
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* Terminate all workers in the farm and abort active jobs.
|
|
81
|
+
* @param reason Optional error delivered to active jobs.
|
|
82
|
+
* @note Can free up significant memory
|
|
83
|
+
*/
|
|
84
|
+
destroy(reason?: unknown): Promise<void> {
|
|
85
|
+
const pools = [...this.workerPools.values()];
|
|
86
|
+
this.workerPools.clear();
|
|
87
|
+
return Promise.all(pools.map(pool => pool.destroy(reason))).then(() => undefined);
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* Set props used when initializing worker pools
|
|
92
|
+
* @param props
|
|
93
|
+
*/
|
|
94
|
+
setProps(props: WorkerFarmProps): void {
|
|
95
|
+
// Task payloads, signals and callbacks outside farm configuration must not be retained.
|
|
96
|
+
const configuration: WorkerFarmProps = {};
|
|
97
|
+
for (const key of Object.keys(DEFAULT_PROPS)) {
|
|
98
|
+
if (props[key] !== undefined) configuration[key] = props[key];
|
|
99
|
+
}
|
|
100
|
+
this.props = {...this.props, ...configuration};
|
|
101
|
+
// Update worker pool props
|
|
102
|
+
for (const workerPool of this.workerPools.values()) {
|
|
103
|
+
workerPool.setProps(this._getWorkerPoolProps());
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* Returns a worker pool for the specified worker
|
|
109
|
+
* @param options Worker target used to identify and initialize the pool.
|
|
110
|
+
* @param options.name Worker name used as the base pool identity.
|
|
111
|
+
* @param options.url Worker script URL.
|
|
112
|
+
* @param options.source Inline worker source.
|
|
113
|
+
* @param options.loadWorker Built-in browser worker factory.
|
|
114
|
+
* @example
|
|
115
|
+
* const job = WorkerFarm.getWorkerFarm().getWorkerPool({name, url}).startJob(...);
|
|
116
|
+
*/
|
|
117
|
+
getWorkerPool(options: WorkerPoolTarget): WorkerPool {
|
|
118
|
+
const {name, source, url, getUrl, loadWorker, workerType} = options;
|
|
119
|
+
const workerPoolKey = getWorkerPoolKey(options);
|
|
120
|
+
let workerPool = this.workerPools.get(workerPoolKey);
|
|
121
|
+
if (!workerPool) {
|
|
122
|
+
workerPool = new WorkerPool({
|
|
123
|
+
name,
|
|
124
|
+
source,
|
|
125
|
+
url,
|
|
126
|
+
getUrl,
|
|
127
|
+
loadWorker,
|
|
128
|
+
workerType
|
|
129
|
+
});
|
|
130
|
+
workerPool.setProps(this._getWorkerPoolProps());
|
|
131
|
+
this.workerPools.set(workerPoolKey, workerPool);
|
|
132
|
+
}
|
|
133
|
+
return workerPool;
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
_getWorkerPoolProps() {
|
|
137
|
+
return {
|
|
138
|
+
maxQueuedJobs: this.props.maxQueuedJobs,
|
|
139
|
+
maxPendingAdmissions: this.props.maxPendingAdmissions,
|
|
140
|
+
priorityAgingMs: this.props.priorityAgingMs,
|
|
141
|
+
onMetrics: this.props.onMetrics,
|
|
142
|
+
performanceMarks: this.props.performanceMarks,
|
|
143
|
+
maxQueuedBytes: this.props.maxQueuedBytes,
|
|
144
|
+
maxInFlightBytes: this.props.maxInFlightBytes,
|
|
145
|
+
maxConcurrency: this.props.maxConcurrency,
|
|
146
|
+
maxMobileConcurrency: this.props.maxMobileConcurrency,
|
|
147
|
+
reuseWorkers: this.props.reuseWorkers,
|
|
148
|
+
onDebug: this.props.onDebug
|
|
149
|
+
};
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
/** Returns a cache key that distinguishes each worker source and factory. */
|
|
154
|
+
function getWorkerPoolKey(options: WorkerPoolTarget): string {
|
|
155
|
+
const {name, source, url, urlKey, loadWorker, workerType} = options;
|
|
156
|
+
const sourceKey = source ? `source:${source}` : '';
|
|
157
|
+
const urlKeyPart = url || urlKey ? `url:${url || urlKey}` : '';
|
|
158
|
+
const loadWorkerKey = loadWorker ? `loadWorker:${getLoadWorkerId(loadWorker)}` : '';
|
|
159
|
+
return [name, sourceKey, urlKeyPart, loadWorkerKey, `type:${workerType || 'classic'}`]
|
|
160
|
+
.filter(Boolean)
|
|
161
|
+
.join('|');
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
/** Returns a stable identifier for a worker factory function. */
|
|
165
|
+
function getLoadWorkerId(loadWorker: LoadWorker): number {
|
|
166
|
+
let loadWorkerId = loadWorkerIds.get(loadWorker);
|
|
167
|
+
if (!loadWorkerId) {
|
|
168
|
+
loadWorkerId = nextLoadWorkerId++;
|
|
169
|
+
loadWorkerIds.set(loadWorker, loadWorkerId);
|
|
170
|
+
}
|
|
171
|
+
return loadWorkerId;
|
|
172
|
+
}
|
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
// loaders.gl
|
|
2
|
+
// SPDX-License-Identifier: MIT
|
|
3
|
+
// SPDX-FileCopyrightText: Copyright (c) vis.gl contributors
|
|
4
|
+
|
|
5
|
+
import type {WorkerMessageType, WorkerMessagePayload} from '../../types';
|
|
6
|
+
import WorkerThread from './worker-thread';
|
|
7
|
+
import type {SharedWorkerState} from '../worker-api/shared-state';
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Represents one Job handled by a WorkerPool or WorkerFarm
|
|
11
|
+
*/
|
|
12
|
+
export default class WorkerJob {
|
|
13
|
+
readonly name: string;
|
|
14
|
+
readonly workerThread: WorkerThread;
|
|
15
|
+
isRunning: boolean = true;
|
|
16
|
+
isCancelled = false;
|
|
17
|
+
sharedState?: SharedWorkerState;
|
|
18
|
+
failed = false;
|
|
19
|
+
postMessageMs = 0;
|
|
20
|
+
executionMs?: number;
|
|
21
|
+
/** Settles only when the worker can be reused, even after cooperative cancellation. */
|
|
22
|
+
readonly completion: Promise<void>;
|
|
23
|
+
private complete: () => void = () => {};
|
|
24
|
+
private processStarted = false;
|
|
25
|
+
private cancellationTimer?: ReturnType<typeof setTimeout>;
|
|
26
|
+
/** Promise that resolves when Job is done */
|
|
27
|
+
readonly result: Promise<any>;
|
|
28
|
+
|
|
29
|
+
private _resolve: (value: any) => void = () => {};
|
|
30
|
+
private _reject: (reason?: any) => void = () => {};
|
|
31
|
+
|
|
32
|
+
constructor(jobName: string, workerThread: WorkerThread) {
|
|
33
|
+
this.name = jobName;
|
|
34
|
+
this.workerThread = workerThread;
|
|
35
|
+
this.completion = new Promise(resolve => {
|
|
36
|
+
this.complete = resolve;
|
|
37
|
+
});
|
|
38
|
+
this.result = new Promise((resolve, reject) => {
|
|
39
|
+
this._resolve = resolve;
|
|
40
|
+
this._reject = reject;
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Send a message to the job's worker thread
|
|
46
|
+
* @param data any data structure, ideally consisting mostly of transferrable objects
|
|
47
|
+
*/
|
|
48
|
+
postMessage(
|
|
49
|
+
type: WorkerMessageType,
|
|
50
|
+
payload: WorkerMessagePayload,
|
|
51
|
+
transferList?: Transferable[]
|
|
52
|
+
): void {
|
|
53
|
+
if (!this.isRunning || this.isCancelled) return;
|
|
54
|
+
const startedAt = performance.now();
|
|
55
|
+
if (type === 'process' || type === 'process-in-batches' || type === 'preload')
|
|
56
|
+
this.processStarted = true;
|
|
57
|
+
try {
|
|
58
|
+
this.workerThread.postMessage(
|
|
59
|
+
{
|
|
60
|
+
source: 'loaders.gl', // Lets worker ignore unrelated messages
|
|
61
|
+
type,
|
|
62
|
+
payload
|
|
63
|
+
},
|
|
64
|
+
transferList
|
|
65
|
+
);
|
|
66
|
+
} finally {
|
|
67
|
+
this.postMessageMs += performance.now() - startedAt;
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* Call to resolve the `result` Promise with the supplied value
|
|
73
|
+
*/
|
|
74
|
+
done(value: any): void {
|
|
75
|
+
if (!this.isRunning) {
|
|
76
|
+
return;
|
|
77
|
+
}
|
|
78
|
+
this.isRunning = false;
|
|
79
|
+
this.executionMs = value?.executionMs;
|
|
80
|
+
clearTimeout(this.cancellationTimer);
|
|
81
|
+
this.complete();
|
|
82
|
+
if (!this.isCancelled) this._resolve(value);
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* Call to reject the `result` Promise with the supplied error
|
|
87
|
+
*/
|
|
88
|
+
error(error: unknown): void {
|
|
89
|
+
if (!this.isRunning) {
|
|
90
|
+
return;
|
|
91
|
+
}
|
|
92
|
+
this.isRunning = false;
|
|
93
|
+
this.failed = true;
|
|
94
|
+
if (this.sharedState?.snapshot.cancelled) this.isCancelled = true;
|
|
95
|
+
clearTimeout(this.cancellationTimer);
|
|
96
|
+
this.complete();
|
|
97
|
+
this._reject(error);
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/** Reject immediately, but keep the lease until acknowledgement or hard-cancel timeout. */
|
|
101
|
+
cancel(reason?: unknown, timeout = 1000): void {
|
|
102
|
+
if (!this.isRunning || this.isCancelled) return;
|
|
103
|
+
if (!Number.isFinite(timeout) || timeout < 0) {
|
|
104
|
+
this.abort(new RangeError('cancellationTimeout must be finite and nonnegative'));
|
|
105
|
+
return;
|
|
106
|
+
}
|
|
107
|
+
this.isCancelled = true;
|
|
108
|
+
this.sharedState?.cancel();
|
|
109
|
+
const abortReason = arguments.length
|
|
110
|
+
? reason
|
|
111
|
+
: createAbortError(`Worker job "${this.name}" was cancelled`);
|
|
112
|
+
if (!this.processStarted) {
|
|
113
|
+
// Nothing was posted: settle the lease without terminating or waiting for an acknowledgement.
|
|
114
|
+
this.error(abortReason);
|
|
115
|
+
return;
|
|
116
|
+
}
|
|
117
|
+
this._reject(abortReason);
|
|
118
|
+
try {
|
|
119
|
+
this.workerThread.postMessage({source: 'loaders.gl', type: 'cancel', payload: {}}, []);
|
|
120
|
+
this.cancellationTimer = setTimeout(() => this.abort(reason), timeout);
|
|
121
|
+
} catch (error) {
|
|
122
|
+
this.abort(error);
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/**
|
|
127
|
+
* Terminates the worker executing this job and rejects its result.
|
|
128
|
+
* @param reason Abort reason supplied by the caller, or a cross-runtime `AbortError`.
|
|
129
|
+
*/
|
|
130
|
+
abort(reason?: unknown): void {
|
|
131
|
+
if (!this.isRunning) {
|
|
132
|
+
return;
|
|
133
|
+
}
|
|
134
|
+
this.isCancelled = true;
|
|
135
|
+
this.sharedState?.cancel();
|
|
136
|
+
this.workerThread.destroy();
|
|
137
|
+
this.error(
|
|
138
|
+
arguments.length ? reason : createAbortError(`Worker job "${this.name}" was aborted`)
|
|
139
|
+
);
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
/** Creates an abort error without requiring the DOMException global. */
|
|
144
|
+
function createAbortError(message: string): Error {
|
|
145
|
+
const error = new Error(message);
|
|
146
|
+
error.name = 'AbortError';
|
|
147
|
+
return error;
|
|
148
|
+
}
|