@atolljs/core 0.1.13 → 0.1.15

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 (40) hide show
  1. package/dist/contract/listSchema.js +1 -2
  2. package/dist/contract/memoryDevtools.js +2 -3
  3. package/dist/contract/msgpackrCodec.js +1 -2
  4. package/dist/contract/reef.js +1 -2
  5. package/dist/contract/sharedMemory.js +61 -62
  6. package/dist/contract/types.d.ts +24 -0
  7. package/dist/contract/zod.js +1 -2
  8. package/dist/devtools.js +32 -39
  9. package/dist/index.d.ts +1 -1
  10. package/dist/index.js +22 -22
  11. package/dist/log.js +1 -2
  12. package/dist/observable.js +18 -19
  13. package/dist/pool/dedicatedWorker.d.ts +3 -0
  14. package/dist/pool/dedicatedWorker.js +72 -68
  15. package/dist/pool/devtoolsCommands.js +1 -2
  16. package/dist/pool/errors.js +1 -2
  17. package/dist/pool/memory.js +1 -2
  18. package/dist/pool/subWorkerClient.js +1 -2
  19. package/dist/pool/workerChannel.js +21 -22
  20. package/dist/pool/workerClient.d.ts +15 -1
  21. package/dist/pool/workerClient.js +14 -10
  22. package/dist/pool/workerPool.d.ts +3 -0
  23. package/dist/pool/workerPool.js +119 -121
  24. package/dist/reactive.js +1 -2
  25. package/dist/reactiveGraph.js +1 -2
  26. package/dist/service.js +1 -2
  27. package/dist/shared/sharedWorkerClient.js +2 -9
  28. package/dist/shared/sharedWorkerHost.js +22 -27
  29. package/dist/task.js +1 -2
  30. package/dist/testing/inProcessWorker.js +3 -8
  31. package/dist/userTiming.js +1 -2
  32. package/dist/worker/defineWorker.js +8 -9
  33. package/dist/worker/registry.js +1 -2
  34. package/dist/worker/workerBootstrap.js +40 -52
  35. package/package.json +11 -7
  36. package/src/contract/types.ts +26 -0
  37. package/src/index.ts +2 -0
  38. package/src/pool/dedicatedWorker.ts +19 -1
  39. package/src/pool/workerClient.ts +30 -1
  40. package/src/pool/workerPool.ts +11 -1
@@ -1,11 +1,11 @@
1
- import { devtoolsEnabled as e, emitDevtools as t, estimateCloneBytes as n, nextDevtoolsId as r } from "../devtools.js";
2
- import { fmtBytes as i, scoped as a } from "../log.js";
3
- import { PoolQueueFullError as o, TaskAbortedError as s, TaskTimeoutError as c, WorkerCrashedError as l } from "./errors.js";
4
- import { measureTaskCall as u } from "../userTiming.js";
5
- import { forwardWorkerDevtools as d, sendInit as f, setupChannelMemory as p } from "./workerChannel.js";
1
+ import { PoolQueueFullError as e, TaskAbortedError as t, TaskTimeoutError as n, WorkerCrashedError as r } from "./errors.js";
2
+ import { devtoolsEnabled as i, emitDevtools as a, estimateCloneBytes as o, nextDevtoolsId as s } from "../devtools.js";
3
+ import { measureTaskCall as c } from "../userTiming.js";
4
+ import { forwardWorkerDevtools as l, sendInit as u, setupChannelMemory as d } from "./workerChannel.js";
5
+ import { fmtBytes as f, scoped as p } from "../log.js";
6
6
  import { CHAOS_FAILURE as m, KILL_MESSAGE as h, applyChaos as g, registerDevtoolsRunner as _ } from "./devtoolsCommands.js";
