@loaders.gl/worker-utils 4.0.0-alpha.6 → 4.0.0-alpha.8
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 +2 -2
- package/dist/es5/lib/library-utils/library-utils.js +1 -1
- package/dist/es5/lib/worker-api/get-worker-url.js +3 -2
- package/dist/es5/lib/worker-api/get-worker-url.js.map +1 -1
- package/dist/es5/lib/worker-farm/worker-pool.js +1 -1
- package/dist/es5/lib/worker-farm/worker-pool.js.map +1 -1
- package/dist/esm/lib/env-utils/version.js +2 -2
- package/dist/esm/lib/library-utils/library-utils.js +1 -1
- package/dist/esm/lib/worker-api/get-worker-url.js +3 -2
- package/dist/esm/lib/worker-api/get-worker-url.js.map +1 -1
- package/dist/esm/lib/worker-farm/worker-pool.js +2 -2
- package/dist/esm/lib/worker-farm/worker-pool.js.map +1 -1
- package/dist/lib/worker-api/create-worker.js +1 -1
- package/dist/lib/worker-api/get-worker-url.d.ts.map +1 -1
- package/dist/lib/worker-api/get-worker-url.js +2 -1
- package/dist/lib/worker-farm/worker-pool.d.ts.map +1 -1
- package/dist/lib/worker-farm/worker-pool.js +10 -1
- package/dist/null-worker-node.js +244 -0
- package/dist/null-worker-node.js.map +7 -0
- package/package.json +5 -4
- package/src/lib/worker-api/get-worker-url.ts +2 -1
- package/src/lib/worker-farm/worker-pool.ts +10 -2
|
@@ -5,9 +5,9 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
5
5
|
});
|
|
6
6
|
exports.VERSION = void 0;
|
|
7
7
|
var DEFAULT_VERSION = 'beta';
|
|
8
|
-
var VERSION = typeof "4.0.0-alpha.
|
|
8
|
+
var VERSION = typeof "4.0.0-alpha.8" !== 'undefined' ? "4.0.0-alpha.8" : DEFAULT_VERSION;
|
|
9
9
|
exports.VERSION = VERSION;
|
|
10
|
-
if (typeof "4.0.0-alpha.
|
|
10
|
+
if (typeof "4.0.0-alpha.8" === 'undefined') {
|
|
11
11
|
console.error('loaders.gl: The __VERSION__ variable is not injected using babel plugin. Latest unstable workers would be fetched from the CDN.');
|
|
12
12
|
}
|
|
13
13
|
//# sourceMappingURL=version.js.map
|
|
@@ -16,7 +16,7 @@ var _version = require("../env-utils/version");
|
|
|
16
16
|
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
|
|
17
17
|
function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
|
|
18
18
|
var LATEST = 'beta';
|
|
19
|
-
var VERSION = typeof "4.0.0-alpha.
|
|
19
|
+
var VERSION = typeof "4.0.0-alpha.8" !== 'undefined' ? "4.0.0-alpha.8" : LATEST;
|
|
20
20
|
var loadLibraryPromises = {};
|
|
21
21
|
function loadLibrary(_x) {
|
|
22
22
|
return _loadLibrary.apply(this, arguments);
|
|
@@ -6,9 +6,10 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
6
6
|
exports.getWorkerName = getWorkerName;
|
|
7
7
|
exports.getWorkerURL = getWorkerURL;
|
|
8
8
|
var _assert = require("../env-utils/assert");
|
|
9
|
+
var _globals = require("../env-utils/globals");
|
|
9
10
|
var _version = require("../env-utils/version");
|
|
10
11
|
var NPM_TAG = 'beta';
|
|
11
|
-
var VERSION = typeof "4.0.0-alpha.
|
|
12
|
+
var VERSION = typeof "4.0.0-alpha.8" !== 'undefined' ? "4.0.0-alpha.8" : NPM_TAG;
|
|
12
13
|
function getWorkerName(worker) {
|
|
13
14
|
var warning = worker.version !== VERSION ? " (worker-utils@".concat(VERSION, ")") : '';
|
|
14
15
|
return "".concat(worker.name, "@").concat(worker.version).concat(warning);
|
|
@@ -16,7 +17,7 @@ function getWorkerName(worker) {
|
|
|
16
17
|
function getWorkerURL(worker) {
|
|
17
18
|
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
|
|
18
19
|
var workerOptions = options[worker.id] || {};
|
|
19
|
-
var workerFile = "".concat(worker.id, "-worker.js");
|
|
20
|
+
var workerFile = _globals.isBrowser ? "".concat(worker.id, "-worker.js") : "".concat(worker.id, "-worker-node.js");
|
|
20
21
|
var url = workerOptions.workerUrl;
|
|
21
22
|
if (!url && worker.id === 'compression') {
|
|
22
23
|
url = options.workerUrl;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"get-worker-url.js","names":["_assert","require","_version","NPM_TAG","VERSION","getWorkerName","worker","warning","version","concat","name","getWorkerURL","options","arguments","length","undefined","workerOptions","id","workerFile","url","workerUrl","_workerType","module","versionTag","assert"],"sources":["../../../../src/lib/worker-api/get-worker-url.ts"],"sourcesContent":["// loaders.gl, MIT license\n\nimport type {WorkerObject, WorkerOptions} from '../../types';\nimport {assert} from '../env-utils/assert';\nimport {VERSION as __VERSION__} from '../env-utils/version';\n\nconst NPM_TAG = 'beta'; // 'beta', or 'latest' on release-branch\nconst VERSION = typeof __VERSION__ !== 'undefined' ? __VERSION__ : NPM_TAG;\n\n/**\n * Gets worker object's name (for debugging in Chrome thread inspector window)\n */\nexport function getWorkerName(worker: WorkerObject): string {\n const warning = worker.version !== VERSION ? ` (worker-utils@${VERSION})` : '';\n return `${worker.name}@${worker.version}${warning}`;\n}\n\n/**\n * Generate a worker URL based on worker object and options\n * @returns A URL to one of the following:\n * - a published worker on unpkg CDN\n * - a local test worker\n * - a URL provided by the user in options\n */\nexport function getWorkerURL(worker: WorkerObject, options: WorkerOptions = {}): string {\n const workerOptions = options[worker.id] || {};\n\n const workerFile = `${worker.id}-worker.js`;\n\n let url = workerOptions.workerUrl;\n\n // HACK: Allow for non-nested workerUrl for the CompressionWorker.\n // For the compression worker, workerOptions is currently not nested correctly. For most loaders,\n // you'd have options within an object, i.e. `{mvt: {coordinates: ...}}` but the CompressionWorker\n // puts options at the top level, not within a `compression` key (its `id`). For this reason, the\n // above `workerOptions` will always be a string (i.e. `'gzip'`) for the CompressionWorker. To not\n // break backwards compatibility, we allow the CompressionWorker to have options at the top level.\n if (!url && worker.id === 'compression') {\n url = options.workerUrl;\n }\n\n // If URL is test, generate local loaders.gl url\n // @ts-ignore _workerType\n if (options._workerType === 'test') {\n url = `modules/${worker.module}/dist/${workerFile}`;\n }\n\n // If url override is not provided, generate a URL to published version on npm CDN unpkg.com\n if (!url) {\n // GENERATE\n let version = worker.version;\n // On master we need to load npm alpha releases published with the `beta` tag\n if (version === 'latest') {\n // throw new Error('latest worker version specified');\n version = NPM_TAG;\n }\n const versionTag = version ? `@${version}` : '';\n url = `https://unpkg.com/@loaders.gl/${worker.module}${versionTag}/dist/${workerFile}`;\n }\n\n assert(url);\n\n // Allow user to override location\n return url;\n}\n"],"mappings":";;;;;;;AAGA,IAAAA,OAAA,GAAAC,OAAA;AACA,IAAAC,QAAA,GAAAD,OAAA;AAEA,
|
|
1
|
+
{"version":3,"file":"get-worker-url.js","names":["_assert","require","_globals","_version","NPM_TAG","VERSION","getWorkerName","worker","warning","version","concat","name","getWorkerURL","options","arguments","length","undefined","workerOptions","id","workerFile","isBrowser","url","workerUrl","_workerType","module","versionTag","assert"],"sources":["../../../../src/lib/worker-api/get-worker-url.ts"],"sourcesContent":["// loaders.gl, MIT license\n\nimport type {WorkerObject, WorkerOptions} from '../../types';\nimport {assert} from '../env-utils/assert';\nimport {isBrowser} from '../env-utils/globals';\nimport {VERSION as __VERSION__} from '../env-utils/version';\n\nconst NPM_TAG = 'beta'; // 'beta', or 'latest' on release-branch\nconst VERSION = typeof __VERSION__ !== 'undefined' ? __VERSION__ : NPM_TAG;\n\n/**\n * Gets worker object's name (for debugging in Chrome thread inspector window)\n */\nexport function getWorkerName(worker: WorkerObject): string {\n const warning = worker.version !== VERSION ? ` (worker-utils@${VERSION})` : '';\n return `${worker.name}@${worker.version}${warning}`;\n}\n\n/**\n * Generate a worker URL based on worker object and options\n * @returns A URL to one of the following:\n * - a published worker on unpkg CDN\n * - a local test worker\n * - a URL provided by the user in options\n */\nexport function getWorkerURL(worker: WorkerObject, options: WorkerOptions = {}): string {\n const workerOptions = options[worker.id] || {};\n\n const workerFile = isBrowser ? `${worker.id}-worker.js` : `${worker.id}-worker-node.js`;\n\n let url = workerOptions.workerUrl;\n\n // HACK: Allow for non-nested workerUrl for the CompressionWorker.\n // For the compression worker, workerOptions is currently not nested correctly. For most loaders,\n // you'd have options within an object, i.e. `{mvt: {coordinates: ...}}` but the CompressionWorker\n // puts options at the top level, not within a `compression` key (its `id`). For this reason, the\n // above `workerOptions` will always be a string (i.e. `'gzip'`) for the CompressionWorker. To not\n // break backwards compatibility, we allow the CompressionWorker to have options at the top level.\n if (!url && worker.id === 'compression') {\n url = options.workerUrl;\n }\n\n // If URL is test, generate local loaders.gl url\n // @ts-ignore _workerType\n if (options._workerType === 'test') {\n url = `modules/${worker.module}/dist/${workerFile}`;\n }\n\n // If url override is not provided, generate a URL to published version on npm CDN unpkg.com\n if (!url) {\n // GENERATE\n let version = worker.version;\n // On master we need to load npm alpha releases published with the `beta` tag\n if (version === 'latest') {\n // throw new Error('latest worker version specified');\n version = NPM_TAG;\n }\n const versionTag = version ? `@${version}` : '';\n url = `https://unpkg.com/@loaders.gl/${worker.module}${versionTag}/dist/${workerFile}`;\n }\n\n assert(url);\n\n //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AQ,EAAE;MAExBA,OAAO,GAAGL,OAAO;IACnB;IACA,IAAMqB,UAAU,GAAGhB,OAAO,OAAAC,MAAA,CAAOD,OAAO,IAAK,EAAE;IAC/CY,GAAG,oCAAAX,MAAA,CAAoCH,MAAM,CAACiB,MAAM,EAAAd,MAAA,CAAGe,UAAU,YAAAf,MAAA,CAASS,UAAU,CAAE;EACxF;EAEA,IAAAO,cAAM,EAACL,GAAG,CAAC;EAGX,OAAOA,GAAG;AACZ"}
|
|
@@ -166,7 +166,7 @@ var WorkerPool = function () {
|
|
|
166
166
|
}, {
|
|
167
167
|
key: "returnWorkerToQueue",
|
|
168
168
|
value: function returnWorkerToQueue(worker) {
|
|
169
|
-
var shouldDestroyWorker = this.isDestroyed || !this.reuseWorkers || this.count > this._getMaxConcurrency();
|
|
169
|
+
var shouldDestroyWorker = !_globals.isBrowser || this.isDestroyed || !this.reuseWorkers || this.count > this._getMaxConcurrency();
|
|
170
170
|
if (shouldDestroyWorker) {
|
|
171
171
|
worker.destroy();
|
|
172
172
|
this.count--;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"worker-pool.js","names":["_globals","require","_workerThread","_interopRequireDefault","_workerJob","ownKeys","object","enumerableOnly","keys","Object","getOwnPropertySymbols","symbols","filter","sym","getOwnPropertyDescriptor","enumerable","push","apply","_objectSpread","target","i","arguments","length","source","forEach","key","_defineProperty2","default","getOwnPropertyDescriptors","defineProperties","defineProperty","WorkerPool","props","_classCallCheck2","url","setProps","_createClass2","value","destroy","idleQueue","worker","isDestroyed","name","undefined","maxConcurrency","maxMobileConcurrency","reuseWorkers","onDebug","_startJob","_asyncToGenerator2","_regenerator","mark","_callee","_this","onMessage","onError","startPromise","_args","wrap","_callee$","_context","prev","next","job","type","data","done","error","Promise","onStart","jobQueue","_startQueuedJob","abrupt","sent","stop","startJob","_x","_startQueuedJob2","_callee2","workerThread","queuedJob","_job","_callee2$","_context2","_getAvailableWorker","shift","message","backlog","WorkerJob","payload","result","returnWorkerToQueue","finish","shouldDestroyWorker","count","_getMaxConcurrency","concat","toLowerCase","WorkerThread","isMobile","isSupported","exports"],"sources":["../../../../src/lib/worker-farm/worker-pool.ts"],"sourcesContent":["import type {WorkerMessageType, WorkerMessagePayload} from '../../types';\nimport {isMobile} from '../env-utils/globals';\nimport WorkerThread from './worker-thread';\nimport WorkerJob from './worker-job';\n\n/** WorkerPool onDebug Callback Parameters */\ntype OnDebugParameters = {\n message: string;\n worker: string;\n name: string;\n job: string;\n backlog: number;\n workerThread: WorkerThread;\n};\n\n/** WorkerPool Properties */\nexport type WorkerPoolProps = {\n name?: string;\n source?: string; // | Function;\n url?: string;\n maxConcurrency?: number;\n maxMobileConcurrency?: number;\n onDebug?: (options: OnDebugParameters) => any;\n reuseWorkers?: boolean;\n};\n\n/** Private helper types */\ntype OnMessage = (job: WorkerJob, type: WorkerMessageType, payload: WorkerMessagePayload) => void;\ntype OnError = (job: WorkerJob, error: Error) => void;\n\ntype QueuedJob = {\n name: string;\n onMessage: OnMessage;\n onError: OnError;\n onStart: (value: any) => void; // Resolve job start promise\n};\n\n/**\n * Process multiple data messages with small pool of identical workers\n */\nexport default class WorkerPool {\n name: string = 'unnamed';\n source?: string; // | Function;\n url?: string;\n maxConcurrency: number = 1;\n maxMobileConcurrency: number = 1;\n onDebug: (options: OnDebugParameters) => any = () => {};\n reuseWorkers: boolean = true;\n\n private props: WorkerPoolProps = {};\n private jobQueue: QueuedJob[] = [];\n private idleQueue: WorkerThread[] = [];\n private count = 0;\n private isDestroyed = false;\n\n /** Checks if workers are supported on this platform */\n static isSupported(): boolean {\n return WorkerThread.isSupported();\n }\n\n /**\n * @param processor - worker function\n * @param maxConcurrency - max count of workers\n */\n constructor(props: WorkerPoolProps) {\n this.source = props.source;\n this.url = props.url;\n this.setProps(props);\n }\n\n /**\n * Terminates all workers in the pool\n * @note Can free up significant memory\n */\n destroy(): void {\n // Destroy idle workers, active Workers will be destroyed on completion\n this.idleQueue.forEach((worker) => worker.destroy());\n this.isDestroyed = true;\n }\n\n setProps(props: WorkerPoolProps) {\n this.props = {...this.props, ...props};\n\n if (props.name !== undefined) {\n this.name = props.name;\n }\n if (props.maxConcurrency !== undefined) {\n this.maxConcurrency = props.maxConcurrency;\n }\n if (props.maxMobileConcurrency !== undefined) {\n this.maxMobileConcurrency = props.maxMobileConcurrency;\n }\n if (props.reuseWorkers !== undefined) {\n this.reuseWorkers = props.reuseWorkers;\n }\n if (props.onDebug !== undefined) {\n this.onDebug = props.onDebug;\n }\n }\n\n async startJob(\n name: string,\n onMessage: OnMessage = (job, type, data) => job.done(data),\n onError: OnError = (job, error) => job.error(error)\n ): Promise<WorkerJob> {\n // Promise resolves when thread starts working on this job\n const startPromise = new Promise<WorkerJob>((onStart) => {\n // Promise resolves when thread completes or fails working on this job\n this.jobQueue.push({name, onMessage, onError, onStart});\n return this;\n });\n this._startQueuedJob(); // eslint-disable-line @typescript-eslint/no-floating-promises\n return await startPromise;\n }\n\n // PRIVATE\n\n /**\n * Starts first queued job if worker is available or can be created\n * Called when job is started and whenever a worker returns to the idleQueue\n */\n async _startQueuedJob(): Promise<void> {\n if (!this.jobQueue.length) {\n return;\n }\n\n const workerThread = this._getAvailableWorker();\n if (!workerThread) {\n return;\n }\n\n // We have a worker, dequeue and start the job\n const queuedJob = this.jobQueue.shift();\n if (queuedJob) {\n // Emit a debug event\n // @ts-ignore\n this.onDebug({\n message: 'Starting job',\n name: queuedJob.name,\n workerThread,\n backlog: this.jobQueue.length\n });\n\n // Create a worker job to let the app access thread and manage job completion\n const job = new WorkerJob(queuedJob.name, workerThread);\n\n // Set the worker thread's message handlers\n workerThread.onMessage = (data) => queuedJob.onMessage(job, data.type, data.payload);\n workerThread.onError = (error) => queuedJob.onError(job, error);\n\n // Resolve the start promise so that the app can start sending messages to worker\n queuedJob.onStart(job);\n\n // Wait for the app to signal that the job is complete, then return worker to queue\n try {\n await job.result;\n } finally {\n this.returnWorkerToQueue(workerThread);\n }\n }\n }\n\n /**\n * Returns a worker to the idle queue\n * Destroys the worker if\n * - pool is destroyed\n * - if this pool doesn't reuse workers\n * - if maxConcurrency has been lowered\n * @param worker\n */\n returnWorkerToQueue(worker: WorkerThread) {\n const shouldDestroyWorker =\n this.isDestroyed || !this.reuseWorkers || this.count > this._getMaxConcurrency();\n\n if (shouldDestroyWorker) {\n worker.destroy();\n this.count--;\n } else {\n this.idleQueue.push(worker);\n }\n\n if (!this.isDestroyed) {\n this._startQueuedJob(); // eslint-disable-line @typescript-eslint/no-floating-promises\n }\n }\n\n /**\n * Returns idle worker or creates new worker if maxConcurrency has not been reached\n */\n _getAvailableWorker(): WorkerThread | null {\n // If a worker has completed and returned to the queue, it can be used\n if (this.idleQueue.length > 0) {\n return this.idleQueue.shift() || null;\n }\n\n // Create fresh worker if we haven't yet created the max amount of worker threads for this worker source\n if (this.count < this._getMaxConcurrency()) {\n this.count++;\n const name = `${this.name.toLowerCase()} (#${this.count} of ${this.maxConcurrency})`;\n return new WorkerThread({name, source: this.source, url: this.url});\n }\n\n // No worker available, have to wait\n return null;\n }\n\n _getMaxConcurrency() {\n return isMobile ? this.maxMobileConcurrency : this.maxConcurrency;\n }\n}\n"],"mappings":";;;;;;;;;;;;AACA,IAAAA,QAAA,GAAAC,OAAA;AACA,IAAAC,aAAA,GAAAC,sBAAA,CAAAF,OAAA;AACA,IAAAG,UAAA,GAAAD,sBAAA,CAAAF,OAAA;AAAqC,SAAAI,QAAAC,MAAA,EAAAC,cAAA,QAAAC,IAAA,GAAAC,MAAA,CAAAD,IAAA,CAAAF,MAAA,OAAAG,MAAA,CAAAC,qBAAA,QAAAC,OAAA,GAAAF,MAAA,CAAAC,qBAAA,CAAAJ,MAAA,GAAAC,cAAA,KAAAI,OAAA,GAAAA,OAAA,CAAAC,MAAA,WAAAC,GAAA,WAAAJ,MAAA,CAAAK,wBAAA,CAAAR,MAAA,EAAAO,GAAA,EAAAE,UAAA,OAAAP,IAAA,CAAAQ,IAAA,CAAAC,KAAA,CAAAT,IAA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AAI;MACvC;MAGA,IAAI,IAAI,CAACS,KAAK,GAAG,IAAI,CAACC,kBAAkB,CAAC,CAAC,EAAE;QAC1C,IAAI,CAACD,KAAK,EAAE;QACZ,IAAMpD,IAAI,MAAAsD,MAAA,CAAM,IAAI,CAACtD,IAAI,CAACuD,WAAW,CAAC,CAAC,SAAAD,MAAA,CAAM,IAAI,CAACF,KAAK,UAAAE,MAAA,CAAO,IAAI,CAACpD,cAAc,MAAG;QACpF,OAAO,IAAIsD,qBAAY,CAAC;UAACxD,IAAI,EAAJA,IAAI;UAAEnB,MAAM,EAAE,IAAI,CAACA,MAAM;UAAEW,GAAG,EAAE,IAAI,CAACA;QAAG,CAAC,CAAC;MACrE;MAGA,OAAO,IAAI;IACb;EAAC;IAAAT,GAAA;IAAAY,KAAA,EAED,SAAA0D,mBAAA,EAAqB;MACnB,OAAOI,iBAAQ,GAAG,IAAI,CAACtD,oBAAoB,GAAG,IAAI,CAACD,cAAc;IACnE;EAAC;IAAAnB,GAAA;IAAAY,KAAA,EAxJD,SAAA+D,YAAA,EAA8B;MAC5B,OAAOF,qBAAY,CAACE,WAAW,CAAC,CAAC;IACnC;EAAC;EAAA,OAAArE,UAAA;AAAA;AAAAsE,OAAA,CAAA1E,OAAA,GAAAI,UAAA"}
|
|
1
|
+
{"version":3,"file":"worker-pool.js","names":["_globals","require","_workerThread","_interopRequireDefault","_workerJob","ownKeys","object","enumerableOnly","keys","Object","getOwnPropertySymbols","symbols","filter","sym","getOwnPropertyDescriptor","enumerable","push","apply","_objectSpread","target","i","arguments","length","source","forEach","key","_defineProperty2","default","getOwnPropertyDescriptors","defineProperties","defineProperty","WorkerPool","props","_classCallCheck2","url","setProps","_createClass2","value","destroy","idleQueue","worker","isDestroyed","name","undefined","maxConcurrency","maxMobileConcurrency","reuseWorkers","onDebug","_startJob","_asyncToGenerator2","_regenerator","mark","_callee","_this","onMessage","onError","startPromise","_args","wrap","_callee$","_context","prev","next","job","type","data","done","error","Promise","onStart","jobQueue","_startQueuedJob","abrupt","sent","stop","startJob","_x","_startQueuedJob2","_callee2","workerThread","queuedJob","_job","_callee2$","_context2","_getAvailableWorker","shift","message","backlog","WorkerJob","payload","result","returnWorkerToQueue","finish","shouldDestroyWorker","isBrowser","count","_getMaxConcurrency","concat","toLowerCase","WorkerThread","isMobile","isSupported","exports"],"sources":["../../../../src/lib/worker-farm/worker-pool.ts"],"sourcesContent":["import type {WorkerMessageType, WorkerMessagePayload} from '../../types';\nimport {isMobile, isBrowser} from '../env-utils/globals';\nimport WorkerThread from './worker-thread';\nimport WorkerJob from './worker-job';\n\n/** WorkerPool onDebug Callback Parameters */\ntype OnDebugParameters = {\n message: string;\n worker: string;\n name: string;\n job: string;\n backlog: number;\n workerThread: WorkerThread;\n};\n\n/** WorkerPool Properties */\nexport type WorkerPoolProps = {\n name?: string;\n source?: string; // | Function;\n url?: string;\n maxConcurrency?: number;\n maxMobileConcurrency?: number;\n onDebug?: (options: OnDebugParameters) => any;\n reuseWorkers?: boolean;\n};\n\n/** Private helper types */\ntype OnMessage = (job: WorkerJob, type: WorkerMessageType, payload: WorkerMessagePayload) => void;\ntype OnError = (job: WorkerJob, error: Error) => void;\n\ntype QueuedJob = {\n name: string;\n onMessage: OnMessage;\n onError: OnError;\n onStart: (value: any) => void; // Resolve job start promise\n};\n\n/**\n * Process multiple data messages with small pool of identical workers\n */\nexport default class WorkerPool {\n name: string = 'unnamed';\n source?: string; // | Function;\n url?: string;\n maxConcurrency: number = 1;\n maxMobileConcurrency: number = 1;\n onDebug: (options: OnDebugParameters) => any = () => {};\n reuseWorkers: boolean = true;\n\n private props: WorkerPoolProps = {};\n private jobQueue: QueuedJob[] = [];\n private idleQueue: WorkerThread[] = [];\n private count = 0;\n private isDestroyed = false;\n\n /** Checks if workers are supported on this platform */\n static isSupported(): boolean {\n return WorkerThread.isSupported();\n }\n\n /**\n * @param processor - worker function\n * @param maxConcurrency - max count of workers\n */\n constructor(props: WorkerPoolProps) {\n this.source = props.source;\n this.url = props.url;\n this.setProps(props);\n }\n\n /**\n * Terminates all workers in the pool\n * @note Can free up significant memory\n */\n destroy(): void {\n // Destroy idle workers, active Workers will be destroyed on completion\n this.idleQueue.forEach((worker) => worker.destroy());\n this.isDestroyed = true;\n }\n\n setProps(props: WorkerPoolProps) {\n this.props = {...this.props, ...props};\n\n if (props.name !== undefined) {\n this.name = props.name;\n }\n if (props.maxConcurrency !== undefined) {\n this.maxConcurrency = props.maxConcurrency;\n }\n if (props.maxMobileConcurrency !== undefined) {\n this.maxMobileConcurrency = props.maxMobileConcurrency;\n }\n if (props.reuseWorkers !== undefined) {\n this.reuseWorkers = props.reuseWorkers;\n }\n if (props.onDebug !== undefined) {\n this.onDebug = props.onDebug;\n }\n }\n\n async startJob(\n name: string,\n onMessage: OnMessage = (job, type, data) => job.done(data),\n onError: OnError = (job, error) => job.error(error)\n ): Promise<WorkerJob> {\n // Promise resolves when thread starts working on this job\n const startPromise = new Promise<WorkerJob>((onStart) => {\n // Promise resolves when thread completes or fails working on this job\n this.jobQueue.push({name, onMessage, onError, onStart});\n return this;\n });\n this._startQueuedJob(); // eslint-disable-line @typescript-eslint/no-floating-promises\n return await startPromise;\n }\n\n // PRIVATE\n\n /**\n * Starts first queued job if worker is available or can be created\n * Called when job is started and whenever a worker returns to the idleQueue\n */\n async _startQueuedJob(): Promise<void> {\n if (!this.jobQueue.length) {\n return;\n }\n\n const workerThread = this._getAvailableWorker();\n if (!workerThread) {\n return;\n }\n\n // We have a worker, dequeue and start the job\n const queuedJob = this.jobQueue.shift();\n if (queuedJob) {\n // Emit a debug event\n // @ts-ignore\n this.onDebug({\n message: 'Starting job',\n name: queuedJob.name,\n workerThread,\n backlog: this.jobQueue.length\n });\n\n // Create a worker job to let the app access thread and manage job completion\n const job = new WorkerJob(queuedJob.name, workerThread);\n\n // Set the worker thread's message handlers\n workerThread.onMessage = (data) => queuedJob.onMessage(job, data.type, data.payload);\n workerThread.onError = (error) => queuedJob.onError(job, error);\n\n // Resolve the start promise so that the app can start sending messages to worker\n queuedJob.onStart(job);\n\n // Wait for the app to signal that the job is complete, then return worker to queue\n try {\n await job.result;\n } finally {\n this.returnWorkerToQueue(workerThread);\n }\n }\n }\n\n /**\n * Returns a worker to the idle queue\n * Destroys the worker if\n * - pool is destroyed\n * - if this pool doesn't reuse workers\n * - if maxConcurrency has been lowered\n * @param worker\n */\n returnWorkerToQueue(worker: WorkerThread) {\n const shouldDestroyWorker =\n // Workers on Node.js prevent the process from exiting.\n // Until we figure out how to close them before exit, we always destroy them\n !isBrowser ||\n // If the pool is destroyed, there is no reason to keep the worker around\n this.isDestroyed ||\n // If the app has disabled worker reuse, any completed workers should be destroyed\n !this.reuseWorkers ||\n // If concurrency has been lowered, this worker might be surplus to requirements\n this.count > this._getMaxConcurrency();\n\n if (shouldDestroyWorker) {\n worker.destroy();\n this.count--;\n } else {\n this.idleQueue.push(worker);\n }\n\n if (!this.isDestroyed) {\n this._startQueuedJob(); // eslint-disable-line @typescript-eslint/no-floating-promises\n }\n }\n\n /**\n * Returns idle worker or creates new worker if maxConcurrency has not been reached\n */\n _getAvailableWorker(): WorkerThread | null {\n // If a worker has completed and returned to the queue, it can be used\n if (this.idleQueue.length > 0) {\n return this.idleQueue.shift() || null;\n }\n\n // Create fresh worker if we haven't yet created the max amount of worker threads for this worker source\n if (this.count < this._getMaxConcurrency()) {\n this.count++;\n const name = `${this.name.toLowerCase()} (#${this.count} of ${this.maxConcurrency})`;\n return new WorkerThread({name, source: this.source, url: this.url});\n }\n\n // No worker available, have to wait\n return null;\n }\n\n _getMaxConcurrency() {\n return isMobile ? this.maxMobileConcurrency : this.maxConcurrency;\n }\n}\n"],"mappings":";;;;;;;;;;;;AACA,IAAAA,QAAA,GAAAC,OAAA;AACA,IAAAC,aAAA,GAAAC,sBAAA,CAAAF,OAAA;AACA,IAAAG,UAAA,GAAAD,sBAAA,CAAAF,OAAA;AAAqC,SAAAI,QAAAC,MAAA,EAAAC,cAAA,QAAAC,IAAA,GAAAC,MA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4.0.0-alpha.
|
|
2
|
+
export const VERSION = typeof "4.0.0-alpha.8" !== 'undefined' ? "4.0.0-alpha.8" : DEFAULT_VERSION;
|
|
3
|
+
if (typeof "4.0.0-alpha.8" === 'undefined') {
|
|
4
4
|
console.error('loaders.gl: The __VERSION__ variable is not injected using babel plugin. Latest unstable workers would be fetched from the CDN.');
|
|
5
5
|
}
|
|
6
6
|
//# sourceMappingURL=version.js.map
|
|
@@ -3,7 +3,7 @@ import * as node from '../node/require-utils.node';
|
|
|
3
3
|
import { assert } from '../env-utils/assert';
|
|
4
4
|
import { VERSION as __VERSION__ } from '../env-utils/version';
|
|
5
5
|
const LATEST = 'beta';
|
|
6
|
-
const VERSION = typeof "4.0.0-alpha.
|
|
6
|
+
const VERSION = typeof "4.0.0-alpha.8" !== 'undefined' ? "4.0.0-alpha.8" : LATEST;
|
|
7
7
|
const loadLibraryPromises = {};
|
|
8
8
|
export async function loadLibrary(libraryUrl) {
|
|
9
9
|
let moduleName = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : null;
|
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
import { assert } from '../env-utils/assert';
|
|
2
|
+
import { isBrowser } from '../env-utils/globals';
|
|
2
3
|
import { VERSION as __VERSION__ } from '../env-utils/version';
|
|
3
4
|
const NPM_TAG = 'beta';
|
|
4
|
-
const VERSION = typeof "4.0.0-alpha.
|
|
5
|
+
const VERSION = typeof "4.0.0-alpha.8" !== 'undefined' ? "4.0.0-alpha.8" : NPM_TAG;
|
|
5
6
|
export function getWorkerName(worker) {
|
|
6
7
|
const warning = worker.version !== VERSION ? " (worker-utils@".concat(VERSION, ")") : '';
|
|
7
8
|
return "".concat(worker.name, "@").concat(worker.version).concat(warning);
|
|
@@ -9,7 +10,7 @@ export function getWorkerName(worker) {
|
|
|
9
10
|
export function getWorkerURL(worker) {
|
|
10
11
|
let options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
|
|
11
12
|
const workerOptions = options[worker.id] || {};
|
|
12
|
-
const workerFile = "".concat(worker.id, "-worker.js");
|
|
13
|
+
const workerFile = isBrowser ? "".concat(worker.id, "-worker.js") : "".concat(worker.id, "-worker-node.js");
|
|
13
14
|
let url = workerOptions.workerUrl;
|
|
14
15
|
if (!url && worker.id === 'compression') {
|
|
15
16
|
url = options.workerUrl;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"get-worker-url.js","names":["assert","VERSION","__VERSION__","NPM_TAG","getWorkerName","worker","warning","version","concat","name","getWorkerURL","options","arguments","length","undefined","workerOptions","id","workerFile","url","workerUrl","_workerType","module","versionTag"],"sources":["../../../../src/lib/worker-api/get-worker-url.ts"],"sourcesContent":["// loaders.gl, MIT license\n\nimport type {WorkerObject, WorkerOptions} from '../../types';\nimport {assert} from '../env-utils/assert';\nimport {VERSION as __VERSION__} from '../env-utils/version';\n\nconst NPM_TAG = 'beta'; // 'beta', or 'latest' on release-branch\nconst VERSION = typeof __VERSION__ !== 'undefined' ? __VERSION__ : NPM_TAG;\n\n/**\n * Gets worker object's name (for debugging in Chrome thread inspector window)\n */\nexport function getWorkerName(worker: WorkerObject): string {\n const warning = worker.version !== VERSION ? ` (worker-utils@${VERSION})` : '';\n return `${worker.name}@${worker.version}${warning}`;\n}\n\n/**\n * Generate a worker URL based on worker object and options\n * @returns A URL to one of the following:\n * - a published worker on unpkg CDN\n * - a local test worker\n * - a URL provided by the user in options\n */\nexport function getWorkerURL(worker: WorkerObject, options: WorkerOptions = {}): string {\n const workerOptions = options[worker.id] || {};\n\n const workerFile = `${worker.id}-worker.js`;\n\n let url = workerOptions.workerUrl;\n\n // HACK: Allow for non-nested workerUrl for the CompressionWorker.\n // For the compression worker, workerOptions is currently not nested correctly. For most loaders,\n // you'd have options within an object, i.e. `{mvt: {coordinates: ...}}` but the CompressionWorker\n // puts options at the top level, not within a `compression` key (its `id`). For this reason, the\n // above `workerOptions` will always be a string (i.e. `'gzip'`) for the CompressionWorker. To not\n // break backwards compatibility, we allow the CompressionWorker to have options at the top level.\n if (!url && worker.id === 'compression') {\n url = options.workerUrl;\n }\n\n // If URL is test, generate local loaders.gl url\n // @ts-ignore _workerType\n if (options._workerType === 'test') {\n url = `modules/${worker.module}/dist/${workerFile}`;\n }\n\n // If url override is not provided, generate a URL to published version on npm CDN unpkg.com\n if (!url) {\n // GENERATE\n let version = worker.version;\n // On master we need to load npm alpha releases published with the `beta` tag\n if (version === 'latest') {\n // throw new Error('latest worker version specified');\n version = NPM_TAG;\n }\n const versionTag = version ? `@${version}` : '';\n url = `https://unpkg.com/@loaders.gl/${worker.module}${versionTag}/dist/${workerFile}`;\n }\n\n assert(url);\n\n // Allow user to override location\n return url;\n}\n"],"mappings":"AAGA,SAAQA,MAAM,QAAO,qBAAqB;AAC1C,SAAQC,OAAO,IAAIC,WAAW,QAAO,sBAAsB;AAE3D,MAAMC,OAAO,GAAG,MAAM;AACtB,MAAMF,OAAO,GAAG,sBAAkB,KAAK,WAAW,qBAAiBE,OAAO;AAK1E,OAAO,SAASC,aAAaA,CAACC,MAAoB,EAAU;EAC1D,MAAMC,OAAO,GAAGD,MAAM,CAACE,OAAO,KAAKN,OAAO,qBAAAO,MAAA,CAAqBP,OAAO,SAAM,EAAE;EAC9E,UAAAO,MAAA,CAAUH,MAAM,CAACI,IAAI,OAAAD,MAAA,CAAIH,MAAM,CAACE,OAAO,EAAAC,MAAA,CAAGF,OAAO;AACnD;AASA,OAAO,SAASI,YAAYA,CAACL,MAAoB,EAAuC;EAAA,IAArCM,OAAsB,GAAAC,SAAA,CAAAC,MAAA,QAAAD,SAAA,QAAAE,SAAA,GAAAF,SAAA,MAAG,CAAC,CAAC;EAC5E,MAAMG,aAAa,GAAGJ,OAAO,CAACN,MAAM,CAACW,EAAE,CAAC,IAAI,CAAC,CAAC;EAE9C,MAAMC,UAAU,
|
|
1
|
+
{"version":3,"file":"get-worker-url.js","names":["assert","isBrowser","VERSION","__VERSION__","NPM_TAG","getWorkerName","worker","warning","version","concat","name","getWorkerURL","options","arguments","length","undefined","workerOptions","id","workerFile","url","workerUrl","_workerType","module","versionTag"],"sources":["../../../../src/lib/worker-api/get-worker-url.ts"],"sourcesContent":["// loaders.gl, MIT license\n\nimport type {WorkerObject, WorkerOptions} from '../../types';\nimport {assert} from '../env-utils/assert';\nimport {isBrowser} from '../env-utils/globals';\nimport {VERSION as __VERSION__} from '../env-utils/version';\n\nconst NPM_TAG = 'beta'; // 'beta', or 'latest' on release-branch\nconst VERSION = typeof __VERSION__ !== 'undefined' ? __VERSION__ : NPM_TAG;\n\n/**\n * Gets worker object's name (for debugging in Chrome thread inspector window)\n */\nexport function getWorkerName(worker: WorkerObject): string {\n const warning = worker.version !== VERSION ? ` (worker-utils@${VERSION})` : '';\n return `${worker.name}@${worker.version}${warning}`;\n}\n\n/**\n * Generate a worker URL based on worker object and options\n * @returns A URL to one of the following:\n * - a published worker on unpkg CDN\n * - a local test worker\n * - a URL provided by the user in options\n */\nexport function getWorkerURL(worker: WorkerObject, options: WorkerOptions = {}): string {\n const workerOptions = options[worker.id] || {};\n\n const workerFile = isBrowser ? `${worker.id}-worker.js` : `${worker.id}-worker-node.js`;\n\n let url = workerOptions.workerUrl;\n\n // HACK: Allow for non-nested workerUrl for the CompressionWorker.\n // For the compression worker, workerOptions is currently not nested correctly. For most loaders,\n // you'd have options within an object, i.e. `{mvt: {coordinates: ...}}` but the CompressionWorker\n // puts options at the top level, not within a `compression` key (its `id`). For this reason, the\n // above `workerOptions` will always be a string (i.e. `'gzip'`) for the CompressionWorker. To not\n // break backwards compatibility, we allow the CompressionWorker to have options at the top level.\n if (!url && worker.id === 'compression') {\n url = options.workerUrl;\n }\n\n // If URL is test, generate local loaders.gl url\n // @ts-ignore _workerType\n if (options._workerType === 'test') {\n url = `modules/${worker.module}/dist/${workerFile}`;\n }\n\n // If url override is not provided, generate a URL to published version on npm CDN unpkg.com\n if (!url) {\n // GENERATE\n let version = worker.version;\n // On master we need to load npm alpha releases published with the `beta` tag\n if (version === 'latest') {\n // throw new Error('latest worker version specified');\n version = NPM_TAG;\n }\n const versionTag = version ? `@${version}` : '';\n url = `https://unpkg.com/@loaders.gl/${worker.module}${versionTag}/dist/${workerFile}`;\n }\n\n assert(url);\n\n // Allow user to override location\n return url;\n}\n"],"mappings":"AAGA,SAAQA,MAAM,QAAO,qBAAqB;AAC1C,SAAQC,SAAS,QAAO,sBAAsB;AAC9C,SAAQC,OAAO,IAAIC,WAAW,QAAO,sBAAsB;AAE3D,MAAMC,OAAO,GAAG,MAAM;AACtB,MAAMF,OAAO,GAAG,sBAAkB,KAAK,WAAW,qBAAiBE,OAAO;AAK1E,OAAO,SAASC,aAAaA,CAACC,MAAoB,EAAU;EAC1D,MAAMC,OAAO,GAAGD,MAAM,CAACE,OAAO,KAAKN,OAAO,qBAAAO,MAAA,CAAqBP,OAAO,SAAM,EAAE;EAC9E,UAAAO,MAAA,CAAUH,MAAM,CAACI,IAAI,OAAAD,MAAA,CAAIH,MAAM,CAACE,OAAO,EAAAC,MAAA,CAAGF,OAAO;AACnD;AASA,OAAO,SAASI,YAAYA,CAACL,MAAoB,EAAuC;EAAA,IAArCM,OAAsB,GAAAC,SAAA,CAAAC,MAAA,QAAAD,SAAA,QAAAE,SAAA,GAAAF,SAAA,MAAG,CAAC,CAAC;EAC5E,MAAMG,aAAa,GAAGJ,OAAO,CAACN,MAAM,CAACW,EAAE,CAAC,IAAI,CAAC,CAAC;EAE9C,MAAMC,UAAU,GAAGjB,SAAS,MAAAQ,MAAA,CAAMH,MAAM,CAACW,EAAE,qBAAAR,MAAA,CAAkBH,MAAM,CAACW,EAAE,oBAAiB;EAEvF,IAAIE,GAAG,GAAGH,aAAa,CAACI,SAAS;EAQjC,IAAI,CAACD,GAAG,IAAIb,MAAM,CAACW,EAAE,KAAK,aAAa,EAAE;IACvCE,GAAG,GAAGP,OAAO,CAACQ,SAAS;EACzB;EAIA,IAAIR,OAAO,CAACS,WAAW,KAAK,MAAM,EAAE;IAClCF,GAAG,cAAAV,MAAA,CAAcH,MAAM,CAACgB,MAAM,YAAAb,MAAA,CAASS,UAAU,CAAE;EACrD;EAGA,IAAI,CAACC,GAAG,EAAE;IAER,IAAIX,OAAO,GAAGF,MAAM,CAACE,OAAO;IAE5B,IAAIA,OAAO,KAAK,QAAQ,EAAE;MAExBA,OAAO,GAAGJ,OAAO;IACnB;IACA,MAAMmB,UAAU,GAAGf,OAAO,OAAAC,MAAA,CAAOD,OAAO,IAAK,EAAE;IAC/CW,GAAG,oCAAAV,MAAA,CAAoCH,MAAM,CAACgB,MAAM,EAAAb,MAAA,CAAGc,UAAU,YAAAd,MAAA,CAASS,UAAU,CAAE;EACxF;EAEAlB,MAAM,CAACmB,GAAG,CAAC;EAGX,OAAOA,GAAG;AACZ"}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
|
|
2
|
-
import { isMobile } from '../env-utils/globals';
|
|
2
|
+
import { isMobile, isBrowser } from '../env-utils/globals';
|
|
3
3
|
import WorkerThread from './worker-thread';
|
|
4
4
|
import WorkerJob from './worker-job';
|
|
5
5
|
export default class WorkerPool {
|
|
@@ -91,7 +91,7 @@ export default class WorkerPool {
|
|
|
91
91
|
}
|
|
92
92
|
}
|
|
93
93
|
returnWorkerToQueue(worker) {
|
|
94
|
-
const shouldDestroyWorker = this.isDestroyed || !this.reuseWorkers || this.count > this._getMaxConcurrency();
|
|
94
|
+
const shouldDestroyWorker = !isBrowser || this.isDestroyed || !this.reuseWorkers || this.count > this._getMaxConcurrency();
|
|
95
95
|
if (shouldDestroyWorker) {
|
|
96
96
|
worker.destroy();
|
|
97
97
|
this.count--;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"worker-pool.js","names":["isMobile","WorkerThread","WorkerJob","WorkerPool","isSupported","constructor","props","_defineProperty","source","url","setProps","destroy","idleQueue","forEach","worker","isDestroyed","name","undefined","maxConcurrency","maxMobileConcurrency","reuseWorkers","onDebug","startJob","onMessage","arguments","length","job","type","data","done","onError","error","startPromise","Promise","onStart","jobQueue","push","_startQueuedJob","workerThread","_getAvailableWorker","queuedJob","shift","message","backlog","payload","result","returnWorkerToQueue","shouldDestroyWorker","count","_getMaxConcurrency","concat","toLowerCase"],"sources":["../../../../src/lib/worker-farm/worker-pool.ts"],"sourcesContent":["import type {WorkerMessageType, WorkerMessagePayload} from '../../types';\nimport {isMobile} from '../env-utils/globals';\nimport WorkerThread from './worker-thread';\nimport WorkerJob from './worker-job';\n\n/** WorkerPool onDebug Callback Parameters */\ntype OnDebugParameters = {\n message: string;\n worker: string;\n name: string;\n job: string;\n backlog: number;\n workerThread: WorkerThread;\n};\n\n/** WorkerPool Properties */\nexport type WorkerPoolProps = {\n name?: string;\n source?: string; // | Function;\n url?: string;\n maxConcurrency?: number;\n maxMobileConcurrency?: number;\n onDebug?: (options: OnDebugParameters) => any;\n reuseWorkers?: boolean;\n};\n\n/** Private helper types */\ntype OnMessage = (job: WorkerJob, type: WorkerMessageType, payload: WorkerMessagePayload) => void;\ntype OnError = (job: WorkerJob, error: Error) => void;\n\ntype QueuedJob = {\n name: string;\n onMessage: OnMessage;\n onError: OnError;\n onStart: (value: any) => void; // Resolve job start promise\n};\n\n/**\n * Process multiple data messages with small pool of identical workers\n */\nexport default class WorkerPool {\n name: string = 'unnamed';\n source?: string; // | Function;\n url?: string;\n maxConcurrency: number = 1;\n maxMobileConcurrency: number = 1;\n onDebug: (options: OnDebugParameters) => any = () => {};\n reuseWorkers: boolean = true;\n\n private props: WorkerPoolProps = {};\n private jobQueue: QueuedJob[] = [];\n private idleQueue: WorkerThread[] = [];\n private count = 0;\n private isDestroyed = false;\n\n /** Checks if workers are supported on this platform */\n static isSupported(): boolean {\n return WorkerThread.isSupported();\n }\n\n /**\n * @param processor - worker function\n * @param maxConcurrency - max count of workers\n */\n constructor(props: WorkerPoolProps) {\n this.source = props.source;\n this.url = props.url;\n this.setProps(props);\n }\n\n /**\n * Terminates all workers in the pool\n * @note Can free up significant memory\n */\n destroy(): void {\n // Destroy idle workers, active Workers will be destroyed on completion\n this.idleQueue.forEach((worker) => worker.destroy());\n this.isDestroyed = true;\n }\n\n setProps(props: WorkerPoolProps) {\n this.props = {...this.props, ...props};\n\n if (props.name !== undefined) {\n this.name = props.name;\n }\n if (props.maxConcurrency !== undefined) {\n this.maxConcurrency = props.maxConcurrency;\n }\n if (props.maxMobileConcurrency !== undefined) {\n this.maxMobileConcurrency = props.maxMobileConcurrency;\n }\n if (props.reuseWorkers !== undefined) {\n this.reuseWorkers = props.reuseWorkers;\n }\n if (props.onDebug !== undefined) {\n this.onDebug = props.onDebug;\n }\n }\n\n async startJob(\n name: string,\n onMessage: OnMessage = (job, type, data) => job.done(data),\n onError: OnError = (job, error) => job.error(error)\n ): Promise<WorkerJob> {\n // Promise resolves when thread starts working on this job\n const startPromise = new Promise<WorkerJob>((onStart) => {\n // Promise resolves when thread completes or fails working on this job\n this.jobQueue.push({name, onMessage, onError, onStart});\n return this;\n });\n this._startQueuedJob(); // eslint-disable-line @typescript-eslint/no-floating-promises\n return await startPromise;\n }\n\n // PRIVATE\n\n /**\n * Starts first queued job if worker is available or can be created\n * Called when job is started and whenever a worker returns to the idleQueue\n */\n async _startQueuedJob(): Promise<void> {\n if (!this.jobQueue.length) {\n return;\n }\n\n const workerThread = this._getAvailableWorker();\n if (!workerThread) {\n return;\n }\n\n // We have a worker, dequeue and start the job\n const queuedJob = this.jobQueue.shift();\n if (queuedJob) {\n // Emit a debug event\n // @ts-ignore\n this.onDebug({\n message: 'Starting job',\n name: queuedJob.name,\n workerThread,\n backlog: this.jobQueue.length\n });\n\n // Create a worker job to let the app access thread and manage job completion\n const job = new WorkerJob(queuedJob.name, workerThread);\n\n // Set the worker thread's message handlers\n workerThread.onMessage = (data) => queuedJob.onMessage(job, data.type, data.payload);\n workerThread.onError = (error) => queuedJob.onError(job, error);\n\n // Resolve the start promise so that the app can start sending messages to worker\n queuedJob.onStart(job);\n\n // Wait for the app to signal that the job is complete, then return worker to queue\n try {\n await job.result;\n } finally {\n this.returnWorkerToQueue(workerThread);\n }\n }\n }\n\n /**\n * Returns a worker to the idle queue\n * Destroys the worker if\n * - pool is destroyed\n * - if this pool doesn't reuse workers\n * - if maxConcurrency has been lowered\n * @param worker\n */\n returnWorkerToQueue(worker: WorkerThread) {\n const shouldDestroyWorker =\n this.isDestroyed || !this.reuseWorkers || this.count > this._getMaxConcurrency();\n\n if (shouldDestroyWorker) {\n worker.destroy();\n this.count--;\n } else {\n this.idleQueue.push(worker);\n }\n\n if (!this.isDestroyed) {\n this._startQueuedJob(); // eslint-disable-line @typescript-eslint/no-floating-promises\n }\n }\n\n /**\n * Returns idle worker or creates new worker if maxConcurrency has not been reached\n */\n _getAvailableWorker(): WorkerThread | null {\n // If a worker has completed and returned to the queue, it can be used\n if (this.idleQueue.length > 0) {\n return this.idleQueue.shift() || null;\n }\n\n // Create fresh worker if we haven't yet created the max amount of worker threads for this worker source\n if (this.count < this._getMaxConcurrency()) {\n this.count++;\n const name = `${this.name.toLowerCase()} (#${this.count} of ${this.maxConcurrency})`;\n return new WorkerThread({name, source: this.source, url: this.url});\n }\n\n // No worker available, have to wait\n return null;\n }\n\n _getMaxConcurrency() {\n return isMobile ? this.maxMobileConcurrency : this.maxConcurrency;\n }\n}\n"],"mappings":";AACA,SAAQA,QAAQ,QAAO,sBAAsB;AAC7C,OAAOC,YAAY,MAAM,iBAAiB;AAC1C,OAAOC,SAAS,MAAM,cAAc;AAqCpC,eAAe,MAAMC,UAAU,CAAC;EAgB9B,OAAOC,WAAWA,CAAA,EAAY;IAC5B,OAAOH,YAAY,CAACG,WAAW,CAAC,CAAC;EACnC;EAMAC,WAAWA,CAACC,KAAsB,EAAE;IAAAC,eAAA,eAvBrB,SAAS;IAAAA,eAAA;IAAAA,eAAA;IAAAA,eAAA,yBAGC,CAAC;IAAAA,eAAA,+BACK,CAAC;IAAAA,eAAA,kBACe,MAAM,CAAC,CAAC;IAAAA,eAAA,uBAC/B,IAAI;IAA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EAAE,IAAI,CAACA;MAAG,CAAC,CAAC;IACrE;IAGA,OAAO,IAAI;EACb;EAEAwC,kBAAkBA,CAAA,EAAG;IACnB,OAAOjD,QAAQ,GAAG,IAAI,CAACmB,oBAAoB,GAAG,IAAI,CAACD,cAAc;EACnE;AACF"}
|
|
1
|
+
{"version":3,"file":"worker-pool.js","names":["isMobile","isBrowser","WorkerThread","WorkerJob","WorkerPool","isSupported","constructor","props","_defineProperty","source","url","setProps","destroy","idleQueue","forEach","worker","isDestroyed","name","undefined","maxConcurrency","maxMobileConcurrency","reuseWorkers","onDebug","startJob","onMessage","arguments","length","job","type","data","done","onError","error","startPromise","Promise","onStart","jobQueue","push","_startQueuedJob","workerThread","_getAvailableWorker","queuedJob","shift","message","backlog","payload","result","returnWorkerToQueue","shouldDestroyWorker","count","_getMaxConcurrency","concat","toLowerCase"],"sources":["../../../../src/lib/worker-farm/worker-pool.ts"],"sourcesContent":["import type {WorkerMessageType, WorkerMessagePayload} from '../../types';\nimport {isMobile, isBrowser} from '../env-utils/globals';\nimport WorkerThread from './worker-thread';\nimport WorkerJob from './worker-job';\n\n/** WorkerPool onDebug Callback Parameters */\ntype OnDebugParameters = {\n message: string;\n worker: string;\n name: string;\n job: string;\n backlog: number;\n workerThread: WorkerThread;\n};\n\n/** WorkerPool Properties */\nexport type WorkerPoolProps = {\n name?: string;\n source?: string; // | Function;\n url?: string;\n maxConcurrency?: number;\n maxMobileConcurrency?: number;\n onDebug?: (options: OnDebugParameters) => any;\n reuseWorkers?: boolean;\n};\n\n/** Private helper types */\ntype OnMessage = (job: WorkerJob, type: WorkerMessageType, payload: WorkerMessagePayload) => void;\ntype OnError = (job: WorkerJob, error: Error) => void;\n\ntype QueuedJob = {\n name: string;\n onMessage: OnMessage;\n onError: OnError;\n onStart: (value: any) => void; // Resolve job start promise\n};\n\n/**\n * Process multiple data messages with small pool of identical workers\n */\nexport default class WorkerPool {\n name: string = 'unnamed';\n source?: string; // | Function;\n url?: string;\n maxConcurrency: number = 1;\n maxMobileConcurrency: number = 1;\n onDebug: (options: OnDebugParameters) => any = () => {};\n reuseWorkers: boolean = true;\n\n private props: WorkerPoolProps = {};\n private jobQueue: QueuedJob[] = [];\n private idleQueue: WorkerThread[] = [];\n private count = 0;\n private isDestroyed = false;\n\n /** Checks if workers are supported on this platform */\n static isSupported(): boolean {\n return WorkerThread.isSupported();\n }\n\n /**\n * @param processor - worker function\n * @param maxConcurrency - max count of workers\n */\n constructor(props: WorkerPoolProps) {\n this.source = props.source;\n this.url = props.url;\n this.setProps(props);\n }\n\n /**\n * Terminates all workers in the pool\n * @note Can free up significant memory\n */\n destroy(): void {\n // Destroy idle workers, active Workers will be destroyed on completion\n this.idleQueue.forEach((worker) => worker.destroy());\n this.isDestroyed = true;\n }\n\n setProps(props: WorkerPoolProps) {\n this.props = {...this.props, ...props};\n\n if (props.name !== undefined) {\n this.name = props.name;\n }\n if (props.maxConcurrency !== undefined) {\n this.maxConcurrency = props.maxConcurrency;\n }\n if (props.maxMobileConcurrency !== undefined) {\n this.maxMobileConcurrency = props.maxMobileConcurrency;\n }\n if (props.reuseWorkers !== undefined) {\n this.reuseWorkers = props.reuseWorkers;\n }\n if (props.onDebug !== undefined) {\n this.onDebug = props.onDebug;\n }\n }\n\n async startJob(\n name: string,\n onMessage: OnMessage = (job, type, data) => job.done(data),\n onError: OnError = (job, error) => job.error(error)\n ): Promise<WorkerJob> {\n // Promise resolves when thread starts working on this job\n const startPromise = new Promise<WorkerJob>((onStart) => {\n // Promise resolves when thread completes or fails working on this job\n this.jobQueue.push({name, onMessage, onError, onStart});\n return this;\n });\n this._startQueuedJob(); // eslint-disable-line @typescript-eslint/no-floating-promises\n return await startPromise;\n }\n\n // PRIVATE\n\n /**\n * Starts first queued job if worker is available or can be created\n * Called when job is started and whenever a worker returns to the idleQueue\n */\n async _startQueuedJob(): Promise<void> {\n if (!this.jobQueue.length) {\n return;\n }\n\n const workerThread = this._getAvailableWorker();\n if (!workerThread) {\n return;\n }\n\n // We have a worker, dequeue and start the job\n const queuedJob = this.jobQueue.shift();\n if (queuedJob) {\n // Emit a debug event\n // @ts-ignore\n this.onDebug({\n message: 'Starting job',\n name: queuedJob.name,\n workerThread,\n backlog: this.jobQueue.length\n });\n\n // Create a worker job to let the app access thread and manage job completion\n const job = new WorkerJob(queuedJob.name, workerThread);\n\n // Set the worker thread's message handlers\n workerThread.onMessage = (data) => queuedJob.onMessage(job, data.type, data.payload);\n workerThread.onError = (error) => queuedJob.onError(job, error);\n\n // Resolve the start promise so that the app can start sending messages to worker\n queuedJob.onStart(job);\n\n // Wait for the app to signal that the job is complete, then return worker to queue\n try {\n await job.result;\n } finally {\n this.returnWorkerToQueue(workerThread);\n }\n }\n }\n\n /**\n * Returns a worker to the idle queue\n * Destroys the worker if\n * - pool is destroyed\n * - if this pool doesn't reuse workers\n * - if maxConcurrency has been lowered\n * @param worker\n */\n returnWorkerToQueue(worker: WorkerThread) {\n const shouldDestroyWorker =\n // Workers on Node.js prevent the process from exiting.\n // Until we figure out how to close them before exit, we always destroy them\n !isBrowser ||\n // If the pool is destroyed, there is no reason to keep the worker around\n this.isDestroyed ||\n // If the app has disabled worker reuse, any completed workers should be destroyed\n !this.reuseWorkers ||\n // If concurrency has been lowered, this worker might be surplus to requirements\n this.count > this._getMaxConcurrency();\n\n if (shouldDestroyWorker) {\n worker.destroy();\n this.count--;\n } else {\n this.idleQueue.push(worker);\n }\n\n if (!this.isDestroyed) {\n this._startQueuedJob(); // eslint-disable-line @typescript-eslint/no-floating-promises\n }\n }\n\n /**\n * Returns idle worker or creates new worker if maxConcurrency has not been reached\n */\n _getAvailableWorker(): WorkerThread | null {\n // If a worker has completed and returned to the queue, it can be used\n if (this.idleQueue.length > 0) {\n return this.idleQueue.shift() || null;\n }\n\n // Create fresh worker if we haven't yet created the max amount of worker threads for this worker source\n if (this.count < this._getMaxConcurrency()) {\n this.count++;\n const name = `${this.name.toLowerCase()} (#${this.count} of ${this.maxConcurrency})`;\n return new WorkerThread({name, source: this.source, url: this.url});\n }\n\n // No worker available, have to wait\n return null;\n }\n\n _getMaxConcurrency() {\n return isMobile ? this.maxMobileConcurrency : this.maxConcurrency;\n }\n}\n"],"mappings":";AACA,SAAQA,QAAQ,EAAEC,SAAS,QAAO,sBAAsB;AACxD,OAAOC,YAAY,MAAM,iBAAiB;AAC1C,OAAOC,SAAS,MAAM,cAAc;AAqCpC,eAAe,MAAMC,UAAU,CAAC;EAgB9B,OAAOC,WAAWA,CAAA,EAAY;IAC5B,OAAOH,YAAY,CAACG,WAAW,CAAC,CAAC;EACnC;EAMAC,WAAWA,CAACC,KAAsB,EAAE;IAAAC,eAAA,eAvBrB,SAAS;IAAAA,eAAA;IAAAA,eAAA;IAAAA,eAAA,yBAGC,CAAC;IAAAA,eAAA,+BACK,CAAC;IAAAA,eAAA,kBACe,MAAM,CAAC,CAAC;IAAAA,eAAA,uBAC/B,IAAI;IAAAA,eAAA,gBAEK,CAAC,CAAC;IAAAA,eAAA,mBACH,EAAE;IAAAA,eAAA,oBACE,EAAE;IAAAA,eAAA,gBACtB,CAAC;IAAAA,eAAA,sBACK,KAAK;IAYzB,IAAI,CAACC,MAAM,GAAGF,KAAK,CAACE,MAAM;IAC1B,IAAI,CAACC,GAAG,GAAGH,KAAK,CAACG,GAAG;IACpB,IAAI,CAACC,QAAQ,CAACJ,KAAK,CAAC;EACtB;EAMAK,OAAOA,CAAA,EAAS;IAEd,IAAI,CAACC,SAAS,CAACC,OAAO,CAAEC,MAAM,IAAKA,MAAM,CAACH,OAAO,CAAC,CAAC,CAAC;IACpD,IAAI,CAACI,WAAW,GAAG,IAAI;EACzB;EAEAL,QAAQA,CAACJ,KAAsB,EAAE;IAC/B,IAAI,CAACA,KAAK,GAAG;MAAC,GAAG,IAAI,CAACA,KAAK;MAAE,GAAGA;IAAK,CAAC;IAEtC,IAAIA,KAAK,CAACU,IAAI,KAAKC,SAAS,EAAE;MAC5B,IAAI,CAACD,IAAI,GAAGV,KAAK,CAACU,IAAI;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;MACrB,IAAI,CAACsB,eAAe,CAAC,CAAC;IACxB;EACF;EAKAE,mBAAmBA,CAAA,EAAwB;IAEzC,IAAI,IAAI,CAAC3B,SAAS,CAACa,MAAM,GAAG,CAAC,EAAE;MAC7B,OAAO,IAAI,CAACb,SAAS,CAAC6B,KAAK,CAAC,CAAC,IAAI,IAAI;IACvC;IAGA,IAAI,IAAI,CAACO,KAAK,GAAG,IAAI,CAACC,kBAAkB,CAAC,CAAC,EAAE;MAC1C,IAAI,CAACD,KAAK,EAAE;MACZ,MAAMhC,IAAI,MAAAkC,MAAA,CAAM,IAAI,CAAClC,IAAI,CAACmC,WAAW,CAAC,CAAC,SAAAD,MAAA,CAAM,IAAI,CAACF,KAAK,UAAAE,MAAA,CAAO,IAAI,CAAChC,cAAc,MAAG;MACpF,OAAO,IAAIjB,YAAY,CAAC;QAACe,IAAI;QAAER,MAAM,EAAE,IAAI,CAACA,MAAM;QAAEC,GAAG,EAAE,IAAI,CAACA;MAAG,CAAC,CAAC;IACrE;IAGA,OAAO,IAAI;EACb;EAEAwC,kBAAkBA,CAAA,EAAG;IACnB,OAAOlD,QAAQ,GAAG,IAAI,CAACoB,oBAAoB,GAAG,IAAI,CAACD,cAAc;EACnE;AACF"}
|
|
@@ -6,7 +6,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
6
6
|
exports.createWorker = void 0;
|
|
7
7
|
const async_queue_1 = __importDefault(require("../async-queue/async-queue"));
|
|
8
8
|
const worker_body_1 = __importDefault(require("../worker-farm/worker-body"));
|
|
9
|
-
// import {validateWorkerVersion} from './validate-worker-version
|
|
9
|
+
// import {validateWorkerVersion} from './validate-worker-version';
|
|
10
10
|
/** Counter for jobs */
|
|
11
11
|
let requestId = 0;
|
|
12
12
|
let inputBatches;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"get-worker-url.d.ts","sourceRoot":"","sources":["../../../src/lib/worker-api/get-worker-url.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAC,YAAY,EAAE,aAAa,EAAC,MAAM,aAAa,CAAC;
|
|
1
|
+
{"version":3,"file":"get-worker-url.d.ts","sourceRoot":"","sources":["../../../src/lib/worker-api/get-worker-url.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAC,YAAY,EAAE,aAAa,EAAC,MAAM,aAAa,CAAC;AAQ7D;;GAEG;AACH,wBAAgB,aAAa,CAAC,MAAM,EAAE,YAAY,GAAG,MAAM,CAG1D;AAED;;;;;;GAMG;AACH,wBAAgB,YAAY,CAAC,MAAM,EAAE,YAAY,EAAE,OAAO,GAAE,aAAkB,GAAG,MAAM,CAwCtF"}
|
|
@@ -3,6 +3,7 @@
|
|
|
3
3
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
4
|
exports.getWorkerURL = exports.getWorkerName = void 0;
|
|
5
5
|
const assert_1 = require("../env-utils/assert");
|
|
6
|
+
const globals_1 = require("../env-utils/globals");
|
|
6
7
|
const version_1 = require("../env-utils/version");
|
|
7
8
|
const NPM_TAG = 'beta'; // 'beta', or 'latest' on release-branch
|
|
8
9
|
const VERSION = typeof version_1.VERSION !== 'undefined' ? version_1.VERSION : NPM_TAG;
|
|
@@ -23,7 +24,7 @@ exports.getWorkerName = getWorkerName;
|
|
|
23
24
|
*/
|
|
24
25
|
function getWorkerURL(worker, options = {}) {
|
|
25
26
|
const workerOptions = options[worker.id] || {};
|
|
26
|
-
const workerFile = `${worker.id}-worker.js`;
|
|
27
|
+
const workerFile = globals_1.isBrowser ? `${worker.id}-worker.js` : `${worker.id}-worker-node.js`;
|
|
27
28
|
let url = workerOptions.workerUrl;
|
|
28
29
|
// HACK: Allow for non-nested workerUrl for the CompressionWorker.
|
|
29
30
|
// For the compression worker, workerOptions is currently not nested correctly. For most loaders,
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"worker-pool.d.ts","sourceRoot":"","sources":["../../../src/lib/worker-farm/worker-pool.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK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CAAC,MAAM,EAAE,YAAY;IAwBxC;;OAEG;IACH,mBAAmB,IAAI,YAAY,GAAG,IAAI;IAiB1C,kBAAkB;CAGnB"}
|
|
@@ -119,7 +119,16 @@ class WorkerPool {
|
|
|
119
119
|
* @param worker
|
|
120
120
|
*/
|
|
121
121
|
returnWorkerToQueue(worker) {
|
|
122
|
-
const shouldDestroyWorker =
|
|
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();
|
|
123
132
|
if (shouldDestroyWorker) {
|
|
124
133
|
worker.destroy();
|
|
125
134
|
this.count--;
|
|
@@ -0,0 +1,244 @@
|
|
|
1
|
+
// src/lib/async-queue/async-queue.ts
|
|
2
|
+
var AsyncQueue = class {
|
|
3
|
+
constructor() {
|
|
4
|
+
this._values = [];
|
|
5
|
+
this._settlers = [];
|
|
6
|
+
this._closed = false;
|
|
7
|
+
}
|
|
8
|
+
[Symbol.asyncIterator]() {
|
|
9
|
+
return this;
|
|
10
|
+
}
|
|
11
|
+
push(value) {
|
|
12
|
+
return this.enqueue(value);
|
|
13
|
+
}
|
|
14
|
+
enqueue(value) {
|
|
15
|
+
if (this._closed) {
|
|
16
|
+
throw new Error("Closed");
|
|
17
|
+
}
|
|
18
|
+
if (this._settlers.length > 0) {
|
|
19
|
+
if (this._values.length > 0) {
|
|
20
|
+
throw new Error("Illegal internal state");
|
|
21
|
+
}
|
|
22
|
+
const settler = this._settlers.shift();
|
|
23
|
+
if (value instanceof Error) {
|
|
24
|
+
settler.reject(value);
|
|
25
|
+
} else {
|
|
26
|
+
settler.resolve({ value });
|
|
27
|
+
}
|
|
28
|
+
} else {
|
|
29
|
+
this._values.push(value);
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
close() {
|
|
33
|
+
while (this._settlers.length > 0) {
|
|
34
|
+
const settler = this._settlers.shift();
|
|
35
|
+
settler.resolve({ done: true });
|
|
36
|
+
}
|
|
37
|
+
this._closed = true;
|
|
38
|
+
}
|
|
39
|
+
next() {
|
|
40
|
+
if (this._values.length > 0) {
|
|
41
|
+
const value = this._values.shift();
|
|
42
|
+
if (value instanceof Error) {
|
|
43
|
+
return Promise.reject(value);
|
|
44
|
+
}
|
|
45
|
+
return Promise.resolve({ done: false, value });
|
|
46
|
+
}
|
|
47
|
+
if (this._closed) {
|
|
48
|
+
if (this._settlers.length > 0) {
|
|
49
|
+
throw new Error("Illegal internal state");
|
|
50
|
+
}
|
|
51
|
+
return Promise.resolve({ done: true, value: void 0 });
|
|
52
|
+
}
|
|
53
|
+
return new Promise((resolve, reject) => {
|
|
54
|
+
this._settlers.push({ resolve, reject });
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
};
|
|
58
|
+
|
|
59
|
+
// src/lib/worker-utils/get-transfer-list.ts
|
|
60
|
+
function getTransferList(object, recursive = true, transfers) {
|
|
61
|
+
const transfersSet = transfers || new Set();
|
|
62
|
+
if (!object) {
|
|
63
|
+
} else if (isTransferable(object)) {
|
|
64
|
+
transfersSet.add(object);
|
|
65
|
+
} else if (isTransferable(object.buffer)) {
|
|
66
|
+
transfersSet.add(object.buffer);
|
|
67
|
+
} else if (ArrayBuffer.isView(object)) {
|
|
68
|
+
} else if (recursive && typeof object === "object") {
|
|
69
|
+
for (const key in object) {
|
|
70
|
+
getTransferList(object[key], recursive, transfersSet);
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
return transfers === void 0 ? Array.from(transfersSet) : [];
|
|
74
|
+
}
|
|
75
|
+
function isTransferable(object) {
|
|
76
|
+
if (!object) {
|
|
77
|
+
return false;
|
|
78
|
+
}
|
|
79
|
+
if (object instanceof ArrayBuffer) {
|
|
80
|
+
return true;
|
|
81
|
+
}
|
|
82
|
+
if (typeof MessagePort !== "undefined" && object instanceof MessagePort) {
|
|
83
|
+
return true;
|
|
84
|
+
}
|
|
85
|
+
if (typeof ImageBitmap !== "undefined" && object instanceof ImageBitmap) {
|
|
86
|
+
return true;
|
|
87
|
+
}
|
|
88
|
+
if (typeof OffscreenCanvas !== "undefined" && object instanceof OffscreenCanvas) {
|
|
89
|
+
return true;
|
|
90
|
+
}
|
|
91
|
+
return false;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
// src/lib/worker-farm/worker-body.ts
|
|
95
|
+
function getParentPort() {
|
|
96
|
+
let parentPort;
|
|
97
|
+
try {
|
|
98
|
+
eval("globalThis.parentPort = require('worker_threads').parentPort");
|
|
99
|
+
parentPort = globalThis.parentPort;
|
|
100
|
+
} catch {
|
|
101
|
+
}
|
|
102
|
+
return parentPort;
|
|
103
|
+
}
|
|
104
|
+
var onMessageWrapperMap = new Map();
|
|
105
|
+
var WorkerBody = class {
|
|
106
|
+
static inWorkerThread() {
|
|
107
|
+
return typeof self !== "undefined" || Boolean(getParentPort());
|
|
108
|
+
}
|
|
109
|
+
static set onmessage(onMessage) {
|
|
110
|
+
function handleMessage(message) {
|
|
111
|
+
const parentPort3 = getParentPort();
|
|
112
|
+
const { type, payload } = parentPort3 ? message : message.data;
|
|
113
|
+
onMessage(type, payload);
|
|
114
|
+
}
|
|
115
|
+
const parentPort2 = getParentPort();
|
|
116
|
+
if (parentPort2) {
|
|
117
|
+
parentPort2.on("message", handleMessage);
|
|
118
|
+
parentPort2.on("exit", () => console.debug("Node worker closing"));
|
|
119
|
+
} else {
|
|
120
|
+
globalThis.onmessage = handleMessage;
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
static addEventListener(onMessage) {
|
|
124
|
+
let onMessageWrapper = onMessageWrapperMap.get(onMessage);
|
|
125
|
+
if (!onMessageWrapper) {
|
|
126
|
+
onMessageWrapper = (message) => {
|
|
127
|
+
if (!isKnownMessage(message)) {
|
|
128
|
+
return;
|
|
129
|
+
}
|
|
130
|
+
const parentPort3 = getParentPort();
|
|
131
|
+
const { type, payload } = parentPort3 ? message : message.data;
|
|
132
|
+
onMessage(type, payload);
|
|
133
|
+
};
|
|
134
|
+
}
|
|
135
|
+
const parentPort2 = getParentPort();
|
|
136
|
+
if (parentPort2) {
|
|
137
|
+
console.error("not implemented");
|
|
138
|
+
} else {
|
|
139
|
+
globalThis.addEventListener("message", onMessageWrapper);
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
static removeEventListener(onMessage) {
|
|
143
|
+
const onMessageWrapper = onMessageWrapperMap.get(onMessage);
|
|
144
|
+
onMessageWrapperMap.delete(onMessage);
|
|
145
|
+
const parentPort2 = getParentPort();
|
|
146
|
+
if (parentPort2) {
|
|
147
|
+
console.error("not implemented");
|
|
148
|
+
} else {
|
|
149
|
+
globalThis.removeEventListener("message", onMessageWrapper);
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
static postMessage(type, payload) {
|
|
153
|
+
const data = { source: "loaders.gl", type, payload };
|
|
154
|
+
const transferList = getTransferList(payload);
|
|
155
|
+
const parentPort2 = getParentPort();
|
|
156
|
+
if (parentPort2) {
|
|
157
|
+
parentPort2.postMessage(data, transferList);
|
|
158
|
+
} else {
|
|
159
|
+
globalThis.postMessage(data, transferList);
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
};
|
|
163
|
+
function isKnownMessage(message) {
|
|
164
|
+
const { type, data } = message;
|
|
165
|
+
return type === "message" && data && typeof data.source === "string" && data.source.startsWith("loaders.gl");
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
// src/lib/worker-api/create-worker.ts
|
|
169
|
+
var requestId = 0;
|
|
170
|
+
var inputBatches;
|
|
171
|
+
var options;
|
|
172
|
+
function createWorker(process, processInBatches) {
|
|
173
|
+
if (!WorkerBody.inWorkerThread()) {
|
|
174
|
+
return;
|
|
175
|
+
}
|
|
176
|
+
const context = {
|
|
177
|
+
process: processOnMainThread
|
|
178
|
+
};
|
|
179
|
+
WorkerBody.onmessage = async (type, payload) => {
|
|
180
|
+
try {
|
|
181
|
+
switch (type) {
|
|
182
|
+
case "process":
|
|
183
|
+
if (!process) {
|
|
184
|
+
throw new Error("Worker does not support atomic processing");
|
|
185
|
+
}
|
|
186
|
+
const result = await process(payload.input, payload.options || {}, context);
|
|
187
|
+
WorkerBody.postMessage("done", { result });
|
|
188
|
+
break;
|
|
189
|
+
case "process-in-batches":
|
|
190
|
+
if (!processInBatches) {
|
|
191
|
+
throw new Error("Worker does not support batched processing");
|
|
192
|
+
}
|
|
193
|
+
inputBatches = new AsyncQueue();
|
|
194
|
+
options = payload.options || {};
|
|
195
|
+
const resultIterator = processInBatches(inputBatches, options, context);
|
|
196
|
+
for await (const batch of resultIterator) {
|
|
197
|
+
WorkerBody.postMessage("output-batch", { result: batch });
|
|
198
|
+
}
|
|
199
|
+
WorkerBody.postMessage("done", {});
|
|
200
|
+
break;
|
|
201
|
+
case "input-batch":
|
|
202
|
+
inputBatches.push(payload.input);
|
|
203
|
+
break;
|
|
204
|
+
case "input-done":
|
|
205
|
+
inputBatches.close();
|
|
206
|
+
break;
|
|
207
|
+
default:
|
|
208
|
+
}
|
|
209
|
+
} catch (error) {
|
|
210
|
+
const message = error instanceof Error ? error.message : "";
|
|
211
|
+
WorkerBody.postMessage("error", { error: message });
|
|
212
|
+
}
|
|
213
|
+
};
|
|
214
|
+
}
|
|
215
|
+
function processOnMainThread(arrayBuffer, options2 = {}) {
|
|
216
|
+
return new Promise((resolve, reject) => {
|
|
217
|
+
const id = requestId++;
|
|
218
|
+
const onMessage = (type, payload2) => {
|
|
219
|
+
if (payload2.id !== id) {
|
|
220
|
+
return;
|
|
221
|
+
}
|
|
222
|
+
switch (type) {
|
|
223
|
+
case "done":
|
|
224
|
+
WorkerBody.removeEventListener(onMessage);
|
|
225
|
+
resolve(payload2.result);
|
|
226
|
+
break;
|
|
227
|
+
case "error":
|
|
228
|
+
WorkerBody.removeEventListener(onMessage);
|
|
229
|
+
reject(payload2.error);
|
|
230
|
+
break;
|
|
231
|
+
default:
|
|
232
|
+
}
|
|
233
|
+
};
|
|
234
|
+
WorkerBody.addEventListener(onMessage);
|
|
235
|
+
const payload = { id, input: arrayBuffer, options: options2 };
|
|
236
|
+
WorkerBody.postMessage("process", payload);
|
|
237
|
+
});
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
// src/workers/null-worker.ts
|
|
241
|
+
createWorker(async (data) => {
|
|
242
|
+
return data;
|
|
243
|
+
});
|
|
244
|
+
//# sourceMappingURL=null-worker-node.js.map
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../src/lib/async-queue/async-queue.ts", "../src/lib/worker-utils/get-transfer-list.ts", "../src/lib/worker-farm/worker-body.ts", "../src/lib/worker-api/create-worker.ts", "../src/workers/null-worker.ts"],
|
|
4
|
+
"sourcesContent": ["// From https://github.com/rauschma/async-iter-demo/tree/master/src under MIT license\n// http://2ality.com/2016/10/asynchronous-iteration.html\n\n/**\n * Async Queue\n * - AsyncIterable: An async iterator can be\n * - Values can be pushed onto the queue\n * @example\n * const asyncQueue = new AsyncQueue();\n * setTimeout(() => asyncQueue.enqueue('tick'), 1000);\n * setTimeout(() => asyncQueue.enqueue(new Error('done')), 10000);\n * for await (const value of asyncQueue) {\n * console.log(value); // tick\n * }\n */\nexport default class AsyncQueue<T> {\n private _values: any[];\n private _settlers: any[];\n private _closed: boolean;\n\n constructor() {\n this._values = []; // enqueues > dequeues\n this._settlers = []; // dequeues > enqueues\n this._closed = false;\n }\n\n /** Return an async iterator for this queue */\n [Symbol.asyncIterator](): AsyncIterator<T> {\n return this;\n }\n\n /** Push a new value - the async iterator will yield a promise resolved to this value */\n push(value: T): void {\n return this.enqueue(value);\n }\n\n /**\n * Push a new value - the async iterator will yield a promise resolved to this value\n * Add an error - the async iterator will yield a promise rejected with this value\n */\n enqueue(value: T | Error): void {\n if (this._closed) {\n throw new Error('Closed');\n }\n\n if (this._settlers.length > 0) {\n if (this._values.length > 0) {\n throw new Error('Illegal internal state');\n }\n const settler = this._settlers.shift();\n if (value instanceof Error) {\n settler.reject(value);\n } else {\n settler.resolve({value});\n }\n } else {\n this._values.push(value);\n }\n }\n\n /** Indicate that we not waiting for more values - The async iterator will be done */\n close(): void {\n while (this._settlers.length > 0) {\n const settler = this._settlers.shift();\n settler.resolve({done: true});\n }\n this._closed = true;\n }\n\n // ITERATOR IMPLEMENTATION\n\n /** @returns a Promise for an IteratorResult */\n next(): Promise<IteratorResult<T, any>> {\n // If values in queue, yield the first value\n if (this._values.length > 0) {\n const value = this._values.shift();\n if (value instanceof Error) {\n return Promise.reject(value);\n }\n return Promise.resolve({done: false, value});\n }\n\n // If queue is closed, the iterator is done\n if (this._closed) {\n if (this._settlers.length > 0) {\n throw new Error('Illegal internal state');\n }\n return Promise.resolve({done: true, value: undefined});\n }\n\n // Yield a promise that waits for new values to be enqueued\n return new Promise((resolve, reject) => {\n this._settlers.push({resolve, reject});\n });\n }\n}\n", "// NOTE - there is a copy of this function is both in core and loader-utils\n// core does not need all the utils in loader-utils, just this one.\n\n/**\n * Returns an array of Transferrable objects that can be used with postMessage\n * https://developer.mozilla.org/en-US/docs/Web/API/Worker/postMessage\n * @param object data to be sent via postMessage\n * @param recursive - not for application use\n * @param transfers - not for application use\n * @returns a transfer list that can be passed to postMessage\n */\nexport function getTransferList(\n object: any,\n recursive: boolean = true,\n transfers?: Set<any>\n): Transferable[] {\n // Make sure that items in the transfer list is unique\n const transfersSet = transfers || new Set();\n\n if (!object) {\n // ignore\n } else if (isTransferable(object)) {\n transfersSet.add(object);\n } else if (isTransferable(object.buffer)) {\n // Typed array\n transfersSet.add(object.buffer);\n } else if (ArrayBuffer.isView(object)) {\n // object is a TypeArray viewing into a SharedArrayBuffer (not transferable)\n // Do not iterate through the content in this case\n } else if (recursive && typeof object === 'object') {\n for (const key in object) {\n // Avoid perf hit - only go one level deep\n getTransferList(object[key], recursive, transfersSet);\n }\n }\n\n // If transfers is defined, is internal recursive call\n // Otherwise it's called by the user\n return transfers === undefined ? Array.from(transfersSet) : [];\n}\n\n// https://developer.mozilla.org/en-US/docs/Web/API/Transferable\nfunction isTransferable(object: unknown) {\n if (!object) {\n return false;\n }\n if (object instanceof ArrayBuffer) {\n return true;\n }\n if (typeof MessagePort !== 'undefined' && object instanceof MessagePort) {\n return true;\n }\n if (typeof ImageBitmap !== 'undefined' && object instanceof ImageBitmap) {\n return true;\n }\n // @ts-ignore\n if (typeof OffscreenCanvas !== 'undefined' && object instanceof OffscreenCanvas) {\n return true;\n }\n return false;\n}\n\n/**\n * Recursively drop non serializable values like functions and regexps.\n * @param object\n */\nexport function getTransferListForWriter(object: object | null): object {\n if (object === null) {\n return {};\n }\n const clone = Object.assign({}, object);\n\n Object.keys(clone).forEach((key) => {\n // Typed Arrays and Arrays are passed with no change\n if (\n typeof object[key] === 'object' &&\n !ArrayBuffer.isView(object[key]) &&\n !(object[key] instanceof Array)\n ) {\n clone[key] = getTransferListForWriter(object[key]);\n } else if (typeof clone[key] === 'function' || clone[key] instanceof RegExp) {\n clone[key] = {};\n } else {\n clone[key] = object[key];\n }\n });\n\n return clone;\n}\n", "import type {WorkerMessageData, WorkerMessageType, WorkerMessagePayload} from '../../types';\nimport {getTransferList} from '../worker-utils/get-transfer-list';\n\n/** Vile hack to defeat over-zealous bundlers from stripping out the require */\nfunction getParentPort() {\n // const isNode = globalThis.process;\n let parentPort;\n try {\n // prettier-ignore\n eval('globalThis.parentPort = require(\\'worker_threads\\').parentPort'); // eslint-disable-line no-eval\n parentPort = globalThis.parentPort;\n // eslint-disable-next-line no-empty\n } catch {}\n return parentPort;\n}\n\nconst onMessageWrapperMap = new Map();\n\n/**\n * Type safe wrapper for worker code\n */\nexport default class WorkerBody {\n /** Check that we are actually in a worker thread */\n static inWorkerThread(): boolean {\n return typeof self !== 'undefined' || Boolean(getParentPort());\n }\n\n /*\n * (type: WorkerMessageType, payload: WorkerMessagePayload) => any\n */\n static set onmessage(onMessage: (type: WorkerMessageType, payload: WorkerMessagePayload) => any) {\n function handleMessage(message) {\n // Confusingly the message itself also has a 'type' field which is always set to 'message'\n const parentPort = getParentPort();\n const {type, payload} = parentPort ? message : message.data;\n // if (!isKnownMessage(message)) {\n // return;\n // }\n onMessage(type, payload);\n }\n\n const parentPort = getParentPort();\n if (parentPort) {\n parentPort.on('message', handleMessage);\n // if (message == 'exit') { parentPort.unref(); }\n // eslint-disable-next-line\n parentPort.on('exit', () => console.debug('Node worker closing'));\n } else {\n // eslint-disable-next-line no-restricted-globals\n globalThis.onmessage = handleMessage;\n }\n }\n\n static addEventListener(\n onMessage: (type: WorkerMessageType, payload: WorkerMessagePayload) => any\n ) {\n let onMessageWrapper = onMessageWrapperMap.get(onMessage);\n\n if (!onMessageWrapper) {\n onMessageWrapper = (message: MessageEvent<any>) => {\n if (!isKnownMessage(message)) {\n return;\n }\n\n // Confusingly in the browser, the message itself also has a 'type' field which is always set to 'message'\n const parentPort = getParentPort();\n const {type, payload} = parentPort ? message : message.data;\n onMessage(type, payload);\n };\n }\n\n const parentPort = getParentPort();\n if (parentPort) {\n console.error('not implemented'); // eslint-disable-line\n } else {\n globalThis.addEventListener('message', onMessageWrapper);\n }\n }\n\n static removeEventListener(\n onMessage: (type: WorkerMessageType, payload: WorkerMessagePayload) => any\n ) {\n const onMessageWrapper = onMessageWrapperMap.get(onMessage);\n onMessageWrapperMap.delete(onMessage);\n const parentPort = getParentPort();\n if (parentPort) {\n console.error('not implemented'); // eslint-disable-line\n } else {\n globalThis.removeEventListener('message', onMessageWrapper);\n }\n }\n\n /**\n * Send a message from a worker to creating thread (main thread)\n * @param type\n * @param payload\n */\n static postMessage(type: WorkerMessageType, payload: WorkerMessagePayload): void {\n const data: WorkerMessageData = {source: 'loaders.gl', type, payload};\n // console.log('posting message', data);\n const transferList = getTransferList(payload);\n\n const parentPort = getParentPort();\n if (parentPort) {\n parentPort.postMessage(data, transferList);\n // console.log('posted message', data);\n } else {\n // @ts-ignore\n globalThis.postMessage(data, transferList);\n }\n }\n}\n\n// Filter out noise messages sent to workers\nfunction isKnownMessage(message: MessageEvent<any>) {\n const {type, data} = message;\n return (\n type === 'message' &&\n data &&\n typeof data.source === 'string' &&\n data.source.startsWith('loaders.gl')\n );\n}\n", "import type {\n WorkerMessageType,\n WorkerMessagePayload,\n WorkerContext,\n Process,\n ProcessInBatches\n} from '../../types';\nimport AsyncQueue from '../async-queue/async-queue';\nimport WorkerBody from '../worker-farm/worker-body';\n// import {validateWorkerVersion} from './validate-worker-version';\n\n/** Counter for jobs */\nlet requestId = 0;\nlet inputBatches: AsyncQueue<any>;\nlet options: {[key: string]: any};\n\nexport type ProcessOnMainThread = (\n data: any,\n options?: {[key: string]: any},\n context?: WorkerContext\n) => any;\n\n/**\n * Set up a WebWorkerGlobalScope to talk with the main thread\n */\nexport function createWorker(process: Process, processInBatches?: ProcessInBatches): void {\n if (!WorkerBody.inWorkerThread()) {\n return;\n }\n\n const context: WorkerContext = {\n process: processOnMainThread\n };\n\n // eslint-disable-next-line complexity\n WorkerBody.onmessage = async (type: WorkerMessageType, payload: WorkerMessagePayload) => {\n try {\n switch (type) {\n case 'process':\n if (!process) {\n throw new Error('Worker does not support atomic processing');\n }\n const result = await process(payload.input, payload.options || {}, context);\n WorkerBody.postMessage('done', {result});\n break;\n\n case 'process-in-batches':\n if (!processInBatches) {\n throw new Error('Worker does not support batched processing');\n }\n inputBatches = new AsyncQueue<any>();\n options = payload.options || {};\n const resultIterator = processInBatches(inputBatches, options, context);\n for await (const batch of resultIterator) {\n WorkerBody.postMessage('output-batch', {result: batch});\n }\n WorkerBody.postMessage('done', {});\n break;\n\n case 'input-batch':\n inputBatches.push(payload.input);\n break;\n\n case 'input-done':\n inputBatches.close();\n break;\n\n default:\n }\n } catch (error) {\n const message = error instanceof Error ? error.message : '';\n WorkerBody.postMessage('error', {error: message});\n }\n };\n}\n\nfunction processOnMainThread(arrayBuffer: ArrayBuffer, options = {}) {\n return new Promise((resolve, reject) => {\n const id = requestId++;\n\n /**\n */\n const onMessage = (type: string, payload: WorkerMessagePayload) => {\n if (payload.id !== id) {\n // not ours\n return;\n }\n\n switch (type) {\n case 'done':\n WorkerBody.removeEventListener(onMessage);\n resolve(payload.result);\n break;\n\n case 'error':\n WorkerBody.removeEventListener(onMessage);\n reject(payload.error);\n break;\n\n default:\n // ignore\n }\n };\n\n WorkerBody.addEventListener(onMessage);\n\n // Ask the main thread to decode data\n const payload = {id, input: arrayBuffer, options};\n WorkerBody.postMessage('process', payload);\n });\n}\n", "import {createWorker} from '../lib/worker-api/create-worker';\n\ncreateWorker(async (data) => {\n // @ts-ignore\n return data;\n});\n"],
|
|
5
|
+
"mappings": ";AAeA,uBAAmC;AAAA,EAKjC,cAAc;AACZ,SAAK,UAAU;AACf,SAAK,YAAY;AACjB,SAAK,UAAU;AAAA;AAAA,GAIhB,OAAO,iBAAmC;AACzC,WAAO;AAAA;AAAA,EAIT,KAAK,OAAgB;AACnB,WAAO,KAAK,QAAQ;AAAA;AAAA,EAOtB,QAAQ,OAAwB;AAC9B,QAAI,KAAK,SAAS;AAChB,YAAM,IAAI,MAAM;AAAA;AAGlB,QAAI,KAAK,UAAU,SAAS,GAAG;AAC7B,UAAI,KAAK,QAAQ,SAAS,GAAG;AAC3B,cAAM,IAAI,MAAM;AAAA;AAElB,YAAM,UAAU,KAAK,UAAU;AAC/B,UAAI,iBAAiB,OAAO;AAC1B,gBAAQ,OAAO;AAAA,aACV;AACL,gBAAQ,QAAQ,EAAC;AAAA;AAAA,WAEd;AACL,WAAK,QAAQ,KAAK;AAAA;AAAA;AAAA,EAKtB,QAAc;AACZ,WAAO,KAAK,UAAU,SAAS,GAAG;AAChC,YAAM,UAAU,KAAK,UAAU;AAC/B,cAAQ,QAAQ,EAAC,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BAAQ,SAAQ;AAChB;AAAA,aAEG;AACH,qBAAW,oBAAoB;AAC/B,iBAAO,SAAQ;AACf;AAAA;AAAA;AAAA;AAON,eAAW,iBAAiB;AAG5B,UAAM,UAAU,EAAC,IAAI,OAAO,aAAa;AACzC,eAAW,YAAY,WAAW;AAAA;AAAA;;;AC1GtC,aAAa,OAAO,SAAS;AAE3B,SAAO;AAAA;",
|
|
6
|
+
"names": []
|
|
7
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@loaders.gl/worker-utils",
|
|
3
|
-
"version": "4.0.0-alpha.
|
|
3
|
+
"version": "4.0.0-alpha.8",
|
|
4
4
|
"description": "Utilities for running tasks on worker threads",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"publishConfig": {
|
|
@@ -41,13 +41,14 @@
|
|
|
41
41
|
"./dist/esm/lib/process-utils/child-process-proxy.js": false
|
|
42
42
|
},
|
|
43
43
|
"scripts": {
|
|
44
|
-
"pre-build": "npm run build-
|
|
44
|
+
"pre-build": "npm run build-worker && npm run build-worker-node",
|
|
45
45
|
"pre-build-disabled": "npm run build-bundle && npm run build-workers",
|
|
46
46
|
"build-bundle": "esbuild src/bundle.ts --outfile=dist/dist.min.js",
|
|
47
|
-
"build-
|
|
47
|
+
"build-worker": "esbuild src/workers/null-worker.ts --outfile=dist/null-worker.js --target=esnext --bundle --sourcemap --define:__VERSION__=\\\"$npm_package_version\\\"",
|
|
48
|
+
"build-worker-node": "esbuild src/workers/null-worker.ts --outfile=dist/null-worker-node.js --platform=node --target=node16 --bundle --sourcemap --define:__VERSION__=\\\"$npm_package_version\\\""
|
|
48
49
|
},
|
|
49
50
|
"dependencies": {
|
|
50
51
|
"@babel/runtime": "^7.3.1"
|
|
51
52
|
},
|
|
52
|
-
"gitHead": "
|
|
53
|
+
"gitHead": "c35d6640c8d26deab99e277a8e698b5174233789"
|
|
53
54
|
}
|
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
import type {WorkerObject, WorkerOptions} from '../../types';
|
|
4
4
|
import {assert} from '../env-utils/assert';
|
|
5
|
+
import {isBrowser} from '../env-utils/globals';
|
|
5
6
|
import {VERSION as __VERSION__} from '../env-utils/version';
|
|
6
7
|
|
|
7
8
|
const NPM_TAG = 'beta'; // 'beta', or 'latest' on release-branch
|
|
@@ -25,7 +26,7 @@ export function getWorkerName(worker: WorkerObject): string {
|
|
|
25
26
|
export function getWorkerURL(worker: WorkerObject, options: WorkerOptions = {}): string {
|
|
26
27
|
const workerOptions = options[worker.id] || {};
|
|
27
28
|
|
|
28
|
-
const workerFile = `${worker.id}-worker.js`;
|
|
29
|
+
const workerFile = isBrowser ? `${worker.id}-worker.js` : `${worker.id}-worker-node.js`;
|
|
29
30
|
|
|
30
31
|
let url = workerOptions.workerUrl;
|
|
31
32
|
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type {WorkerMessageType, WorkerMessagePayload} from '../../types';
|
|
2
|
-
import {isMobile} from '../env-utils/globals';
|
|
2
|
+
import {isMobile, isBrowser} from '../env-utils/globals';
|
|
3
3
|
import WorkerThread from './worker-thread';
|
|
4
4
|
import WorkerJob from './worker-job';
|
|
5
5
|
|
|
@@ -170,7 +170,15 @@ export default class WorkerPool {
|
|
|
170
170
|
*/
|
|
171
171
|
returnWorkerToQueue(worker: WorkerThread) {
|
|
172
172
|
const shouldDestroyWorker =
|
|
173
|
-
|
|
173
|
+
// Workers on Node.js prevent the process from exiting.
|
|
174
|
+
// Until we figure out how to close them before exit, we always destroy them
|
|
175
|
+
!isBrowser ||
|
|
176
|
+
// If the pool is destroyed, there is no reason to keep the worker around
|
|
177
|
+
this.isDestroyed ||
|
|
178
|
+
// If the app has disabled worker reuse, any completed workers should be destroyed
|
|
179
|
+
!this.reuseWorkers ||
|
|
180
|
+
// If concurrency has been lowered, this worker might be surplus to requirements
|
|
181
|
+
this.count > this._getMaxConcurrency();
|
|
174
182
|
|
|
175
183
|
if (shouldDestroyWorker) {
|
|
176
184
|
worker.destroy();
|