@loaders.gl/core 5.0.0-beta.1 → 5.0.0-beta.4
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/README.md +3 -1
- package/dist/cluster-source.cjs +7228 -0
- package/dist/cluster-source.cjs.map +6 -0
- package/dist/cluster-source.d.ts +2 -0
- package/dist/cluster-source.d.ts.map +1 -0
- package/dist/cluster-source.js +2 -0
- package/dist/cluster-source.js.map +1 -0
- package/dist/dist.dev.js +2141 -438
- package/dist/dist.min.js +3 -3
- package/dist/federated-table-scan-source.cjs +2677 -0
- package/dist/federated-table-scan-source.cjs.map +6 -0
- package/dist/federated-table-scan-source.d.ts +2 -0
- package/dist/federated-table-scan-source.d.ts.map +1 -0
- package/dist/federated-table-scan-source.js +2 -0
- package/dist/federated-table-scan-source.js.map +1 -0
- package/dist/index.cjs +268 -960
- package/dist/index.cjs.map +3 -3
- package/dist/lib/init.js +1 -1
- package/dist/loader-utils/index.cjs +418 -1234
- package/dist/loader-utils/index.cjs.map +3 -3
- package/dist/loader-utils/json-loader.js +1 -1
- package/dist/loader-utils/lib/log-utils/log.js +1 -1
- package/dist/loader-utils/lib/worker-loader-utils/encode-with-worker.d.ts.map +1 -1
- package/dist/loader-utils/lib/worker-loader-utils/encode-with-worker.js +2 -5
- package/dist/loader-utils/lib/worker-loader-utils/encode-with-worker.js.map +1 -1
- package/dist/loader-utils/lib/worker-loader-utils/parse-with-worker.d.ts.map +1 -1
- package/dist/loader-utils/lib/worker-loader-utils/parse-with-worker.js +14 -6
- package/dist/loader-utils/lib/worker-loader-utils/parse-with-worker.js.map +1 -1
- package/dist/loader-utils/loader-types.d.ts +2 -2
- package/dist/loader-utils/loader-types.d.ts.map +1 -1
- package/dist/null-loader.js +1 -1
- package/dist/null-worker-node.js +538 -82
- package/dist/null-worker.js +514 -81
- package/dist/polyfills/images/encode-image-node.d.ts +18 -15
- package/dist/polyfills/images/encode-image-node.d.ts.map +1 -1
- package/dist/polyfills/images/encode-image-node.js +19 -33
- package/dist/polyfills/images/encode-image-node.js.map +1 -1
- package/dist/polyfills/images/image-codecs-node.d.ts +2 -6
- package/dist/polyfills/images/image-codecs-node.d.ts.map +1 -1
- package/dist/polyfills/images/image-codecs-node.js +2 -24
- package/dist/polyfills/images/image-codecs-node.js.map +1 -1
- package/dist/polyfills/images/load-sharp.d.ts +3 -0
- package/dist/polyfills/images/load-sharp.d.ts.map +1 -0
- package/dist/polyfills/images/load-sharp.js +11 -0
- package/dist/polyfills/images/load-sharp.js.map +1 -0
- package/dist/polyfills/images/node-image-bitmap.d.ts +1 -1
- package/dist/polyfills/images/node-image-bitmap.js +1 -1
- package/dist/polyfills/images/node-image-formats.d.ts +1 -1
- package/dist/polyfills/images/node-image-formats.d.ts.map +1 -1
- package/dist/polyfills/images/node-image-formats.js +10 -4
- package/dist/polyfills/images/node-image-formats.js.map +1 -1
- package/dist/polyfills/images/parse-image-node.d.ts +10 -2
- package/dist/polyfills/images/parse-image-node.d.ts.map +1 -1
- package/dist/polyfills/images/parse-image-node.js +20 -32
- package/dist/polyfills/images/parse-image-node.js.map +1 -1
- package/dist/polyfills/index.cjs +73 -149
- package/dist/vector-table-scan-source.cjs +1703 -0
- package/dist/vector-table-scan-source.cjs.map +6 -0
- package/dist/vector-table-scan-source.d.ts +2 -0
- package/dist/vector-table-scan-source.d.ts.map +1 -0
- package/dist/vector-table-scan-source.js +2 -0
- package/dist/vector-table-scan-source.js.map +1 -0
- package/dist/worker-utils/index.cjs +371 -1182
- package/dist/worker-utils/index.cjs.map +3 -3
- package/dist/worker-utils/index.d.ts +5 -8
- package/dist/worker-utils/index.d.ts.map +1 -1
- package/dist/worker-utils/index.js +4 -10
- package/dist/worker-utils/index.js.map +1 -1
- package/dist/worker-utils/lib/env-utils/version.js +1 -1
- package/dist/worker-utils/lib/worker-api/create-worker.d.ts +5 -7
- package/dist/worker-utils/lib/worker-api/create-worker.d.ts.map +1 -1
- package/dist/worker-utils/lib/worker-api/create-worker.js +76 -146
- package/dist/worker-utils/lib/worker-api/create-worker.js.map +1 -1
- package/dist/worker-utils/lib/worker-api/process-on-worker.d.ts +16 -47
- package/dist/worker-utils/lib/worker-api/process-on-worker.d.ts.map +1 -1
- package/dist/worker-utils/lib/worker-api/process-on-worker.js +214 -312
- package/dist/worker-utils/lib/worker-api/process-on-worker.js.map +1 -1
- package/dist/worker-utils/lib/worker-api/task-runtime.d.ts +36 -0
- package/dist/worker-utils/lib/worker-api/task-runtime.d.ts.map +1 -0
- package/dist/worker-utils/lib/worker-api/task-runtime.js +23 -0
- package/dist/worker-utils/lib/worker-api/task-runtime.js.map +1 -0
- package/dist/worker-utils/lib/worker-utils/get-transfer-list.d.ts +1 -0
- package/dist/worker-utils/lib/worker-utils/get-transfer-list.d.ts.map +1 -1
- package/dist/worker-utils/lib/worker-utils/get-transfer-list.js +3 -1
- package/dist/worker-utils/lib/worker-utils/get-transfer-list.js.map +1 -1
- package/dist/worker-utils/null-worker-node.js +603 -147
- package/dist/worker-utils/null-worker-node.js.map +4 -4
- package/dist/worker-utils/null-worker.js +576 -143
- package/dist/worker-utils/null-worker.js.map +4 -4
- package/dist/worker-utils/types.d.ts +55 -72
- package/dist/worker-utils/types.d.ts.map +1 -1
- package/package.json +27 -29
- package/src/cluster-source.ts +1 -0
- package/src/federated-table-scan-source.ts +1 -0
- package/src/loader-utils/lib/worker-loader-utils/encode-with-worker.ts +5 -6
- package/src/loader-utils/lib/worker-loader-utils/parse-with-worker.ts +14 -6
- package/src/loader-utils/loader-types.ts +2 -2
- package/src/polyfills/README.md +2 -2
- package/src/polyfills/images/encode-image-node.ts +44 -37
- package/src/polyfills/images/image-codecs-node.ts +2 -37
- package/src/polyfills/images/load-sharp.ts +13 -0
- package/src/polyfills/images/node-image-bitmap.ts +1 -1
- package/src/polyfills/images/node-image-formats.ts +10 -4
- package/src/polyfills/images/parse-image-node.ts +31 -40
- package/src/vector-table-scan-source.ts +1 -0
- package/src/worker-utils/index.ts +17 -17
- package/src/worker-utils/lib/worker-api/create-worker.ts +89 -188
- package/src/worker-utils/lib/worker-api/process-on-worker.ts +249 -403
- package/src/worker-utils/lib/worker-api/task-runtime.ts +66 -0
- package/src/worker-utils/lib/worker-utils/get-transfer-list.ts +2 -0
- package/src/worker-utils/types.ts +74 -109
- package/dist/worker-utils/lib/async-queue/async-queue.d.ts +0 -32
- package/dist/worker-utils/lib/async-queue/async-queue.d.ts.map +0 -1
- package/dist/worker-utils/lib/async-queue/async-queue.js +0 -91
- package/dist/worker-utils/lib/async-queue/async-queue.js.map +0 -1
- package/dist/worker-utils/lib/worker-farm/worker-body.d.ts +0 -20
- package/dist/worker-utils/lib/worker-farm/worker-body.d.ts.map +0 -1
- package/dist/worker-utils/lib/worker-farm/worker-body.js +0 -131
- package/dist/worker-utils/lib/worker-farm/worker-body.js.map +0 -1
- package/dist/worker-utils/lib/worker-farm/worker-farm.d.ts +0 -58
- package/dist/worker-utils/lib/worker-farm/worker-farm.d.ts.map +0 -1
- package/dist/worker-utils/lib/worker-farm/worker-farm.js +0 -116
- package/dist/worker-utils/lib/worker-farm/worker-farm.js.map +0 -1
- package/dist/worker-utils/lib/worker-farm/worker-job.d.ts +0 -34
- package/dist/worker-utils/lib/worker-farm/worker-job.d.ts.map +0 -1
- package/dist/worker-utils/lib/worker-farm/worker-job.js +0 -74
- package/dist/worker-utils/lib/worker-farm/worker-job.js.map +0 -1
- package/dist/worker-utils/lib/worker-farm/worker-pool.d.ts +0 -94
- package/dist/worker-utils/lib/worker-farm/worker-pool.d.ts.map +0 -1
- package/dist/worker-utils/lib/worker-farm/worker-pool.js +0 -246
- package/dist/worker-utils/lib/worker-farm/worker-pool.js.map +0 -1
- package/dist/worker-utils/lib/worker-farm/worker-thread.d.ts +0 -67
- package/dist/worker-utils/lib/worker-farm/worker-thread.d.ts.map +0 -1
- package/dist/worker-utils/lib/worker-farm/worker-thread.js +0 -175
- package/dist/worker-utils/lib/worker-farm/worker-thread.js.map +0 -1
- package/src/polyfills/images/encode-image.node.ts.disabled +0 -41
- package/src/polyfills/images/parse-image.node.ts.disabled +0 -55
- package/src/worker-utils/lib/async-queue/async-queue.ts +0 -100
- package/src/worker-utils/lib/worker-farm/worker-body.ts +0 -145
- package/src/worker-utils/lib/worker-farm/worker-farm.ts +0 -144
- package/src/worker-utils/lib/worker-farm/worker-job.ts +0 -85
- package/src/worker-utils/lib/worker-farm/worker-pool.ts +0 -316
- package/src/worker-utils/lib/worker-farm/worker-thread.ts +0 -206
|
@@ -1,85 +0,0 @@
|
|
|
1
|
-
// loaders.gl
|
|
2
|
-
// SPDX-License-Identifier: MIT
|
|
3
|
-
// Copyright (c) vis.gl contributors
|
|
4
|
-
|
|
5
|
-
import type {WorkerMessageType, WorkerMessagePayload} from '../../types';
|
|
6
|
-
import WorkerThread from './worker-thread';
|
|
7
|
-
|
|
8
|
-
/**
|
|
9
|
-
* Represents one Job handled by a WorkerPool or WorkerFarm
|
|
10
|
-
*/
|
|
11
|
-
export default class WorkerJob {
|
|
12
|
-
readonly name: string;
|
|
13
|
-
readonly workerThread: WorkerThread;
|
|
14
|
-
isRunning: boolean = true;
|
|
15
|
-
/** Promise that resolves when Job is done */
|
|
16
|
-
readonly result: Promise<any>;
|
|
17
|
-
|
|
18
|
-
private _resolve: (value: any) => void = () => {};
|
|
19
|
-
private _reject: (reason?: any) => void = () => {};
|
|
20
|
-
|
|
21
|
-
constructor(jobName: string, workerThread: WorkerThread) {
|
|
22
|
-
this.name = jobName;
|
|
23
|
-
this.workerThread = workerThread;
|
|
24
|
-
this.result = new Promise((resolve, reject) => {
|
|
25
|
-
this._resolve = resolve;
|
|
26
|
-
this._reject = reject;
|
|
27
|
-
});
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
/**
|
|
31
|
-
* Send a message to the job's worker thread
|
|
32
|
-
* @param data any data structure, ideally consisting mostly of transferrable objects
|
|
33
|
-
*/
|
|
34
|
-
postMessage(type: WorkerMessageType, payload: WorkerMessagePayload): void {
|
|
35
|
-
if (!this.isRunning) return;
|
|
36
|
-
this.workerThread.postMessage({
|
|
37
|
-
source: 'loaders.gl', // Lets worker ignore unrelated messages
|
|
38
|
-
type,
|
|
39
|
-
payload
|
|
40
|
-
});
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
/**
|
|
44
|
-
* Call to resolve the `result` Promise with the supplied value
|
|
45
|
-
*/
|
|
46
|
-
done(value: any): void {
|
|
47
|
-
if (!this.isRunning) {
|
|
48
|
-
return;
|
|
49
|
-
}
|
|
50
|
-
this.isRunning = false;
|
|
51
|
-
this._resolve(value);
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
/**
|
|
55
|
-
* Call to reject the `result` Promise with the supplied error
|
|
56
|
-
*/
|
|
57
|
-
error(error: unknown): void {
|
|
58
|
-
if (!this.isRunning) {
|
|
59
|
-
return;
|
|
60
|
-
}
|
|
61
|
-
this.isRunning = false;
|
|
62
|
-
this._reject(error);
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
/**
|
|
66
|
-
* Terminates the worker executing this job and rejects its result.
|
|
67
|
-
* @param reason Abort reason supplied by the caller, or a cross-runtime `AbortError`.
|
|
68
|
-
*/
|
|
69
|
-
abort(reason?: unknown): void {
|
|
70
|
-
if (!this.isRunning) {
|
|
71
|
-
return;
|
|
72
|
-
}
|
|
73
|
-
this.workerThread.destroy();
|
|
74
|
-
this.error(
|
|
75
|
-
arguments.length ? reason : createAbortError(`Worker job "${this.name}" was aborted`)
|
|
76
|
-
);
|
|
77
|
-
}
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
/** Creates an abort error without requiring the DOMException global. */
|
|
81
|
-
function createAbortError(message: string): Error {
|
|
82
|
-
const error = new Error(message);
|
|
83
|
-
error.name = 'AbortError';
|
|
84
|
-
return error;
|
|
85
|
-
}
|
|
@@ -1,316 +0,0 @@
|
|
|
1
|
-
// loaders.gl
|
|
2
|
-
// SPDX-License-Identifier: MIT
|
|
3
|
-
// Copyright (c) vis.gl contributors
|
|
4
|
-
|
|
5
|
-
import type {LoadWorker, WorkerMessageType, WorkerMessagePayload} from '../../types';
|
|
6
|
-
import {isMobile} from '../env-utils/globals';
|
|
7
|
-
import WorkerThread from './worker-thread';
|
|
8
|
-
import WorkerJob from './worker-job';
|
|
9
|
-
|
|
10
|
-
/** WorkerPool onDebug Callback Parameters */
|
|
11
|
-
type OnDebugParameters = {
|
|
12
|
-
message: string;
|
|
13
|
-
worker: string;
|
|
14
|
-
name: string;
|
|
15
|
-
job: string;
|
|
16
|
-
backlog: number;
|
|
17
|
-
workerThread: WorkerThread;
|
|
18
|
-
};
|
|
19
|
-
|
|
20
|
-
/** WorkerPool Properties */
|
|
21
|
-
export type WorkerPoolProps = {
|
|
22
|
-
name?: string;
|
|
23
|
-
source?: string; // | Function;
|
|
24
|
-
url?: string;
|
|
25
|
-
/** Lazily resolves a classic worker fallback URL. */
|
|
26
|
-
getUrl?: () => string;
|
|
27
|
-
/** Creates a browser Worker directly. */
|
|
28
|
-
loadWorker?: LoadWorker;
|
|
29
|
-
maxConcurrency?: number;
|
|
30
|
-
maxMobileConcurrency?: number;
|
|
31
|
-
onDebug?: (options: OnDebugParameters) => any;
|
|
32
|
-
reuseWorkers?: boolean;
|
|
33
|
-
};
|
|
34
|
-
|
|
35
|
-
/** Worker-pool target, including values used only to distinguish cached pools. */
|
|
36
|
-
export type WorkerPoolTarget = WorkerPoolProps & {
|
|
37
|
-
/** Worker name used as the base pool identity. */
|
|
38
|
-
name: string;
|
|
39
|
-
/** Stable identity for a lazily resolved URL. */
|
|
40
|
-
urlKey?: string;
|
|
41
|
-
};
|
|
42
|
-
|
|
43
|
-
/** Private helper types */
|
|
44
|
-
type OnMessage = (job: WorkerJob, type: WorkerMessageType, payload: WorkerMessagePayload) => void;
|
|
45
|
-
type OnError = (job: WorkerJob, error: Error) => void;
|
|
46
|
-
|
|
47
|
-
type QueuedJob = {
|
|
48
|
-
name: string;
|
|
49
|
-
onMessage: OnMessage;
|
|
50
|
-
onError: OnError;
|
|
51
|
-
onStart: (value: any) => void; // Resolve job start promise
|
|
52
|
-
onReject: (reason: unknown) => void; // Reject job start promise
|
|
53
|
-
/** Removes the queued cancellation listener when the job leaves the queue. */
|
|
54
|
-
removeAbortListener: () => void;
|
|
55
|
-
/** Cancels this queued or active job. */
|
|
56
|
-
signal?: AbortSignal;
|
|
57
|
-
};
|
|
58
|
-
|
|
59
|
-
/**
|
|
60
|
-
* Process multiple data messages with small pool of identical workers
|
|
61
|
-
*/
|
|
62
|
-
export default class WorkerPool {
|
|
63
|
-
name: string = 'unnamed';
|
|
64
|
-
source?: string; // | Function;
|
|
65
|
-
url?: string;
|
|
66
|
-
/** Lazily resolves a classic worker fallback URL. */
|
|
67
|
-
getUrl?: () => string;
|
|
68
|
-
/** Creates a browser Worker directly. */
|
|
69
|
-
loadWorker?: LoadWorker;
|
|
70
|
-
maxConcurrency: number = 1;
|
|
71
|
-
maxMobileConcurrency: number = 1;
|
|
72
|
-
onDebug: (options: OnDebugParameters) => any = () => {};
|
|
73
|
-
reuseWorkers: boolean = true;
|
|
74
|
-
|
|
75
|
-
private props: WorkerPoolProps = {};
|
|
76
|
-
private jobQueue: QueuedJob[] = [];
|
|
77
|
-
private idleQueue: WorkerThread[] = [];
|
|
78
|
-
private activeJobs = new Set<WorkerJob>();
|
|
79
|
-
private count = 0;
|
|
80
|
-
private isDestroyed = false;
|
|
81
|
-
|
|
82
|
-
/** Checks if workers are supported on this platform */
|
|
83
|
-
static isSupported(): boolean {
|
|
84
|
-
return WorkerThread.isSupported();
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
/**
|
|
88
|
-
* @param processor - worker function
|
|
89
|
-
* @param maxConcurrency - max count of workers
|
|
90
|
-
*/
|
|
91
|
-
constructor(props: WorkerPoolProps) {
|
|
92
|
-
this.source = props.source;
|
|
93
|
-
this.url = props.url;
|
|
94
|
-
this.getUrl = props.getUrl;
|
|
95
|
-
this.loadWorker = props.loadWorker;
|
|
96
|
-
this.setProps(props);
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
/**
|
|
100
|
-
* Terminates all workers in the pool and aborts active jobs.
|
|
101
|
-
* @param reason Optional error delivered to active jobs.
|
|
102
|
-
* @note Can free up significant memory
|
|
103
|
-
*/
|
|
104
|
-
destroy(reason: unknown = new Error('Worker pool was destroyed')): void {
|
|
105
|
-
// Abort active jobs so callers receive an error when a development worker is invalidated.
|
|
106
|
-
this.isDestroyed = true;
|
|
107
|
-
this.activeJobs.forEach(job => job.abort(reason));
|
|
108
|
-
const queuedJobs = this.jobQueue.splice(0);
|
|
109
|
-
queuedJobs.forEach(queuedJob => {
|
|
110
|
-
queuedJob.removeAbortListener();
|
|
111
|
-
queuedJob.onReject(reason);
|
|
112
|
-
});
|
|
113
|
-
this.idleQueue.forEach(worker => worker.destroy());
|
|
114
|
-
this.isDestroyed = true;
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
setProps(props: WorkerPoolProps) {
|
|
118
|
-
this.props = {...this.props, ...props};
|
|
119
|
-
|
|
120
|
-
if (props.name !== undefined) {
|
|
121
|
-
this.name = props.name;
|
|
122
|
-
}
|
|
123
|
-
if (props.maxConcurrency !== undefined) {
|
|
124
|
-
this.maxConcurrency = props.maxConcurrency;
|
|
125
|
-
}
|
|
126
|
-
if (props.maxMobileConcurrency !== undefined) {
|
|
127
|
-
this.maxMobileConcurrency = props.maxMobileConcurrency;
|
|
128
|
-
}
|
|
129
|
-
if (props.reuseWorkers !== undefined) {
|
|
130
|
-
this.reuseWorkers = props.reuseWorkers;
|
|
131
|
-
}
|
|
132
|
-
if (props.onDebug !== undefined) {
|
|
133
|
-
this.onDebug = props.onDebug;
|
|
134
|
-
}
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
async startJob(
|
|
138
|
-
name: string,
|
|
139
|
-
onMessage: OnMessage = (job, type, data) => job.done(data),
|
|
140
|
-
onError: OnError = (job, error) => job.error(error),
|
|
141
|
-
signal?: AbortSignal
|
|
142
|
-
): Promise<WorkerJob> {
|
|
143
|
-
if (signal?.aborted) {
|
|
144
|
-
throw signal.reason;
|
|
145
|
-
}
|
|
146
|
-
if (this.isDestroyed) {
|
|
147
|
-
return Promise.reject(new Error('Worker pool was destroyed'));
|
|
148
|
-
}
|
|
149
|
-
// Promise resolves when thread starts working on this job
|
|
150
|
-
const startPromise = new Promise<WorkerJob>((onStart, onReject) => {
|
|
151
|
-
// Promise resolves when thread completes or fails working on this job
|
|
152
|
-
const abortQueuedJob = (): void => {
|
|
153
|
-
const index = this.jobQueue.indexOf(queuedJob);
|
|
154
|
-
if (index >= 0) {
|
|
155
|
-
this.jobQueue.splice(index, 1);
|
|
156
|
-
queuedJob.removeAbortListener();
|
|
157
|
-
onReject(signal?.reason);
|
|
158
|
-
}
|
|
159
|
-
};
|
|
160
|
-
const queuedJob: QueuedJob = {
|
|
161
|
-
name,
|
|
162
|
-
onMessage,
|
|
163
|
-
onError,
|
|
164
|
-
onStart,
|
|
165
|
-
onReject,
|
|
166
|
-
signal,
|
|
167
|
-
removeAbortListener: () => signal?.removeEventListener('abort', abortQueuedJob)
|
|
168
|
-
};
|
|
169
|
-
this.jobQueue.push(queuedJob);
|
|
170
|
-
signal?.addEventListener('abort', abortQueuedJob, {once: true});
|
|
171
|
-
return this;
|
|
172
|
-
});
|
|
173
|
-
this._startQueuedJob(); // eslint-disable-line @typescript-eslint/no-floating-promises
|
|
174
|
-
return await startPromise;
|
|
175
|
-
}
|
|
176
|
-
|
|
177
|
-
// PRIVATE
|
|
178
|
-
|
|
179
|
-
/**
|
|
180
|
-
* Starts first queued job if worker is available or can be created
|
|
181
|
-
* Called when job is started and whenever a worker returns to the idleQueue
|
|
182
|
-
*/
|
|
183
|
-
async _startQueuedJob(): Promise<void> {
|
|
184
|
-
if (!this.jobQueue.length) {
|
|
185
|
-
return;
|
|
186
|
-
}
|
|
187
|
-
|
|
188
|
-
let workerThread: WorkerThread | null;
|
|
189
|
-
try {
|
|
190
|
-
workerThread = this._getAvailableWorker();
|
|
191
|
-
} catch (error) {
|
|
192
|
-
const queuedJob = this.jobQueue.shift();
|
|
193
|
-
queuedJob?.removeAbortListener();
|
|
194
|
-
queuedJob?.onReject(error);
|
|
195
|
-
void this._startQueuedJob();
|
|
196
|
-
return;
|
|
197
|
-
}
|
|
198
|
-
if (!workerThread) {
|
|
199
|
-
return;
|
|
200
|
-
}
|
|
201
|
-
|
|
202
|
-
// We have a worker, dequeue and start the job
|
|
203
|
-
const queuedJob = this.jobQueue.shift();
|
|
204
|
-
if (queuedJob) {
|
|
205
|
-
// Emit a debug event
|
|
206
|
-
// @ts-ignore
|
|
207
|
-
this.onDebug({
|
|
208
|
-
message: 'Starting job',
|
|
209
|
-
name: queuedJob.name,
|
|
210
|
-
workerThread,
|
|
211
|
-
backlog: this.jobQueue.length
|
|
212
|
-
});
|
|
213
|
-
|
|
214
|
-
// Create a worker job to let the app access thread and manage job completion
|
|
215
|
-
const job = new WorkerJob(queuedJob.name, workerThread);
|
|
216
|
-
queuedJob.removeAbortListener();
|
|
217
|
-
const abortJob = (): void => job.abort(queuedJob.signal?.reason);
|
|
218
|
-
queuedJob.signal?.addEventListener('abort', abortJob, {once: true});
|
|
219
|
-
if (queuedJob.signal?.aborted) abortJob();
|
|
220
|
-
this.activeJobs.add(job);
|
|
221
|
-
workerThread.ref();
|
|
222
|
-
|
|
223
|
-
// Set the worker thread's message handlers
|
|
224
|
-
workerThread.onMessage = data => {
|
|
225
|
-
if (job.isRunning) queuedJob.onMessage(job, data.type, data.payload);
|
|
226
|
-
};
|
|
227
|
-
workerThread.onError = error => queuedJob.onError(job, error);
|
|
228
|
-
|
|
229
|
-
// Resolve the start promise so that the app can start sending messages to worker
|
|
230
|
-
queuedJob.onStart(job);
|
|
231
|
-
|
|
232
|
-
// Wait for the app to signal that the job is complete, then return worker to queue
|
|
233
|
-
try {
|
|
234
|
-
await job.result;
|
|
235
|
-
} catch {
|
|
236
|
-
// The job result promise carries worker errors back to the caller; do not duplicate-log
|
|
237
|
-
// handled rejections here.
|
|
238
|
-
} finally {
|
|
239
|
-
queuedJob.signal?.removeEventListener('abort', abortJob);
|
|
240
|
-
this.activeJobs.delete(job);
|
|
241
|
-
this.returnWorkerToQueue(workerThread);
|
|
242
|
-
}
|
|
243
|
-
} else {
|
|
244
|
-
this.returnWorkerToQueue(workerThread);
|
|
245
|
-
}
|
|
246
|
-
}
|
|
247
|
-
|
|
248
|
-
/**
|
|
249
|
-
* Returns a worker to the idle queue
|
|
250
|
-
* Destroys the worker if
|
|
251
|
-
* - pool is destroyed
|
|
252
|
-
* - if this pool doesn't reuse workers
|
|
253
|
-
* - if maxConcurrency has been lowered
|
|
254
|
-
* @param worker
|
|
255
|
-
*/
|
|
256
|
-
returnWorkerToQueue(worker: WorkerThread) {
|
|
257
|
-
const shouldDestroyWorker =
|
|
258
|
-
// Aborted jobs terminate their worker immediately.
|
|
259
|
-
worker.terminated ||
|
|
260
|
-
// If the pool is destroyed, there is no reason to keep the worker around
|
|
261
|
-
this.isDestroyed ||
|
|
262
|
-
// If the app has disabled worker reuse, any completed workers should be destroyed
|
|
263
|
-
!this.reuseWorkers ||
|
|
264
|
-
// If concurrency has been lowered, this worker might be surplus to requirements
|
|
265
|
-
this.count > this._getMaxConcurrency();
|
|
266
|
-
|
|
267
|
-
if (shouldDestroyWorker) {
|
|
268
|
-
if (!worker.terminated) {
|
|
269
|
-
worker.destroy();
|
|
270
|
-
}
|
|
271
|
-
this.count--;
|
|
272
|
-
} else {
|
|
273
|
-
worker.unref();
|
|
274
|
-
this.idleQueue.push(worker);
|
|
275
|
-
}
|
|
276
|
-
|
|
277
|
-
if (!this.isDestroyed) {
|
|
278
|
-
this._startQueuedJob(); // eslint-disable-line @typescript-eslint/no-floating-promises
|
|
279
|
-
}
|
|
280
|
-
}
|
|
281
|
-
|
|
282
|
-
/**
|
|
283
|
-
* Returns idle worker or creates new worker if maxConcurrency has not been reached
|
|
284
|
-
*/
|
|
285
|
-
_getAvailableWorker(): WorkerThread | null {
|
|
286
|
-
// If a worker has completed and returned to the queue, it can be used
|
|
287
|
-
if (this.idleQueue.length > 0) {
|
|
288
|
-
return this.idleQueue.shift() || null;
|
|
289
|
-
}
|
|
290
|
-
|
|
291
|
-
// Create fresh worker if we haven't yet created the max amount of worker threads for this worker source
|
|
292
|
-
if (this.count < this._getMaxConcurrency()) {
|
|
293
|
-
this.count++;
|
|
294
|
-
const name = `${this.name.toLowerCase()} (#${this.count} of ${this.maxConcurrency})`;
|
|
295
|
-
try {
|
|
296
|
-
return new WorkerThread({
|
|
297
|
-
name,
|
|
298
|
-
source: this.source,
|
|
299
|
-
url: this.url,
|
|
300
|
-
getUrl: this.getUrl,
|
|
301
|
-
loadWorker: this.loadWorker
|
|
302
|
-
});
|
|
303
|
-
} catch (error) {
|
|
304
|
-
this.count--;
|
|
305
|
-
throw error;
|
|
306
|
-
}
|
|
307
|
-
}
|
|
308
|
-
|
|
309
|
-
// No worker available, have to wait
|
|
310
|
-
return null;
|
|
311
|
-
}
|
|
312
|
-
|
|
313
|
-
_getMaxConcurrency() {
|
|
314
|
-
return isMobile ? this.maxMobileConcurrency : this.maxConcurrency;
|
|
315
|
-
}
|
|
316
|
-
}
|
|
@@ -1,206 +0,0 @@
|
|
|
1
|
-
// loaders.gl
|
|
2
|
-
// SPDX-License-Identifier: MIT
|
|
3
|
-
// Copyright (c) vis.gl contributors
|
|
4
|
-
|
|
5
|
-
import type {LoadWorker} from '../../types';
|
|
6
|
-
import {NodeWorker, NodeWorkerType} from '../node/worker_threads';
|
|
7
|
-
import {isBrowser} from '../env-utils/globals';
|
|
8
|
-
import {assert} from '../env-utils/assert';
|
|
9
|
-
import {getLoadableWorkerURL} from '../worker-utils/get-loadable-worker-url';
|
|
10
|
-
import {getTransferList} from '../worker-utils/get-transfer-list';
|
|
11
|
-
|
|
12
|
-
const NOOP = () => {};
|
|
13
|
-
|
|
14
|
-
export type WorkerThreadProps = {
|
|
15
|
-
/** Human-readable worker name. */
|
|
16
|
-
name: string;
|
|
17
|
-
/** Inline worker source. */
|
|
18
|
-
source?: string;
|
|
19
|
-
/** Worker script URL. */
|
|
20
|
-
url?: string;
|
|
21
|
-
/** Lazily resolves the classic worker URL used as a fallback. */
|
|
22
|
-
getUrl?: () => string;
|
|
23
|
-
/** Creates a browser Worker directly. */
|
|
24
|
-
loadWorker?: LoadWorker;
|
|
25
|
-
};
|
|
26
|
-
|
|
27
|
-
/**
|
|
28
|
-
* Represents one worker thread
|
|
29
|
-
*/
|
|
30
|
-
export default class WorkerThread {
|
|
31
|
-
readonly name: string;
|
|
32
|
-
readonly source: string | undefined;
|
|
33
|
-
readonly url: string | undefined;
|
|
34
|
-
/** Lazily resolves the classic worker URL used as a fallback. */
|
|
35
|
-
readonly getUrl: (() => string) | undefined;
|
|
36
|
-
/** Creates a browser Worker directly. */
|
|
37
|
-
readonly loadWorker: LoadWorker | undefined;
|
|
38
|
-
terminated: boolean = false;
|
|
39
|
-
worker: Worker | NodeWorkerType;
|
|
40
|
-
onMessage: (message: any) => void;
|
|
41
|
-
onError: (error: Error) => void;
|
|
42
|
-
|
|
43
|
-
private _loadableURL: string = '';
|
|
44
|
-
|
|
45
|
-
/** Checks if workers are supported on this platform */
|
|
46
|
-
static isSupported(): boolean {
|
|
47
|
-
return (
|
|
48
|
-
(typeof Worker !== 'undefined' && isBrowser) ||
|
|
49
|
-
(typeof NodeWorker !== 'undefined' && !isBrowser)
|
|
50
|
-
);
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
constructor(props: WorkerThreadProps) {
|
|
54
|
-
const {name, source, url, getUrl, loadWorker} = props;
|
|
55
|
-
assert(source || url || getUrl || loadWorker);
|
|
56
|
-
this.name = name;
|
|
57
|
-
this.source = source;
|
|
58
|
-
this.url = url;
|
|
59
|
-
this.getUrl = getUrl;
|
|
60
|
-
this.loadWorker = loadWorker;
|
|
61
|
-
this.onMessage = NOOP;
|
|
62
|
-
this.onError = error => console.log(error); // eslint-disable-line
|
|
63
|
-
|
|
64
|
-
this.worker = isBrowser ? this._createBrowserWorker() : this._createNodeWorker();
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
/**
|
|
68
|
-
* Terminate this worker thread
|
|
69
|
-
* @note Can free up significant memory
|
|
70
|
-
*/
|
|
71
|
-
destroy(): void {
|
|
72
|
-
this.onMessage = NOOP;
|
|
73
|
-
this.onError = NOOP;
|
|
74
|
-
this.worker.terminate(); // eslint-disable-line @typescript-eslint/no-floating-promises
|
|
75
|
-
this.terminated = true;
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
/** Keeps this worker from preventing Node.js process exit while idle. */
|
|
79
|
-
unref(): void {
|
|
80
|
-
if (!isBrowser && typeof (this.worker as NodeWorkerType).unref === 'function') {
|
|
81
|
-
(this.worker as NodeWorkerType).unref();
|
|
82
|
-
}
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
/** Keeps this worker alive while it is actively processing a job. */
|
|
86
|
-
ref(): void {
|
|
87
|
-
if (!isBrowser && typeof (this.worker as NodeWorkerType).ref === 'function') {
|
|
88
|
-
(this.worker as NodeWorkerType).ref();
|
|
89
|
-
}
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
get isRunning() {
|
|
93
|
-
return Boolean(this.onMessage);
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
/**
|
|
97
|
-
* Send a message to this worker thread
|
|
98
|
-
* @param data any data structure, ideally consisting mostly of transferrable objects
|
|
99
|
-
* @param transferList If not supplied, calculated automatically by traversing data
|
|
100
|
-
*/
|
|
101
|
-
postMessage(data: any, transferList?: any[]): void {
|
|
102
|
-
transferList = transferList || getTransferList(data);
|
|
103
|
-
// @ts-ignore
|
|
104
|
-
this.worker.postMessage(data, transferList);
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
// PRIVATE
|
|
108
|
-
|
|
109
|
-
/**
|
|
110
|
-
* Generate a standard Error from an ErrorEvent
|
|
111
|
-
* @param event
|
|
112
|
-
*/
|
|
113
|
-
_getErrorFromErrorEvent(event: ErrorEvent): Error {
|
|
114
|
-
// Note Error object does not have the expected fields if loading failed completely
|
|
115
|
-
// https://developer.mozilla.org/en-US/docs/Web/API/Worker#Event_handlers
|
|
116
|
-
// https://developer.mozilla.org/en-US/docs/Web/API/ErrorEvent
|
|
117
|
-
let message = 'Failed to load ';
|
|
118
|
-
message += `worker ${this.name} from ${this.url || 'built-in worker factory'}. `;
|
|
119
|
-
if (event.message) {
|
|
120
|
-
message += `${event.message} in `;
|
|
121
|
-
}
|
|
122
|
-
// const hasFilename = event.filename && !event.filename.startsWith('blob:');
|
|
123
|
-
// message += hasFilename ? event.filename : this.source.slice(0, 100);
|
|
124
|
-
if (event.lineno) {
|
|
125
|
-
message += `:${event.lineno}:${event.colno}`;
|
|
126
|
-
}
|
|
127
|
-
return new Error(message);
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
/**
|
|
131
|
-
* Creates a worker thread on the browser
|
|
132
|
-
*/
|
|
133
|
-
_createBrowserWorker(): Worker {
|
|
134
|
-
if (this.loadWorker) {
|
|
135
|
-
try {
|
|
136
|
-
const worker = this.loadWorker();
|
|
137
|
-
if (worker) {
|
|
138
|
-
return this._initializeBrowserWorker(worker);
|
|
139
|
-
}
|
|
140
|
-
} catch (error) {
|
|
141
|
-
if (!this.source && !this.url && !this.getUrl) {
|
|
142
|
-
throw error;
|
|
143
|
-
}
|
|
144
|
-
}
|
|
145
|
-
}
|
|
146
|
-
|
|
147
|
-
const url = this.url || this.getUrl?.();
|
|
148
|
-
this._loadableURL = getLoadableWorkerURL({source: this.source, url});
|
|
149
|
-
const worker = new Worker(this._loadableURL, {name: this.name});
|
|
150
|
-
|
|
151
|
-
return this._initializeBrowserWorker(worker);
|
|
152
|
-
}
|
|
153
|
-
|
|
154
|
-
/** Attaches protocol handlers to a browser worker. */
|
|
155
|
-
_initializeBrowserWorker(worker: Worker): Worker {
|
|
156
|
-
worker.onmessage = event => {
|
|
157
|
-
if (!event.data) {
|
|
158
|
-
this.onError(new Error('No data received'));
|
|
159
|
-
} else {
|
|
160
|
-
this.onMessage(event.data);
|
|
161
|
-
}
|
|
162
|
-
};
|
|
163
|
-
// This callback represents an uncaught exception in the worker thread
|
|
164
|
-
worker.onerror = (error: ErrorEvent): void => {
|
|
165
|
-
this.onError(this._getErrorFromErrorEvent(error));
|
|
166
|
-
this.terminated = true;
|
|
167
|
-
};
|
|
168
|
-
// TODO - not clear when this would be called, for now just log in case it happens
|
|
169
|
-
worker.onmessageerror = event => console.error(event); // eslint-disable-line
|
|
170
|
-
|
|
171
|
-
return worker;
|
|
172
|
-
}
|
|
173
|
-
|
|
174
|
-
/**
|
|
175
|
-
* Creates a worker thread in node.js
|
|
176
|
-
* @todo https://nodejs.org/api/async_hooks.html#async-resource-worker-pool
|
|
177
|
-
*/
|
|
178
|
-
_createNodeWorker(): NodeWorkerType {
|
|
179
|
-
let worker: NodeWorkerType;
|
|
180
|
-
if (this.url) {
|
|
181
|
-
// Make sure relative URLs start with './'
|
|
182
|
-
const absolute = this.url.includes(':/') || this.url.startsWith('/');
|
|
183
|
-
const url = absolute ? this.url : `./${this.url}`;
|
|
184
|
-
const workerUrl = url.startsWith('file:') ? new URL(url) : url;
|
|
185
|
-
const type = this.url.endsWith('.ts') || this.url.endsWith('.mjs') ? 'module' : 'commonjs';
|
|
186
|
-
// console.log('Starting work from', url);
|
|
187
|
-
// @ts-expect-error type is not known
|
|
188
|
-
worker = new NodeWorker(workerUrl, {eval: false, type});
|
|
189
|
-
} else if (this.source) {
|
|
190
|
-
worker = new NodeWorker(this.source, {eval: true});
|
|
191
|
-
} else {
|
|
192
|
-
throw new Error('no worker');
|
|
193
|
-
}
|
|
194
|
-
worker.on('message', data => {
|
|
195
|
-
// console.error('message', data);
|
|
196
|
-
this.onMessage(data);
|
|
197
|
-
});
|
|
198
|
-
worker.on('error', error => {
|
|
199
|
-
this.onError(error as Error);
|
|
200
|
-
});
|
|
201
|
-
worker.on('exit', _code => {
|
|
202
|
-
// console.error('exit', code);
|
|
203
|
-
});
|
|
204
|
-
return worker;
|
|
205
|
-
}
|
|
206
|
-
}
|