7
- //#region src/pool/workerPool.ts
8
- var v = a("pool"), y = a("task"), b = () => ({
7
+ // src/pool/workerPool.ts
8
+ var v = p("pool"), y = p("task"), b = () => ({
9
9
  count: 0,
10
10
  mean: 0,
11
11
  max: 0,
@@ -25,6 +25,7 @@ var v = a("pool"), y = a("task"), b = () => ({
25
25
  concurrency;
26
26
  maxQueue;
27
27
  respawn;
28
+ onLifecycle;
28
29
  taskTimeout;
29
30
  closed = !1;
30
31
  chaos = null;
@@ -42,21 +43,21 @@ var v = a("pool"), y = a("task"), b = () => ({
42
43
  nextCallId = 0;
43
44
  sharedMemory;
44
45
  persistence;
45
- constructor(n) {
46
- let a = p(n, "WorkerPool");
47
- if (!n.workerUrl && !n.createWorker) throw Error("WorkerPool requires either `workerUrl` or `createWorker` in its config.");
48
- this.workerUrl = n.workerUrl, this.spawn = n.createWorker ?? (() => new Worker(this.workerUrl, { type: "module" })), this.concurrency = n.concurrency ?? 1, this.maxQueue = n.maxQueue ?? Infinity, this.respawn = n.respawn ?? !0, this.taskTimeout = n.taskTimeout, this.label = n.name, this.poolId = r("pool", n.name), this.mem = a, this.sharedMemory = a.sharedMemory, this.memoryManager = a.memoryManager, this.injectedBuffer = a.injectedBuffer, this.persistence = a.persistence;
49
- let o = n.poolSize ?? "auto", s = o === "auto" ? typeof navigator < "u" ? navigator.hardwareConcurrency ?? 4 : 4 : o;
50
- v.info(this.sharedMemory ? `spawning ${s} worker(s) with ${i(this.sharedMemory.totalBytes)} shared buffer` : this.injectedBuffer ? `spawning ${s} worker(s) with ${i(this.injectedBuffer.byteLength)} inherited buffer` : `spawning ${s} worker(s), message-only`, { workerUrl: n.workerUrl ? String(n.workerUrl) : "createWorker()" }), v.debug("tip: setLogLevel('trace') for per-task detail"), t({
46
+ constructor(e) {
47
+ let t = d(e, "WorkerPool");
48
+ if (!e.workerUrl && !e.createWorker) throw Error("WorkerPool requires either `workerUrl` or `createWorker` in its config.");
49
+ this.workerUrl = e.workerUrl, this.spawn = e.createWorker ?? (() => new Worker(this.workerUrl, { type: "module" })), this.concurrency = e.concurrency ?? 1, this.maxQueue = e.maxQueue ?? Infinity, this.respawn = e.respawn ?? !0, this.onLifecycle = e.onLifecycle, this.taskTimeout = e.taskTimeout, this.label = e.name, this.poolId = s("pool", e.name), this.mem = t, this.sharedMemory = t.sharedMemory, this.memoryManager = t.memoryManager, this.injectedBuffer = t.injectedBuffer, this.persistence = t.persistence;
50
+ let n = e.poolSize ?? "auto", r = n === "auto" ? typeof navigator < "u" ? navigator.hardwareConcurrency ?? 4 : 4 : n;
51
+ v.info(this.sharedMemory ? `spawning ${r} worker(s) with ${f(this.sharedMemory.totalBytes)} shared buffer` : this.injectedBuffer ? `spawning ${r} worker(s) with ${f(this.injectedBuffer.byteLength)} inherited buffer` : `spawning ${r} worker(s), message-only`, { workerUrl: e.workerUrl ? String(e.workerUrl) : "createWorker()" }), v.debug("tip: setLogLevel('trace') for per-task detail"), a({
51
52
  type: "pool:init",
52
53
  poolId: this.poolId,
53
54
  label: this.label,
54
- poolSize: s,
55
+ poolSize: r,
55
56
  concurrency: this.concurrency,
56
57
  memoryBytes: this.sharedMemory?.totalBytes ?? this.injectedBuffer?.byteLength
57
58
  });
58
- for (let e = 0; e < s; e++) this.addWorker(this.spawn());
59
- e() && (this.devtoolsOff = _({
59
+ for (let e = 0; e < r; e++) this.addWorker(this.spawn());
60
+ i() && (this.devtoolsOff = _({
60
61
  poolId: this.poolId,
61
62
  label: this.label,
62
63
  dedicated: !1,
@@ -72,100 +73,98 @@ var v = a("pool"), y = a("task"), b = () => ({
72
73
  this.chaos = e;
73
74
  }
74
75
  }));
75
- let c = n.tasks ?? {};
76
- for (let [e, t] of Object.entries(c)) {
77
- if (e in this) {
78
- v.warn(`task "${e}" collides with a WorkerPool member — not installed`);
79
- continue;
80
- }
81
- this[e] = (...e) => this.runTask(t, ...e);
82
- }
83
- Object.keys(c).length > 0 && v.info(`task methods: ${Object.keys(c).join(", ")}`);
76
+ let o = e.tasks ?? {};
77
+ for (let [e, t] of Object.entries(o)) e in this ? v.warn(`task "${e}" collides with a WorkerPool member — not installed`) : this[e] = (...e) => this.runTask(t, ...e);
78
+ Object.keys(o).length > 0 && v.info(`task methods: ${Object.keys(o).join(", ")}`);
84
79
  }
85
80
  addWorker(e) {
86
- let n = {
81
+ let t = {
87
82
  worker: e,
88
83
  inFlight: 0
89
84
  };
90
85
  e.addEventListener("message", (e) => {
91
- let t = e.data;
92
- if (t.type === "ATOLL_DEVTOOLS" && t.event) {
93
- d(this.poolId, this.slots.indexOf(n), t.event);
94
- return;
95
- }
96
- this.handleReply(n, t);
86
+ let n = e.data;
87
+ n.type === "ATOLL_DEVTOOLS" && n.event ? l(this.poolId, this.slots.indexOf(t), n.event) : this.handleReply(t, n);
97
88
  }), e.addEventListener("error", (e) => {
98
- this.handleWorkerError(n, e);
89
+ this.handleWorkerError(t, e);
99
90
  });
100
- let r = f(e, this.mem);
101
- return this.slots.push(n), v.debug(`worker spawned, ${r} sent`), t({
91
+ let n = u(e, this.mem);
92
+ return this.slots.push(t), v.debug(`worker spawned, ${n} sent`), a({
102
93
  type: "worker:spawn",
103
94
  poolId: this.poolId,
104
- slot: this.slots.indexOf(n)
105
- }), n;
95
+ slot: this.slots.indexOf(t)
96
+ }), t;
106
97
  }
107
- handleReply(r, i) {
108
- let a = i.messageId === void 0 ? void 0 : this.pending.get(i.messageId);
109
- if (a) {
110
- if (this.pending.delete(i.messageId), r.inFlight--, this.detach(a), !a.done) {
111
- a.done = !0;
112
- let o = a.sentAt ?? a.enqueuedAt, s = performance.now() - o;
113
- this.statsAgg.runMs.add(s), i.success ? (this.statsAgg.completed++, y.debug(`← ${a.contract.taskId} ok`), e() && (t({
98
+ handleReply(e, t) {
99
+ let n = t.messageId === void 0 ? void 0 : this.pending.get(t.messageId);
100
+ if (n) {
101
+ if (this.pending.delete(t.messageId), e.inFlight--, this.detach(n), !n.done) {
102
+ n.done = !0;
103
+ let r = n.sentAt ?? n.enqueuedAt, s = performance.now() - r;
104
+ this.statsAgg.runMs.add(s), t.success ? (this.statsAgg.completed++, y.debug(`← ${n.contract.taskId} ok`), i() && (a({
114
105
  type: "task:settle",
115
106
  poolId: this.poolId,
116
- callId: a.callId,
117
- taskId: a.contract.taskId,
107
+ callId: n.callId,
108
+ taskId: n.contract.taskId,
118
109
  outcome: "ok",
119
110
  runMs: s,
120
- resultBytes: n(i.result)
121
- }), u(this.poolId, a.contract.taskId, a.callId, this.slots.indexOf(r), o, o + s, !0)), a.resolve(i.result)) : (this.statsAgg.failed++, y.warn(`← ${a.contract.taskId} failed: ${i.error}`), e() && (t({
111
+ resultBytes: o(t.result)
112
+ }), c(this.poolId, n.contract.taskId, n.callId, this.slots.indexOf(e), r, r + s, !0)), n.resolve(t.result)) : (this.statsAgg.failed++, y.warn(`← ${n.contract.taskId} failed: ${t.error}`), i() && (a({
122
113
  type: "task:settle",
123
114
  poolId: this.poolId,
124
- callId: a.callId,
125
- taskId: a.contract.taskId,
115
+ callId: n.callId,
116
+ taskId: n.contract.taskId,
126
117
  outcome: "error",
127
118
  runMs: s,
128
- error: i.error
129
- }), u(this.poolId, a.contract.taskId, a.callId, this.slots.indexOf(r), o, o + s, !1)), a.reject(Error(i.error)));
119
+ error: t.error
120
+ }), c(this.poolId, n.contract.taskId, n.callId, this.slots.indexOf(e), r, r + s, !1)), n.reject(Error(t.error)));
130
121
  }
131
122
  this.drainQueue(), this.checkDrained();
132
123
  }
133
124
  }
134
- handleWorkerError(e, n) {
125
+ lifecycle(e, t, n) {
126
+ this.onLifecycle?.({
127
+ type: e,
128
+ poolId: this.poolId,
129
+ slot: t,
130
+ message: n
131
+ });
132
+ }
133
+ handleWorkerError(e, t) {
135
134
  if (this.closed) return;
136
- let r = n, i = r?.message ?? (r?.error ? String(r.error) : "worker error");
135
+ let n = t, i = n?.message ?? (n?.error ? String(n.error) : "worker error");
137
136
  v.warn(`worker error: ${i}`);
138
- let a = this.slots.indexOf(e);
139
- t({
137
+ let o = this.slots.indexOf(e);
138
+ a({
140
139
  type: "worker:error",
141
140
  poolId: this.poolId,
142
- slot: a,
141
+ slot: o,
143
142
  message: i
144
- });
145
- for (let [n, r] of [...this.pending]) if (r.slot === e) {
146
- if (this.pending.delete(n), this.detach(r), r.done) continue;
147
- r.done = !0, this.statsAgg.failed++, t({
143
+ }), this.lifecycle("error", o >= 0 ? o : void 0, i);
144
+ for (let [t, n] of [...this.pending]) if (n.slot === e) {
145
+ if (this.pending.delete(t), this.detach(n), n.done) continue;
146
+ n.done = !0, this.statsAgg.failed++, a({
148
147
  type: "task:settle",
149
148
  poolId: this.poolId,
150
- callId: r.callId,
151
- taskId: r.contract.taskId,
149
+ callId: n.callId,
150
+ taskId: n.contract.taskId,
152
151
  outcome: "crashed",
153
152
  error: i
154
- }), r.reject(new l(i));
153
+ }), n.reject(new r(i));
155
154
  }
156
- if (a >= 0 && this.slots.splice(a, 1), e.worker.terminate(), this.respawn) v.info("respawning crashed worker"), this.addWorker(this.spawn()), t({
155
+ if (o >= 0 && this.slots.splice(o, 1), e.worker.terminate(), this.respawn) v.info("respawning crashed worker"), this.addWorker(this.spawn()), a({
157
156
  type: "worker:respawn",
158
157
  poolId: this.poolId,
159
158
  slot: this.slots.length - 1
160
- });
161
- else if (this.slots.length === 0) for (let e of this.queue.splice(0)) e.done = !0, this.detach(e), this.statsAgg.failed++, t({
159
+ }), this.lifecycle("respawn", this.slots.length - 1);
160
+ else if (this.slots.length === 0) for (let e of this.queue.splice(0)) e.done = !0, this.detach(e), this.statsAgg.failed++, a({
162
161
  type: "task:settle",
163
162
  poolId: this.poolId,
164
163
  callId: e.callId,
165
164
  taskId: e.contract.taskId,
166
165
  outcome: "crashed",
167
166
  error: i
168
- }), e.reject(new l(i));
167
+ }), e.reject(new r(i));
169
168
  this.drainQueue(), this.checkDrained();
170
169
  }
171
170
  pickWorker() {
@@ -180,55 +179,55 @@ var v = a("pool"), y = a("task"), b = () => ({
180
179
  t.done || this.send(e, t);
181
180
  }
182
181
  }
183
- send(r, i) {
184
- let a = this.nextMessageId++;
185
- i.messageId = a, i.slot = r, i.sentAt = performance.now();
186
- let o = i.sentAt - i.enqueuedAt;
187
- this.statsAgg.waitMs.add(o), r.inFlight++, this.pending.set(a, i);
188
- let s = this.slots.indexOf(r);
189
- y.debug(`→ ${i.contract.taskId}`, {
190
- worker: s,
191
- args: i.args
192
- }), e() && t({
182
+ send(e, t) {
183
+ let r = this.nextMessageId++;
184
+ t.messageId = r, t.slot = e, t.sentAt = performance.now();
185
+ let s = t.sentAt - t.enqueuedAt;
186
+ this.statsAgg.waitMs.add(s), e.inFlight++, this.pending.set(r, t);
187
+ let c = this.slots.indexOf(e);
188
+ y.debug(`→ ${t.contract.taskId}`, {
189
+ worker: c,
190
+ args: t.args
191
+ }), i() && a({
193
192
  type: "task:dispatch",
194
193
  poolId: this.poolId,
195
- callId: i.callId,
196
- taskId: i.contract.taskId,
197
- slot: s,
198
- waitMs: o,
199
- argBytes: n(i.args)
194
+ callId: t.callId,
195
+ taskId: t.contract.taskId,
196
+ slot: c,
197
+ waitMs: s,
198
+ argBytes: o(t.args)
200
199
  });
201
200
  let l = {
202
201
  type: "EXECUTE_TASK",
203
- messageId: a,
204
- taskId: i.contract.taskId,
205
- args: i.args
202
+ messageId: r,
203
+ taskId: t.contract.taskId,
204
+ args: t.args
206
205
  };
207
206
  this.chaos ? g(this.chaos, {
208
- live: () => this.pending.get(a) === i,
209
- fail: () => this.handleReply(r, {
210
- messageId: a,
207
+ live: () => this.pending.get(r) === t,
208
+ fail: () => this.handleReply(e, {
209
+ messageId: r,
211
210
  taskId: l.taskId,
212
211
  success: !1,
213
212
  error: m
214
213
  }),
215
- timeout: () => this.cancel(i, new c()),
216
- post: () => r.worker.postMessage(l)
217
- }) : r.worker.postMessage(l);
214
+ timeout: () => this.cancel(t, new n()),
215
+ post: () => e.worker.postMessage(l)
216
+ }) : e.worker.postMessage(l);
218
217
  }
219
218
  detach(e) {
220
219
  e.timer && clearTimeout(e.timer), e.signal && e.onSignalAbort && e.signal.removeEventListener("abort", e.onSignalAbort);
221
220
  }
222
- cancel(e, n) {
221
+ cancel(e, t) {
223
222
  if (e.done) return;
224
223
  let r = this.queue.indexOf(e);
225
- r >= 0 && this.queue.splice(r, 1), e.done = !0, this.detach(e), this.statsAgg.aborted++, t({
224
+ r >= 0 && this.queue.splice(r, 1), e.done = !0, this.detach(e), this.statsAgg.aborted++, a({
226
225
  type: "task:settle",
227
226
  poolId: this.poolId,
228
227
  callId: e.callId,
229
228
  taskId: e.contract.taskId,
230
- outcome: n instanceof c ? "timeout" : "aborted"
231
- }), e.reject(n), this.drainQueue(), this.checkDrained();
229
+ outcome: t instanceof n ? "timeout" : "aborted"
230
+ }), e.reject(t), this.drainQueue(), this.checkDrained();
232
231
  }
233
232
  checkDrained() {
234
233
  if (this.queue.length === 0 && this.pending.size === 0) {
@@ -236,36 +235,36 @@ var v = a("pool"), y = a("task"), b = () => ({
236
235
  for (let t of e) t();
237
236
  }
238
237
  }
239
- dispatch(e, n, r) {
240
- return this.closed || this.slots.length === 0 ? Promise.reject(new l("WorkerPool has been terminated — no workers available.")) : r?.signal?.aborted ? Promise.reject(new s()) : new Promise((i, a) => {
238
+ dispatch(i, o, s) {
239
+ return this.closed || this.slots.length === 0 ? Promise.reject(new r("WorkerPool has been terminated — no workers available.")) : s?.signal?.aborted ? Promise.reject(new t()) : new Promise((r, c) => {
241
240
  let l = {
242
- contract: e,
243
- args: n,
241
+ contract: i,
242
+ args: o,
244
243
  callId: ++this.nextCallId,
245
244
  enqueuedAt: performance.now(),
246
- resolve: i,
247
- reject: a,
245
+ resolve: r,
246
+ reject: c,
248
247
  done: !1,
249
- signal: r?.signal
248
+ signal: s?.signal
250
249
  };
251
- r?.signal && (l.onSignalAbort = () => this.cancel(l, new s()), r.signal.addEventListener("abort", l.onSignalAbort, { once: !0 }));
252
- let u = r?.timeout ?? this.taskTimeout;
253
- u !== void 0 && (l.timer = setTimeout(() => this.cancel(l, new c()), u)), t({
250
+ s?.signal && (l.onSignalAbort = () => this.cancel(l, new t()), s.signal.addEventListener("abort", l.onSignalAbort, { once: !0 }));
251
+ let u = s?.timeout ?? this.taskTimeout;
252
+ u !== void 0 && (l.timer = setTimeout(() => this.cancel(l, new n()), u)), a({
254
253
  type: "task:enqueue",
255
254
  poolId: this.poolId,
256
255
  callId: l.callId,
257
- taskId: e.taskId
256
+ taskId: i.taskId
258
257
  });
259
258
  let d = this.pickWorker();
260
259
  if (d) this.send(d, l);
261
260
  else if (this.queue.length >= this.maxQueue) {
262
- l.done = !0, this.detach(l), t({
261
+ l.done = !0, this.detach(l), a({
263
262
  type: "task:settle",
264
263
  poolId: this.poolId,
265
264
  callId: l.callId,
266
- taskId: e.taskId,
265
+ taskId: i.taskId,
267
266
  outcome: "queue-full"
268
- }), a(new o());
267
+ }), c(new e());
269
268
  return;
270
269
  } else this.queue.push(l);
271
270
  });
@@ -308,31 +307,30 @@ var v = a("pool"), y = a("task"), b = () => ({
308
307
  });
309
308
  }
310
309
  terminate() {
311
- this.closed = !0, this.devtoolsOff?.(), this.persistence?.stop()?.catch?.((e) => v.warn("memory persistence stop failed", e)), v.info(`terminating ${this.slots.length} worker(s)`), t({
310
+ this.closed = !0, this.lifecycle("terminate"), this.devtoolsOff?.(), this.persistence?.stop()?.catch?.((e) => v.warn("memory persistence stop failed", e)), v.info(`terminating ${this.slots.length} worker(s)`), a({
312
311
  type: "pool:terminate",
313
312
  poolId: this.poolId
314
313
  });
315
- let e = new l("pool terminated");
316
- for (let [, n] of this.pending) this.detach(n), !n.done && (n.done = !0, this.statsAgg.failed++, t({
314
+ let e = new r("pool terminated");
315
+ for (let [, t] of this.pending) this.detach(t), t.done || (t.done = !0, this.statsAgg.failed++, a({
317
316
  type: "task:settle",
318
317
  poolId: this.poolId,
319
- callId: n.callId,
320
- taskId: n.contract.taskId,
318
+ callId: t.callId,
319
+ taskId: t.contract.taskId,
321
320
  outcome: "crashed",
322
321
  error: "pool terminated"
323
- }), n.reject(e));
322
+ }), t.reject(e));
324
323
  this.pending.clear();
325
- for (let n of this.queue.splice(0)) n.done = !0, this.detach(n), this.statsAgg.failed++, t({
324
+ for (let t of this.queue.splice(0)) t.done = !0, this.detach(t), this.statsAgg.failed++, a({
326
325
  type: "task:settle",
327
326
  poolId: this.poolId,
328
- callId: n.callId,
329
- taskId: n.contract.taskId,
327
+ callId: t.callId,
328
+ taskId: t.contract.taskId,
330
329
  outcome: "crashed",
331
330
  error: "pool terminated"
332
- }), n.reject(e);
331
+ }), t.reject(e);
333
332
  for (let e of this.slots) e.worker.terminate();
334
333
  this.slots = [], this.checkDrained();
335
334
  }
336
335
  };
337
- //#endregion
338
336
  export { x as WorkerPool };
package/dist/reactive.js CHANGED
@@ -1,7 +1,7 @@
1
1
  import { scoped as e } from "./log.js";
2
2
  import { disposeNodes as t, graphNode as n, graphSource as r } from "./reactiveGraph.js";
3
3
  import { createEffect as i, createMemo as a, createRenderEffect as o, createRoot as s, createSignal as c } from "solid-js";
4
- //#region src/reactive.ts
4
+ // src/reactive.ts
5
5
  var l = e("reactive");
6
6
  function u(e, t) {
7
7
  if (e === t) return !0;
@@ -109,5 +109,4 @@ function v(e, r, o, u, f) {
109
109
  }
110
110
  };
111
111
  }
112
- //#endregion
113
112
  export { g as reactive, u as shallowEqual, _ as watch, v as watchNodes };
@@ -1,5 +1,5 @@
1
1
  import { devtoolsEnabled as e, emitDevtools as t, registerDevtoolsCommand as n } from "./devtools.js";
2
- //#region src/reactiveGraph.ts
2
+ // src/reactiveGraph.ts
3
3
  var r = typeof WorkerGlobalScope < "u" && self instanceof WorkerGlobalScope ? "worker" : "main", i = 0, a = /* @__PURE__ */ new Map(), o = !1, s = (e, n) => {
4
4
  t({
5
5
  type: "reactive:node",
@@ -42,5 +42,4 @@ var r = typeof WorkerGlobalScope < "u" && self instanceof WorkerGlobalScope ? "w
42
42
  t && (a.delete(n), e() && s(t, !0));
43
43
  }
44
44
  };
45
- //#endregion
46
45
  export { f as disposeNodes, u as graphNode, l as graphSource, d as relabelNode };
package/dist/service.js CHANGED
@@ -1,6 +1,6 @@
1
1
  import { scoped as e } from "./log.js";
2
2
  import { TaskRegistry as t } from "./worker/registry.js";
3
- //#region src/service.ts
3
+ // src/service.ts
4
4
  var n = e("service");
5
5
  function r(e = {}) {
6
6
  return e;
@@ -44,5 +44,4 @@ function s(e, t) {
44
44
  for (let [r, i] of Object.entries(e.tasks)) n[r] = (...e) => t.runTask(i, ...e);
45
45
  return n;
46
46
  }
47
- //#endregion
48
47
  export { s as createClient, a as defineService, o as implementService, r as rpc, i as serviceMethod };
@@ -1,5 +1,5 @@
1
1
  import { scoped as e } from "../log.js";
2
- //#region src/shared/sharedWorkerClient.ts
2
+ // src/shared/sharedWorkerClient.ts
3
3
  var t = e("shared-client"), n = e("task"), r = class {
4
4
  clientIndex;
5
5
  sharedMemory;
@@ -7,13 +7,7 @@ var t = e("shared-client"), n = e("task"), r = class {
7
7
  constructor(e, n, r) {
8
8
  this.clientIndex = n, this.port = e, this.sharedMemory = r.sharedMemory;
9
9
  let i = r.tasks ?? {};
10
- for (let [e, n] of Object.entries(i)) {
11
- if (e in this) {
12
- t.warn(`task "${e}" collides with a SharedWorkerClient member — not installed`);
13
- continue;
14
- }
15
- this[e] = (...e) => this.runTask(n, ...e);
16
- }
10
+ for (let [e, n] of Object.entries(i)) e in this ? t.warn(`task "${e}" collides with a SharedWorkerClient member — not installed`) : this[e] = (...e) => this.runTask(n, ...e);
17
11
  Object.keys(i).length > 0 && t.info(`task methods: ${Object.keys(i).join(", ")}`);
18
12
  }
19
13
  runTask(e, ...t) {
@@ -60,5 +54,4 @@ async function i(e) {
60
54
  });
61
55
  });
62
56
  }
63
- //#endregion
64
57
  export { i as connectSharedWorker };
@@ -1,32 +1,30 @@
1
- import { fmtBytes as e, scoped as t } from "../log.js";
2
- import { bindSharedMemories as n } from "../contract/sharedMemory.js";
3
- import { MemoryManager as r } from "../pool/memory.js";
4
- import { TaskRegistry as i } from "../worker/registry.js";
5
- //#region src/shared/sharedWorkerHost.ts
6
- var a = t("shared-worker"), o = null, s = 0;
7
- function c(t, i) {
8
- if (o) return o.buffer.byteLength < t && a.warn(`client requested ${e(t)} but buffer is ${e(o.buffer.byteLength)} — first client's capacity wins`), o;
9
- let s = new r(i);
10
- return s.ensureCapacity(t), o = s.memory, n(s.getBuffer()), a.info(`shared buffer allocated (${e(o.buffer.byteLength)})`), o;
1
+ import { bindSharedMemories as e } from "../contract/sharedMemory.js";
2
+ import { MemoryManager as t } from "../pool/memory.js";
3
+ import { TaskRegistry as n } from "../worker/registry.js";
4
+ import { fmtBytes as r, scoped as i } from "../log.js";
5
+ // src/shared/sharedWorkerHost.ts
6
+ var a = i("shared-worker"), o = null, s = 0;
7
+ function c(n, i) {
8
+ if (o) return o.buffer.byteLength < n && a.warn(`client requested ${r(n)} but buffer is ${r(o.buffer.byteLength)} — first client's capacity wins`), o;
9
+ let s = new t(i);
10
+ return s.ensureCapacity(n), o = s.memory, e(s.getBuffer()), a.info(`shared buffer allocated (${r(o.buffer.byteLength)})`), o;
11
11
  }
12
12
  function l(e) {
13
13
  let t = ++s;
14
- a.info(`client #${t} connected`), e.onmessage = async (n) => {
15
- let r = n.data;
16
- if (r?.type === "SHARED_CONNECT") {
17
- let n = c(r.memoryBytes ?? 0, r.memory);
14
+ a.info(`client #${t} connected`), e.onmessage = async (r) => {
15
+ let i = r.data;
16
+ if (i?.type === "SHARED_CONNECT") {
17
+ let n = c(i.memoryBytes ?? 0, i.memory);
18
18
  e.postMessage({
19
19
  type: "SHARED_MEMORY",
20
20
  buffer: n.buffer,
21
21
  clientIndex: t
22
22
  });
23
- return;
24
- }
25
- if (r?.type === "EXECUTE_TASK") {
26
- let { messageId: n, taskId: s, args: c } = r;
23
+ } else if (i?.type === "EXECUTE_TASK") {
24
+ let { messageId: r, taskId: s, args: c } = i;
27
25
  if (!o) {
28
26
  e.postMessage({
29
- messageId: n,
27
+ messageId: r,
30
28
  success: !1,
31
29
  error: "Shared memory not initialized in worker."
32
30
  });
@@ -34,22 +32,20 @@ function l(e) {
34
32
  }
35
33
  let l = performance.now();
36
34
  try {
37
- let r = await i.execute(s, ...c || []);
35
+ let i = await n.execute(s, ...c || []);
38
36
  a.debug(`task ${s} → ${(performance.now() - l).toFixed(1)}ms (client #${t})`), e.postMessage({
39
- messageId: n,
37
+ messageId: r,
40
38
  success: !0,
41
- result: r
39
+ result: i
42
40
  });
43
41
  } catch (t) {
44
42
  a.warn(`task ${s} failed: ${t.message || String(t)}`), e.postMessage({
45
- messageId: n,
43
+ messageId: r,
46
44
  success: !1,
47
45
  error: t.message || String(t)
48
46
  });
49
47
  }
50
- return;
51
- }
52
- r?.type === "SHARED_DISCONNECT" && (e.close(), a.info(`client #${t} disconnected`));
48
+ } else i?.type === "SHARED_DISCONNECT" && (e.close(), a.info(`client #${t} disconnected`));
53
49
  }, e.start();
54
50
  }
55
51
  function u() {
@@ -57,5 +53,4 @@ function u() {
57
53
  l(e.ports[0]);
58
54
  };
59
55
  }
60
- //#endregion
61
56
  export { l as attachSharedPort, u as sharedWorkerHost };
package/dist/task.js CHANGED
@@ -1,5 +1,5 @@
1
1
  import { scoped as e } from "./log.js";
2
- //#region src/task.ts
2
+ // src/task.ts
3
3
  var t = e("task");
4
4
  function n(e) {
5
5
  let n = 0, r = {
@@ -53,5 +53,4 @@ function r(e) {
53
53
  function i(e) {
54
54
  return r(e) ? e : n(e);
55
55
  }
56
- //#endregion
57
56
  export { n as defineTask, r as isAsyncTask, i as toTask };
@@ -1,6 +1,6 @@
1
1
  import { bindSharedMemories as e } from "../contract/sharedMemory.js";
2
2
  import { TaskRegistry as t } from "../worker/registry.js";
3
- //#region src/testing/inProcessWorker.ts
3
+ // src/testing/inProcessWorker.ts
4
4
  var n = class n {
5
5
  url;
6
6
  options;
@@ -46,9 +46,7 @@ var n = class n {
46
46
  } catch (e) {
47
47
  this.crashOnBoot(e);
48
48
  }
49
- return;
50
- }
51
- if (n.type === "INIT_MEMORY") {
49
+ } else if (n.type === "INIT_MEMORY") {
52
50
  let t = n.memory?.buffer ?? n.buffer;
53
51
  this.ready ??= this.boot().then(() => {
54
52
  t && e(t);
@@ -58,9 +56,7 @@ var n = class n {
58
56
  } catch (e) {
59
57
  this.crashOnBoot(e);
60
58
  }
61
- return;
62
- }
63
- if (n.type === "EXECUTE_TASK") {
59
+ } else if (n.type === "EXECUTE_TASK") {
64
60
  if (this.dead) return;
65
61
  try {
66
62
  await this.ready;
@@ -106,5 +102,4 @@ var n = class n {
106
102
  this.terminated = !0;
107
103
  }
108
104
  }, r = () => new Promise((e) => setTimeout(e, 5));
109
- //#endregion
110
105
  export { n as InProcessWorker, r as flushObservers };
@@ -1,4 +1,4 @@
1
- //#region src/userTiming.ts
1
+ // src/userTiming.ts
2
2
  var e = (e, t, n, r) => {
3
3
  let i = globalThis.performance;
4
4
  if (typeof i?.measure == "function") try {
@@ -26,5 +26,4 @@ var e = (e, t, n, r) => {
26
26
  ],
27
27
  tooltipText: `${n} on ${t}#${i}: ${(o - a).toFixed(1)}ms`
28
28
  });
29
- //#endregion
30
29
  export { e as measureAtoll, t as measureTaskCall };
@@ -1,8 +1,8 @@
1
- import { scoped as e } from "../log.js";
1
+ import { installWorkerListener as e } from "./workerBootstrap.js";
2
2
  import { TaskRegistry as t } from "./registry.js";
3
- import { installWorkerListener as n } from "./workerBootstrap.js";
4
- //#region src/worker/defineWorker.ts
5
- var r = e("worker"), i = [
3
+ import { scoped as n } from "../log.js";
4
+ // src/worker/defineWorker.ts
5
+ var r = n("worker"), i = [
6
6
  "start",
7
7
  "terminate",
8
8
  "pool",
@@ -10,9 +10,9 @@ var r = e("worker"), i = [
10
10
  "then",
11
11
  "with"
12
12
  ], a = (e) => typeof e == "object" && !!e && "run" in e, o = new Set(i);
13
- function s(e) {
14
- n();
15
- let i = e.methods ?? {}, s = e.services ?? {}, c = 0, l = (e, n, r, i) => {
13
+ function s(n) {
14
+ e();
15
+ let i = n.methods ?? {}, s = n.services ?? {}, c = 0, l = (e, n, r, i) => {
16
16
  if (o.has(n)) throw Error(`defineWorker: "${n}" is reserved for the client API — rename the method`);
17
17
  if (a(r)) {
18
18
  let { def: n } = r;
@@ -35,7 +35,6 @@ function s(e) {
35
35
  methods: i,
36
36
  services: s
37
37
  };
38
- return e.sharedMemory && (u.sharedMemory = e.sharedMemory), u;
38
+ return n.sharedMemory && (u.sharedMemory = n.sharedMemory), u;
39
39
  }
40
- //#endregion
41
40
  export { i as RESERVED_CLIENT_KEYS, s as defineWorker };
@@ -1,5 +1,5 @@
1
1
  import { scoped as e } from "../log.js";
2
- //#region src/worker/registry.ts
2
+ // src/worker/registry.ts
3
3
  var t = e("registry"), n = class {
4
4
  static tasks = /* @__PURE__ */ new Map();
5
5
  static register(e, n) {
@@ -21,5 +21,4 @@ var t = e("registry"), n = class {
21
21
  return r.resultSchema ? r.resultSchema.parse(a) : a;
22
22
  }
23
23
  };
24
- //#endregion
25
24
  export { n as TaskRegistry };