@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 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"worker-thread.d.ts","sourceRoot":"","sources":["../../../../src/worker-utils/lib/worker-farm/worker-thread.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAAC,UAAU,EAAC,uBAAoB;AAC5C,OAAO,EAAa,cAAc,EAAC,kCAA+B;AAQlE,MAAM,MAAM,iBAAiB,GAAG;IAC9B,kCAAkC;IAClC,IAAI,EAAE,MAAM,CAAC;IACb,4BAA4B;IAC5B,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,yBAAyB;IACzB,GAAG,CAAC,EAAE,MAAM,CAAC;IACb,iEAAiE;IACjE,MAAM,CAAC,EAAE,MAAM,MAAM,CAAC;IACtB,yCAAyC;IACzC,UAAU,CAAC,EAAE,UAAU,CAAC;CACzB,CAAC;AAEF;;GAEG;AACH,MAAM,CAAC,OAAO,OAAO,YAAY;IAC/B,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IACtB,QAAQ,CAAC,MAAM,EAAE,MAAM,GAAG,SAAS,CAAC;IACpC,QAAQ,CAAC,GAAG,EAAE,MAAM,GAAG,SAAS,CAAC;IACjC,iEAAiE;IACjE,QAAQ,CAAC,MAAM,EAAE,CAAC,MAAM,MAAM,CAAC,GAAG,SAAS,CAAC;IAC5C,yCAAyC;IACzC,QAAQ,CAAC,UAAU,EAAE,UAAU,GAAG,SAAS,CAAC;IAC5C,UAAU,EAAE,OAAO,CAAS;IAC5B,MAAM,EAAE,MAAM,GAAG,cAAc,CAAC;IAChC,SAAS,EAAE,CAAC,OAAO,EAAE,GAAG,KAAK,IAAI,CAAC;IAClC,OAAO,EAAE,CAAC,KAAK,EAAE,KAAK,KAAK,IAAI,CAAC;IAEhC,OAAO,CAAC,YAAY,CAAc;IAElC,uDAAuD;IACvD,MAAM,CAAC,WAAW,IAAI,OAAO;gBAOjB,KAAK,EAAE,iBAAiB;IAcpC;;;OAGG;IACH,OAAO,IAAI,IAAI;IAOf,yEAAyE;IACzE,KAAK,IAAI,IAAI;IAMb,qEAAqE;IACrE,GAAG,IAAI,IAAI;IAMX,IAAI,SAAS,YAEZ;IAED;;;;OAIG;IACH,WAAW,CAAC,IAAI,EAAE,GAAG,EAAE,YAAY,CAAC,EAAE,GAAG,EAAE,GAAG,IAAI;IAQlD;;;OAGG;IACH,uBAAuB,CAAC,KAAK,EAAE,UAAU,GAAG,KAAK;IAiBjD;;OAEG;IACH,oBAAoB,IAAI,MAAM;IAqB9B,sDAAsD;IACtD,wBAAwB,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM;IAmBhD;;;OAGG;IACH,iBAAiB,IAAI,cAAc;CA4BpC"}
|
|
@@ -1,175 +0,0 @@
|
|
|
1
|
-
// loaders.gl
|
|
2
|
-
// SPDX-License-Identifier: MIT
|
|
3
|
-
// Copyright (c) vis.gl contributors
|
|
4
|
-
import { NodeWorker } from "../node/worker_threads.js";
|
|
5
|
-
import { isBrowser } from "../env-utils/globals.js";
|
|
6
|
-
import { assert } from "../env-utils/assert.js";
|
|
7
|
-
import { getLoadableWorkerURL } from "../worker-utils/get-loadable-worker-url.js";
|
|
8
|
-
import { getTransferList } from "../worker-utils/get-transfer-list.js";
|
|
9
|
-
const NOOP = () => { };
|
|
10
|
-
/**
|
|
11
|
-
* Represents one worker thread
|
|
12
|
-
*/
|
|
13
|
-
export default class WorkerThread {
|
|
14
|
-
name;
|
|
15
|
-
source;
|
|
16
|
-
url;
|
|
17
|
-
/** Lazily resolves the classic worker URL used as a fallback. */
|
|
18
|
-
getUrl;
|
|
19
|
-
/** Creates a browser Worker directly. */
|
|
20
|
-
loadWorker;
|
|
21
|
-
terminated = false;
|
|
22
|
-
worker;
|
|
23
|
-
onMessage;
|
|
24
|
-
onError;
|
|
25
|
-
_loadableURL = '';
|
|
26
|
-
/** Checks if workers are supported on this platform */
|
|
27
|
-
static isSupported() {
|
|
28
|
-
return ((typeof Worker !== 'undefined' && isBrowser) ||
|
|
29
|
-
(typeof NodeWorker !== 'undefined' && !isBrowser));
|
|
30
|
-
}
|
|
31
|
-
constructor(props) {
|
|
32
|
-
const { name, source, url, getUrl, loadWorker } = props;
|
|
33
|
-
assert(source || url || getUrl || loadWorker);
|
|
34
|
-
this.name = name;
|
|
35
|
-
this.source = source;
|
|
36
|
-
this.url = url;
|
|
37
|
-
this.getUrl = getUrl;
|
|
38
|
-
this.loadWorker = loadWorker;
|
|
39
|
-
this.onMessage = NOOP;
|
|
40
|
-
this.onError = error => console.log(error); // eslint-disable-line
|
|
41
|
-
this.worker = isBrowser ? this._createBrowserWorker() : this._createNodeWorker();
|
|
42
|
-
}
|
|
43
|
-
/**
|
|
44
|
-
* Terminate this worker thread
|
|
45
|
-
* @note Can free up significant memory
|
|
46
|
-
*/
|
|
47
|
-
destroy() {
|
|
48
|
-
this.onMessage = NOOP;
|
|
49
|
-
this.onError = NOOP;
|
|
50
|
-
this.worker.terminate(); // eslint-disable-line @typescript-eslint/no-floating-promises
|
|
51
|
-
this.terminated = true;
|
|
52
|
-
}
|
|
53
|
-
/** Keeps this worker from preventing Node.js process exit while idle. */
|
|
54
|
-
unref() {
|
|
55
|
-
if (!isBrowser && typeof this.worker.unref === 'function') {
|
|
56
|
-
this.worker.unref();
|
|
57
|
-
}
|
|
58
|
-
}
|
|
59
|
-
/** Keeps this worker alive while it is actively processing a job. */
|
|
60
|
-
ref() {
|
|
61
|
-
if (!isBrowser && typeof this.worker.ref === 'function') {
|
|
62
|
-
this.worker.ref();
|
|
63
|
-
}
|
|
64
|
-
}
|
|
65
|
-
get isRunning() {
|
|
66
|
-
return Boolean(this.onMessage);
|
|
67
|
-
}
|
|
68
|
-
/**
|
|
69
|
-
* Send a message to this worker thread
|
|
70
|
-
* @param data any data structure, ideally consisting mostly of transferrable objects
|
|
71
|
-
* @param transferList If not supplied, calculated automatically by traversing data
|
|
72
|
-
*/
|
|
73
|
-
postMessage(data, transferList) {
|
|
74
|
-
transferList = transferList || getTransferList(data);
|
|
75
|
-
// @ts-ignore
|
|
76
|
-
this.worker.postMessage(data, transferList);
|
|
77
|
-
}
|
|
78
|
-
// PRIVATE
|
|
79
|
-
/**
|
|
80
|
-
* Generate a standard Error from an ErrorEvent
|
|
81
|
-
* @param event
|
|
82
|
-
*/
|
|
83
|
-
_getErrorFromErrorEvent(event) {
|
|
84
|
-
// Note Error object does not have the expected fields if loading failed completely
|
|
85
|
-
// https://developer.mozilla.org/en-US/docs/Web/API/Worker#Event_handlers
|
|
86
|
-
// https://developer.mozilla.org/en-US/docs/Web/API/ErrorEvent
|
|
87
|
-
let message = 'Failed to load ';
|
|
88
|
-
message += `worker ${this.name} from ${this.url || 'built-in worker factory'}. `;
|
|
89
|
-
if (event.message) {
|
|
90
|
-
message += `${event.message} in `;
|
|
91
|
-
}
|
|
92
|
-
// const hasFilename = event.filename && !event.filename.startsWith('blob:');
|
|
93
|
-
// message += hasFilename ? event.filename : this.source.slice(0, 100);
|
|
94
|
-
if (event.lineno) {
|
|
95
|
-
message += `:${event.lineno}:${event.colno}`;
|
|
96
|
-
}
|
|
97
|
-
return new Error(message);
|
|
98
|
-
}
|
|
99
|
-
/**
|
|
100
|
-
* Creates a worker thread on the browser
|
|
101
|
-
*/
|
|
102
|
-
_createBrowserWorker() {
|
|
103
|
-
if (this.loadWorker) {
|
|
104
|
-
try {
|
|
105
|
-
const worker = this.loadWorker();
|
|
106
|
-
if (worker) {
|
|
107
|
-
return this._initializeBrowserWorker(worker);
|
|
108
|
-
}
|
|
109
|
-
}
|
|
110
|
-
catch (error) {
|
|
111
|
-
if (!this.source && !this.url && !this.getUrl) {
|
|
112
|
-
throw error;
|
|
113
|
-
}
|
|
114
|
-
}
|
|
115
|
-
}
|
|
116
|
-
const url = this.url || this.getUrl?.();
|
|
117
|
-
this._loadableURL = getLoadableWorkerURL({ source: this.source, url });
|
|
118
|
-
const worker = new Worker(this._loadableURL, { name: this.name });
|
|
119
|
-
return this._initializeBrowserWorker(worker);
|
|
120
|
-
}
|
|
121
|
-
/** Attaches protocol handlers to a browser worker. */
|
|
122
|
-
_initializeBrowserWorker(worker) {
|
|
123
|
-
worker.onmessage = event => {
|
|
124
|
-
if (!event.data) {
|
|
125
|
-
this.onError(new Error('No data received'));
|
|
126
|
-
}
|
|
127
|
-
else {
|
|
128
|
-
this.onMessage(event.data);
|
|
129
|
-
}
|
|
130
|
-
};
|
|
131
|
-
// This callback represents an uncaught exception in the worker thread
|
|
132
|
-
worker.onerror = (error) => {
|
|
133
|
-
this.onError(this._getErrorFromErrorEvent(error));
|
|
134
|
-
this.terminated = true;
|
|
135
|
-
};
|
|
136
|
-
// TODO - not clear when this would be called, for now just log in case it happens
|
|
137
|
-
worker.onmessageerror = event => console.error(event); // eslint-disable-line
|
|
138
|
-
return worker;
|
|
139
|
-
}
|
|
140
|
-
/**
|
|
141
|
-
* Creates a worker thread in node.js
|
|
142
|
-
* @todo https://nodejs.org/api/async_hooks.html#async-resource-worker-pool
|
|
143
|
-
*/
|
|
144
|
-
_createNodeWorker() {
|
|
145
|
-
let worker;
|
|
146
|
-
if (this.url) {
|
|
147
|
-
// Make sure relative URLs start with './'
|
|
148
|
-
const absolute = this.url.includes(':/') || this.url.startsWith('/');
|
|
149
|
-
const url = absolute ? this.url : `./${this.url}`;
|
|
150
|
-
const workerUrl = url.startsWith('file:') ? new URL(url) : url;
|
|
151
|
-
const type = this.url.endsWith('.ts') || this.url.endsWith('.mjs') ? 'module' : 'commonjs';
|
|
152
|
-
// console.log('Starting work from', url);
|
|
153
|
-
// @ts-expect-error type is not known
|
|
154
|
-
worker = new NodeWorker(workerUrl, { eval: false, type });
|
|
155
|
-
}
|
|
156
|
-
else if (this.source) {
|
|
157
|
-
worker = new NodeWorker(this.source, { eval: true });
|
|
158
|
-
}
|
|
159
|
-
else {
|
|
160
|
-
throw new Error('no worker');
|
|
161
|
-
}
|
|
162
|
-
worker.on('message', data => {
|
|
163
|
-
// console.error('message', data);
|
|
164
|
-
this.onMessage(data);
|
|
165
|
-
});
|
|
166
|
-
worker.on('error', error => {
|
|
167
|
-
this.onError(error);
|
|
168
|
-
});
|
|
169
|
-
worker.on('exit', _code => {
|
|
170
|
-
// console.error('exit', code);
|
|
171
|
-
});
|
|
172
|
-
return worker;
|
|
173
|
-
}
|
|
174
|
-
}
|
|
175
|
-
//# sourceMappingURL=worker-thread.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"worker-thread.js","sourceRoot":"","sources":["../../../../src/worker-utils/lib/worker-farm/worker-thread.ts"],"names":[],"mappings":"AAAA,aAAa;AACb,+BAA+B;AAC/B,oCAAoC;AAGpC,OAAO,EAAC,UAAU,EAAiB,kCAA+B;AAClE,OAAO,EAAC,SAAS,EAAC,gCAA6B;AAC/C,OAAO,EAAC,MAAM,EAAC,+BAA4B;AAC3C,OAAO,EAAC,oBAAoB,EAAC,mDAAgD;AAC7E,OAAO,EAAC,eAAe,EAAC,6CAA0C;AAElE,MAAM,IAAI,GAAG,GAAG,EAAE,GAAE,CAAC,CAAC;AAetB;;GAEG;AACH,MAAM,CAAC,OAAO,OAAO,YAAY;IACtB,IAAI,CAAS;IACb,MAAM,CAAqB;IAC3B,GAAG,CAAqB;IACjC,iEAAiE;IACxD,MAAM,CAA6B;IAC5C,yCAAyC;IAChC,UAAU,CAAyB;IAC5C,UAAU,GAAY,KAAK,CAAC;IAC5B,MAAM,CAA0B;IAChC,SAAS,CAAyB;IAClC,OAAO,CAAyB;IAExB,YAAY,GAAW,EAAE,CAAC;IAElC,uDAAuD;IACvD,MAAM,C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AE,CAAC,SAAS,EAAE,IAAI,CAAC,EAAE;YAC1B,kCAAkC;YAClC,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC,CAAC;QACvB,CAAC,CAAC,CAAC;QACH,MAAM,CAAC,EAAE,CAAC,OAAO,EAAE,KAAK,CAAC,EAAE;YACzB,IAAI,CAAC,OAAO,CAAC,KAAc,CAAC,CAAC;QAC/B,CAAC,CAAC,CAAC;QACH,MAAM,CAAC,EAAE,CAAC,MAAM,EAAE,KAAK,CAAC,EAAE;YACxB,+BAA+B;QACjC,CAAC,CAAC,CAAC;QACH,OAAO,MAAM,CAAC;IAChB,CAAC;CACF"}
|
|
@@ -1,41 +0,0 @@
|
|
|
1
|
-
// Use stackgl modules for DOM-less reading and writing of images
|
|
2
|
-
|
|
3
|
-
import savePixels from 'save-pixels';
|
|
4
|
-
import ndarray from 'ndarray';
|
|
5
|
-
import {bufferToArrayBuffer} from '../buffer/to-array-buffer.node';
|
|
6
|
-
|
|
7
|
-
/**
|
|
8
|
-
* Returns data bytes representing a compressed image in PNG or JPG format,
|
|
9
|
-
* This data can be saved using file system (f) methods or
|
|
10
|
-
* used in a request.
|
|
11
|
-
* @param image to save
|
|
12
|
-
* @param options
|
|
13
|
-
* @param options.type='png' - png, jpg or image/png, image/jpg are valid
|
|
14
|
-
* @param options.dataURI - Whether to include a data URI header
|
|
15
|
-
* @return {*} bytes
|
|
16
|
-
*/
|
|
17
|
-
export function encodeImageToStreamNode(
|
|
18
|
-
image: {data: any; width: number; height: number},
|
|
19
|
-
options: {type?: string; dataURI?: string}
|
|
20
|
-
) {
|
|
21
|
-
// Support MIME type strings
|
|
22
|
-
const type = options.type ? options.type.replace('image/', '') : 'jpeg';
|
|
23
|
-
const pixels = ndarray(image.data, [image.width, image.height, 4], [4, image.width * 4, 1], 0);
|
|
24
|
-
|
|
25
|
-
// Note: savePixels returns a stream
|
|
26
|
-
return savePixels(pixels, type, options);
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
export function encodeImageNode(image, options) {
|
|
30
|
-
const imageStream = encodeImageToStreamNode(image, options);
|
|
31
|
-
|
|
32
|
-
return new Promise((resolve) => {
|
|
33
|
-
const buffers: any[] = [];
|
|
34
|
-
imageStream.on('data', (buffer) => buffers.push(buffer));
|
|
35
|
-
// TODO - convert to arraybuffer?
|
|
36
|
-
imageStream.on('end', () => {
|
|
37
|
-
const buffer = Buffer.concat(buffers);
|
|
38
|
-
resolve(bufferToArrayBuffer(buffer));
|
|
39
|
-
});
|
|
40
|
-
});
|
|
41
|
-
}
|
|
@@ -1,55 +0,0 @@
|
|
|
1
|
-
// loaders.gl
|
|
2
|
-
// SPDX-License-Identifier: MIT
|
|
3
|
-
// Copyright (c) vis.gl contributors
|
|
4
|
-
|
|
5
|
-
import getPixels from 'get-pixels';
|
|
6
|
-
|
|
7
|
-
/** Declares which image format mime types this loader polyfill supports */
|
|
8
|
-
export const NODE_FORMAT_SUPPORT = ['image/png', 'image/jpeg', 'image/gif'];
|
|
9
|
-
|
|
10
|
-
// Note: These types are also defined in @loaders.gl/images and need to be kept in sync
|
|
11
|
-
type NDArray = {
|
|
12
|
-
shape: number[];
|
|
13
|
-
data: Uint8Array;
|
|
14
|
-
width: number;
|
|
15
|
-
height: number;
|
|
16
|
-
components: number;
|
|
17
|
-
layers: number[];
|
|
18
|
-
};
|
|
19
|
-
|
|
20
|
-
export async function parseImageNode(arrayBuffer: ArrayBuffer, mimeType: string): Promise<NDArray> {
|
|
21
|
-
if (!mimeType) {
|
|
22
|
-
throw new Error('MIMEType is required to parse image under Node.js');
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
const buffer = arrayBuffer instanceof Buffer ? arrayBuffer : Buffer.from(arrayBuffer);
|
|
26
|
-
const ndarray = await getPixelsAsync(buffer, mimeType);
|
|
27
|
-
return ndarray;
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
// TODO - check if getPixels callback is asynchronous if provided with buffer input
|
|
31
|
-
// if not, parseImage can be a sync function
|
|
32
|
-
function getPixelsAsync(buffer: Buffer, mimeType: string): Promise<NDArray> {
|
|
33
|
-
return new Promise<NDArray>((resolve) =>
|
|
34
|
-
getPixels(buffer as any, mimeType, (err, ndarray) => {
|
|
35
|
-
if (err) {
|
|
36
|
-
throw err;
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
const shape = [...ndarray.shape];
|
|
40
|
-
const layers = ndarray.shape.length === 4 ? ndarray.shape.shift() : 1;
|
|
41
|
-
const data = ndarray.data instanceof Buffer ? new Uint8Array(ndarray.data) : ndarray.data;
|
|
42
|
-
|
|
43
|
-
// extract width/height etc
|
|
44
|
-
resolve({
|
|
45
|
-
shape,
|
|
46
|
-
data,
|
|
47
|
-
width: ndarray.shape[0],
|
|
48
|
-
height: ndarray.shape[1],
|
|
49
|
-
components: ndarray.shape[2],
|
|
50
|
-
// TODO - error
|
|
51
|
-
layers: layers ? [layers] : []
|
|
52
|
-
});
|
|
53
|
-
})
|
|
54
|
-
);
|
|
55
|
-
}
|
|
@@ -1,100 +0,0 @@
|
|
|
1
|
-
// loaders.gl
|
|
2
|
-
// SPDX-License-Identifier: MIT
|
|
3
|
-
// Copyright (c) vis.gl contributors
|
|
4
|
-
|
|
5
|
-
// From https://github.com/rauschma/async-iter-demo/tree/master/src under MIT license
|
|
6
|
-
// http://2ality.com/2016/10/asynchronous-iteration.html
|
|
7
|
-
|
|
8
|
-
/**
|
|
9
|
-
* Async Queue
|
|
10
|
-
* - AsyncIterable: An async iterator can be
|
|
11
|
-
* - Values can be pushed onto the queue
|
|
12
|
-
* @example
|
|
13
|
-
* const asyncQueue = new AsyncQueue();
|
|
14
|
-
* setTimeout(() => asyncQueue.enqueue('tick'), 1000);
|
|
15
|
-
* setTimeout(() => asyncQueue.enqueue(new Error('done')), 10000);
|
|
16
|
-
* for await (const value of asyncQueue) {
|
|
17
|
-
* console.log(value); // tick
|
|
18
|
-
* }
|
|
19
|
-
*/
|
|
20
|
-
export default class AsyncQueue<T> {
|
|
21
|
-
private _values: any[];
|
|
22
|
-
private _settlers: any[];
|
|
23
|
-
private _closed: boolean;
|
|
24
|
-
|
|
25
|
-
constructor() {
|
|
26
|
-
this._values = []; // enqueues > dequeues
|
|
27
|
-
this._settlers = []; // dequeues > enqueues
|
|
28
|
-
this._closed = false;
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
/** Return an async iterator for this queue */
|
|
32
|
-
[Symbol.asyncIterator](): AsyncIterator<T> {
|
|
33
|
-
return this;
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
/** Push a new value - the async iterator will yield a promise resolved to this value */
|
|
37
|
-
push(value: T): void {
|
|
38
|
-
return this.enqueue(value);
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
/**
|
|
42
|
-
* Push a new value - the async iterator will yield a promise resolved to this value
|
|
43
|
-
* Add an error - the async iterator will yield a promise rejected with this value
|
|
44
|
-
*/
|
|
45
|
-
enqueue(value: T | Error): void {
|
|
46
|
-
if (this._closed) {
|
|
47
|
-
throw new Error('Closed');
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
if (this._settlers.length > 0) {
|
|
51
|
-
if (this._values.length > 0) {
|
|
52
|
-
throw new Error('Illegal internal state');
|
|
53
|
-
}
|
|
54
|
-
const settler = this._settlers.shift();
|
|
55
|
-
if (value instanceof Error) {
|
|
56
|
-
settler.reject(value);
|
|
57
|
-
} else {
|
|
58
|
-
settler.resolve({value});
|
|
59
|
-
}
|
|
60
|
-
} else {
|
|
61
|
-
this._values.push(value);
|
|
62
|
-
}
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
/** Indicate that we not waiting for more values - The async iterator will be done */
|
|
66
|
-
close(): void {
|
|
67
|
-
while (this._settlers.length > 0) {
|
|
68
|
-
const settler = this._settlers.shift();
|
|
69
|
-
settler.resolve({done: true});
|
|
70
|
-
}
|
|
71
|
-
this._closed = true;
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
// ITERATOR IMPLEMENTATION
|
|
75
|
-
|
|
76
|
-
/** @returns a Promise for an IteratorResult */
|
|
77
|
-
next(): Promise<IteratorResult<T, any>> {
|
|
78
|
-
// If values in queue, yield the first value
|
|
79
|
-
if (this._values.length > 0) {
|
|
80
|
-
const value = this._values.shift();
|
|
81
|
-
if (value instanceof Error) {
|
|
82
|
-
return Promise.reject(value);
|
|
83
|
-
}
|
|
84
|
-
return Promise.resolve({done: false, value});
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
// If queue is closed, the iterator is done
|
|
88
|
-
if (this._closed) {
|
|
89
|
-
if (this._settlers.length > 0) {
|
|
90
|
-
throw new Error('Illegal internal state');
|
|
91
|
-
}
|
|
92
|
-
return Promise.resolve({done: true, value: undefined});
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
// Yield a promise that waits for new values to be enqueued
|
|
96
|
-
return new Promise((resolve, reject) => {
|
|
97
|
-
this._settlers.push({resolve, reject});
|
|
98
|
-
});
|
|
99
|
-
}
|
|
100
|
-
}
|
|
@@ -1,145 +0,0 @@
|
|
|
1
|
-
// loaders.gl
|
|
2
|
-
// SPDX-License-Identifier: MIT
|
|
3
|
-
// Copyright (c) vis.gl contributors
|
|
4
|
-
|
|
5
|
-
import type {WorkerMessageData, WorkerMessageType, WorkerMessagePayload} from '../../types';
|
|
6
|
-
import {getTransferList} from '../worker-utils/get-transfer-list';
|
|
7
|
-
// import type {TransferListItem} from '../node/worker_threads';
|
|
8
|
-
import {parentPort} from '../node/worker_threads';
|
|
9
|
-
|
|
10
|
-
type TransferListItem = any;
|
|
11
|
-
type WorkerMessageEvent = MessageEvent<WorkerMessageData> | WorkerMessageData;
|
|
12
|
-
type WorkerMessageListener = (type: WorkerMessageType, payload: WorkerMessagePayload) => any;
|
|
13
|
-
|
|
14
|
-
/** Vile hack to defeat over-zealous bundlers from stripping out the require */
|
|
15
|
-
async function getParentPort() {
|
|
16
|
-
// const isNode = globalThis.process;
|
|
17
|
-
// let parentPort;
|
|
18
|
-
// try {
|
|
19
|
-
// // biome-ignore format: preserve intentional fixture layout
|
|
20
|
-
// eval('globalThis.parentPort = require(\'worker_threads\').parentPort'); // eslint-disable-line no-eval
|
|
21
|
-
// parentPort = globalThis.parentPort;
|
|
22
|
-
// } catch {
|
|
23
|
-
// try {
|
|
24
|
-
// // biome-ignore format: preserve intentional fixture layout
|
|
25
|
-
// eval('globalThis.workerThreadsPromise = import(\'worker_threads\')'); // eslint-disable-line no-eval
|
|
26
|
-
// const workerThreads = await globalThis.workerThreadsPromise;
|
|
27
|
-
// parentPort = workerThreads.parentPort;
|
|
28
|
-
// } catch (error) {
|
|
29
|
-
// console.error((error as Error).message); // eslint-disable-line no-console
|
|
30
|
-
// }
|
|
31
|
-
// }
|
|
32
|
-
return parentPort;
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
const onMessageWrapperMap = new Map<WorkerMessageListener, (message: WorkerMessageEvent) => void>();
|
|
36
|
-
|
|
37
|
-
/**
|
|
38
|
-
* Type safe wrapper for worker code
|
|
39
|
-
*/
|
|
40
|
-
export default class WorkerBody {
|
|
41
|
-
/** Check that we are actually in a worker thread */
|
|
42
|
-
static async inWorkerThread(): Promise<boolean> {
|
|
43
|
-
return typeof self !== 'undefined' || Boolean(await getParentPort());
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
/*
|
|
47
|
-
* (type: WorkerMessageType, payload: WorkerMessagePayload) => any
|
|
48
|
-
*/
|
|
49
|
-
static set onmessage(onMessage: (type: WorkerMessageType, payload: WorkerMessagePayload) => any) {
|
|
50
|
-
async function handleMessage(message) {
|
|
51
|
-
const parentPort = await getParentPort();
|
|
52
|
-
// Confusingly the message itself also has a 'type' field which is always set to 'message'
|
|
53
|
-
const {type, payload} = parentPort ? message : message.data;
|
|
54
|
-
// if (!isKnownMessage(message)) {
|
|
55
|
-
// return;
|
|
56
|
-
// }
|
|
57
|
-
onMessage(type, payload);
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
getParentPort().then(parentPort => {
|
|
61
|
-
if (parentPort) {
|
|
62
|
-
parentPort.on('message', message => {
|
|
63
|
-
handleMessage(message);
|
|
64
|
-
});
|
|
65
|
-
// if (message == 'exit') { parentPort.unref(); }
|
|
66
|
-
// eslint-disable-next-line
|
|
67
|
-
parentPort.on('exit', () => console.debug('Node worker closing'));
|
|
68
|
-
} else {
|
|
69
|
-
// eslint-disable-next-line no-restricted-globals
|
|
70
|
-
globalThis.onmessage = handleMessage;
|
|
71
|
-
}
|
|
72
|
-
});
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
static async addEventListener(onMessage: WorkerMessageListener) {
|
|
76
|
-
let onMessageWrapper = onMessageWrapperMap.get(onMessage);
|
|
77
|
-
|
|
78
|
-
if (!onMessageWrapper) {
|
|
79
|
-
onMessageWrapper = (message: WorkerMessageEvent) => {
|
|
80
|
-
const messageData = getWorkerMessageData(message);
|
|
81
|
-
if (!messageData) {
|
|
82
|
-
return;
|
|
83
|
-
}
|
|
84
|
-
onMessage(messageData.type, messageData.payload);
|
|
85
|
-
};
|
|
86
|
-
onMessageWrapperMap.set(onMessage, onMessageWrapper);
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
const parentPort = await getParentPort();
|
|
90
|
-
if (parentPort) {
|
|
91
|
-
parentPort.on('message', onMessageWrapper);
|
|
92
|
-
} else {
|
|
93
|
-
globalThis.addEventListener('message', onMessageWrapper);
|
|
94
|
-
}
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
static async removeEventListener(onMessage: WorkerMessageListener) {
|
|
98
|
-
const onMessageWrapper = onMessageWrapperMap.get(onMessage);
|
|
99
|
-
onMessageWrapperMap.delete(onMessage);
|
|
100
|
-
if (!onMessageWrapper) {
|
|
101
|
-
return;
|
|
102
|
-
}
|
|
103
|
-
const parentPort = await getParentPort();
|
|
104
|
-
if (parentPort) {
|
|
105
|
-
parentPort.off('message', onMessageWrapper);
|
|
106
|
-
} else {
|
|
107
|
-
globalThis.removeEventListener('message', onMessageWrapper);
|
|
108
|
-
}
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
/**
|
|
112
|
-
* Send a message from a worker to creating thread (main thread)
|
|
113
|
-
* @param type
|
|
114
|
-
* @param payload
|
|
115
|
-
*/
|
|
116
|
-
static async postMessage(type: WorkerMessageType, payload: WorkerMessagePayload): Promise<void> {
|
|
117
|
-
const data: WorkerMessageData = {source: 'loaders.gl', type, payload};
|
|
118
|
-
// console.log('posting message', data);
|
|
119
|
-
|
|
120
|
-
// Cast to Node compatible transfer list
|
|
121
|
-
const transferList = getTransferList(payload) as TransferListItem[];
|
|
122
|
-
|
|
123
|
-
const parentPort = await getParentPort();
|
|
124
|
-
if (parentPort) {
|
|
125
|
-
parentPort.postMessage(data, transferList);
|
|
126
|
-
// console.log('posted message', data);
|
|
127
|
-
} else {
|
|
128
|
-
// @ts-expect-error Outside of worker scopes this call has a third parameter
|
|
129
|
-
globalThis.postMessage(data, transferList);
|
|
130
|
-
}
|
|
131
|
-
}
|
|
132
|
-
}
|
|
133
|
-
|
|
134
|
-
// Filter out noise messages sent to workers
|
|
135
|
-
function getWorkerMessageData(message: WorkerMessageEvent): WorkerMessageData | null {
|
|
136
|
-
if (!message || typeof message !== 'object') {
|
|
137
|
-
return null;
|
|
138
|
-
}
|
|
139
|
-
const messageData = 'data' in message && message.type === 'message' ? message.data : message;
|
|
140
|
-
return messageData &&
|
|
141
|
-
typeof messageData.source === 'string' &&
|
|
142
|
-
messageData.source.startsWith('loaders.gl')
|
|
143
|
-
? messageData
|
|
144
|
-
: null;
|
|
145
|
-
}
|
|
@@ -1,144 +0,0 @@
|
|
|
1
|
-
// loaders.gl
|
|
2
|
-
// SPDX-License-Identifier: MIT
|
|
3
|
-
// 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} 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
|
-
maxConcurrency?: number;
|
|
19
|
-
maxMobileConcurrency?: number;
|
|
20
|
-
reuseWorkers?: boolean;
|
|
21
|
-
onDebug?: () => void;
|
|
22
|
-
};
|
|
23
|
-
|
|
24
|
-
const DEFAULT_PROPS: Required<WorkerFarmProps> = {
|
|
25
|
-
maxConcurrency: 3,
|
|
26
|
-
maxMobileConcurrency: 1,
|
|
27
|
-
reuseWorkers: true,
|
|
28
|
-
onDebug: () => {}
|
|
29
|
-
};
|
|
30
|
-
|
|
31
|
-
/** Stable ids used to distinguish worker factories in the pool cache. */
|
|
32
|
-
const loadWorkerIds = new WeakMap<LoadWorker, number>();
|
|
33
|
-
/** Next stable worker-factory id. */
|
|
34
|
-
let nextLoadWorkerId = 1;
|
|
35
|
-
|
|
36
|
-
/**
|
|
37
|
-
* Process multiple jobs with a "farm" of different workers in worker pools.
|
|
38
|
-
*/
|
|
39
|
-
export default class WorkerFarm {
|
|
40
|
-
private props: WorkerFarmProps;
|
|
41
|
-
private workerPools = new Map<string, WorkerPool>();
|
|
42
|
-
// singleton
|
|
43
|
-
private static _workerFarm?: WorkerFarm;
|
|
44
|
-
|
|
45
|
-
/** Checks if workers are supported on this platform */
|
|
46
|
-
static isSupported(): boolean {
|
|
47
|
-
return WorkerThread.isSupported();
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
/** Get the singleton instance of the global worker farm */
|
|
51
|
-
static getWorkerFarm(props: WorkerFarmProps = {}): WorkerFarm {
|
|
52
|
-
WorkerFarm._workerFarm = WorkerFarm._workerFarm || new WorkerFarm({});
|
|
53
|
-
WorkerFarm._workerFarm.setProps(props);
|
|
54
|
-
return WorkerFarm._workerFarm;
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
/** get global instance with WorkerFarm.getWorkerFarm() */
|
|
58
|
-
private constructor(props: WorkerFarmProps) {
|
|
59
|
-
this.props = {...DEFAULT_PROPS};
|
|
60
|
-
this.setProps(props);
|
|
61
|
-
/** @type Map<string, WorkerPool>} */
|
|
62
|
-
this.workerPools = new Map();
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
/**
|
|
66
|
-
* Terminate all workers in the farm and abort active jobs.
|
|
67
|
-
* @param reason Optional error delivered to active jobs.
|
|
68
|
-
* @note Can free up significant memory
|
|
69
|
-
*/
|
|
70
|
-
destroy(reason?: unknown): void {
|
|
71
|
-
for (const workerPool of this.workerPools.values()) {
|
|
72
|
-
workerPool.destroy(reason);
|
|
73
|
-
}
|
|
74
|
-
this.workerPools = new Map<string, WorkerPool>();
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
/**
|
|
78
|
-
* Set props used when initializing worker pools
|
|
79
|
-
* @param props
|
|
80
|
-
*/
|
|
81
|
-
setProps(props: WorkerFarmProps): void {
|
|
82
|
-
this.props = {...this.props, ...props};
|
|
83
|
-
// Update worker pool props
|
|
84
|
-
for (const workerPool of this.workerPools.values()) {
|
|
85
|
-
workerPool.setProps(this._getWorkerPoolProps());
|
|
86
|
-
}
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
/**
|
|
90
|
-
* Returns a worker pool for the specified worker
|
|
91
|
-
* @param options Worker target used to identify and initialize the pool.
|
|
92
|
-
* @param options.name Worker name used as the base pool identity.
|
|
93
|
-
* @param options.url Worker script URL.
|
|
94
|
-
* @param options.source Inline worker source.
|
|
95
|
-
* @param options.loadWorker Built-in browser worker factory.
|
|
96
|
-
* @example
|
|
97
|
-
* const job = WorkerFarm.getWorkerFarm().getWorkerPool({name, url}).startJob(...);
|
|
98
|
-
*/
|
|
99
|
-
getWorkerPool(options: WorkerPoolTarget): WorkerPool {
|
|
100
|
-
const {name, source, url, getUrl, loadWorker} = options;
|
|
101
|
-
const workerPoolKey = getWorkerPoolKey(options);
|
|
102
|
-
let workerPool = this.workerPools.get(workerPoolKey);
|
|
103
|
-
if (!workerPool) {
|
|
104
|
-
workerPool = new WorkerPool({
|
|
105
|
-
name,
|
|
106
|
-
source,
|
|
107
|
-
url,
|
|
108
|
-
getUrl,
|
|
109
|
-
loadWorker
|
|
110
|
-
});
|
|
111
|
-
workerPool.setProps(this._getWorkerPoolProps());
|
|
112
|
-
this.workerPools.set(workerPoolKey, workerPool);
|
|
113
|
-
}
|
|
114
|
-
return workerPool;
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
_getWorkerPoolProps() {
|
|
118
|
-
return {
|
|
119
|
-
maxConcurrency: this.props.maxConcurrency,
|
|
120
|
-
maxMobileConcurrency: this.props.maxMobileConcurrency,
|
|
121
|
-
reuseWorkers: this.props.reuseWorkers,
|
|
122
|
-
onDebug: this.props.onDebug
|
|
123
|
-
};
|
|
124
|
-
}
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
/** Returns a cache key that distinguishes each worker source and factory. */
|
|
128
|
-
function getWorkerPoolKey(options: WorkerPoolTarget): string {
|
|
129
|
-
const {name, source, url, urlKey, loadWorker} = options;
|
|
130
|
-
const sourceKey = source ? `source:${source}` : '';
|
|
131
|
-
const urlKeyPart = url || urlKey ? `url:${url || urlKey}` : '';
|
|
132
|
-
const loadWorkerKey = loadWorker ? `loadWorker:${getLoadWorkerId(loadWorker)}` : '';
|
|
133
|
-
return [name, sourceKey, urlKeyPart, loadWorkerKey].filter(Boolean).join('|');
|
|
134
|
-
}
|
|
135
|
-
|
|
136
|
-
/** Returns a stable identifier for a worker factory function. */
|
|
137
|
-
function getLoadWorkerId(loadWorker: LoadWorker): number {
|
|
138
|
-
let loadWorkerId = loadWorkerIds.get(loadWorker);
|
|
139
|
-
if (!loadWorkerId) {
|
|
140
|
-
loadWorkerId = nextLoadWorkerId++;
|
|
141
|
-
loadWorkerIds.set(loadWorker, loadWorkerId);
|
|
142
|
-
}
|
|
143
|
-
return loadWorkerId;
|
|
144
|
-
}
|