@loaders.gl/core 5.0.0-beta.2 → 5.0.0-beta.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (89) hide show
  1. package/dist/dist.dev.js +1115 -33
  2. package/dist/dist.min.js +3 -3
  3. package/dist/index.cjs +356 -15
  4. package/dist/index.cjs.map +3 -3
  5. package/dist/lib/init.js +1 -1
  6. package/dist/loader-utils/index.cjs +489 -17
  7. package/dist/loader-utils/index.cjs.map +3 -3
  8. package/dist/loader-utils/json-loader.js +1 -1
  9. package/dist/loader-utils/lib/log-utils/log.js +1 -1
  10. package/dist/loader-utils/lib/worker-loader-utils/encode-with-worker.d.ts.map +1 -1
  11. package/dist/loader-utils/lib/worker-loader-utils/encode-with-worker.js +2 -5
  12. package/dist/loader-utils/lib/worker-loader-utils/encode-with-worker.js.map +1 -1
  13. package/dist/loader-utils/lib/worker-loader-utils/parse-with-worker.d.ts.map +1 -1
  14. package/dist/loader-utils/lib/worker-loader-utils/parse-with-worker.js +14 -6
  15. package/dist/loader-utils/lib/worker-loader-utils/parse-with-worker.js.map +1 -1
  16. package/dist/loader-utils/loader-types.d.ts +2 -2
  17. package/dist/loader-utils/loader-types.d.ts.map +1 -1
  18. package/dist/null-loader.js +1 -1
  19. package/dist/null-worker-node.js +398 -32
  20. package/dist/null-worker.js +354 -32
  21. package/dist/worker-utils/index.cjs +385 -43
  22. package/dist/worker-utils/index.cjs.map +3 -3
  23. package/dist/worker-utils/index.d.ts +5 -8
  24. package/dist/worker-utils/index.d.ts.map +1 -1
  25. package/dist/worker-utils/index.js +4 -10
  26. package/dist/worker-utils/index.js.map +1 -1
  27. package/dist/worker-utils/lib/env-utils/version.js +1 -1
  28. package/dist/worker-utils/lib/worker-api/create-worker.d.ts +6 -1
  29. package/dist/worker-utils/lib/worker-api/create-worker.d.ts.map +1 -1
  30. package/dist/worker-utils/lib/worker-api/create-worker.js +87 -1
  31. package/dist/worker-utils/lib/worker-api/create-worker.js.map +1 -1
  32. package/dist/worker-utils/lib/worker-api/process-on-worker.d.ts +18 -2
  33. package/dist/worker-utils/lib/worker-api/process-on-worker.d.ts.map +1 -1
  34. package/dist/worker-utils/lib/worker-api/process-on-worker.js +247 -1
  35. package/dist/worker-utils/lib/worker-api/process-on-worker.js.map +1 -1
  36. package/dist/worker-utils/lib/worker-api/task-runtime.d.ts +36 -0
  37. package/dist/worker-utils/lib/worker-api/task-runtime.d.ts.map +1 -0
  38. package/dist/worker-utils/lib/worker-api/task-runtime.js +23 -0
  39. package/dist/worker-utils/lib/worker-api/task-runtime.js.map +1 -0
  40. package/dist/worker-utils/lib/worker-utils/get-transfer-list.d.ts +1 -0
  41. package/dist/worker-utils/lib/worker-utils/get-transfer-list.d.ts.map +1 -1
  42. package/dist/worker-utils/lib/worker-utils/get-transfer-list.js +3 -1
  43. package/dist/worker-utils/lib/worker-utils/get-transfer-list.js.map +1 -1
  44. package/dist/worker-utils/null-worker-node.js +397 -31
  45. package/dist/worker-utils/null-worker-node.js.map +4 -4
  46. package/dist/worker-utils/null-worker.js +353 -31
  47. package/dist/worker-utils/null-worker.js.map +4 -4
  48. package/dist/worker-utils/types.d.ts +82 -1
  49. package/dist/worker-utils/types.d.ts.map +1 -1
  50. package/package.json +5 -5
  51. package/src/loader-utils/lib/worker-loader-utils/encode-with-worker.ts +5 -6
  52. package/src/loader-utils/lib/worker-loader-utils/parse-with-worker.ts +14 -6
  53. package/src/loader-utils/loader-types.ts +2 -2
  54. package/src/worker-utils/index.ts +17 -17
  55. package/src/worker-utils/lib/worker-api/create-worker.ts +102 -1
  56. package/src/worker-utils/lib/worker-api/process-on-worker.ts +309 -7
  57. package/src/worker-utils/lib/worker-api/task-runtime.ts +66 -0
  58. package/src/worker-utils/lib/worker-utils/get-transfer-list.ts +2 -0
  59. package/src/worker-utils/types.ts +109 -14
  60. package/dist/worker-utils/lib/async-queue/async-queue.d.ts +0 -2
  61. package/dist/worker-utils/lib/async-queue/async-queue.d.ts.map +0 -1
  62. package/dist/worker-utils/lib/async-queue/async-queue.js +0 -5
  63. package/dist/worker-utils/lib/async-queue/async-queue.js.map +0 -1
  64. package/dist/worker-utils/lib/worker-farm/worker-body.d.ts +0 -2
  65. package/dist/worker-utils/lib/worker-farm/worker-body.d.ts.map +0 -1
  66. package/dist/worker-utils/lib/worker-farm/worker-body.js +0 -5
  67. package/dist/worker-utils/lib/worker-farm/worker-body.js.map +0 -1
  68. package/dist/worker-utils/lib/worker-farm/worker-farm.d.ts +0 -2
  69. package/dist/worker-utils/lib/worker-farm/worker-farm.d.ts.map +0 -1
  70. package/dist/worker-utils/lib/worker-farm/worker-farm.js +0 -5
  71. package/dist/worker-utils/lib/worker-farm/worker-farm.js.map +0 -1
  72. package/dist/worker-utils/lib/worker-farm/worker-job.d.ts +0 -2
  73. package/dist/worker-utils/lib/worker-farm/worker-job.d.ts.map +0 -1
  74. package/dist/worker-utils/lib/worker-farm/worker-job.js +0 -5
  75. package/dist/worker-utils/lib/worker-farm/worker-job.js.map +0 -1
  76. package/dist/worker-utils/lib/worker-farm/worker-pool.d.ts +0 -2
  77. package/dist/worker-utils/lib/worker-farm/worker-pool.d.ts.map +0 -1
  78. package/dist/worker-utils/lib/worker-farm/worker-pool.js +0 -5
  79. package/dist/worker-utils/lib/worker-farm/worker-pool.js.map +0 -1
  80. package/dist/worker-utils/lib/worker-farm/worker-thread.d.ts +0 -2
  81. package/dist/worker-utils/lib/worker-farm/worker-thread.d.ts.map +0 -1
  82. package/dist/worker-utils/lib/worker-farm/worker-thread.js +0 -5
  83. package/dist/worker-utils/lib/worker-farm/worker-thread.js.map +0 -1
  84. package/src/worker-utils/lib/async-queue/async-queue.ts +0 -5
  85. package/src/worker-utils/lib/worker-farm/worker-body.ts +0 -5
  86. package/src/worker-utils/lib/worker-farm/worker-farm.ts +0 -5
  87. package/src/worker-utils/lib/worker-farm/worker-job.ts +0 -5
  88. package/src/worker-utils/lib/worker-farm/worker-pool.ts +0 -5
  89. package/src/worker-utils/lib/worker-farm/worker-thread.ts +0 -5
