@loaders.gl/worker-utils 4.2.0-alpha.3 → 4.2.0-alpha.5
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/index.cjs +172 -63
- package/dist/index.cjs.map +7 -0
- package/dist/index.d.ts +18 -18
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +19 -8
- package/dist/lib/async-queue/async-queue.js +80 -68
- package/dist/lib/env-utils/assert.js +10 -4
- package/dist/lib/env-utils/globals.js +19 -7
- package/dist/lib/env-utils/version.js +20 -11
- package/dist/lib/library-utils/library-utils.js +148 -73
- package/dist/lib/node/require-utils.node.js +72 -43
- package/dist/lib/node/worker_threads-browser.js +9 -2
- package/dist/lib/node/worker_threads.js +4 -2
- package/dist/lib/process-utils/child-process-proxy.js +101 -93
- package/dist/lib/process-utils/process-utils.js +28 -23
- package/dist/lib/worker-api/create-worker.d.ts +1 -1
- package/dist/lib/worker-api/create-worker.d.ts.map +1 -1
- package/dist/lib/worker-api/create-worker.js +77 -74
- package/dist/lib/worker-api/get-worker-url.d.ts +1 -1
- package/dist/lib/worker-api/get-worker-url.d.ts.map +1 -1
- package/dist/lib/worker-api/get-worker-url.js +52 -26
- package/dist/lib/worker-api/process-on-worker.d.ts +1 -1
- package/dist/lib/worker-api/process-on-worker.d.ts.map +1 -1
- package/dist/lib/worker-api/process-on-worker.js +70 -67
- package/dist/lib/worker-api/validate-worker-version.d.ts +1 -1
- package/dist/lib/worker-api/validate-worker-version.d.ts.map +1 -1
- package/dist/lib/worker-api/validate-worker-version.js +29 -14
- package/dist/lib/worker-farm/worker-body.d.ts +1 -1
- package/dist/lib/worker-farm/worker-body.d.ts.map +1 -1
- package/dist/lib/worker-farm/worker-body.js +106 -68
- package/dist/lib/worker-farm/worker-farm.d.ts +1 -1
- package/dist/lib/worker-farm/worker-farm.d.ts.map +1 -1
- package/dist/lib/worker-farm/worker-farm.js +77 -62
- package/dist/lib/worker-farm/worker-job.d.ts +2 -2
- package/dist/lib/worker-farm/worker-job.d.ts.map +1 -1
- package/dist/lib/worker-farm/worker-job.js +44 -32
- package/dist/lib/worker-farm/worker-pool.d.ts +3 -3
- package/dist/lib/worker-farm/worker-pool.d.ts.map +1 -1
- package/dist/lib/worker-farm/worker-pool.js +151 -111
- package/dist/lib/worker-farm/worker-thread.d.ts +1 -1
- package/dist/lib/worker-farm/worker-thread.d.ts.map +1 -1
- package/dist/lib/worker-farm/worker-thread.js +120 -94
- package/dist/lib/worker-utils/get-loadable-worker-url.js +54 -24
- package/dist/lib/worker-utils/get-transfer-list.js +79 -44
- package/dist/lib/worker-utils/remove-nontransferable-options.js +23 -14
- package/dist/types.js +3 -1
- package/dist/workers/null-worker.js +6 -3
- package/package.json +4 -4
- package/dist/index.js.map +0 -1
- package/dist/lib/async-queue/async-queue.js.map +0 -1
- package/dist/lib/env-utils/assert.js.map +0 -1
- package/dist/lib/env-utils/globals.js.map +0 -1
- package/dist/lib/env-utils/version.js.map +0 -1
- package/dist/lib/library-utils/library-utils.js.map +0 -1
- package/dist/lib/node/require-utils.node.js.map +0 -1
- package/dist/lib/node/worker_threads-browser.js.map +0 -1
- package/dist/lib/node/worker_threads.js.map +0 -1
- package/dist/lib/process-utils/child-process-proxy.js.map +0 -1
- package/dist/lib/process-utils/process-utils.js.map +0 -1
- package/dist/lib/worker-api/create-worker.js.map +0 -1
- package/dist/lib/worker-api/get-worker-url.js.map +0 -1
- package/dist/lib/worker-api/process-on-worker.js.map +0 -1
- package/dist/lib/worker-api/validate-worker-version.js.map +0 -1
- package/dist/lib/worker-farm/worker-body.js.map +0 -1
- package/dist/lib/worker-farm/worker-farm.js.map +0 -1
- package/dist/lib/worker-farm/worker-job.js.map +0 -1
- package/dist/lib/worker-farm/worker-pool.js.map +0 -1
- package/dist/lib/worker-farm/worker-thread.js.map +0 -1
- package/dist/lib/worker-utils/get-loadable-worker-url.js.map +0 -1
- package/dist/lib/worker-utils/get-transfer-list.js.map +0 -1
- package/dist/lib/worker-utils/remove-nontransferable-options.js.map +0 -1
- package/dist/types.js.map +0 -1
- package/dist/workers/null-worker.js.map +0 -1
|
@@ -1,71 +1,86 @@
|
|
|
1
|
+
// loaders.gl
|
|
2
|
+
// SPDX-License-Identifier: MIT
|
|
3
|
+
// Copyright (c) vis.gl contributors
|
|
1
4
|
import WorkerPool from "./worker-pool.js";
|
|
2
5
|
import WorkerThread from "./worker-thread.js";
|
|
3
6
|
const DEFAULT_PROPS = {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
7
|
+
maxConcurrency: 3,
|
|
8
|
+
maxMobileConcurrency: 1,
|
|
9
|
+
reuseWorkers: true,
|
|
10
|
+
onDebug: () => { }
|
|
8
11
|
};
|
|
12
|
+
/**
|
|
13
|
+
* Process multiple jobs with a "farm" of different workers in worker pools.
|
|
14
|
+
*/
|
|
9
15
|
export default class WorkerFarm {
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
static getWorkerFarm() {
|
|
14
|
-
let props = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
|
|
15
|
-
WorkerFarm._workerFarm = WorkerFarm._workerFarm || new WorkerFarm({});
|
|
16
|
-
WorkerFarm._workerFarm.setProps(props);
|
|
17
|
-
return WorkerFarm._workerFarm;
|
|
18
|
-
}
|
|
19
|
-
constructor(props) {
|
|
20
|
-
this.props = void 0;
|
|
21
|
-
this.workerPools = new Map();
|
|
22
|
-
this.props = {
|
|
23
|
-
...DEFAULT_PROPS
|
|
24
|
-
};
|
|
25
|
-
this.setProps(props);
|
|
26
|
-
this.workerPools = new Map();
|
|
27
|
-
}
|
|
28
|
-
destroy() {
|
|
29
|
-
for (const workerPool of this.workerPools.values()) {
|
|
30
|
-
workerPool.destroy();
|
|
16
|
+
/** Checks if workers are supported on this platform */
|
|
17
|
+
static isSupported() {
|
|
18
|
+
return WorkerThread.isSupported();
|
|
31
19
|
}
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
...props
|
|
38
|
-
};
|
|
39
|
-
for (const workerPool of this.workerPools.values()) {
|
|
40
|
-
workerPool.setProps(this._getWorkerPoolProps());
|
|
20
|
+
/** Get the singleton instance of the global worker farm */
|
|
21
|
+
static getWorkerFarm(props = {}) {
|
|
22
|
+
WorkerFarm._workerFarm = WorkerFarm._workerFarm || new WorkerFarm({});
|
|
23
|
+
WorkerFarm._workerFarm.setProps(props);
|
|
24
|
+
return WorkerFarm._workerFarm;
|
|
41
25
|
}
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
26
|
+
/** get global instance with WorkerFarm.getWorkerFarm() */
|
|
27
|
+
constructor(props) {
|
|
28
|
+
this.workerPools = new Map();
|
|
29
|
+
this.props = { ...DEFAULT_PROPS };
|
|
30
|
+
this.setProps(props);
|
|
31
|
+
/** @type Map<string, WorkerPool>} */
|
|
32
|
+
this.workerPools = new Map();
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Terminate all workers in the farm
|
|
36
|
+
* @note Can free up significant memory
|
|
37
|
+
*/
|
|
38
|
+
destroy() {
|
|
39
|
+
for (const workerPool of this.workerPools.values()) {
|
|
40
|
+
workerPool.destroy();
|
|
41
|
+
}
|
|
42
|
+
this.workerPools = new Map();
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* Set props used when initializing worker pools
|
|
46
|
+
* @param props
|
|
47
|
+
*/
|
|
48
|
+
setProps(props) {
|
|
49
|
+
this.props = { ...this.props, ...props };
|
|
50
|
+
// Update worker pool props
|
|
51
|
+
for (const workerPool of this.workerPools.values()) {
|
|
52
|
+
workerPool.setProps(this._getWorkerPoolProps());
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* Returns a worker pool for the specified worker
|
|
57
|
+
* @param options - only used first time for a specific worker name
|
|
58
|
+
* @param options.name - the name of the worker - used to identify worker pool
|
|
59
|
+
* @param options.url -
|
|
60
|
+
* @param options.source -
|
|
61
|
+
* @example
|
|
62
|
+
* const job = WorkerFarm.getWorkerFarm().getWorkerPool({name, url}).startJob(...);
|
|
63
|
+
*/
|
|
64
|
+
getWorkerPool(options) {
|
|
65
|
+
const { name, source, url } = options;
|
|
66
|
+
let workerPool = this.workerPools.get(name);
|
|
67
|
+
if (!workerPool) {
|
|
68
|
+
workerPool = new WorkerPool({
|
|
69
|
+
name,
|
|
70
|
+
source,
|
|
71
|
+
url
|
|
72
|
+
});
|
|
73
|
+
workerPool.setProps(this._getWorkerPoolProps());
|
|
74
|
+
this.workerPools.set(name, workerPool);
|
|
75
|
+
}
|
|
76
|
+
return workerPool;
|
|
77
|
+
}
|
|
78
|
+
_getWorkerPoolProps() {
|
|
79
|
+
return {
|
|
80
|
+
maxConcurrency: this.props.maxConcurrency,
|
|
81
|
+
maxMobileConcurrency: this.props.maxMobileConcurrency,
|
|
82
|
+
reuseWorkers: this.props.reuseWorkers,
|
|
83
|
+
onDebug: this.props.onDebug
|
|
84
|
+
};
|
|
58
85
|
}
|
|
59
|
-
return workerPool;
|
|
60
|
-
}
|
|
61
|
-
_getWorkerPoolProps() {
|
|
62
|
-
return {
|
|
63
|
-
maxConcurrency: this.props.maxConcurrency,
|
|
64
|
-
maxMobileConcurrency: this.props.maxMobileConcurrency,
|
|
65
|
-
reuseWorkers: this.props.reuseWorkers,
|
|
66
|
-
onDebug: this.props.onDebug
|
|
67
|
-
};
|
|
68
|
-
}
|
|
69
86
|
}
|
|
70
|
-
WorkerFarm._workerFarm = void 0;
|
|
71
|
-
//# sourceMappingURL=worker-farm.js.map
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import type { WorkerMessageType, WorkerMessagePayload } from
|
|
2
|
-
import WorkerThread from
|
|
1
|
+
import type { WorkerMessageType, WorkerMessagePayload } from "../../types.js";
|
|
2
|
+
import WorkerThread from "./worker-thread.js";
|
|
3
3
|
/**
|
|
4
4
|
* Represents one Job handled by a WorkerPool or WorkerFarm
|
|
5
5
|
*/
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"worker-job.d.ts","sourceRoot":"","sources":["../../../src/lib/worker-farm/worker-job.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAAC,iBAAiB,EAAE,oBAAoB,EAAC,
|
|
1
|
+
{"version":3,"file":"worker-job.d.ts","sourceRoot":"","sources":["../../../src/lib/worker-farm/worker-job.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAAC,iBAAiB,EAAE,oBAAoB,EAAC,uBAAoB;AACzE,OAAO,YAAY,2BAAwB;AAG3C;;GAEG;AACH,MAAM,CAAC,OAAO,OAAO,SAAS;IAC5B,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IACtB,QAAQ,CAAC,YAAY,EAAE,YAAY,CAAC;IACpC,SAAS,EAAE,OAAO,CAAQ;IAC1B,6CAA6C;IAC7C,QAAQ,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,CAAC,CAAC;IAE9B,OAAO,CAAC,QAAQ,CAAkC;IAClD,OAAO,CAAC,OAAO,CAAoC;gBAEvC,OAAO,EAAE,MAAM,EAAE,YAAY,EAAE,YAAY;IASvD;;;OAGG;IACH,WAAW,CAAC,IAAI,EAAE,iBAAiB,EAAE,OAAO,EAAE,oBAAoB,GAAG,IAAI;IAQzE;;OAEG;IACH,IAAI,CAAC,KAAK,EAAE,GAAG,GAAG,IAAI;IAMtB;;OAEG;IACH,KAAK,CAAC,KAAK,EAAE,KAAK,GAAG,IAAI;CAK1B"}
|
|
@@ -1,35 +1,47 @@
|
|
|
1
|
+
// loaders.gl
|
|
2
|
+
// SPDX-License-Identifier: MIT
|
|
3
|
+
// Copyright (c) vis.gl contributors
|
|
1
4
|
import { assert } from "../env-utils/assert.js";
|
|
5
|
+
/**
|
|
6
|
+
* Represents one Job handled by a WorkerPool or WorkerFarm
|
|
7
|
+
*/
|
|
2
8
|
export default class WorkerJob {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
9
|
+
constructor(jobName, workerThread) {
|
|
10
|
+
this.isRunning = true;
|
|
11
|
+
this._resolve = () => { };
|
|
12
|
+
this._reject = () => { };
|
|
13
|
+
this.name = jobName;
|
|
14
|
+
this.workerThread = workerThread;
|
|
15
|
+
this.result = new Promise((resolve, reject) => {
|
|
16
|
+
this._resolve = resolve;
|
|
17
|
+
this._reject = reject;
|
|
18
|
+
});
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Send a message to the job's worker thread
|
|
22
|
+
* @param data any data structure, ideally consisting mostly of transferrable objects
|
|
23
|
+
*/
|
|
24
|
+
postMessage(type, payload) {
|
|
25
|
+
this.workerThread.postMessage({
|
|
26
|
+
source: 'loaders.gl', // Lets worker ignore unrelated messages
|
|
27
|
+
type,
|
|
28
|
+
payload
|
|
29
|
+
});
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Call to resolve the `result` Promise with the supplied value
|
|
33
|
+
*/
|
|
34
|
+
done(value) {
|
|
35
|
+
assert(this.isRunning);
|
|
36
|
+
this.isRunning = false;
|
|
37
|
+
this._resolve(value);
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* Call to reject the `result` Promise with the supplied error
|
|
41
|
+
*/
|
|
42
|
+
error(error) {
|
|
43
|
+
assert(this.isRunning);
|
|
44
|
+
this.isRunning = false;
|
|
45
|
+
this._reject(error);
|
|
46
|
+
}
|
|
34
47
|
}
|
|
35
|
-
//# sourceMappingURL=worker-job.js.map
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import type { WorkerMessageType, WorkerMessagePayload } from
|
|
2
|
-
import WorkerThread from
|
|
3
|
-
import WorkerJob from
|
|
1
|
+
import type { WorkerMessageType, WorkerMessagePayload } from "../../types.js";
|
|
2
|
+
import WorkerThread from "./worker-thread.js";
|
|
3
|
+
import WorkerJob from "./worker-job.js";
|
|
4
4
|
/** WorkerPool onDebug Callback Parameters */
|
|
5
5
|
type OnDebugParameters = {
|
|
6
6
|
message: string;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"worker-pool.d.ts","sourceRoot":"","sources":["../../../src/lib/worker-farm/worker-pool.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAAC,iBAAiB,EAAE,oBAAoB,EAAC,
|
|
1
|
+
{"version":3,"file":"worker-pool.d.ts","sourceRoot":"","sources":["../../../src/lib/worker-farm/worker-pool.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAAC,iBAAiB,EAAE,oBAAoB,EAAC,uBAAoB;AAEzE,OAAO,YAAY,2BAAwB;AAC3C,OAAO,SAAS,wBAAqB;AAErC,6CAA6C;AAC7C,KAAK,iBAAiB,GAAG;IACvB,OAAO,EAAE,MAAM,CAAC;IAChB,MAAM,EAAE,MAAM,CAAC;IACf,IAAI,EAAE,MAAM,CAAC;IACb,GAAG,EAAE,MAAM,CAAC;IACZ,OAAO,EAAE,MAAM,CAAC;IAChB,YAAY,EAAE,YAAY,CAAC;CAC5B,CAAC;AAEF,4BAA4B;AAC5B,MAAM,MAAM,eAAe,GAAG;IAC5B,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,GAAG,CAAC,EAAE,MAAM,CAAC;IACb,cAAc,CAAC,EAAE,MAAM,CAAC;IACxB,oBAAoB,CAAC,EAAE,MAAM,CAAC;IAC9B,OAAO,CAAC,EAAE,CAAC,OAAO,EAAE,iBAAiB,KAAK,GAAG,CAAC;IAC9C,YAAY,CAAC,EAAE,OAAO,CAAC;CACxB,CAAC;AAEF,2BAA2B;AAC3B,KAAK,SAAS,GAAG,CAAC,GAAG,EAAE,SAAS,EAAE,IAAI,EAAE,iBAAiB,EAAE,OAAO,EAAE,oBAAoB,KAAK,IAAI,CAAC;AAClG,KAAK,OAAO,GAAG,CAAC,GAAG,EAAE,SAAS,EAAE,KAAK,EAAE,KAAK,KAAK,IAAI,CAAC;AAStD;;GAEG;AACH,MAAM,CAAC,OAAO,OAAO,UAAU;IAC7B,IAAI,EAAE,MAAM,CAAa;IACzB,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,GAAG,CAAC,EAAE,MAAM,CAAC;IACb,cAAc,EAAE,MAAM,CAAK;IAC3B,oBAAoB,EAAE,MAAM,CAAK;IACjC,OAAO,EAAE,CAAC,OAAO,EAAE,iBAAiB,KAAK,GAAG,CAAY;IACxD,YAAY,EAAE,OAAO,CAAQ;IAE7B,OAAO,CAAC,KAAK,CAAuB;IACpC,OAAO,CAAC,QAAQ,CAAmB;IACnC,OAAO,CAAC,SAAS,CAAsB;IACvC,OAAO,CAAC,KAAK,CAAK;IAClB,OAAO,CAAC,WAAW,CAAS;IAE5B,uDAAuD;IACvD,MAAM,CAAC,WAAW,IAAI,OAAO;IAI7B;;;OAGG;gBACS,KAAK,EAAE,eAAe;IAMlC;;;OAGG;IACH,OAAO,IAAI,IAAI;IAMf,QAAQ,CAAC,KAAK,EAAE,eAAe;IAoBzB,QAAQ,CACZ,IAAI,EAAE,MAAM,EACZ,SAAS,GAAE,SAA+C,EAC1D,OAAO,GAAE,OAA0C,GAClD,OAAO,CAAC,SAAS,CAAC;IAarB;;;OAGG;IACG,eAAe,IAAI,OAAO,CAAC,IAAI,CAAC;IA4CtC;;;;;;;OAOG;IACH,mBAAmB,CAAC,MAAM,EAAE,YAAY;IAwBxC;;OAEG;IACH,mBAAmB,IAAI,YAAY,GAAG,IAAI;IAiB1C,kBAAkB;CAGnB"}
|
|
@@ -1,125 +1,165 @@
|
|
|
1
|
+
// loaders.gl
|
|
2
|
+
// SPDX-License-Identifier: MIT
|
|
3
|
+
// Copyright (c) vis.gl contributors
|
|
1
4
|
import { isMobile, isBrowser } from "../env-utils/globals.js";
|
|
2
5
|
import WorkerThread from "./worker-thread.js";
|
|
3
6
|
import WorkerJob from "./worker-job.js";
|
|
7
|
+
/**
|
|
8
|
+
* Process multiple data messages with small pool of identical workers
|
|
9
|
+
*/
|
|
4
10
|
export default class WorkerPool {
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
constructor(props) {
|
|
9
|
-
this.name = 'unnamed';
|
|
10
|
-
this.source = void 0;
|
|
11
|
-
this.url = void 0;
|
|
12
|
-
this.maxConcurrency = 1;
|
|
13
|
-
this.maxMobileConcurrency = 1;
|
|
14
|
-
this.onDebug = () => {};
|
|
15
|
-
this.reuseWorkers = true;
|
|
16
|
-
this.props = {};
|
|
17
|
-
this.jobQueue = [];
|
|
18
|
-
this.idleQueue = [];
|
|
19
|
-
this.count = 0;
|
|
20
|
-
this.isDestroyed = false;
|
|
21
|
-
this.source = props.source;
|
|
22
|
-
this.url = props.url;
|
|
23
|
-
this.setProps(props);
|
|
24
|
-
}
|
|
25
|
-
destroy() {
|
|
26
|
-
this.idleQueue.forEach(worker => worker.destroy());
|
|
27
|
-
this.isDestroyed = true;
|
|
28
|
-
}
|
|
29
|
-
setProps(props) {
|
|
30
|
-
this.props = {
|
|
31
|
-
...this.props,
|
|
32
|
-
...props
|
|
33
|
-
};
|
|
34
|
-
if (props.name !== undefined) {
|
|
35
|
-
this.name = props.name;
|
|
11
|
+
/** Checks if workers are supported on this platform */
|
|
12
|
+
static isSupported() {
|
|
13
|
+
return WorkerThread.isSupported();
|
|
36
14
|
}
|
|
37
|
-
|
|
38
|
-
|
|
15
|
+
/**
|
|
16
|
+
* @param processor - worker function
|
|
17
|
+
* @param maxConcurrency - max count of workers
|
|
18
|
+
*/
|
|
19
|
+
constructor(props) {
|
|
20
|
+
this.name = 'unnamed';
|
|
21
|
+
this.maxConcurrency = 1;
|
|
22
|
+
this.maxMobileConcurrency = 1;
|
|
23
|
+
this.onDebug = () => { };
|
|
24
|
+
this.reuseWorkers = true;
|
|
25
|
+
this.props = {};
|
|
26
|
+
this.jobQueue = [];
|
|
27
|
+
this.idleQueue = [];
|
|
28
|
+
this.count = 0;
|
|
29
|
+
this.isDestroyed = false;
|
|
30
|
+
this.source = props.source;
|
|
31
|
+
this.url = props.url;
|
|
32
|
+
this.setProps(props);
|
|
39
33
|
}
|
|
40
|
-
|
|
41
|
-
|
|
34
|
+
/**
|
|
35
|
+
* Terminates all workers in the pool
|
|
36
|
+
* @note Can free up significant memory
|
|
37
|
+
*/
|
|
38
|
+
destroy() {
|
|
39
|
+
// Destroy idle workers, active Workers will be destroyed on completion
|
|
40
|
+
this.idleQueue.forEach((worker) => worker.destroy());
|
|
41
|
+
this.isDestroyed = true;
|
|
42
42
|
}
|
|
43
|
-
|
|
44
|
-
|
|
43
|
+
setProps(props) {
|
|
44
|
+
this.props = { ...this.props, ...props };
|
|
45
|
+
if (props.name !== undefined) {
|
|
46
|
+
this.name = props.name;
|
|
47
|
+
}
|
|
48
|
+
if (props.maxConcurrency !== undefined) {
|
|
49
|
+
this.maxConcurrency = props.maxConcurrency;
|
|
50
|
+
}
|
|
51
|
+
if (props.maxMobileConcurrency !== undefined) {
|
|
52
|
+
this.maxMobileConcurrency = props.maxMobileConcurrency;
|
|
53
|
+
}
|
|
54
|
+
if (props.reuseWorkers !== undefined) {
|
|
55
|
+
this.reuseWorkers = props.reuseWorkers;
|
|
56
|
+
}
|
|
57
|
+
if (props.onDebug !== undefined) {
|
|
58
|
+
this.onDebug = props.onDebug;
|
|
59
|
+
}
|
|
45
60
|
}
|
|
46
|
-
|
|
47
|
-
|
|
61
|
+
async startJob(name, onMessage = (job, type, data) => job.done(data), onError = (job, error) => job.error(error)) {
|
|
62
|
+
// Promise resolves when thread starts working on this job
|
|
63
|
+
const startPromise = new Promise((onStart) => {
|
|
64
|
+
// Promise resolves when thread completes or fails working on this job
|
|
65
|
+
this.jobQueue.push({ name, onMessage, onError, onStart });
|
|
66
|
+
return this;
|
|
67
|
+
});
|
|
68
|
+
this._startQueuedJob(); // eslint-disable-line @typescript-eslint/no-floating-promises
|
|
69
|
+
return await startPromise;
|
|
48
70
|
}
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
71
|
+
// PRIVATE
|
|
72
|
+
/**
|
|
73
|
+
* Starts first queued job if worker is available or can be created
|
|
74
|
+
* Called when job is started and whenever a worker returns to the idleQueue
|
|
75
|
+
*/
|
|
76
|
+
async _startQueuedJob() {
|
|
77
|
+
if (!this.jobQueue.length) {
|
|
78
|
+
return;
|
|
79
|
+
}
|
|
80
|
+
const workerThread = this._getAvailableWorker();
|
|
81
|
+
if (!workerThread) {
|
|
82
|
+
return;
|
|
83
|
+
}
|
|
84
|
+
// We have a worker, dequeue and start the job
|
|
85
|
+
const queuedJob = this.jobQueue.shift();
|
|
86
|
+
if (queuedJob) {
|
|
87
|
+
// Emit a debug event
|
|
88
|
+
// @ts-ignore
|
|
89
|
+
this.onDebug({
|
|
90
|
+
message: 'Starting job',
|
|
91
|
+
name: queuedJob.name,
|
|
92
|
+
workerThread,
|
|
93
|
+
backlog: this.jobQueue.length
|
|
94
|
+
});
|
|
95
|
+
// Create a worker job to let the app access thread and manage job completion
|
|
96
|
+
const job = new WorkerJob(queuedJob.name, workerThread);
|
|
97
|
+
// Set the worker thread's message handlers
|
|
98
|
+
workerThread.onMessage = (data) => queuedJob.onMessage(job, data.type, data.payload);
|
|
99
|
+
workerThread.onError = (error) => queuedJob.onError(job, error);
|
|
100
|
+
// Resolve the start promise so that the app can start sending messages to worker
|
|
101
|
+
queuedJob.onStart(job);
|
|
102
|
+
// Wait for the app to signal that the job is complete, then return worker to queue
|
|
103
|
+
try {
|
|
104
|
+
await job.result;
|
|
105
|
+
}
|
|
106
|
+
catch (error) {
|
|
107
|
+
// eslint-disable-next-line no-console
|
|
108
|
+
console.error(`Worker exception: ${error}`);
|
|
109
|
+
}
|
|
110
|
+
finally {
|
|
111
|
+
this.returnWorkerToQueue(workerThread);
|
|
112
|
+
}
|
|
113
|
+
}
|
|
68
114
|
}
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
115
|
+
/**
|
|
116
|
+
* Returns a worker to the idle queue
|
|
117
|
+
* Destroys the worker if
|
|
118
|
+
* - pool is destroyed
|
|
119
|
+
* - if this pool doesn't reuse workers
|
|
120
|
+
* - if maxConcurrency has been lowered
|
|
121
|
+
* @param worker
|
|
122
|
+
*/
|
|
123
|
+
returnWorkerToQueue(worker) {
|
|
124
|
+
const shouldDestroyWorker =
|
|
125
|
+
// Workers on Node.js prevent the process from exiting.
|
|
126
|
+
// Until we figure out how to close them before exit, we always destroy them
|
|
127
|
+
!isBrowser ||
|
|
128
|
+
// If the pool is destroyed, there is no reason to keep the worker around
|
|
129
|
+
this.isDestroyed ||
|
|
130
|
+
// If the app has disabled worker reuse, any completed workers should be destroyed
|
|
131
|
+
!this.reuseWorkers ||
|
|
132
|
+
// If concurrency has been lowered, this worker might be surplus to requirements
|
|
133
|
+
this.count > this._getMaxConcurrency();
|
|
134
|
+
if (shouldDestroyWorker) {
|
|
135
|
+
worker.destroy();
|
|
136
|
+
this.count--;
|
|
137
|
+
}
|
|
138
|
+
else {
|
|
139
|
+
this.idleQueue.push(worker);
|
|
140
|
+
}
|
|
141
|
+
if (!this.isDestroyed) {
|
|
142
|
+
this._startQueuedJob(); // eslint-disable-line @typescript-eslint/no-floating-promises
|
|
143
|
+
}
|
|
72
144
|
}
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
} finally {
|
|
90
|
-
this.returnWorkerToQueue(workerThread);
|
|
91
|
-
}
|
|
145
|
+
/**
|
|
146
|
+
* Returns idle worker or creates new worker if maxConcurrency has not been reached
|
|
147
|
+
*/
|
|
148
|
+
_getAvailableWorker() {
|
|
149
|
+
// If a worker has completed and returned to the queue, it can be used
|
|
150
|
+
if (this.idleQueue.length > 0) {
|
|
151
|
+
return this.idleQueue.shift() || null;
|
|
152
|
+
}
|
|
153
|
+
// Create fresh worker if we haven't yet created the max amount of worker threads for this worker source
|
|
154
|
+
if (this.count < this._getMaxConcurrency()) {
|
|
155
|
+
this.count++;
|
|
156
|
+
const name = `${this.name.toLowerCase()} (#${this.count} of ${this.maxConcurrency})`;
|
|
157
|
+
return new WorkerThread({ name, source: this.source, url: this.url });
|
|
158
|
+
}
|
|
159
|
+
// No worker available, have to wait
|
|
160
|
+
return null;
|
|
92
161
|
}
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
const shouldDestroyWorker = !isBrowser || this.isDestroyed || !this.reuseWorkers || this.count > this._getMaxConcurrency();
|
|
96
|
-
if (shouldDestroyWorker) {
|
|
97
|
-
worker.destroy();
|
|
98
|
-
this.count--;
|
|
99
|
-
} else {
|
|
100
|
-
this.idleQueue.push(worker);
|
|
162
|
+
_getMaxConcurrency() {
|
|
163
|
+
return isMobile ? this.maxMobileConcurrency : this.maxConcurrency;
|
|
101
164
|
}
|
|
102
|
-
if (!this.isDestroyed) {
|
|
103
|
-
this._startQueuedJob();
|
|
104
|
-
}
|
|
105
|
-
}
|
|
106
|
-
_getAvailableWorker() {
|
|
107
|
-
if (this.idleQueue.length > 0) {
|
|
108
|
-
return this.idleQueue.shift() || null;
|
|
109
|
-
}
|
|
110
|
-
if (this.count < this._getMaxConcurrency()) {
|
|
111
|
-
this.count++;
|
|
112
|
-
const name = `${this.name.toLowerCase()} (#${this.count} of ${this.maxConcurrency})`;
|
|
113
|
-
return new WorkerThread({
|
|
114
|
-
name,
|
|
115
|
-
source: this.source,
|
|
116
|
-
url: this.url
|
|
117
|
-
});
|
|
118
|
-
}
|
|
119
|
-
return null;
|
|
120
|
-
}
|
|
121
|
-
_getMaxConcurrency() {
|
|
122
|
-
return isMobile ? this.maxMobileConcurrency : this.maxConcurrency;
|
|
123
|
-
}
|
|
124
165
|
}
|
|
125
|
-
//# sourceMappingURL=worker-pool.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"worker-thread.d.ts","sourceRoot":"","sources":["../../../src/lib/worker-farm/worker-thread.ts"],"names":[],"mappings":"AAIA,OAAO,EAAa,cAAc,EAAC,
|
|
1
|
+
{"version":3,"file":"worker-thread.d.ts","sourceRoot":"","sources":["../../../src/lib/worker-farm/worker-thread.ts"],"names":[],"mappings":"AAIA,OAAO,EAAa,cAAc,EAAC,kCAA+B;AAQlE,MAAM,MAAM,iBAAiB,GAAG;IAC9B,IAAI,EAAE,MAAM,CAAC;IACb,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,GAAG,CAAC,EAAE,MAAM,CAAC;CACd,CAAC;AAEF;;GAEG;AACH,MAAM,CAAC,OAAO,OAAO,YAAY;IAC/B,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IACtB,QAAQ,CAAC,MAAM,EAAE,MAAM,GAAG,SAAS,CAAC;IACpC,QAAQ,CAAC,GAAG,EAAE,MAAM,GAAG,SAAS,CAAC;IACjC,UAAU,EAAE,OAAO,CAAS;IAC5B,MAAM,EAAE,MAAM,GAAG,cAAc,CAAC;IAChC,SAAS,EAAE,CAAC,OAAO,EAAE,GAAG,KAAK,IAAI,CAAC;IAClC,OAAO,EAAE,CAAC,KAAK,EAAE,KAAK,KAAK,IAAI,CAAC;IAEhC,OAAO,CAAC,YAAY,CAAc;IAElC,uDAAuD;IACvD,MAAM,CAAC,WAAW,IAAI,OAAO;gBAOjB,KAAK,EAAE,iBAAiB;IAYpC;;;OAGG;IACH,OAAO,IAAI,IAAI;IAOf,IAAI,SAAS,YAEZ;IAED;;;;OAIG;IACH,WAAW,CAAC,IAAI,EAAE,GAAG,EAAE,YAAY,CAAC,EAAE,GAAG,EAAE,GAAG,IAAI;IAQlD;;;OAGG;IACH,uBAAuB,CAAC,KAAK,EAAE,UAAU,GAAG,KAAK;IAiBjD;;OAEG;IACH,oBAAoB,IAAI,MAAM;IAsB9B;;;OAGG;IACH,iBAAiB,IAAI,cAAc;CA0BpC"}
|