@loaders.gl/worker-utils 3.1.0-beta.7 → 3.1.0

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.
Files changed (55) hide show
  1. package/dist/es5/index.js +19 -19
  2. package/dist/es5/index.js.map +1 -1
  3. package/dist/es5/lib/async-queue/async-queue.js +76 -62
  4. package/dist/es5/lib/async-queue/async-queue.js.map +1 -1
  5. package/dist/es5/lib/env-utils/globals.js +15 -10
  6. package/dist/es5/lib/env-utils/globals.js.map +1 -1
  7. package/dist/es5/lib/env-utils/version.js +3 -3
  8. package/dist/es5/lib/env-utils/version.js.map +1 -1
  9. package/dist/es5/lib/library-utils/library-utils.js +127 -29
  10. package/dist/es5/lib/library-utils/library-utils.js.map +1 -1
  11. package/dist/es5/lib/node/require-utils.node.js +56 -16
  12. package/dist/es5/lib/node/require-utils.node.js.map +1 -1
  13. package/dist/es5/lib/process-utils/child-process-proxy.js +199 -90
  14. package/dist/es5/lib/process-utils/child-process-proxy.js.map +1 -1
  15. package/dist/es5/lib/process-utils/process-utils.js +9 -8
  16. package/dist/es5/lib/process-utils/process-utils.js.map +1 -1
  17. package/dist/es5/lib/worker-api/create-worker.js +162 -52
  18. package/dist/es5/lib/worker-api/create-worker.js.map +1 -1
  19. package/dist/es5/lib/worker-api/get-worker-url.js +14 -9
  20. package/dist/es5/lib/worker-api/get-worker-url.js.map +1 -1
  21. package/dist/es5/lib/worker-api/process-on-worker.js +130 -59
  22. package/dist/es5/lib/worker-api/process-on-worker.js.map +1 -1
  23. package/dist/es5/lib/worker-api/validate-worker-version.js +4 -3
  24. package/dist/es5/lib/worker-api/validate-worker-version.js.map +1 -1
  25. package/dist/es5/lib/worker-farm/worker-body.js +58 -46
  26. package/dist/es5/lib/worker-farm/worker-body.js.map +1 -1
  27. package/dist/es5/lib/worker-farm/worker-farm.js +99 -60
  28. package/dist/es5/lib/worker-farm/worker-farm.js.map +1 -1
  29. package/dist/es5/lib/worker-farm/worker-job.js +40 -28
  30. package/dist/es5/lib/worker-farm/worker-job.js.map +1 -1
  31. package/dist/es5/lib/worker-farm/worker-pool.js +204 -108
  32. package/dist/es5/lib/worker-farm/worker-pool.js.map +1 -1
  33. package/dist/es5/lib/worker-farm/worker-thread.js +85 -67
  34. package/dist/es5/lib/worker-farm/worker-thread.js.map +1 -1
  35. package/dist/es5/lib/worker-utils/get-loadable-worker-url.js +4 -4
  36. package/dist/es5/lib/worker-utils/get-loadable-worker-url.js.map +1 -1
  37. package/dist/es5/lib/worker-utils/get-transfer-list.js +10 -4
  38. package/dist/es5/lib/worker-utils/get-transfer-list.js.map +1 -1
  39. package/dist/es5/lib/worker-utils/remove-nontransferable-options.js +7 -3
  40. package/dist/es5/lib/worker-utils/remove-nontransferable-options.js.map +1 -1
  41. package/dist/es5/workers/null-worker.js +26 -3
  42. package/dist/es5/workers/null-worker.js.map +1 -1
  43. package/dist/esm/lib/env-utils/version.js +2 -2
  44. package/dist/esm/lib/env-utils/version.js.map +1 -1
  45. package/dist/esm/lib/library-utils/library-utils.js +1 -1
  46. package/dist/esm/lib/library-utils/library-utils.js.map +1 -1
  47. package/dist/esm/lib/worker-api/get-worker-url.js +5 -1
  48. package/dist/esm/lib/worker-api/get-worker-url.js.map +1 -1
  49. package/dist/lib/worker-api/get-worker-url.d.ts.map +1 -1
  50. package/dist/lib/worker-api/get-worker-url.js +9 -0
  51. package/dist/types.d.ts +1 -0
  52. package/dist/types.d.ts.map +1 -1
  53. package/package.json +2 -2
  54. package/src/lib/worker-api/get-worker-url.ts +10 -0
  55. package/src/types.ts +1 -0
@@ -7,6 +7,14 @@ Object.defineProperty(exports, "__esModule", {
7
7
  });
8
8
  exports.default = void 0;
9
9
 
10
+ var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator"));
11
+
12
+ var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator"));
13
+
14
+ var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));
15
+
16
+ var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));
17
+
10
18
  var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
11
19
 
12
20
  var _globals = require("../env-utils/globals");
@@ -15,14 +23,19 @@ var _workerThread = _interopRequireDefault(require("./worker-thread"));
15
23
 
16
24
  var _workerJob = _interopRequireDefault(require("./worker-job"));
17
25
 