@@ -1,4 +1,80 @@
1
1
  "use strict";
2
+ var __create = Object.create;
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
7
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
8
+ var __esm = (fn, res, err) => function __init() {
9
+ if (err) throw err[0];
10
+ try {
11
+ return fn && (res = (0, fn[__getOwnPropNames(fn)[0]])(fn = 0)), res;
12
+ } catch (e) {
13
+ throw err = [e], e;
14
+ }
15
+ };
16
+ var __export = (target, all) => {
17
+ for (var name in all)
18
+ __defProp(target, name, { get: all[name], enumerable: true });
19
+ };
20
+ var __copyProps = (to, from, except, desc) => {
21
+ if (from && typeof from === "object" || typeof from === "function") {
22
+ for (let key of __getOwnPropNames(from))
23
+ if (!__hasOwnProp.call(to, key) && key !== except)
24
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
25
+ }
26
+ return to;
27
+ };
28
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
29
+ // If the importer is in node compatibility mode or this is not an ESM
30
+ // file that has been converted to a CommonJS file using a Babel-
31
+ // compatible transform (i.e. "__esModule" has not been set), then set
32
+ // "default" to the CommonJS "module.exports" for node compatibility.
33
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
34
+ mod
35
+ ));
36
+
37
+ // src/worker-utils/lib/node/worker_threads.ts
38
+ var worker_threads_exports = {};
39
+ __export(worker_threads_exports, {
40
+ BroadcastChannel: () => import_worker_threads3.BroadcastChannel,
41
+ MessageChannel: () => import_worker_threads3.MessageChannel,
42
+ MessagePort: () => import_worker_threads3.MessagePort,
43
+ NodeWorker: () => NodeWorker,
44
+ SHARE_ENV: () => import_worker_threads3.SHARE_ENV,
45
+ Worker: () => import_worker_threads3.Worker,
46
+ getEnvironmentData: () => import_worker_threads3.getEnvironmentData,
47
+ isInternalThread: () => isInternalThread2,
48
+ isMainThread: () => import_worker_threads3.isMainThread,
49
+ isMarkedAsUntransferable: () => import_worker_threads3.isMarkedAsUntransferable,
50
+ locks: () => locks2,
51
+ markAsUncloneable: () => import_worker_threads3.markAsUncloneable,
52
+ markAsUntransferable: () => import_worker_threads3.markAsUntransferable,
53
+ moveMessagePortToContext: () => import_worker_threads3.moveMessagePortToContext,
54
+ parentPort: () => parentPort2,
55
+ postMessageToThread: () => import_worker_threads3.postMessageToThread,
56
+ receiveMessageOnPort: () => import_worker_threads3.receiveMessageOnPort,
57
+ resourceLimits: () => import_worker_threads3.resourceLimits,
58
+ setEnvironmentData: () => import_worker_threads3.setEnvironmentData,
59
+ structuredClone: () => structuredClone3,
60
+ threadId: () => import_worker_threads3.threadId,
61
+ threadName: () => threadName2,
62
+ workerData: () => import_worker_threads3.workerData
63
+ });
64
+ var WorkerThreads, import_worker_threads3, parentPort2, NodeWorker, isInternalThread2, threadName2, locks2, structuredClone3;
65
+ var init_worker_threads = __esm({
66
+ "src/worker-utils/lib/node/worker_threads.ts"() {
67
+ "use strict";
68
+ WorkerThreads = __toESM(require("worker_threads"), 1);
69
+ import_worker_threads3 = require("worker_threads");
70
+ parentPort2 = WorkerThreads == null ? void 0 : WorkerThreads.parentPort;
71
+ NodeWorker = WorkerThreads.Worker;
72
+ isInternalThread2 = WorkerThreads.isInternalThread;
73
+ threadName2 = WorkerThreads.threadName;
74
+ locks2 = WorkerThreads.locks;
75
+ structuredClone3 = WorkerThreads.structuredClone;
76
+ }
77
+ });
2
78
 
