@loaders.gl/worker-utils 4.0.0-alpha.9 → 4.0.0-beta.2
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/dist/es5/lib/env-utils/version.js +17 -6
- package/dist/es5/lib/env-utils/version.js.map +1 -1
- package/dist/es5/lib/library-utils/library-utils.js +106 -43
- package/dist/es5/lib/library-utils/library-utils.js.map +1 -1
- package/dist/es5/lib/node/require-utils.node.js +78 -11
- package/dist/es5/lib/node/require-utils.node.js.map +1 -1
- package/dist/es5/lib/worker-api/get-worker-url.js +2 -4
- package/dist/es5/lib/worker-api/get-worker-url.js.map +1 -1
- package/dist/esm/lib/env-utils/version.js +14 -4
- package/dist/esm/lib/env-utils/version.js.map +1 -1
- package/dist/esm/lib/library-utils/library-utils.js +30 -16
- package/dist/esm/lib/library-utils/library-utils.js.map +1 -1
- package/dist/esm/lib/node/require-utils.node.js +17 -0
- package/dist/esm/lib/node/require-utils.node.js.map +1 -1
- package/dist/esm/lib/worker-api/get-worker-url.js +1 -3
- package/dist/esm/lib/worker-api/get-worker-url.js.map +1 -1
- package/dist/lib/env-utils/version.d.ts +6 -1
- package/dist/lib/env-utils/version.d.ts.map +1 -1
- package/dist/lib/library-utils/library-utils.d.ts +2 -2
- package/dist/lib/library-utils/library-utils.d.ts.map +1 -1
- package/dist/lib/node/require-utils.node.d.ts +12 -0
- package/dist/lib/node/require-utils.node.d.ts.map +1 -1
- package/dist/lib/worker-api/get-worker-url.d.ts.map +1 -1
- package/package.json +4 -5
- package/src/lib/env-utils/version.ts +25 -9
- package/src/lib/library-utils/library-utils.ts +42 -23
- package/src/lib/node/require-utils.node.ts +29 -0
- package/src/lib/worker-api/get-worker-url.ts +1 -4
- package/dist/index.js +0 -59
- package/dist/lib/async-queue/async-queue.js +0 -87
- package/dist/lib/env-utils/assert.js +0 -13
- package/dist/lib/env-utils/globals.js +0 -32
- package/dist/lib/env-utils/version.js +0 -12
- package/dist/lib/library-utils/library-utils.js +0 -170
- package/dist/lib/node/require-utils.node.js +0 -60
- package/dist/lib/node/worker_threads-browser.js +0 -15
- package/dist/lib/node/worker_threads.js +0 -33
- package/dist/lib/process-utils/child-process-proxy.js +0 -136
- package/dist/lib/process-utils/process-utils.js +0 -35
- package/dist/lib/worker-api/create-worker.js +0 -91
- package/dist/lib/worker-api/get-worker-url.js +0 -59
- package/dist/lib/worker-api/process-on-worker.js +0 -83
- package/dist/lib/worker-api/validate-worker-version.js +0 -35
- package/dist/lib/worker-farm/worker-body.js +0 -111
- package/dist/lib/worker-farm/worker-farm.js +0 -89
- package/dist/lib/worker-farm/worker-job.js +0 -47
- package/dist/lib/worker-farm/worker-pool.js +0 -164
- package/dist/lib/worker-farm/worker-thread.js +0 -131
- package/dist/lib/worker-utils/get-loadable-worker-url.js +0 -72
- package/dist/lib/worker-utils/get-transfer-list.js +0 -87
- package/dist/lib/worker-utils/remove-nontransferable-options.js +0 -27
- package/dist/types.js +0 -2
- package/dist/workers/null-worker.js +0 -7
|
@@ -1,91 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
-
};
|
|
5
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.createWorker = void 0;
|
|
7
|
-
const async_queue_1 = __importDefault(require("../async-queue/async-queue"));
|
|
8
|
-
const worker_body_1 = __importDefault(require("../worker-farm/worker-body"));
|
|
9
|
-
// import {validateWorkerVersion} from './validate-worker-version';
|
|
10
|
-
/** Counter for jobs */
|
|
11
|
-
let requestId = 0;
|
|
12
|
-
let inputBatches;
|
|
13
|
-
let options;
|
|
14
|
-
/**
|
|
15
|
-
* Set up a WebWorkerGlobalScope to talk with the main thread
|
|
16
|
-
*/
|
|
17
|
-
function createWorker(process, processInBatches) {
|
|
18
|
-
if (!worker_body_1.default.inWorkerThread()) {
|
|
19
|
-
return;
|
|
20
|
-
}
|
|
21
|
-
const context = {
|
|
22
|
-
process: processOnMainThread
|
|
23
|
-
};
|
|
24
|
-
// eslint-disable-next-line complexity
|
|
25
|
-
worker_body_1.default.onmessage = async (type, payload) => {
|
|
26
|
-
try {
|
|
27
|
-
switch (type) {
|
|
28
|
-
case 'process':
|
|
29
|
-
if (!process) {
|
|
30
|
-
throw new Error('Worker does not support atomic processing');
|
|
31
|
-
}
|
|
32
|
-
const result = await process(payload.input, payload.options || {}, context);
|
|
33
|
-
worker_body_1.default.postMessage('done', { result });
|
|
34
|
-
break;
|
|
35
|
-
case 'process-in-batches':
|
|
36
|
-
if (!processInBatches) {
|
|
37
|
-
throw new Error('Worker does not support batched processing');
|
|
38
|
-
}
|
|
39
|
-
inputBatches = new async_queue_1.default();
|
|
40
|
-
options = payload.options || {};
|
|
41
|
-
const resultIterator = processInBatches(inputBatches, options, context);
|
|
42
|
-
for await (const batch of resultIterator) {
|
|
43
|
-
worker_body_1.default.postMessage('output-batch', { result: batch });
|
|
44
|
-
}
|
|
45
|
-
worker_body_1.default.postMessage('done', {});
|
|
46
|
-
break;
|
|
47
|
-
case 'input-batch':
|
|
48
|
-
inputBatches.push(payload.input);
|
|
49
|
-
break;
|
|
50
|
-
case 'input-done':
|
|
51
|
-
inputBatches.close();
|
|
52
|
-
break;
|
|
53
|
-
default:
|
|
54
|
-
}
|
|
55
|
-
}
|
|
56
|
-
catch (error) {
|
|
57
|
-
const message = error instanceof Error ? error.message : '';
|
|
58
|
-
worker_body_1.default.postMessage('error', { error: message });
|
|
59
|
-
}
|
|
60
|
-
};
|
|
61
|
-
}
|
|
62
|
-
exports.createWorker = createWorker;
|
|
63
|
-
function processOnMainThread(arrayBuffer, options = {}) {
|
|
64
|
-
return new Promise((resolve, reject) => {
|
|
65
|
-
const id = requestId++;
|
|
66
|
-
/**
|
|
67
|
-
*/
|
|
68
|
-
const onMessage = (type, payload) => {
|
|
69
|
-
if (payload.id !== id) {
|
|
70
|
-
// not ours
|
|
71
|
-
return;
|
|
72
|
-
}
|
|
73
|
-
switch (type) {
|
|
74
|
-
case 'done':
|
|
75
|
-
worker_body_1.default.removeEventListener(onMessage);
|
|
76
|
-
resolve(payload.result);
|
|
77
|
-
break;
|
|
78
|
-
case 'error':
|
|
79
|
-
worker_body_1.default.removeEventListener(onMessage);
|
|
80
|
-
reject(payload.error);
|
|
81
|
-
break;
|
|
82
|
-
default:
|
|
83
|
-
// ignore
|
|
84
|
-
}
|
|
85
|
-
};
|
|
86
|
-
worker_body_1.default.addEventListener(onMessage);
|
|
87
|
-
// Ask the main thread to decode data
|
|
88
|
-
const payload = { id, input: arrayBuffer, options };
|
|
89
|
-
worker_body_1.default.postMessage('process', payload);
|
|
90
|
-
});
|
|
91
|
-
}
|
|
@@ -1,59 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
// loaders.gl, MIT license
|
|
3
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
-
exports.getWorkerURL = exports.getWorkerName = void 0;
|
|
5
|
-
const assert_1 = require("../env-utils/assert");
|
|
6
|
-
const globals_1 = require("../env-utils/globals");
|
|
7
|
-
const version_1 = require("../env-utils/version");
|
|
8
|
-
const NPM_TAG = 'beta'; // 'beta', or 'latest' on release-branch
|
|
9
|
-
const VERSION = typeof version_1.VERSION !== 'undefined' ? version_1.VERSION : NPM_TAG;
|
|
10
|
-
/**
|
|
11
|
-
* Gets worker object's name (for debugging in Chrome thread inspector window)
|
|
12
|
-
*/
|
|
13
|
-
function getWorkerName(worker) {
|
|
14
|
-
const warning = worker.version !== VERSION ? ` (worker-utils@${VERSION})` : '';
|
|
15
|
-
return `${worker.name}@${worker.version}${warning}`;
|
|
16
|
-
}
|
|
17
|
-
exports.getWorkerName = getWorkerName;
|
|
18
|
-
/**
|
|
19
|
-
* Generate a worker URL based on worker object and options
|
|
20
|
-
* @returns A URL to one of the following:
|
|
21
|
-
* - a published worker on unpkg CDN
|
|
22
|
-
* - a local test worker
|
|
23
|
-
* - a URL provided by the user in options
|
|
24
|
-
*/
|
|
25
|
-
function getWorkerURL(worker, options = {}) {
|
|
26
|
-
const workerOptions = options[worker.id] || {};
|
|
27
|
-
const workerFile = globals_1.isBrowser ? `${worker.id}-worker.js` : `${worker.id}-worker-node.js`;
|
|
28
|
-
let url = workerOptions.workerUrl;
|
|
29
|
-
// HACK: Allow for non-nested workerUrl for the CompressionWorker.
|
|
30
|
-
// For the compression worker, workerOptions is currently not nested correctly. For most loaders,
|
|
31
|
-
// you'd have options within an object, i.e. `{mvt: {coordinates: ...}}` but the CompressionWorker
|
|
32
|
-
// puts options at the top level, not within a `compression` key (its `id`). For this reason, the
|
|
33
|
-
// above `workerOptions` will always be a string (i.e. `'gzip'`) for the CompressionWorker. To not
|
|
34
|
-
// break backwards compatibility, we allow the CompressionWorker to have options at the top level.
|
|
35
|
-
if (!url && worker.id === 'compression') {
|
|
36
|
-
url = options.workerUrl;
|
|
37
|
-
}
|
|
38
|
-
// If URL is test, generate local loaders.gl url
|
|
39
|
-
// @ts-ignore _workerType
|
|
40
|
-
if (options._workerType === 'test') {
|
|
41
|
-
url = `modules/${worker.module}/dist/${workerFile}`;
|
|
42
|
-
}
|
|
43
|
-
// If url override is not provided, generate a URL to published version on npm CDN unpkg.com
|
|
44
|
-
if (!url) {
|
|
45
|
-
// GENERATE
|
|
46
|
-
let version = worker.version;
|
|
47
|
-
// On master we need to load npm alpha releases published with the `beta` tag
|
|
48
|
-
if (version === 'latest') {
|
|
49
|
-
// throw new Error('latest worker version specified');
|
|
50
|
-
version = NPM_TAG;
|
|
51
|
-
}
|
|
52
|
-
const versionTag = version ? `@${version}` : '';
|
|
53
|
-
url = `https://unpkg.com/@loaders.gl/${worker.module}${versionTag}/dist/${workerFile}`;
|
|
54
|
-
}
|
|
55
|
-
(0, assert_1.assert)(url);
|
|
56
|
-
// Allow user to override location
|
|
57
|
-
return url;
|
|
58
|
-
}
|
|
59
|
-
exports.getWorkerURL = getWorkerURL;
|
|
@@ -1,83 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
-
};
|
|
5
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.processOnWorker = exports.canProcessOnWorker = void 0;
|
|
7
|
-
const worker_farm_1 = __importDefault(require("../worker-farm/worker-farm"));
|
|
8
|
-
const get_worker_url_1 = require("./get-worker-url");
|
|
9
|
-
const get_transfer_list_1 = require("../worker-utils/get-transfer-list");
|
|
10
|
-
/**
|
|
11
|
-
* Determines if we can parse with worker
|
|
12
|
-
* @param loader
|
|
13
|
-
* @param data
|
|
14
|
-
* @param options
|
|
15
|
-
*/
|
|
16
|
-
function canProcessOnWorker(worker, options) {
|
|
17
|
-
if (!worker_farm_1.default.isSupported()) {
|
|
18
|
-
return false;
|
|
19
|
-
}
|
|
20
|
-
return worker.worker && options?.worker;
|
|
21
|
-
}
|
|
22
|
-
exports.canProcessOnWorker = canProcessOnWorker;
|
|
23
|
-
/**
|
|
24
|
-
* This function expects that the worker thread sends certain messages,
|
|
25
|
-
* Creating such a worker can be automated if the worker is wrapper by a call to
|
|
26
|
-
* createWorker in @loaders.gl/worker-utils.
|
|
27
|
-
*/
|
|
28
|
-
async function processOnWorker(worker, data, options = {}, context = {}) {
|
|
29
|
-
const name = (0, get_worker_url_1.getWorkerName)(worker);
|
|
30
|
-
const workerFarm = worker_farm_1.default.getWorkerFarm(options);
|
|
31
|
-
const { source } = options;
|
|
32
|
-
const workerPoolProps = { name, source };
|
|
33
|
-
if (!source) {
|
|
34
|
-
workerPoolProps.url = (0, get_worker_url_1.getWorkerURL)(worker, options);
|
|
35
|
-
}
|
|
36
|
-
const workerPool = workerFarm.getWorkerPool(workerPoolProps);
|
|
37
|
-
const jobName = options.jobName || worker.name;
|
|
38
|
-
const job = await workerPool.startJob(jobName,
|
|
39
|
-
// eslint-disable-next-line
|
|
40
|
-
onMessage.bind(null, context));
|
|
41
|
-
// Kick off the processing in the worker
|
|
42
|
-
const transferableOptions = (0, get_transfer_list_1.getTransferListForWriter)(options);
|
|
43
|
-
job.postMessage('process', { input: data, options: transferableOptions });
|
|
44
|
-
const result = await job.result;
|
|
45
|
-
return result.result;
|
|
46
|
-
}
|
|
47
|
-
exports.processOnWorker = processOnWorker;
|
|
48
|
-
/**
|
|
49
|
-
* Job completes when we receive the result
|
|
50
|
-
* @param job
|
|
51
|
-
* @param message
|
|
52
|
-
*/
|
|
53
|
-
async function onMessage(context, job, type, payload) {
|
|
54
|
-
switch (type) {
|
|
55
|
-
case 'done':
|
|
56
|
-
// Worker is done
|
|
57
|
-
job.done(payload);
|
|
58
|
-
break;
|
|
59
|
-
case 'error':
|
|
60
|
-
// Worker encountered an error
|
|
61
|
-
job.error(new Error(payload.error));
|
|
62
|
-
break;
|
|
63
|
-
case 'process':
|
|
64
|
-
// Worker is asking for us (main thread) to process something
|
|
65
|
-
const { id, input, options } = payload;
|
|
66
|
-
try {
|
|
67
|
-
if (!context.process) {
|
|
68
|
-
job.postMessage('error', { id, error: 'Worker not set up to process on main thread' });
|
|
69
|
-
return;
|
|
70
|
-
}
|
|
71
|
-
const result = await context.process(input, options);
|
|
72
|
-
job.postMessage('done', { id, result });
|
|
73
|
-
}
|
|
74
|
-
catch (error) {
|
|
75
|
-
const message = error instanceof Error ? error.message : 'unknown error';
|
|
76
|
-
job.postMessage('error', { id, error: message });
|
|
77
|
-
}
|
|
78
|
-
break;
|
|
79
|
-
default:
|
|
80
|
-
// eslint-disable-next-line
|
|
81
|
-
console.warn(`process-on-worker: unknown message ${type}`);
|
|
82
|
-
}
|
|
83
|
-
}
|
|
@@ -1,35 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.validateWorkerVersion = void 0;
|
|
4
|
-
const assert_1 = require("../env-utils/assert");
|
|
5
|
-
const version_1 = require("../env-utils/version");
|
|
6
|
-
/**
|
|
7
|
-
* Check if worker is compatible with this library version
|
|
8
|
-
* @param worker
|
|
9
|
-
* @param libVersion
|
|
10
|
-
* @returns `true` if the two versions are compatible
|
|
11
|
-
*/
|
|
12
|
-
function validateWorkerVersion(worker, coreVersion = version_1.VERSION) {
|
|
13
|
-
(0, assert_1.assert)(worker, 'no worker provided');
|
|
14
|
-
const workerVersion = worker.version;
|
|
15
|
-
if (!coreVersion || !workerVersion) {
|
|
16
|
-
return false;
|
|
17
|
-
}
|
|
18
|
-
// TODO enable when fix the __version__ injection
|
|
19
|
-
// const coreVersions = parseVersion(coreVersion);
|
|
20
|
-
// const workerVersions = parseVersion(workerVersion);
|
|
21
|
-
// assert(
|
|
22
|
-
// coreVersion.major === workerVersion.major && coreVersion.minor <= workerVersion.minor,
|
|
23
|
-
// `worker: ${worker.name} is not compatible. ${coreVersion.major}.${
|
|
24
|
-
// coreVersion.minor
|
|
25
|
-
// }+ is required.`
|
|
26
|
-
// );
|
|
27
|
-
return true;
|
|
28
|
-
}
|
|
29
|
-
exports.validateWorkerVersion = validateWorkerVersion;
|
|
30
|
-
// @ts-ignore
|
|
31
|
-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
32
|
-
function parseVersion(version) {
|
|
33
|
-
const parts = version.split('.').map(Number);
|
|
34
|
-
return { major: parts[0], minor: parts[1] };
|
|
35
|
-
}
|
|
@@ -1,111 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
const get_transfer_list_1 = require("../worker-utils/get-transfer-list");
|
|
4
|
-
/** Vile hack to defeat over-zealous bundlers from stripping out the require */
|
|
5
|
-
function getParentPort() {
|
|
6
|
-
// const isNode = globalThis.process;
|
|
7
|
-
let parentPort;
|
|
8
|
-
try {
|
|
9
|
-
// prettier-ignore
|
|
10
|
-
eval('globalThis.parentPort = require(\'worker_threads\').parentPort'); // eslint-disable-line no-eval
|
|
11
|
-
parentPort = globalThis.parentPort;
|
|
12
|
-
// eslint-disable-next-line no-empty
|
|
13
|
-
}
|
|
14
|
-
catch { }
|
|
15
|
-
return parentPort;
|
|
16
|
-
}
|
|
17
|
-
const onMessageWrapperMap = new Map();
|
|
18
|
-
/**
|
|
19
|
-
* Type safe wrapper for worker code
|
|
20
|
-
*/
|
|
21
|
-
class WorkerBody {
|
|
22
|
-
/** Check that we are actually in a worker thread */
|
|
23
|
-
static inWorkerThread() {
|
|
24
|
-
return typeof self !== 'undefined' || Boolean(getParentPort());
|
|
25
|
-
}
|
|
26
|
-
/*
|
|
27
|
-
* (type: WorkerMessageType, payload: WorkerMessagePayload) => any
|
|
28
|
-
*/
|
|
29
|
-
static set onmessage(onMessage) {
|
|
30
|
-
function handleMessage(message) {
|
|
31
|
-
// Confusingly the message itself also has a 'type' field which is always set to 'message'
|
|
32
|
-
const parentPort = getParentPort();
|
|
33
|
-
const { type, payload } = parentPort ? message : message.data;
|
|
34
|
-
// if (!isKnownMessage(message)) {
|
|
35
|
-
// return;
|
|
36
|
-
// }
|
|
37
|
-
onMessage(type, payload);
|
|
38
|
-
}
|
|
39
|
-
const parentPort = getParentPort();
|
|
40
|
-
if (parentPort) {
|
|
41
|
-
parentPort.on('message', handleMessage);
|
|
42
|
-
// if (message == 'exit') { parentPort.unref(); }
|
|
43
|
-
// eslint-disable-next-line
|
|
44
|
-
parentPort.on('exit', () => console.debug('Node worker closing'));
|
|
45
|
-
}
|
|
46
|
-
else {
|
|
47
|
-
// eslint-disable-next-line no-restricted-globals
|
|
48
|
-
globalThis.onmessage = handleMessage;
|
|
49
|
-
}
|
|
50
|
-
}
|
|
51
|
-
static addEventListener(onMessage) {
|
|
52
|
-
let onMessageWrapper = onMessageWrapperMap.get(onMessage);
|
|
53
|
-
if (!onMessageWrapper) {
|
|
54
|
-
onMessageWrapper = (message) => {
|
|
55
|
-
if (!isKnownMessage(message)) {
|
|
56
|
-
return;
|
|
57
|
-
}
|
|
58
|
-
// Confusingly in the browser, the message itself also has a 'type' field which is always set to 'message'
|
|
59
|
-
const parentPort = getParentPort();
|
|
60
|
-
const { type, payload } = parentPort ? message : message.data;
|
|
61
|
-
onMessage(type, payload);
|
|
62
|
-
};
|
|
63
|
-
}
|
|
64
|
-
const parentPort = getParentPort();
|
|
65
|
-
if (parentPort) {
|
|
66
|
-
console.error('not implemented'); // eslint-disable-line
|
|
67
|
-
}
|
|
68
|
-
else {
|
|
69
|
-
globalThis.addEventListener('message', onMessageWrapper);
|
|
70
|
-
}
|
|
71
|
-
}
|
|
72
|
-
static removeEventListener(onMessage) {
|
|
73
|
-
const onMessageWrapper = onMessageWrapperMap.get(onMessage);
|
|
74
|
-
onMessageWrapperMap.delete(onMessage);
|
|
75
|
-
const parentPort = getParentPort();
|
|
76
|
-
if (parentPort) {
|
|
77
|
-
console.error('not implemented'); // eslint-disable-line
|
|
78
|
-
}
|
|
79
|
-
else {
|
|
80
|
-
globalThis.removeEventListener('message', onMessageWrapper);
|
|
81
|
-
}
|
|
82
|
-
}
|
|
83
|
-
/**
|
|
84
|
-
* Send a message from a worker to creating thread (main thread)
|
|
85
|
-
* @param type
|
|
86
|
-
* @param payload
|
|
87
|
-
*/
|
|
88
|
-
static postMessage(type, payload) {
|
|
89
|
-
const data = { source: 'loaders.gl', type, payload };
|
|
90
|
-
// console.log('posting message', data);
|
|
91
|
-
const transferList = (0, get_transfer_list_1.getTransferList)(payload);
|
|
92
|
-
const parentPort = getParentPort();
|
|
93
|
-
if (parentPort) {
|
|
94
|
-
parentPort.postMessage(data, transferList);
|
|
95
|
-
// console.log('posted message', data);
|
|
96
|
-
}
|
|
97
|
-
else {
|
|
98
|
-
// @ts-ignore
|
|
99
|
-
globalThis.postMessage(data, transferList);
|
|
100
|
-
}
|
|
101
|
-
}
|
|
102
|
-
}
|
|
103
|
-
exports.default = WorkerBody;
|
|
104
|
-
// Filter out noise messages sent to workers
|
|
105
|
-
function isKnownMessage(message) {
|
|
106
|
-
const { type, data } = message;
|
|
107
|
-
return (type === 'message' &&
|
|
108
|
-
data &&
|
|
109
|
-
typeof data.source === 'string' &&
|
|
110
|
-
data.source.startsWith('loaders.gl'));
|
|
111
|
-
}
|
|
@@ -1,89 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
-
};
|
|
5
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
const worker_pool_1 = __importDefault(require("./worker-pool"));
|
|
7
|
-
const worker_thread_1 = __importDefault(require("./worker-thread"));
|
|
8
|
-
const DEFAULT_PROPS = {
|
|
9
|
-
maxConcurrency: 3,
|
|
10
|
-
maxMobileConcurrency: 1,
|
|
11
|
-
reuseWorkers: true,
|
|
12
|
-
onDebug: () => { }
|
|
13
|
-
};
|
|
14
|
-
/**
|
|
15
|
-
* Process multiple jobs with a "farm" of different workers in worker pools.
|
|
16
|
-
*/
|
|
17
|
-
class WorkerFarm {
|
|
18
|
-
/** Checks if workers are supported on this platform */
|
|
19
|
-
static isSupported() {
|
|
20
|
-
return worker_thread_1.default.isSupported();
|
|
21
|
-
}
|
|
22
|
-
/** Get the singleton instance of the global worker farm */
|
|
23
|
-
static getWorkerFarm(props = {}) {
|
|
24
|
-
WorkerFarm._workerFarm = WorkerFarm._workerFarm || new WorkerFarm({});
|
|
25
|
-
WorkerFarm._workerFarm.setProps(props);
|
|
26
|
-
return WorkerFarm._workerFarm;
|
|
27
|
-
}
|
|
28
|
-
/** get global instance with WorkerFarm.getWorkerFarm() */
|
|
29
|
-
constructor(props) {
|
|
30
|
-
this.workerPools = new Map();
|
|
31
|
-
this.props = { ...DEFAULT_PROPS };
|
|
32
|
-
this.setProps(props);
|
|
33
|
-
/** @type Map<string, WorkerPool>} */
|
|
34
|
-
this.workerPools = new Map();
|
|
35
|
-
}
|
|
36
|
-
/**
|
|
37
|
-
* Terminate all workers in the farm
|
|
38
|
-
* @note Can free up significant memory
|
|
39
|
-
*/
|
|
40
|
-
destroy() {
|
|
41
|
-
for (const workerPool of this.workerPools.values()) {
|
|
42
|
-
workerPool.destroy();
|
|
43
|
-
}
|
|
44
|
-
this.workerPools = new Map();
|
|
45
|
-
}
|
|
46
|
-
/**
|
|
47
|
-
* Set props used when initializing worker pools
|
|
48
|
-
* @param props
|
|
49
|
-
*/
|
|
50
|
-
setProps(props) {
|
|
51
|
-
this.props = { ...this.props, ...props };
|
|
52
|
-
// Update worker pool props
|
|
53
|
-
for (const workerPool of this.workerPools.values()) {
|
|
54
|
-
workerPool.setProps(this._getWorkerPoolProps());
|
|
55
|
-
}
|
|
56
|
-
}
|
|
57
|
-
/**
|
|
58
|
-
* Returns a worker pool for the specified worker
|
|
59
|
-
* @param options - only used first time for a specific worker name
|
|
60
|
-
* @param options.name - the name of the worker - used to identify worker pool
|
|
61
|
-
* @param options.url -
|
|
62
|
-
* @param options.source -
|
|
63
|
-
* @example
|
|
64
|
-
* const job = WorkerFarm.getWorkerFarm().getWorkerPool({name, url}).startJob(...);
|
|
65
|
-
*/
|
|
66
|
-
getWorkerPool(options) {
|
|
67
|
-
const { name, source, url } = options;
|
|
68
|
-
let workerPool = this.workerPools.get(name);
|
|
69
|
-
if (!workerPool) {
|
|
70
|
-
workerPool = new worker_pool_1.default({
|
|
71
|
-
name,
|
|
72
|
-
source,
|
|
73
|
-
url
|
|
74
|
-
});
|
|
75
|
-
workerPool.setProps(this._getWorkerPoolProps());
|
|
76
|
-
this.workerPools.set(name, workerPool);
|
|
77
|
-
}
|
|
78
|
-
return workerPool;
|
|
79
|
-
}
|
|
80
|
-
_getWorkerPoolProps() {
|
|
81
|
-
return {
|
|
82
|
-
maxConcurrency: this.props.maxConcurrency,
|
|
83
|
-
maxMobileConcurrency: this.props.maxMobileConcurrency,
|
|
84
|
-
reuseWorkers: this.props.reuseWorkers,
|
|
85
|
-
onDebug: this.props.onDebug
|
|
86
|
-
};
|
|
87
|
-
}
|
|
88
|
-
}
|
|
89
|
-
exports.default = WorkerFarm;
|
|
@@ -1,47 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
const assert_1 = require("../env-utils/assert");
|
|
4
|
-
/**
|
|
5
|
-
* Represents one Job handled by a WorkerPool or WorkerFarm
|
|
6
|
-
*/
|
|
7
|
-
class WorkerJob {
|
|
8
|
-
constructor(jobName, workerThread) {
|
|
9
|
-
this.isRunning = true;
|
|
10
|
-
this._resolve = () => { };
|
|
11
|
-
this._reject = () => { };
|
|
12
|
-
this.name = jobName;
|
|
13
|
-
this.workerThread = workerThread;
|
|
14
|
-
this.result = new Promise((resolve, reject) => {
|
|
15
|
-
this._resolve = resolve;
|
|
16
|
-
this._reject = reject;
|
|
17
|
-
});
|
|
18
|
-
}
|
|
19
|
-
/**
|
|
20
|
-
* Send a message to the job's worker thread
|
|
21
|
-
* @param data any data structure, ideally consisting mostly of transferrable objects
|
|
22
|
-
*/
|
|
23
|
-
postMessage(type, payload) {
|
|
24
|
-
this.workerThread.postMessage({
|
|
25
|
-
source: 'loaders.gl',
|
|
26
|
-
type,
|
|
27
|
-
payload
|
|
28
|
-
});
|
|
29
|
-
}
|
|
30
|
-
/**
|
|
31
|
-
* Call to resolve the `result` Promise with the supplied value
|
|
32
|
-
*/
|
|
33
|
-
done(value) {
|
|
34
|
-
(0, assert_1.assert)(this.isRunning);
|
|
35
|
-
this.isRunning = false;
|
|
36
|
-
this._resolve(value);
|
|
37
|
-
}
|
|
38
|
-
/**
|
|
39
|
-
* Call to reject the `result` Promise with the supplied error
|
|
40
|
-
*/
|
|
41
|
-
error(error) {
|
|
42
|
-
(0, assert_1.assert)(this.isRunning);
|
|
43
|
-
this.isRunning = false;
|
|
44
|
-
this._reject(error);
|
|
45
|
-
}
|
|
46
|
-
}
|
|
47
|
-
exports.default = WorkerJob;
|
|
@@ -1,164 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
-
};
|
|
5
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
const globals_1 = require("../env-utils/globals");
|
|
7
|
-
const worker_thread_1 = __importDefault(require("./worker-thread"));
|
|
8
|
-
const worker_job_1 = __importDefault(require("./worker-job"));
|
|
9
|
-
/**
|
|
10
|
-
* Process multiple data messages with small pool of identical workers
|
|
11
|
-
*/
|
|
12
|
-
class WorkerPool {
|
|
13
|
-
/** Checks if workers are supported on this platform */
|
|
14
|
-
static isSupported() {
|
|
15
|
-
return worker_thread_1.default.isSupported();
|
|
16
|
-
}
|
|
17
|
-
/**
|
|
18
|
-
* @param processor - worker function
|
|
19
|
-
* @param maxConcurrency - max count of workers
|
|
20
|
-
*/
|
|
21
|
-
constructor(props) {
|
|
22
|
-
this.name = 'unnamed';
|
|
23
|
-
this.maxConcurrency = 1;
|
|
24
|
-
this.maxMobileConcurrency = 1;
|
|
25
|
-
this.onDebug = () => { };
|
|
26
|
-
this.reuseWorkers = true;
|
|
27
|
-
this.props = {};
|
|
28
|
-
this.jobQueue = [];
|
|
29
|
-
this.idleQueue = [];
|
|
30
|
-
this.count = 0;
|
|
31
|
-
this.isDestroyed = false;
|
|
32
|
-
this.source = props.source;
|
|
33
|
-
this.url = props.url;
|
|
34
|
-
this.setProps(props);
|
|
35
|
-
}
|
|
36
|
-
/**
|
|
37
|
-
* Terminates all workers in the pool
|
|
38
|
-
* @note Can free up significant memory
|
|
39
|
-
*/
|
|
40
|
-
destroy() {
|
|
41
|
-
// Destroy idle workers, active Workers will be destroyed on completion
|
|
42
|
-
this.idleQueue.forEach((worker) => worker.destroy());
|
|
43
|
-
this.isDestroyed = true;
|
|
44
|
-
}
|
|
45
|
-
setProps(props) {
|
|
46
|
-
this.props = { ...this.props, ...props };
|
|
47
|
-
if (props.name !== undefined) {
|
|
48
|
-
this.name = props.name;
|
|
49
|
-
}
|
|
50
|
-
if (props.maxConcurrency !== undefined) {
|
|
51
|
-
this.maxConcurrency = props.maxConcurrency;
|
|
52
|
-
}
|
|
53
|
-
if (props.maxMobileConcurrency !== undefined) {
|
|
54
|
-
this.maxMobileConcurrency = props.maxMobileConcurrency;
|
|
55
|
-
}
|
|
56
|
-
if (props.reuseWorkers !== undefined) {
|
|
57
|
-
this.reuseWorkers = props.reuseWorkers;
|
|
58
|
-
}
|
|
59
|
-
if (props.onDebug !== undefined) {
|
|
60
|
-
this.onDebug = props.onDebug;
|
|
61
|
-
}
|
|
62
|
-
}
|
|
63
|
-
async startJob(name, onMessage = (job, type, data) => job.done(data), onError = (job, error) => job.error(error)) {
|
|
64
|
-
// Promise resolves when thread starts working on this job
|
|
65
|
-
const startPromise = new Promise((onStart) => {
|
|
66
|
-
// Promise resolves when thread completes or fails working on this job
|
|
67
|
-
this.jobQueue.push({ name, onMessage, onError, onStart });
|
|
68
|
-
return this;
|
|
69
|
-
});
|
|
70
|
-
this._startQueuedJob(); // eslint-disable-line @typescript-eslint/no-floating-promises
|
|
71
|
-
return await startPromise;
|
|
72
|
-
}
|
|
73
|
-
// PRIVATE
|
|
74
|
-
/**
|
|
75
|
-
* Starts first queued job if worker is available or can be created
|
|
76
|
-
* Called when job is started and whenever a worker returns to the idleQueue
|
|
77
|
-
*/
|
|
78
|
-
async _startQueuedJob() {
|
|
79
|
-
if (!this.jobQueue.length) {
|
|
80
|
-
return;
|
|
81
|
-
}
|
|
82
|
-
const workerThread = this._getAvailableWorker();
|
|
83
|
-
if (!workerThread) {
|
|
84
|
-
return;
|
|
85
|
-
}
|
|
86
|
-
// We have a worker, dequeue and start the job
|
|
87
|
-
const queuedJob = this.jobQueue.shift();
|
|
88
|
-
if (queuedJob) {
|
|
89
|
-
// Emit a debug event
|
|
90
|
-
// @ts-ignore
|
|
91
|
-
this.onDebug({
|
|
92
|
-
message: 'Starting job',
|
|
93
|
-
name: queuedJob.name,
|
|
94
|
-
workerThread,
|
|
95
|
-
backlog: this.jobQueue.length
|
|
96
|
-
});
|
|
97
|
-
// Create a worker job to let the app access thread and manage job completion
|
|
98
|
-
const job = new worker_job_1.default(queuedJob.name, workerThread);
|
|
99
|
-
// Set the worker thread's message handlers
|
|
100
|
-
workerThread.onMessage = (data) => queuedJob.onMessage(job, data.type, data.payload);
|
|
101
|
-
workerThread.onError = (error) => queuedJob.onError(job, error);
|
|
102
|
-
// Resolve the start promise so that the app can start sending messages to worker
|
|
103
|
-
queuedJob.onStart(job);
|
|
104
|
-
// Wait for the app to signal that the job is complete, then return worker to queue
|
|
105
|
-
try {
|
|
106
|
-
await job.result;
|
|
107
|
-
}
|
|
108
|
-
finally {
|
|
109
|
-
this.returnWorkerToQueue(workerThread);
|
|
110
|
-
}
|
|
111
|
-
}
|
|
112
|
-
}
|
|
113
|
-
/**
|
|
114
|
-
* Returns a worker to the idle queue
|
|
115
|
-
* Destroys the worker if
|
|
116
|
-
* - pool is destroyed
|
|
117
|
-
* - if this pool doesn't reuse workers
|
|
118
|
-
* - if maxConcurrency has been lowered
|
|
119
|
-
* @param worker
|
|
120
|
-
*/
|
|
121
|
-
returnWorkerToQueue(worker) {
|
|
122
|
-
const shouldDestroyWorker =
|
|
123
|
-
// Workers on Node.js prevent the process from exiting.
|
|
124
|
-
// Until we figure out how to close them before exit, we always destroy them
|
|
125
|
-
!globals_1.isBrowser ||
|
|
126
|
-
// If the pool is destroyed, there is no reason to keep the worker around
|
|
127
|
-
this.isDestroyed ||
|
|
128
|
-
// If the app has disabled worker reuse, any completed workers should be destroyed
|
|
129
|
-
!this.reuseWorkers ||
|
|
130
|
-
// If concurrency has been lowered, this worker might be surplus to requirements
|
|
131
|
-
this.count > this._getMaxConcurrency();
|
|
132
|
-
if (shouldDestroyWorker) {
|
|
133
|
-
worker.destroy();
|
|
134
|
-
this.count--;
|
|
135
|
-
}
|
|
136
|
-
else {
|
|
137
|
-
this.idleQueue.push(worker);
|
|
138
|
-
}
|
|
139
|
-
if (!this.isDestroyed) {
|
|
140
|
-
this._startQueuedJob(); // eslint-disable-line @typescript-eslint/no-floating-promises
|
|
141
|
-
}
|
|
142
|
-
}
|
|
143
|
-
/**
|
|
144
|
-
* Returns idle worker or creates new worker if maxConcurrency has not been reached
|
|
145
|
-
*/
|
|
146
|
-
_getAvailableWorker() {
|
|
147
|
-
// If a worker has completed and returned to the queue, it can be used
|
|
148
|
-
if (this.idleQueue.length > 0) {
|
|
149
|
-
return this.idleQueue.shift() || null;
|
|
150
|
-
}
|
|
151
|
-
// Create fresh worker if we haven't yet created the max amount of worker threads for this worker source
|
|
152
|
-
if (this.count < this._getMaxConcurrency()) {
|
|
153
|
-
this.count++;
|
|
154
|
-
const name = `${this.name.toLowerCase()} (#${this.count} of ${this.maxConcurrency})`;
|
|
155
|
-
return new worker_thread_1.default({ name, source: this.source, url: this.url });
|
|
156
|
-
}
|
|
157
|
-
// No worker available, have to wait
|
|
158
|
-
return null;
|
|
159
|
-
}
|
|
160
|
-
_getMaxConcurrency() {
|
|
161
|
-
return globals_1.isMobile ? this.maxMobileConcurrency : this.maxConcurrency;
|
|
162
|
-
}
|
|
163
|
-
}
|
|
164
|
-
exports.default = WorkerPool;
|