18
- class WorkerPool {
19
- constructor(props) {
26
+ function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) { symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); } keys.push.apply(keys, symbols); } return keys; }
27
+
28
+ function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { (0, _defineProperty2.default)(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
29
+
30
+ var WorkerPool = function () {
31
+ function WorkerPool(props) {
32
+ (0, _classCallCheck2.default)(this, WorkerPool);
20
33
  (0, _defineProperty2.default)(this, "name", 'unnamed');
21
34
  (0, _defineProperty2.default)(this, "source", void 0);
22
35
  (0, _defineProperty2.default)(this, "url", void 0);
23
36
  (0, _defineProperty2.default)(this, "maxConcurrency", 1);
24
37
  (0, _defineProperty2.default)(this, "maxMobileConcurrency", 1);
25
- (0, _defineProperty2.default)(this, "onDebug", () => {});
38
+ (0, _defineProperty2.default)(this, "onDebug", function () {});
26
39
  (0, _defineProperty2.default)(this, "reuseWorkers", true);
27
40
  (0, _defineProperty2.default)(this, "props", {});
28
41
  (0, _defineProperty2.default)(this, "jobQueue", []);
@@ -34,127 +47,210 @@ class WorkerPool {
34
47
  this.setProps(props);
35
48
  }
36
49
 
37
- destroy() {
38
- this.idleQueue.forEach(worker => worker.destroy());
39
- this.isDestroyed = true;
40
- }
41
-
42
- setProps(props) {
43
- this.props = { ...this.props,
44
- ...props
45
- };
46
-
47
- if (props.name !== undefined) {
48
- this.name = props.name;
49
- }
50
-
51
- if (props.maxConcurrency !== undefined) {
52
- this.maxConcurrency = props.maxConcurrency;
53
- }
54
-
55
- if (props.maxMobileConcurrency !== undefined) {
56
- this.maxMobileConcurrency = props.maxMobileConcurrency;
57
- }
58
-
59
- if (props.reuseWorkers !== undefined) {
60
- this.reuseWorkers = props.reuseWorkers;
61
- }
62
-
63
- if (props.onDebug !== undefined) {
64
- this.onDebug = props.onDebug;
65
- }
66
- }
67
-
68
- async startJob(name, onMessage = (job, type, data) => job.done(data), onError = (job, error) => job.error(error)) {
69
- const startPromise = new Promise(onStart => {
70
- this.jobQueue.push({
71
- name,
72
- onMessage,
73
- onError,
74
- onStart
50
+ (0, _createClass2.default)(WorkerPool, [{
51
+ key: "destroy",
52
+ value: function destroy() {
53
+ this.idleQueue.forEach(function (worker) {
54
+ return worker.destroy();
75
55
  });
76
- return this;
77
- });
78
-
79
- this._startQueuedJob();
80
-
81
- return await startPromise;
82
- }
83
-
84
- async _startQueuedJob() {
85
- if (!this.jobQueue.length) {
86
- return;
87
- }
88
-
89
- const workerThread = this._getAvailableWorker();
90
-
91
- if (!workerThread) {
92
- return;
56
+ this.isDestroyed = true;
93
57
  }
58
+ }, {
59
+ key: "setProps",
60
+ value: function setProps(props) {
61
+ this.props = _objectSpread(_objectSpread({}, this.props), props);
94
62
 
95
- const queuedJob = this.jobQueue.shift();
96
-
97
- if (queuedJob) {
98
- this.onDebug({
99
- message: 'Starting job',
100
- name: queuedJob.name,
101
- workerThread,
102
- backlog: this.jobQueue.length
103
- });
104
- const job = new _workerJob.default(queuedJob.name, workerThread);
63
+ if (props.name !== undefined) {
64
+ this.name = props.name;
65
+ }
105
66
 
106
- workerThread.onMessage = data => queuedJob.onMessage(job, data.type, data.payload);
67
+ if (props.maxConcurrency !== undefined) {
68
+ this.maxConcurrency = props.maxConcurrency;
69
+ }
107
70
 
108
- workerThread.onError = error => queuedJob.onError(job, error);
71
+ if (props.maxMobileConcurrency !== undefined) {
72
+ this.maxMobileConcurrency = props.maxMobileConcurrency;
73
+ }
109
74
 
110
- queuedJob.onStart(job);
75
+ if (props.reuseWorkers !== undefined) {
76
+ this.reuseWorkers = props.reuseWorkers;
77
+ }
111
78
 
112
- try {
113
- await job.result;
114
- } finally {
115
- this.returnWorkerToQueue(workerThread);
79
+ if (props.onDebug !== undefined) {
80
+ this.onDebug = props.onDebug;
116
81
  }
117
82
  }
118
- }
83
+ }, {
84
+ key: "startJob",
85
+ value: function () {
86
+ var _startJob = (0, _asyncToGenerator2.default)(_regenerator.default.mark(function _callee(name) {
87
+ var _this = this;
88
+
89
+ var onMessage,
90
+ onError,
91
+ startPromise,
92
+ _args = arguments;
93
+ return _regenerator.default.wrap(function _callee$(_context) {
94
+ while (1) {
95
+ switch (_context.prev = _context.next) {
96
+ case 0:
97
+ onMessage = _args.length > 1 && _args[1] !== undefined ? _args[1] : function (job, type, data) {
98
+ return job.done(data);
99
+ };
100
+ onError = _args.length > 2 && _args[2] !== undefined ? _args[2] : function (job, error) {
101
+ return job.error(error);
102
+ };
103
+ startPromise = new Promise(function (onStart) {
104
+ _this.jobQueue.push({
105
+ name: name,
106
+ onMessage: onMessage,
107
+ onError: onError,
108
+ onStart: onStart
109
+ });
110
+
111
+ return _this;
112
+ });
113
+
114
+ this._startQueuedJob();
115
+
116
+ _context.next = 6;
117
+ return startPromise;
118
+
119
+ case 6:
120
+ return _context.abrupt("return", _context.sent);
121
+
122
+ case 7:
123
+ case "end":
124
+ return _context.stop();
125
+ }
126
+ }
127
+ }, _callee, this);
128
+ }));
129
+
130
+ function startJob(_x) {
131
+ return _startJob.apply(this, arguments);
132
+ }
119
133
 
120
- returnWorkerToQueue(worker) {
121
- const shouldDestroyWorker = this.isDestroyed || !this.reuseWorkers || this.count > this._getMaxConcurrency();
134
+ return startJob;
135
+ }()
136
+ }, {
137
+ key: "_startQueuedJob",
138
+ value: function () {
139
+ var _startQueuedJob2 = (0, _asyncToGenerator2.default)(_regenerator.default.mark(function _callee2() {
140
+ var workerThread, queuedJob, _job;
141
+
142
+ return _regenerator.default.wrap(function _callee2$(_context2) {
143
+ while (1) {
144
+ switch (_context2.prev = _context2.next) {
145
+ case 0:
146
+ if (this.jobQueue.length) {
147
+ _context2.next = 2;
148
+ break;
149
+ }
150
+
151
+ return _context2.abrupt("return");
152
+
153
+ case 2:
154
+ workerThread = this._getAvailableWorker();
155
+
156
+ if (workerThread) {
157
+ _context2.next = 5;
158
+ break;
159
+ }
160
+
161
+ return _context2.abrupt("return");
162
+
163
+ case 5:
164
+ queuedJob = this.jobQueue.shift();
165
+
166
+ if (!queuedJob) {
167
+ _context2.next = 18;
168
+ break;
169
+ }
170
+
171
+ this.onDebug({
172
+ message: 'Starting job',
173
+ name: queuedJob.name,
174
+ workerThread: workerThread,
175
+ backlog: this.jobQueue.length
176
+ });
177
+ _job = new _workerJob.default(queuedJob.name, workerThread);
178
+
179
+ workerThread.onMessage = function (data) {
180
+ return queuedJob.onMessage(_job, data.type, data.payload);
181
+ };
182
+
183
+ workerThread.onError = function (error) {
184
+ return queuedJob.onError(_job, error);
185
+ };
186
+
187
+ queuedJob.onStart(_job);
188
+ _context2.prev = 12;
189
+ _context2.next = 15;
190
+ return _job.result;
191
+
192
+ case 15:
193
+ _context2.prev = 15;
194
+ this.returnWorkerToQueue(workerThread);
195
+ return _context2.finish(15);
196
+
197
+ case 18:
198
+ case "end":
199
+ return _context2.stop();
200
+ }
201
+ }
202
+ }, _callee2, this, [[12,, 15, 18]]);
203
+ }));
204
+
205
+ function _startQueuedJob() {
206
+ return _startQueuedJob2.apply(this, arguments);
207
+ }
122
208
 
123
- if (shouldDestroyWorker) {
124
- worker.destroy();
125
- this.count--;
126
- } else {
127
- this.idleQueue.push(worker);
128
- }
209
+ return _startQueuedJob;
210
+ }()
211
+ }, {
212
+ key: "returnWorkerToQueue",
213
+ value: function returnWorkerToQueue(worker) {
214
+ var shouldDestroyWorker = this.isDestroyed || !this.reuseWorkers || this.count > this._getMaxConcurrency();
215
+
216
+ if (shouldDestroyWorker) {
217
+ worker.destroy();
218
+ this.count--;
219
+ } else {
220
+ this.idleQueue.push(worker);
221
+ }
129
222
 
130
- if (!this.isDestroyed) {
131
- this._startQueuedJob();
223
+ if (!this.isDestroyed) {
224
+ this._startQueuedJob();
225
+ }
132
226
  }
133
- }
227
+ }, {
228
+ key: "_getAvailableWorker",
229
+ value: function _getAvailableWorker() {
230
+ if (this.idleQueue.length > 0) {
231
+ return this.idleQueue.shift() || null;
232
+ }
134
233
 
135
- _getAvailableWorker() {
136
- if (this.idleQueue.length > 0) {
137
- return this.idleQueue.shift() || null;
138
- }
234
+ if (this.count < this._getMaxConcurrency()) {
235
+ this.count++;
236
+ var name = "".concat(this.name.toLowerCase(), " (#").concat(this.count, " of ").concat(this.maxConcurrency, ")");
237
+ return new _workerThread.default({
238
+ name: name,
239
+ source: this.source,
240
+ url: this.url
241
+ });
242
+ }
139
243
 
140
- if (this.count < this._getMaxConcurrency()) {
141
- this.count++;
142
- const name = "".concat(this.name.toLowerCase(), " (#").concat(this.count, " of ").concat(this.maxConcurrency, ")");
143
- return new _workerThread.default({
144
- name,
145
- source: this.source,
146
- url: this.url
147
- });
244
+ return null;
148
245
  }
149
-
150
- return null;
151
- }
152
-
153
- _getMaxConcurrency() {
154
- return _globals.isMobile ? this.maxMobileConcurrency : this.maxConcurrency;
155
- }
156
-
157
- }
246
+ }, {
247
+ key: "_getMaxConcurrency",
248
+ value: function _getMaxConcurrency() {
249
+ return _globals.isMobile ? this.maxMobileConcurrency : this.maxConcurrency;
250
+ }
251
+ }]);
252
+ return WorkerPool;
253
+ }();
158
254
 
159
255
  exports.default = WorkerPool;
160
256
  //# sourceMappingURL=worker-pool.js.map
@@ -1 +1 @@
1
- {"version":3,"sources":["../../../../src/lib/worker-farm/worker-pool.ts"],"names":["WorkerPool","constructor","props","source","url","setProps","destroy","idleQueue","forEach","worker","isDestroyed","name","undefined","maxConcurrency","maxMobileConcurrency","reuseWorkers","onDebug","startJob","onMessage","job","type","data","done","onError","error","startPromise","Promise","onStart","jobQueue","push","_startQueuedJob","length","workerThread","_getAvailableWorker","queuedJob","shift","message","backlog","WorkerJob","payload","result","returnWorkerToQueue","shouldDestroyWorker","count","_getMaxConcurrency","toLowerCase","WorkerThread","isMobile"],"mappings":";;;;;;;;;;;AACA;;AACA;;AACA;;AAqCe,MAAMA,UAAN,CAAiB;AAmB9BC,EAAAA,WAAW,CAACC,KAAD,EAAyB;AAAA,gDAlBrB,SAkBqB;AAAA;AAAA;AAAA,0DAfX,CAeW;AAAA,gEAdL,CAcK;AAAA,mDAbW,MAAM,CAAE,CAanB;AAAA,wDAZZ,IAYY;AAAA,iDAVH,EAUG;AAAA,oDATJ,EASI;AAAA,qDARA,EAQA;AAAA,iDAPpB,CAOoB;AAAA,uDANd,KAMc;AAClC,SAAKC,MAAL,GAAcD,KAAK,CAACC,MAApB;AACA,SAAKC,GAAL,GAAWF,KAAK,CAACE,GAAjB;AACA,SAAKC,QAAL,CAAcH,KAAd;AACD;;AAMDI,EAAAA,OAAO,GAAS;AAEd,SAAKC,SAAL,CAAeC,OAAf,CAAwBC,MAAD,IAAYA,MAAM,CAACH,OAAP,EAAnC;AACA,SAAKI,WAAL,GAAmB,IAAnB;AACD;;AAEDL,EAAAA,QAAQ,CAACH,KAAD,EAAyB;AAC/B,SAAKA,KAAL,GAAa,EAAC,GAAG,KAAKA,KAAT;AAAgB,SAAGA;AAAnB,KAAb;;AAEA,QAAIA,KAAK,CAACS,IAAN,KAAeC,SAAnB,EAA8B;AAC5B,WAAKD,IAAL,GAAYT,KAAK,CAACS,IAAlB;AACD;;AACD,QAAIT,KAAK,CAACW,cAAN,KAAyBD,SAA7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,kBAAL,EAAjB,EAA4C;AAC1C,WAAKD,KAAL;AACA,YAAMhC,IAAI,aAAM,KAAKA,IAAL,CAAUkC,WAAV,EAAN,gBAAmC,KAAKF,KAAxC,iBAAoD,KAAK9B,cAAzD,MAAV;AACA,aAAO,IAAIiC,qBAAJ,CAAiB;AAACnC,QAAAA,IAAD;AAAOR,QAAAA,MAAM,EAAE,KAAKA,MAApB;AAA4BC,QAAAA,GAAG,EAAE,KAAKA;AAAtC,OAAjB,CAAP;AACD;;AAGD,WAAO,IAAP;AACD;;AAEDwC,EAAAA,kBAAkB,GAAG;AACnB,WAAOG,oBAAW,KAAKjC,oBAAhB,GAAuC,KAAKD,cAAnD;AACD;;AAnK6B","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 /**\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"],"file":"worker-pool.js"}
1
+ {"version":3,"sources":["../../../../src/lib/worker-farm/worker-pool.ts"],"names":["WorkerPool","props","source","url","setProps","idleQueue","forEach","worker","destroy","isDestroyed","name","undefined","maxConcurrency","maxMobileConcurrency","reuseWorkers","onDebug","onMessage","job","type","data","done","onError","error","startPromise","Promise","onStart","jobQueue","push","_startQueuedJob","length","workerThread","_getAvailableWorker","queuedJob","shift","message","backlog","WorkerJob","payload","result","returnWorkerToQueue","shouldDestroyWorker","count","_getMaxConcurrency","toLowerCase","WorkerThread","isMobile"],"mappings":";;;;;;;;;;;;;;;;;;;AACA;;AACA;;AACA;;;;;;IAqCqBA,U;AAmBnB,sBAAYC,KAAZ,EAAoC;AAAA;AAAA,gDAlBrB,SAkBqB;AAAA;AAAA;AAAA,0DAfX,CAeW;AAAA,gEAdL,CAcK;AAAA,mDAbW,YAAM,CAAE,CAanB;AAAA,wDAZZ,IAYY;AAAA,iDAVH,EAUG;AAAA,oDATJ,EASI;AAAA,qDARA,EAQA;AAAA,iDAPpB,CAOoB;AAAA,uDANd,KAMc;AAClC,SAAKC,MAAL,GAAcD,KAAK,CAACC,MAApB;AACA,SAAKC,GAAL,GAAWF,KAAK,CAACE,GAAjB;AACA,SAAKC,QAAL,CAAcH,KAAd;AACD;;;;WAMD,mBAAgB;AAEd,WAAKI,SAAL,CAAeC,OAAf,CAAuB,UAACC,MAAD;AAAA,eAAYA,MAAM,CAACC,OAAP,EAAZ;AAAA,OAAvB;AACA,WAAKC,WAAL,GAAmB,IAAnB;AACD;;;WAED,kBAASR,KAAT,EAAiC;AAC/B,WAAKA,KAAL,mCAAiB,KAAKA,KAAtB,GAAgCA,KAAhC;;AAEA,UAAIA,KAAK,CAACS,IAAN,KAAeC,SAAnB,EAA8B;AAC5B,aAAKD,IAAL,GAAYT,KAAK,CAACS,IAAlB;AACD;;AACD,UAAIT,KAAK,CAACW,cAAN,KAAyBD,SAA7B,EAAwC;AACtC,aAAKC,cAAL,GAAsBX,KAAK,CAACW,cAA5B;AACD;;AACD,UAAIX,KAAK,CAACY,oBAAN,KAA+BF,SAAnC,EAA8C;AAC5C,aAAKE,oBAAL,GAA4BZ,KAAK,CAACY,oBAAlC;AACD;;AACD,UAAIZ,KAAK,CAACa,YAAN,KAAuBH,SAA3B,EAAsC;AACpC,aAAKG,YAAL,GAAoBb,KAAK,CAACa,YAA1B;AACD;;AACD,UAAIb,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,CAA5B,EAA+B;AAC7B,eAAO,KAAKxB,SAAL,CAAe4B,KAAf,MAA0B,IAAjC;AACD;;AAGD,UAAI,KAAKQ,KAAL,GAAa,KAAKC,kBAAL,EAAjB,EAA4C;AAC1C,aAAKD,KAAL;AACA,YAAM/B,IAAI,aAAM,KAAKA,IAAL,CAAUiC,WAAV,EAAN,gBAAmC,KAAKF,KAAxC,iBAAoD,KAAK7B,cAAzD,MAAV;AACA,eAAO,IAAIgC,qBAAJ,CAAiB;AAAClC,UAAAA,IAAI,EAAJA,IAAD;AAAOR,UAAAA,MAAM,EAAE,KAAKA,MAApB;AAA4BC,UAAAA,GAAG,EAAE,KAAKA;AAAtC,SAAjB,CAAP;AACD;;AAGD,aAAO,IAAP;AACD;;;WAED,8BAAqB;AACnB,aAAO0C,oBAAW,KAAKhC,oBAAhB,GAAuC,KAAKD,cAAnD;AACD","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 /**\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"],"file":"worker-pool.js"}
@@ -7,6 +7,10 @@ Object.defineProperty(exports, "__esModule", {
7
7
  });
8
8
  exports.default = void 0;
9
9
 
10
+ var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));
11
+
12
+ var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));
13
+
10
14
  var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
11
15
 
12
16
  var _assert = require("../env-utils/assert");
@@ -15,14 +19,11 @@ var _getLoadableWorkerUrl = require("../worker-utils/get-loadable-worker-url");
15
19
 
16
20
  var _getTransferList = require("../worker-utils/get-transfer-list");
17
21
 
18
- const NOOP = () => {};
19
-
20
- class WorkerThread {
21
- static isSupported() {
22
- return typeof Worker !== 'undefined';
23
- }
22
+ var NOOP = function NOOP() {};
24
23
 
25
- constructor(props) {
24
+ var WorkerThread = function () {
25
+ function WorkerThread(props) {
26
+ (0, _classCallCheck2.default)(this, WorkerThread);
26
27
  (0, _defineProperty2.default)(this, "name", void 0);
27
28
  (0, _defineProperty2.default)(this, "source", void 0);
28
29
  (0, _defineProperty2.default)(this, "url", void 0);
@@ -31,81 +32,98 @@ class WorkerThread {
31
32
  (0, _defineProperty2.default)(this, "onMessage", void 0);
32
33
  (0, _defineProperty2.default)(this, "onError", void 0);
33
34
  (0, _defineProperty2.default)(this, "_loadableURL", '');
34
- const {
35
- name,
36
- source,
37
- url
38
- } = props;
35
+ var name = props.name,
36
+ source = props.source,
37
+ url = props.url;
39
38
  (0, _assert.assert)(source || url);
40
39
  this.name = name;
41
40
  this.source = source;
42
41
  this.url = url;
43
42
  this.onMessage = NOOP;
44
43
 
45
- this.onError = error => console.log(error);
44
+ this.onError = function (error) {
45
+ return console.log(error);
46
+ };
46
47
 
47
48
  this.worker = this._createBrowserWorker();
48
49
  }
49
50
 
50
- destroy() {
51
- this.onMessage = NOOP;
52
- this.onError = NOOP;
53
- this.worker.terminate();
54
- this.terminated = true;
55
- }
56
-
57
- get isRunning() {
58
- return Boolean(this.onMessage);
59
- }
60
-
61
- postMessage(data, transferList) {
62
- transferList = transferList || (0, _getTransferList.getTransferList)(data);
63
- this.worker.postMessage(data, transferList);
64
- }
65
-
66
- _getErrorFromErrorEvent(event) {
67
- let message = 'Failed to load ';
68
- message += "worker ".concat(this.name, " from ").concat(this.url, ". ");
69
-
70
- if (event.message) {
71
- message += "".concat(event.message, " in ");
51
+ (0, _createClass2.default)(WorkerThread, [{
52
+ key: "destroy",
53
+ value: function destroy() {
54
+ this.onMessage = NOOP;
55
+ this.onError = NOOP;
56
+ this.worker.terminate();
57
+ this.terminated = true;
72
58
  }
73
-
74
- if (event.lineno) {
75
- message += ":".concat(event.lineno, ":").concat(event.colno);
59
+ }, {
60
+ key: "isRunning",
61
+ get: function get() {
62
+ return Boolean(this.onMessage);
76
63
  }
77
-
78
- return new Error(message);
79
- }
80
-
81
- _createBrowserWorker() {
82
- this._loadableURL = (0, _getLoadableWorkerUrl.getLoadableWorkerURL)({
83
- source: this.source,
84
- url: this.url
85
- });
86
- const worker = new Worker(this._loadableURL, {
87
- name: this.name
88
- });
89
-
90
- worker.onmessage = event => {
91
- if (!event.data) {
92
- this.onError(new Error('No data received'));
93
- } else {
94
- this.onMessage(event.data);
64
+ }, {
65
+ key: "postMessage",
66
+ value: function postMessage(data, transferList) {
67
+ transferList = transferList || (0, _getTransferList.getTransferList)(data);
68
+ this.worker.postMessage(data, transferList);
69
+ }
70
+ }, {
71
+ key: "_getErrorFromErrorEvent",
72
+ value: function _getErrorFromErrorEvent(event) {
73
+ var message = 'Failed to load ';
74
+ message += "worker ".concat(this.name, " from ").concat(this.url, ". ");
75
+
76
+ if (event.message) {
77
+ message += "".concat(event.message, " in ");
95
78
  }
96
- };
97
-
98
- worker.onerror = error => {
99
- this.onError(this._getErrorFromErrorEvent(error));
100
- this.terminated = true;
101
- };
102
-
103
- worker.onmessageerror = event => console.error(event);
104
79
 
105
- return worker;
106
- }
80
+ if (event.lineno) {
81
+ message += ":".concat(event.lineno, ":").concat(event.colno);
82
+ }
107
83
 
108
- }
84
+ return new Error(message);
85
+ }
86
+ }, {
87
+ key: "_createBrowserWorker",
88
+ value: function _createBrowserWorker() {
89
+ var _this = this;
90
+
91
+ this._loadableURL = (0, _getLoadableWorkerUrl.getLoadableWorkerURL)({
92
+ source: this.source,
93
+ url: this.url
94
+ });
95
+ var worker = new Worker(this._loadableURL, {
96
+ name: this.name
97
+ });
98
+
99
+ worker.onmessage = function (event) {
100
+ if (!event.data) {
101
+ _this.onError(new Error('No data received'));
102
+ } else {
103
+ _this.onMessage(event.data);
104
+ }
105
+ };
106
+
107
+ worker.onerror = function (error) {
108
+ _this.onError(_this._getErrorFromErrorEvent(error));
109
+
110
+ _this.terminated = true;
111
+ };
112
+
113
+ worker.onmessageerror = function (event) {
114
+ return console.error(event);
115
+ };
116
+
117
+ return worker;
118
+ }
119
+ }], [{
120
+ key: "isSupported",
121
+ value: function isSupported() {
122
+ return typeof Worker !== 'undefined';
123
+ }
124
+ }]);
125
+ return WorkerThread;
126
+ }();
109
127
 
110
128
  exports.default = WorkerThread;
111
129
  //# sourceMappingURL=worker-thread.js.map
@@ -1 +1 @@
1
- {"version":3,"sources":["../../../../src/lib/worker-farm/worker-thread.ts"],"names":["NOOP","WorkerThread","isSupported","Worker","constructor","props","name","source","url","onMessage","onError","error","console","log","worker","_createBrowserWorker","destroy","terminate","terminated","isRunning","Boolean","postMessage","data","transferList","_getErrorFromErrorEvent","event","message","lineno","colno","Error","_loadableURL","onmessage","onerror","onmessageerror"],"mappings":";;;;;;;;;;;AAAA;;AACA;;AACA;;AAEA,MAAMA,IAAI,GAAG,MAAM,CAAE,CAArB;;AAWe,MAAMC,YAAN,CAAmB;AAWd,SAAXC,WAAW,GAAY;AAC5B,WAAO,OAAOC,MAAP,KAAkB,WAAzB;AACD;;AAEDC,EAAAA,WAAW,CAACC,KAAD,EAA2B;AAAA;AAAA;AAAA;AAAA,sDAXhB,KAWgB;AAAA;AAAA;AAAA;AAAA,wDANP,EAMO;AACpC,UAAM;AAACC,MAAAA,IAAD;AAAOC,MAAAA,MAAP;AAAeC,MAAAA;AAAf,QAAsBH,KAA5B;AACA,wBAAOE,MAAM,IAAIC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AAG,EAAE,KAAKA;AAAhC,KAArB,CAApB;AACA,UAAMM,MAAM,GAAG,IAAIX,MAAJ,CAAW,KAAK2B,YAAhB,EAA8B;AAACxB,MAAAA,IAAI,EAAE,KAAKA;AAAZ,KAA9B,CAAf;;AAEAQ,IAAAA,MAAM,CAACiB,SAAP,GAAoBN,KAAD,IAAW;AAC5B,UAAI,CAACA,KAAK,CAACH,IAAX,EAAiB;AACf,aAAKZ,OAAL,CAAa,IAAImB,KAAJ,CAAU,kBAAV,CAAb;AACD,OAFD,MAEO;AACL,aAAKpB,SAAL,CAAegB,KAAK,CAACH,IAArB;AACD;AACF,KAND;;AAQAR,IAAAA,MAAM,CAACkB,OAAP,GAAkBrB,KAAD,IAA6B;AAC5C,WAAKD,OAAL,CAAa,KAAKc,uBAAL,CAA6Bb,KAA7B,CAAb;AACA,WAAKO,UAAL,GAAkB,IAAlB;AACD,KAHD;;AAKAJ,IAAAA,MAAM,CAACmB,cAAP,GAAyBR,KAAD,IAAWb,OAAO,CAACD,KAAR,CAAcc,KAAd,CAAnC;;AAEA,WAAOX,MAAP;AACD;;AApG+B","sourcesContent":["import {assert} from '../env-utils/assert';\nimport {getLoadableWorkerURL} from '../worker-utils/get-loadable-worker-url';\nimport {getTransferList} from '../worker-utils/get-transfer-list';\n\nconst NOOP = () => {};\n\nexport type WorkerThreadProps = {\n name: string;\n source?: string;\n url?: string;\n};\n\n/**\n * Represents one worker thread\n */\nexport default class WorkerThread {\n readonly name: string;\n readonly source: string | undefined;\n readonly url: string | undefined;\n terminated: boolean = false;\n worker: Worker;\n onMessage: (message: any) => void;\n onError: (error: Error) => void;\n\n private _loadableURL: string = '';\n\n static isSupported(): boolean {\n return typeof Worker !== 'undefined';\n }\n\n constructor(props: WorkerThreadProps) {\n const {name, source, url} = props;\n assert(source || url); // Either source or url must be defined\n this.name = name;\n this.source = source;\n this.url = url;\n this.onMessage = NOOP;\n this.onError = (error) => console.log(error); // eslint-disable-line\n\n this.worker = this._createBrowserWorker();\n }\n\n /**\n * Terminate this worker thread\n * @note Can free up significant memory\n */\n destroy(): void {\n this.onMessage = NOOP;\n this.onError = NOOP;\n // @ts-ignore\n this.worker.terminate();\n this.terminated = true;\n }\n\n get isRunning() {\n return Boolean(this.onMessage);\n }\n\n /**\n * Send a message to this worker thread\n * @param data any data structure, ideally consisting mostly of transferrable objects\n * @param transferList If not supplied, calculated automatically by traversing data\n */\n postMessage(data: any, transferList?: any[]): void {\n transferList = transferList || getTransferList(data);\n // @ts-ignore\n this.worker.postMessage(data, transferList);\n }\n\n // PRIVATE\n\n /**\n * Generate a standard Error from an ErrorEvent\n * @param {ErrorEvent} event\n */\n _getErrorFromErrorEvent(event: ErrorEvent): Error {\n // Note Error object does not have the expected fields if loading failed completely\n // https://developer.mozilla.org/en-US/docs/Web/API/Worker#Event_handlers\n // https://developer.mozilla.org/en-US/docs/Web/API/ErrorEvent\n let message = 'Failed to load ';\n message += `worker ${this.name} from ${this.url}. `;\n if (event.message) {\n message += `${event.message} in `;\n }\n // const hasFilename = event.filename && !event.filename.startsWith('blob:');\n // message += hasFilename ? event.filename : this.source.slice(0, 100);\n if (event.lineno) {\n message += `:${event.lineno}:${event.colno}`;\n }\n return new Error(message);\n }\n\n /**\n * Creates a worker thread on the browser\n */\n _createBrowserWorker() {\n this._loadableURL = getLoadableWorkerURL({source: this.source, url: this.url});\n const worker = new Worker(this._loadableURL, {name: this.name});\n\n worker.onmessage = (event) => {\n if (!event.data) {\n this.onError(new Error('No data received'));\n } else {\n this.onMessage(event.data);\n }\n };\n // This callback represents an uncaught exception in the worker thread\n worker.onerror = (error: ErrorEvent): void => {\n this.onError(this._getErrorFromErrorEvent(error));\n this.terminated = true;\n };\n // TODO - not clear when this would be called, for now just log in case it happens\n worker.onmessageerror = (event) => console.error(event); // eslint-disable-line\n\n return worker;\n }\n}\n"],"file":"worker-thread.js"}
1
+ {"version":3,"sources":["../../../../src/lib/worker-farm/worker-thread.ts"],"names":["NOOP","WorkerThread","props","name","source","url","onMessage","onError","error","console","log","worker","_createBrowserWorker","terminate","terminated","Boolean","data","transferList","postMessage","event","message","lineno","colno","Error","_loadableURL","Worker","onmessage","onerror","_getErrorFromErrorEvent","onmessageerror"],"mappings":";;;;;;;;;;;;;;;AAAA;;AACA;;AACA;;AAEA,IAAMA,IAAI,GAAG,SAAPA,IAAO,GAAM,CAAE,CAArB;;IAWqBC,Y;AAenB,wBAAYC,KAAZ,EAAsC;AAAA;AAAA;AAAA;AAAA;AAAA,sDAXhB,KAWgB;AAAA;AAAA;AAAA;AAAA,wDANP,EAMO;AACpC,QAAOC,IAAP,GAA4BD,KAA5B,CAAOC,IAAP;AAAA,QAAaC,MAAb,GAA4BF,KAA5B,CAAaE,MAAb;AAAA,QAAqBC,GAArB,GAA4BH,KAA5B,CAAqBG,GAArB;AACA,wBAAOD,MAAM,IAAIC,GAAjB;AACA,SAAKF,IAAL,GAAYA,IAAZ;AACA,SAAKC,MAAL,GAAcA,MAAd;AACA,SAAKC,GAAL,GAAWA,GAAX;AACA,SAAKC,SAAL,GAAiBN,IAAjB;;AACA,SAAKO,OAAL,GAAe,UAACC,KAAD;AAAA,aAAWC,OAAO,CAACC,GAAR,CAAYF,KAAZ,CAAX;AAAA,KAAf;;AAEA,SAAKG,MAAL,GAAc,KAAKC,oBAAL,EAAd;AACD;;;;WAMD,mBAAgB;AACd,WAAKN,SAAL,GAAiBN,IAAjB;AACA,WAAKO,OAAL,GAAeP,IAAf;AAEA,WAAKW,MAAL,CAAYE,SAAZ;AACA,WAAKC,UAAL,GAAkB,IAAlB;AACD;;;SAED,eAAgB;AACd,aAAOC,OAAO,CAAC,KAAKT,SAAN,CAAd;AACD;;;WAOD,qBAAYU,IAAZ,EAAuBC,YAAvB,EAAmD;AACjDA,MAAAA,YAAY,GAAGA,YAAY,IAAI,sCAAgBD,IAAhB,CAA/B;AAEA,WAAKL,MAAL,CAAYO,WAAZ,CAAwBF,IAAxB,EAA8BC,YAA9B;AACD;;;WAQD,iCAAwBE,KA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;;AAEA,aAAOR,MAAP;AACD;;;WAzFD,uBAA8B;AAC5B,aAAO,OAAOc,MAAP,KAAkB,WAAzB;AACD","sourcesContent":["import {assert} from '../env-utils/assert';\nimport {getLoadableWorkerURL} from '../worker-utils/get-loadable-worker-url';\nimport {getTransferList} from '../worker-utils/get-transfer-list';\n\nconst NOOP = () => {};\n\nexport type WorkerThreadProps = {\n name: string;\n source?: string;\n url?: string;\n};\n\n/**\n * Represents one worker thread\n */\nexport default class WorkerThread {\n readonly name: string;\n readonly source: string | undefined;\n readonly url: string | undefined;\n terminated: boolean = false;\n worker: Worker;\n onMessage: (message: any) => void;\n onError: (error: Error) => void;\n\n private _loadableURL: string = '';\n\n static isSupported(): boolean {\n return typeof Worker !== 'undefined';\n }\n\n constructor(props: WorkerThreadProps) {\n const {name, source, url} = props;\n assert(source || url); // Either source or url must be defined\n this.name = name;\n this.source = source;\n this.url = url;\n this.onMessage = NOOP;\n this.onError = (error) => console.log(error); // eslint-disable-line\n\n this.worker = this._createBrowserWorker();\n }\n\n /**\n * Terminate this worker thread\n * @note Can free up significant memory\n */\n destroy(): void {\n this.onMessage = NOOP;\n this.onError = NOOP;\n // @ts-ignore\n this.worker.terminate();\n this.terminated = true;\n }\n\n get isRunning() {\n return Boolean(this.onMessage);\n }\n\n /**\n * Send a message to this worker thread\n * @param data any data structure, ideally consisting mostly of transferrable objects\n * @param transferList If not supplied, calculated automatically by traversing data\n */\n postMessage(data: any, transferList?: any[]): void {\n transferList = transferList || getTransferList(data);\n // @ts-ignore\n this.worker.postMessage(data, transferList);\n }\n\n // PRIVATE\n\n /**\n * Generate a standard Error from an ErrorEvent\n * @param {ErrorEvent} event\n */\n _getErrorFromErrorEvent(event: ErrorEvent): Error {\n // Note Error object does not have the expected fields if loading failed completely\n // https://developer.mozilla.org/en-US/docs/Web/API/Worker#Event_handlers\n // https://developer.mozilla.org/en-US/docs/Web/API/ErrorEvent\n let message = 'Failed to load ';\n message += `worker ${this.name} from ${this.url}. `;\n if (event.message) {\n message += `${event.message} in `;\n }\n // const hasFilename = event.filename && !event.filename.startsWith('blob:');\n // message += hasFilename ? event.filename : this.source.slice(0, 100);\n if (event.lineno) {\n message += `:${event.lineno}:${event.colno}`;\n }\n return new Error(message);\n }\n\n /**\n * Creates a worker thread on the browser\n */\n _createBrowserWorker() {\n this._loadableURL = getLoadableWorkerURL({source: this.source, url: this.url});\n const worker = new Worker(this._loadableURL, {name: this.name});\n\n worker.onmessage = (event) => {\n if (!event.data) {\n this.onError(new Error('No data received'));\n } else {\n this.onMessage(event.data);\n }\n };\n // This callback represents an uncaught exception in the worker thread\n worker.onerror = (error: ErrorEvent): void => {\n this.onError(this._getErrorFromErrorEvent(error));\n this.terminated = true;\n };\n // TODO - not clear when this would be called, for now just log in case it happens\n worker.onmessageerror = (event) => console.error(event); // eslint-disable-line\n\n return worker;\n }\n}\n"],"file":"worker-thread.js"}
@@ -7,11 +7,11 @@ exports.getLoadableWorkerURL = getLoadableWorkerURL;
7
7
 
8
8
  var _assert = require("../env-utils/assert");
9
9
 
10
- const workerURLCache = new Map();
10
+ var workerURLCache = new Map();
11
11
 
12
12
  function getLoadableWorkerURL(props) {
13
13
  (0, _assert.assert)(props.source && !props.url || !props.source && props.url);
14
- let workerURL = workerURLCache.get(props.source || props.url);
14
+ var workerURL = workerURLCache.get(props.source || props.url);
15
15
 
16
16
  if (!workerURL) {
17
17
  if (props.url) {
@@ -34,12 +34,12 @@ function getLoadableWorkerURLFromURL(url) {
34
34
  return url;
35
35
  }
36
36
 
37
- const workerSource = buildScriptSource(url);
37
+ var workerSource = buildScriptSource(url);
38
38
  return getLoadableWorkerURLFromSource(workerSource);
39
39
  }
40
40
 
41
41
  function getLoadableWorkerURLFromSource(workerSource) {
42
- const blob = new Blob([workerSource], {
42
+ var blob = new Blob([workerSource], {
43
43
  type: 'application/javascript'
44
44
  });
45
45
  return URL.createObjectURL(blob);