3
79
  // ../../node_modules/@math.gl/core/dist/worker-utils/lib/node/worker_threads.js
4
80
  var import_worker_threads = require("worker_threads");
@@ -17,6 +93,9 @@ function getTransferList(object, recursive = true, transfers) {
17
93
  else if (ArrayBuffer.isView(value)) {
18
94
  if (isTransferable(value.buffer))
19
95
  transfersSet.add(value.buffer);
96
+ } else if (recursive && typeof MessageChannel !== "undefined" && value instanceof MessageChannel) {
97
+ visit(value.port1);
98
+ visit(value.port2);
20
99
  } else if (recursive && value instanceof Map) {
21
100
  for (const [key, item] of value) {
22
101
  visit(key);
@@ -68,16 +147,16 @@ var WorkerBody = class {
68
147
  */
69
148
  static set onmessage(onMessage) {
70
149
  async function handleMessage(message) {
71
- const parentPort2 = await getParentPort();
72
- const { type, payload } = parentPort2 ? message : message.data;
150
+ const parentPort3 = await getParentPort();
151
+ const { type, payload } = parentPort3 ? message : message.data;
73
152
  onMessage(type, payload);
74
153
  }
75
- getParentPort().then((parentPort2) => {
76
- if (parentPort2) {
77
- parentPort2.on("message", (message) => {
154
+ getParentPort().then((parentPort3) => {
155
+ if (parentPort3) {
156
+ parentPort3.on("message", (message) => {
78
157
  handleMessage(message);
79
158
  });
80
- parentPort2.on("exit", () => console.debug("Node worker closing"));
159
+ parentPort3.on("exit", () => console.debug("Node worker closing"));
81
160
  } else {
82
161
  globalThis.onmessage = handleMessage;
83
162
  }
@@ -95,9 +174,9 @@ var WorkerBody = class {
95
174
  };
96
175
  onMessageWrapperMap.set(onMessage, onMessageWrapper);
97
176
  }
98
- const parentPort2 = await getParentPort();
99
- if (parentPort2) {
100
- parentPort2.on("message", onMessageWrapper);
177
+ const parentPort3 = await getParentPort();
178
+ if (parentPort3) {
179
+ parentPort3.on("message", onMessageWrapper);
101
180
  } else {
102
181
  globalThis.addEventListener("message", onMessageWrapper);
103
182
  }
@@ -108,9 +187,9 @@ var WorkerBody = class {
108
187
  if (!onMessageWrapper) {
109
188
  return;
110
189
  }
111
- const parentPort2 = await getParentPort();
112
- if (parentPort2) {
113
- parentPort2.off("message", onMessageWrapper);
190
+ const parentPort3 = await getParentPort();
191
+ if (parentPort3) {
192
+ parentPort3.off("message", onMessageWrapper);
114
193
  } else {
115
194
  globalThis.removeEventListener("message", onMessageWrapper);
116
195
  }
@@ -120,12 +199,12 @@ var WorkerBody = class {
120
199
  * @param type
121
200
  * @param payload
122
201
  */
123
- static async postMessage(type, payload) {
202
+ static async postMessage(type, payload, copy = false) {
124
203
  const data = { source: "loaders.gl", type, payload };
125
- const transferList = getTransferList(payload);
126
- const parentPort2 = await getParentPort();
127
- if (parentPort2) {
128
- parentPort2.postMessage(data, transferList);
204
+ const transferList = copy ? [] : getTransferList(payload);
205
+ const parentPort3 = await getParentPort();
206
+ if (parentPort3) {
207
+ parentPort3.postMessage(data, transferList);
129
208
  } else {
130
209
  globalThis.postMessage(data, transferList);
131
210
  }
@@ -245,12 +324,19 @@ var SharedWorkerState = class {
245
324
  }
246
325
  };
247
326
 
327
+ // ../../node_modules/@math.gl/core/dist/worker-utils/lib/worker-api/validate-output-ownership.js
328
+ function validateOutputOwnership(ownership) {
329
+ if (ownership !== void 0 && ownership !== "copy" && ownership !== "transfer") {
330
+ throw new Error("Invalid worker output ownership");
331
+ }
332
+ }
333
+
248
334
  // ../../node_modules/@math.gl/core/dist/worker-utils/lib/worker-api/create-worker.js
249
335
  var requestId = 0;
250
336
  var activeController = null;
251
337
  var activeInputBatches = null;
252
338
  var activeOutputAcknowledgements = null;
253
- async function createWorker(process, processInBatches2) {
339
+ async function createWorker(process2, processInBatches2, preload) {
254
340
  if (!await WorkerBody.inWorkerThread()) {
255
341
  return;
256
342
  }
@@ -258,7 +344,7 @@ async function createWorker(process, processInBatches2) {
258
344
  process: processOnMainThread
259
345
  };
260
346
  WorkerBody.onmessage = async (type, payload) => {
261
- var _a;
347
+ var _a, _b, _c, _d, _e;
262
348
  if (type === "cancel") {
263
349
  activeController == null ? void 0 : activeController.abort();
264
350
  activeInputBatches == null ? void 0 : activeInputBatches.close();
@@ -292,23 +378,26 @@ async function createWorker(process, processInBatches2) {
292
378
  }
293
379
  switch (type) {
294
380
  case "preload":
381
+ await (preload == null ? void 0 : preload());
295
382
  WorkerBody.postMessage("done", {});
296
383
  break;
297
384
  case "process":
298
385
  throwIfAborted();
299
- if (!process) {
386
+ validateOutputOwnership((_b = payload.options) == null ? void 0 : _b["outputOwnership"]);
387
+ if (!process2) {
300
388
  throw new Error("Worker does not support atomic processing");
301
389
  }
302
- const result = await process(payload.input, payload.options || {}, jobContext, payload.context || {});
390
+ const result = await process2(payload.input, payload.options || {}, jobContext, payload.context || {});
303
391
  throwIfAborted();
304
392
  reporter == null ? void 0 : reporter.finish();
305
393
  await WorkerBody.postMessage("done", {
306
394
  result,
307
395
  executionMs: performance.now() - startedAt
308
- });
396
+ }, ((_c = payload.options) == null ? void 0 : _c["outputOwnership"]) === "copy");
309
397
  break;
310
398
  case "process-in-batches":
311
399
  throwIfAborted();
400
+ validateOutputOwnership((_d = payload.options) == null ? void 0 : _d["outputOwnership"]);
312
401
  if (!processInBatches2) {
313
402
  throw new Error("Worker does not support batched processing");
314
403
  }
@@ -320,7 +409,7 @@ async function createWorker(process, processInBatches2) {
320
409
  const resultIterator = processInBatches2(createDemandDrivenIterator(inputBatches), payload.options || {}, jobContext, payload.context || {});
321
410
  for await (const batch of resultIterator) {
322
411
  throwIfAborted();
323
- await WorkerBody.postMessage("output-batch", { result: batch });
412
+ await WorkerBody.postMessage("output-batch", { result: batch }, ((_e = payload.options) == null ? void 0 : _e["outputOwnership"]) === "copy");
324
413
  await outputAcknowledgements.next();
325
414
  }
326
415
  throwIfAborted();
@@ -418,14 +507,14 @@ function processOnMainThread(arrayBuffer, options = {}, jobContext = {}) {
418
507
  });
419
508
  }
420
509
  function createProgressReporter() {
421
- let pending;
510
+ let pending2;
422
511
  let finished = false;
423
512
  let scheduled = false;
424
513
  const flush = () => {
425
514
  scheduled = false;
426
- if (pending) {
427
- WorkerBody.postMessage("progress", { progress: pending });
428
- pending = void 0;
515
+ if (pending2) {
516
+ WorkerBody.postMessage("progress", { progress: pending2 });
517
+ pending2 = void 0;
429
518
  }
430
519
  };
431
520
  return {
@@ -433,9 +522,9 @@ function createProgressReporter() {
433
522
  report(progress) {
434
523
  if (finished)
435
524
  return;
436
- if (pending && pending.phase !== progress.phase)
525
+ if (pending2 && pending2.phase !== progress.phase)
437
526
  flush();
438
- pending = { ...progress };
527
+ pending2 = { ...progress };
439
528
  if (!scheduled) {
440
529
  scheduled = true;
441
530
  setTimeout(flush, 0);
@@ -444,7 +533,7 @@ function createProgressReporter() {
444
533
  /** Flushes final progress and suppresses reports from stale callbacks. */
445
534
  finish(discard = false) {
446
535
  if (discard)
447
- pending = void 0;
536
+ pending2 = void 0;
448
537
  flush();
449
538
  finished = true;
450
539
  }
@@ -456,8 +545,285 @@ var import_meta = {};
456
545
  var path = typeof __filename === "string" ? __filename.replace(/\\/g, "/") : "";
457
546
  var moduleUrl = path ? `file://${path.startsWith("/") ? "" : "/"}${encodeURI(path).replace(/#/g, "%23").replace(/\?/g, "%3F")}` : import_meta.url;
458
547
 
548
+ // ../../node_modules/@math.gl/core/dist/tasks/task-preload.js
549
+ var pending = /* @__PURE__ */ new WeakMap();
550
+ var loaded = /* @__PURE__ */ new WeakSet();
551
+ function preloadTaskCode(task, signal) {
552
+ if (!task.preload)
553
+ return Promise.resolve();
554
+ let promise = pending.get(task);
555
+ if (!promise) {
556
+ promise = Promise.resolve().then(() => task.preload()).then(() => {
557
+ loaded.add(task);
558
+ });
559
+ pending.set(task, promise);
560
+ void promise.catch(() => pending.delete(task));
561
+ }
562
+ return withSignal(promise, signal);
563
+ }
564
+ function withSignal(operation, signal) {
565
+ if (!signal)
566
+ return operation;
567
+ return new Promise((resolve, reject) => {
568
+ const abort = () => {
569
+ signal.removeEventListener("abort", abort);
570
+ reject(signal.reason);
571
+ };
572
+ signal.addEventListener("abort", abort, { once: true });
573
+ if (signal.aborted)
574
+ abort();
575
+ operation.then((value) => {
576
+ signal.removeEventListener("abort", abort);
577
+ resolve(value);
578
+ }, (error) => {
579
+ signal.removeEventListener("abort", abort);
580
+ reject(error);
581
+ });
582
+ });
583
+ }
584
+
585
+ // ../../node_modules/@math.gl/core/dist/tasks/task-runtime.js
586
+ function assertPayload(value) {
587
+ if (!value || typeof value !== "object" || Object.getPrototypeOf(value) !== Object.prototype) {
588
+ throw new TypeError("Task payloads must be plain objects with named fields");
589
+ }
590
+ }
591
+ function taskContext(state, context = {}) {
592
+ const signal = context.signal ?? new AbortController().signal;
593
+ return {
594
+ state,
595
+ signal,
596
+ throwIfAborted: () => {
597
+ var _a;
598
+ signal.throwIfAborted();
599
+ (_a = context.throwIfAborted) == null ? void 0 : _a.call(context);
600
+ },
601
+ reportProgress: (progress) => {
602
+ var _a;
603
+ return (_a = context.reportProgress) == null ? void 0 : _a.call(context, {
604
+ ...progress,
605
+ phase: progress.phase ?? "process",
606
+ unit: progress.unit ?? "items"
607
+ });
608
+ }
609
+ };
610
+ }
611
+ var TaskRuntime = class {
612
+ constructor(task) {
613
+ this.task = task;
614
+ this.initialized = false;
615
+ }
616
+ async initialize(input, context = {}) {
617
+ var _a, _b, _c;
618
+ if (this.initialized)
619
+ throw new Error("Task is already initialized");
620
+ assertPayload(input);
621
+ await preloadTaskCode(this.task, context.signal);
622
+ (_a = context.signal) == null ? void 0 : _a.throwIfAborted();
623
+ this.state = await ((_c = (_b = this.task).initialize) == null ? void 0 : _c.call(_b, input, taskContext(void 0, context)));
624
+ this.initialized = true;
625
+ return {};
626
+ }
627
+ async destroy() {
628
+ var _a, _b;
629
+ if (this.initialized) {
630
+ this.initialized = false;
631
+ const state = this.state;
632
+ this.state = void 0;
633
+ if (this.task.initialize)
634
+ await ((_b = (_a = this.task).destroy) == null ? void 0 : _b.call(_a, state));
635
+ }
636
+ return {};
637
+ }
638
+ async process(input, options, context = {}) {
639
+ if (!this.initialized)
640
+ throw new Error("Task is not initialized");
641
+ if (!this.task.process)
642
+ throw new Error("Task has no atomic processor");
643
+ assertPayload(input);
644
+ const result = await this.task.process(input, options, taskContext(this.state, context));
645
+ assertPayload(result);
646
+ return result;
647
+ }
648
+ async *stream(inputs, options, context = {}) {
649
+ if (!this.initialized)
650
+ throw new Error("Task is not initialized");
651
+ if (!this.task.processInBatches)
652
+ throw new Error("Task has no streaming processor");
653
+ for await (const output of this.task.processInBatches(inputs, options, taskContext(this.state, context))) {
654
+ assertPayload(output);
655
+ yield output;
656
+ }
657
+ }
658
+ };
659
+ async function initializeTaskWorker(task) {
660
+ if (!await WorkerBody.inWorkerThread())
661
+ throw new Error("initializeTaskWorker must run inside a worker");
662
+ if (installed)
663
+ throw new Error("A task is already installed in this worker");
664
+ installed = true;
665
+ const runtime = new TaskRuntime(task);
666
+ let streamOptions = {};
667
+ return createWorker(async (request, _options, context) => {
668
+ switch (request.operation) {
669
+ case "initialize":
670
+ return runtime.initialize(request.input, context);
671
+ case "destroy":
672
+ return runtime.destroy();
673
+ case "stream-options":
674
+ streamOptions = request.options;
675
+ return {};
676
+ case "process":
677
+ return runtime.process(request.input, request.options, context);
678
+ default:
679
+ throw new Error("Unknown task operation");
680
+ }
681
+ }, async function* (inputs, _options, context) {
682
+ async function* batches() {
683
+ for await (const input of inputs) {
684
+ assertPayload(input);
685
+ yield input;
686
+ }
687
+ }
688
+ try {
689
+ yield* runtime.stream(batches(), streamOptions, context);
690
+ } finally {
691
+ streamOptions = {};
692
+ }
693
+ }, () => preloadTaskCode(task));
694
+ }
695
+ var installed = false;
696
+
697
+ // src/worker-utils/lib/worker-api/task-runtime.ts
698
+ function normalizeTaskProgress(progress) {
699
+ return { ...progress, phase: progress.phase ?? "process", unit: progress.unit ?? "items" };
700
+ }
701
+ async function isWorkerThread() {
702
+ var _a;
703
+ if (typeof self !== "undefined" && typeof document === "undefined" && typeof self.postMessage === "function")
704
+ return true;
705
+ if (typeof process !== "undefined" && ((_a = process.versions) == null ? void 0 : _a.node)) {
706
+ const { isMainThread: isMainThread2 } = await Promise.resolve().then(() => (init_worker_threads(), worker_threads_exports));
707
+ return !isMainThread2;
708
+ }
709
+ return false;
710
+ }
711
+ var initializeTaskWorker2 = initializeTaskWorker;
712
+
713
+ // src/worker-utils/lib/worker-utils/get-transfer-list.ts
714
+ function getTransferList2(object, recursive = true, transfers) {
715
+ const transfersSet = transfers || /* @__PURE__ */ new Set();
716
+ if (!object) {
717
+ } else if (isTransferable2(object)) {
718
+ transfersSet.add(object);
719
+ } else if (isTransferable2(object.buffer)) {
720
+ transfersSet.add(object.buffer);
721
+ } else if (ArrayBuffer.isView(object)) {
722
+ } else if (recursive && typeof object === "object") {
723
+ for (const key in object) {
724
+ getTransferList2(object[key], recursive, transfersSet);
725
+ }
726
+ }
727
+ return transfers === void 0 ? Array.from(transfersSet) : [];
728
+ }
729
+ function isTransferable2(object) {
730
+ if (!object) {
731
+ return false;
732
+ }
733
+ if (object instanceof ArrayBuffer) {
734
+ return true;
735
+ }
736
+ if (typeof MessagePort !== "undefined" && object instanceof MessagePort) {
737
+ return true;
738
+ }
739
+ if (typeof ImageBitmap !== "undefined" && object instanceof ImageBitmap) {
740
+ return true;
741
+ }
742
+ if (typeof OffscreenCanvas !== "undefined" && object instanceof OffscreenCanvas) {
743
+ return true;
744
+ }
745
+ return false;
746
+ }
747
+
748
+ // src/worker-utils/lib/worker-api/create-worker.ts
749
+ async function createWorker2(process2, processInBatches2) {
750
+ if (!await isWorkerThread()) return;
751
+ await initializeTaskWorker2(createWorkerTask(process2, processInBatches2));
752
+ }
753
+ function createWorkerTask(process2, processInBatches2) {
754
+ return {
755
+ id: "loaders.gl",
756
+ initialize: (state) => state,
757
+ destroy: (state) => {
758
+ var _a;
759
+ return (_a = state.port) == null ? void 0 : _a.close();
760
+ },
761
+ process: async ({ value }, options, context) => {
762
+ if (!process2) throw new Error("Worker does not support atomic processing");
763
+ return { value: await process2(value, options, createContext(context), context.state.context) };
764
+ },
765
+ ...processInBatches2 ? {
766
+ processInBatches: async function* (inputs, options, context) {
767
+ async function* values() {
768
+ for await (const input of inputs) yield input.value;
769
+ }
770
+ for await (const value of processInBatches2(
771
+ values(),
772
+ options,
773
+ createContext(context),
774
+ context.state.context
775
+ )) {
776
+ yield { value };
777
+ }
778
+ }
779
+ } : {}
780
+ };
781
+ }
782
+ function createContext(context) {
783
+ let requestIdentifier = 0;
784
+ const port = context.state.port;
785
+ return {
786
+ signal: context.signal,
787
+ throwIfAborted: () => context.throwIfAborted(),
788
+ reportProgress: (progress) => context.reportProgress(normalizeTaskProgress(progress)),
789
+ process: port ? (input, options = {}, jobContext = {}) => {
790
+ context.throwIfAborted();
791
+ const identifier = ++requestIdentifier;
792
+ return new Promise((resolve, reject) => {
793
+ const onMessage = (event) => {
794
+ if (event.data.identifier !== identifier) return;
795
+ cleanup();
796
+ if (event.data.error) {
797
+ const error = new Error(event.data.error.message);
798
+ error.name = event.data.error.name;
799
+ reject(error);
800
+ } else resolve(event.data.value);
801
+ };
802
+ const onAbort = () => {
803
+ cleanup();
804
+ reject(context.signal.reason);
805
+ };
806
+ const cleanup = () => {
807
+ port.removeEventListener("message", onMessage);
808
+ context.signal.removeEventListener("abort", onAbort);
809
+ };
810
+ port.addEventListener("message", onMessage);
811
+ context.signal.addEventListener("abort", onAbort, { once: true });
812
+ port.start();
813
+ try {
814
+ const request = { identifier, input, options, context: jobContext };
815
+ port.postMessage(request, getTransferList2(request));
816
+ } catch (error) {
817
+ cleanup();
818
+ reject(error);
819
+ }
820
+ });
821
+ } : void 0
822
+ };
823
+ }
824
+
459
825
  // src/worker-utils/workers/null-worker.ts
460
- createWorker(
826
+ createWorker2(
461
827
  async (data) => {
462
828
  return data;
463
829
  },