@isoloop/runtime 0.0.0-next.62ef495 → 0.0.0-next.d028bc1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/bootstrap.js CHANGED
@@ -38,6 +38,69 @@ function envelopeGuard(types) {
38
38
  function isSystemTopic(topic) {
39
39
  return topic.startsWith(SYSTEM_TOPIC_PREFIX);
40
40
  }
41
+ function createCommitter(o) {
42
+ const intervalMs = o.intervalMs ?? 100;
43
+ const half = Math.max(1, Math.floor((o.credit ?? 1e3) / 2));
44
+ /** Highest settled offset per topic, not yet sent. */
45
+ const offsets = /* @__PURE__ */ new Map();
46
+ /** Records settled per topic since its last send: the early send at half the credit. */
47
+ const counts = /* @__PURE__ */ new Map();
48
+ let timer;
49
+ let queued = false;
50
+ const take = () => {
51
+ if (timer !== void 0) {
52
+ clearTimeout(timer);
53
+ timer = void 0;
54
+ }
55
+ queued = false;
56
+ if (offsets.size === 0) return void 0;
57
+ const out = [];
58
+ for (const [topic, offset] of offsets) out.push({
59
+ topic,
60
+ offset
61
+ });
62
+ offsets.clear();
63
+ counts.clear();
64
+ return out;
65
+ };
66
+ const flush = () => {
67
+ const out = take();
68
+ if (out) o.send(out);
69
+ };
70
+ const schedule = () => {
71
+ if (intervalMs === 0) {
72
+ if (queued) return;
73
+ queued = true;
74
+ queueMicrotask(() => {
75
+ if (queued) flush();
76
+ });
77
+ return;
78
+ }
79
+ timer ??= setTimeout(flush, intervalMs);
80
+ };
81
+ return {
82
+ settle(topic, offset, records = 1) {
83
+ const prev = offsets.get(topic);
84
+ if (prev === void 0 || offset > prev) offsets.set(topic, offset);
85
+ const n = (counts.get(topic) ?? 0) + records;
86
+ counts.set(topic, n);
87
+ if (n >= half) flush();
88
+ else schedule();
89
+ },
90
+ flush,
91
+ take,
92
+ drop(topic) {
93
+ offsets.delete(topic);
94
+ counts.delete(topic);
95
+ },
96
+ pending: () => offsets,
97
+ dispose() {
98
+ if (timer !== void 0) clearTimeout(timer);
99
+ timer = void 0;
100
+ queued = false;
101
+ }
102
+ };
103
+ }
41
104
  /** Wraps a hand-written check as a Standard Schema that passes valid values through unchanged. */
42
105
  function systemSchema(check) {
43
106
  return { "~standard": {
@@ -301,15 +364,19 @@ function createBusClient(first, o = {}) {
301
364
  send(withId);
302
365
  });
303
366
  };
367
+ const committer = createCommitter({
368
+ send: (offsets) => {
369
+ send({
370
+ v: 1,
371
+ t: "commits",
372
+ p: { offsets }
373
+ });
374
+ },
375
+ ...o.commitIntervalMs === void 0 ? {} : { intervalMs: o.commitIntervalMs },
376
+ ...o.credit === void 0 ? {} : { credit: o.credit }
377
+ });
304
378
  const commit = (topic, offset) => {
305
- send({
306
- v: 1,
307
- t: "commit",
308
- p: {
309
- topic,
310
- offset
311
- }
312
- });
379
+ committer.settle(topic, offset);
313
380
  };
314
381
  const drain = async (sub) => {
315
382
  if (sub.running) return;
@@ -319,7 +386,8 @@ function createBusClient(first, o = {}) {
319
386
  if (!r) break;
320
387
  topicOf.set(r, sub.topic);
321
388
  try {
322
- await sub.fn(r);
389
+ const done = sub.fn(r);
390
+ if (isThenable(done)) await done;
323
391
  } catch (err) {
324
392
  sub.stuck = true;
325
393
  o.onError?.(err);
@@ -370,6 +438,7 @@ function createBusClient(first, o = {}) {
370
438
  const sub = subs.get(msg.p.topic);
371
439
  if (!sub) return;
372
440
  sub.queue.length = 0;
441
+ committer.drop(msg.p.topic);
373
442
  try {
374
443
  sub.opts.onReset?.();
375
444
  } catch (err) {
@@ -418,6 +487,7 @@ function createBusClient(first, o = {}) {
418
487
  return () => {
419
488
  if (subs.get(topic.name) !== sub) return;
420
489
  subs.delete(topic.name);
490
+ committer.flush();
421
491
  send({
422
492
  v: 1,
423
493
  t: "unsubscribe",
@@ -493,14 +563,27 @@ function createBusClient(first, o = {}) {
493
563
  value: null
494
564
  });
495
565
  }
566
+ committer.take();
496
567
  for (const sub of subs.values()) {
497
568
  sub.queue.length = 0;
498
569
  sendSubscribe(sub);
499
570
  }
500
571
  for (const p of unanswered) if (p.msg.t !== "subscribe") send(p.msg);
572
+ },
573
+ async close() {
574
+ const offsets = committer.take();
575
+ committer.dispose();
576
+ if (offsets) await call({
577
+ v: 1,
578
+ t: "commits",
579
+ p: { offsets }
580
+ });
501
581
  }
502
582
  };
503
583
  }
584
+ function isThenable(x) {
585
+ return (typeof x === "object" || typeof x === "function") && x !== null && typeof x.then === "function";
586
+ }
504
587
  var randomEpoch, check, REQUEST_TIMEOUT_MS;
505
588
  var init_bus_client = __esmMin((() => {
506
589
  randomEpoch = () => crypto.getRandomValues(/* @__PURE__ */ new Uint32Array(1))[0] ?? 0;
@@ -1936,6 +2019,7 @@ function createVanillaIsolate(init, post, scope, pooled = false) {
1936
2019
  const awake = createKeepAwake();
1937
2020
  const bus = createBusClient(init.busPort, {
1938
2021
  ...init.wake ? { wake: true } : {},
2022
+ ...init.commitIntervalMs === void 0 ? {} : { commitIntervalMs: init.commitIntervalMs },
1939
2023
  onError: (err) => {
1940
2024
  post({
1941
2025
  v: 1,
@@ -2022,6 +2106,7 @@ function createVanillaIsolate(init, post, scope, pooled = false) {
2022
2106
  },
2023
2107
  async shutdown(reason) {
2024
2108
  await Promise.allSettled(onShutdown.map(async (fn) => fn(reason)));
2109
+ await bus.close();
2025
2110
  render?.flush();
2026
2111
  render?.dispose();
2027
2112
  }
package/dist/worker.js CHANGED
@@ -38,6 +38,69 @@ function envelopeGuard(types) {
38
38
  function isSystemTopic(topic) {
39
39
  return topic.startsWith(SYSTEM_TOPIC_PREFIX);
40
40
  }
41
+ function createCommitter(o) {
42
+ const intervalMs = o.intervalMs ?? 100;
43
+ const half = Math.max(1, Math.floor((o.credit ?? 1e3) / 2));
44
+ /** Highest settled offset per topic, not yet sent. */
45
+ const offsets = /* @__PURE__ */ new Map();
46
+ /** Records settled per topic since its last send: the early send at half the credit. */
47
+ const counts = /* @__PURE__ */ new Map();
48
+ let timer;
49
+ let queued = false;
50
+ const take = () => {
51
+ if (timer !== void 0) {
52
+ clearTimeout(timer);
53
+ timer = void 0;
54
+ }
55
+ queued = false;
56
+ if (offsets.size === 0) return void 0;
57
+ const out = [];
58
+ for (const [topic, offset] of offsets) out.push({
59
+ topic,
60
+ offset
61
+ });
62
+ offsets.clear();
63
+ counts.clear();
64
+ return out;
65
+ };
66
+ const flush = () => {
67
+ const out = take();
68
+ if (out) o.send(out);
69
+ };
70
+ const schedule = () => {
71
+ if (intervalMs === 0) {
72
+ if (queued) return;
73
+ queued = true;
74
+ queueMicrotask(() => {
75
+ if (queued) flush();
76
+ });
77
+ return;
78
+ }
79
+ timer ??= setTimeout(flush, intervalMs);
80
+ };
81
+ return {
82
+ settle(topic, offset, records = 1) {
83
+ const prev = offsets.get(topic);
84
+ if (prev === void 0 || offset > prev) offsets.set(topic, offset);
85
+ const n = (counts.get(topic) ?? 0) + records;
86
+ counts.set(topic, n);
87
+ if (n >= half) flush();
88
+ else schedule();
89
+ },
90
+ flush,
91
+ take,
92
+ drop(topic) {
93
+ offsets.delete(topic);
94
+ counts.delete(topic);
95
+ },
96
+ pending: () => offsets,
97
+ dispose() {
98
+ if (timer !== void 0) clearTimeout(timer);
99
+ timer = void 0;
100
+ queued = false;
101
+ }
102
+ };
103
+ }
41
104
  /** Wraps a hand-written check as a Standard Schema that passes valid values through unchanged. */
42
105
  function systemSchema(check) {
43
106
  return { "~standard": {
@@ -302,15 +365,19 @@ function createBusClient(first, o = {}) {
302
365
  send(withId);
303
366
  });
304
367
  };
368
+ const committer = createCommitter({
369
+ send: (offsets) => {
370
+ send({
371
+ v: 1,
372
+ t: "commits",
373
+ p: { offsets }
374
+ });
375
+ },
376
+ ...o.commitIntervalMs === void 0 ? {} : { intervalMs: o.commitIntervalMs },
377
+ ...o.credit === void 0 ? {} : { credit: o.credit }
378
+ });
305
379
  const commit = (topic, offset) => {
306
- send({
307
- v: 1,
308
- t: "commit",
309
- p: {
310
- topic,
311
- offset
312
- }
313
- });
380
+ committer.settle(topic, offset);
314
381
  };
315
382
  const drain = async (sub) => {
316
383
  if (sub.running) return;
@@ -320,7 +387,8 @@ function createBusClient(first, o = {}) {
320
387
  if (!r) break;
321
388
  topicOf.set(r, sub.topic);
322
389
  try {
323
- await sub.fn(r);
390
+ const done = sub.fn(r);
391
+ if (isThenable(done)) await done;
324
392
  } catch (err) {
325
393
  sub.stuck = true;
326
394
  o.onError?.(err);
@@ -371,6 +439,7 @@ function createBusClient(first, o = {}) {
371
439
  const sub = subs.get(msg.p.topic);
372
440
  if (!sub) return;
373
441
  sub.queue.length = 0;
442
+ committer.drop(msg.p.topic);
374
443
  try {
375
444
  sub.opts.onReset?.();
376
445
  } catch (err) {
@@ -419,6 +488,7 @@ function createBusClient(first, o = {}) {
419
488
  return () => {
420
489
  if (subs.get(topic.name) !== sub) return;
421
490
  subs.delete(topic.name);
491
+ committer.flush();
422
492
  send({
423
493
  v: 1,
424
494
  t: "unsubscribe",
@@ -494,14 +564,27 @@ function createBusClient(first, o = {}) {
494
564
  value: null
495
565
  });
496
566
  }
567
+ committer.take();
497
568
  for (const sub of subs.values()) {
498
569
  sub.queue.length = 0;
499
570
  sendSubscribe(sub);
500
571
  }
501
572
  for (const p of unanswered) if (p.msg.t !== "subscribe") send(p.msg);
573
+ },
574
+ async close() {
575
+ const offsets = committer.take();
576
+ committer.dispose();
577
+ if (offsets) await call({
578
+ v: 1,
579
+ t: "commits",
580
+ p: { offsets }
581
+ });
502
582
  }
503
583
  };
504
584
  }
585
+ function isThenable(x) {
586
+ return (typeof x === "object" || typeof x === "function") && x !== null && typeof x.then === "function";
587
+ }
505
588
  var randomEpoch, check, REQUEST_TIMEOUT_MS;
506
589
  var init_bus_client = __esmMin((() => {
507
590
  init_dist();
@@ -1938,6 +2021,7 @@ function createVanillaIsolate(init, post, scope, pooled = false) {
1938
2021
  const awake = createKeepAwake();
1939
2022
  const bus = createBusClient(init.busPort, {
1940
2023
  ...init.wake ? { wake: true } : {},
2024
+ ...init.commitIntervalMs === void 0 ? {} : { commitIntervalMs: init.commitIntervalMs },
1941
2025
  onError: (err) => {
1942
2026
  post({
1943
2027
  v: 1,
@@ -2024,6 +2108,7 @@ function createVanillaIsolate(init, post, scope, pooled = false) {
2024
2108
  },
2025
2109
  async shutdown(reason) {
2026
2110
  await Promise.allSettled(onShutdown.map(async (fn) => fn(reason)));
2111
+ await bus.close();
2027
2112
  render?.flush();
2028
2113
  render?.dispose();
2029
2114
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@isoloop/runtime",
3
- "version": "0.0.0-next.62ef495",
3
+ "version": "0.0.0-next.d028bc1",
4
4
  "description": "Isolate-side runtime that runs inside each worker.",
5
5
  "license": "MIT",
6
6
  "type": "module",
@@ -35,8 +35,8 @@
35
35
  }
36
36
  },
37
37
  "dependencies": {
38
- "@isoloop/dom": "0.0.0-next.62ef495",
39
- "@isoloop/protocol": "0.0.0-next.62ef495"
38
+ "@isoloop/dom": "0.0.0-next.d028bc1",
39
+ "@isoloop/protocol": "0.0.0-next.d028bc1"
40
40
  },
41
41
  "devDependencies": {
42
42
  "typescript": "7.0.2"