@isoloop/runtime 0.1.0 → 0.2.0-next.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.
- package/dist/bootstrap.js +338 -23
- package/dist/index.d.ts +2 -2
- package/dist/index.js +2 -2
- package/dist/testing.d.ts +14 -3
- package/dist/testing.js +48 -6
- package/dist/{vanilla-C6wHItsG.d.ts → vanilla-Ddt7U5Xp.d.ts} +27 -7
- package/dist/worker.js +339 -22
- package/package.json +3 -3
package/dist/bootstrap.js
CHANGED
|
@@ -146,6 +146,217 @@ function createCommitter(o) {
|
|
|
146
146
|
}
|
|
147
147
|
};
|
|
148
148
|
}
|
|
149
|
+
/** Makes the watches of one bus connection. @internal */
|
|
150
|
+
function createWatches(o) {
|
|
151
|
+
const topics = /* @__PURE__ */ new Map();
|
|
152
|
+
/** The highest settled view per topic, not yet sent. */
|
|
153
|
+
const seen = /* @__PURE__ */ new Map();
|
|
154
|
+
const unionOf = (w) => {
|
|
155
|
+
const union = /* @__PURE__ */ new Set();
|
|
156
|
+
for (const l of w.locals) {
|
|
157
|
+
if (!l.keys) return void 0;
|
|
158
|
+
for (const k of l.keys) union.add(k);
|
|
159
|
+
}
|
|
160
|
+
return union;
|
|
161
|
+
};
|
|
162
|
+
const sendWatch = (w) => {
|
|
163
|
+
const union = unionOf(w);
|
|
164
|
+
w.wire = union;
|
|
165
|
+
if (union) {
|
|
166
|
+
for (const k of w.values.keys()) if (!union.has(k)) w.values.delete(k);
|
|
167
|
+
}
|
|
168
|
+
const gen = ++w.gen;
|
|
169
|
+
w.owed++;
|
|
170
|
+
seen.delete(w.topic);
|
|
171
|
+
const p = { topic: w.topic };
|
|
172
|
+
if (union) p.keys = [...union];
|
|
173
|
+
if (w.values.size > 0) p.have = [...w.values].map(([k, v]) => [k, v.version]);
|
|
174
|
+
o.call({
|
|
175
|
+
v: 1,
|
|
176
|
+
t: "watch",
|
|
177
|
+
p
|
|
178
|
+
}).then((r) => {
|
|
179
|
+
if (r.ok) return;
|
|
180
|
+
if (topics.get(w.topic) === w && w.gen === gen) w.owed = Math.max(0, w.owed - 1);
|
|
181
|
+
o.onError(new BusError(r.error));
|
|
182
|
+
});
|
|
183
|
+
};
|
|
184
|
+
/** Sends one `watch` for the changes of this task: mounts and unmounts come in groups. */
|
|
185
|
+
const rewatchSoon = (w) => {
|
|
186
|
+
if (w.rewatchQueued) return;
|
|
187
|
+
w.rewatchQueued = true;
|
|
188
|
+
queueMicrotask(() => {
|
|
189
|
+
w.rewatchQueued = false;
|
|
190
|
+
if (topics.get(w.topic) !== w || w.locals.size === 0) return;
|
|
191
|
+
if (w.gen === 0 || !sameKeys(unionOf(w), w.wire)) sendWatch(w);
|
|
192
|
+
});
|
|
193
|
+
};
|
|
194
|
+
const viewFor = (w, l) => {
|
|
195
|
+
const view = /* @__PURE__ */ new Map();
|
|
196
|
+
if (l.keys) for (const k of l.keys) {
|
|
197
|
+
const v = w.values.get(k);
|
|
198
|
+
if (v) view.set(k, v.value);
|
|
199
|
+
}
|
|
200
|
+
else for (const [k, v] of w.values) view.set(k, v.value);
|
|
201
|
+
return view;
|
|
202
|
+
};
|
|
203
|
+
const settle = (w, item) => {
|
|
204
|
+
if (item.vseq === void 0 || topics.get(w.topic) !== w || item.gen !== w.gen) return;
|
|
205
|
+
seen.set(w.topic, Math.max(seen.get(w.topic) ?? 0, item.vseq));
|
|
206
|
+
if (item.burst) o.flushNow();
|
|
207
|
+
else o.flushSoon();
|
|
208
|
+
};
|
|
209
|
+
const drain = async (w) => {
|
|
210
|
+
if (w.running) return;
|
|
211
|
+
w.running = true;
|
|
212
|
+
while (w.queue.length > 0 && topics.get(w.topic) === w) {
|
|
213
|
+
const item = w.queue.shift();
|
|
214
|
+
if (!item) break;
|
|
215
|
+
const waits = [];
|
|
216
|
+
for (const l of w.locals) {
|
|
217
|
+
let changed;
|
|
218
|
+
if (l.keys) {
|
|
219
|
+
changed = /* @__PURE__ */ new Set();
|
|
220
|
+
for (const k of item.changed) if (l.keys.has(k)) changed.add(k);
|
|
221
|
+
} else changed = item.changed;
|
|
222
|
+
if (!l.primed) {
|
|
223
|
+
l.primed = true;
|
|
224
|
+
const view = viewFor(w, l);
|
|
225
|
+
changed = /* @__PURE__ */ new Set([...changed, ...view.keys()]);
|
|
226
|
+
waits.push(...run(l, view, changed));
|
|
227
|
+
} else if (changed.size > 0) waits.push(...run(l, viewFor(w, l), changed));
|
|
228
|
+
}
|
|
229
|
+
if (waits.length > 0) await Promise.allSettled(waits);
|
|
230
|
+
settle(w, item);
|
|
231
|
+
}
|
|
232
|
+
w.running = false;
|
|
233
|
+
};
|
|
234
|
+
const run = (l, view, changed) => {
|
|
235
|
+
try {
|
|
236
|
+
const done = l.onView(view, changed);
|
|
237
|
+
if (isThenable(done)) return [done.then(void 0, (err) => {
|
|
238
|
+
o.onError(err);
|
|
239
|
+
})];
|
|
240
|
+
} catch (err) {
|
|
241
|
+
o.onError(err);
|
|
242
|
+
}
|
|
243
|
+
return [];
|
|
244
|
+
};
|
|
245
|
+
return {
|
|
246
|
+
watch(topic, onView, opts = {}) {
|
|
247
|
+
let w = topics.get(topic);
|
|
248
|
+
if (!w) {
|
|
249
|
+
w = {
|
|
250
|
+
topic,
|
|
251
|
+
locals: /* @__PURE__ */ new Set(),
|
|
252
|
+
values: /* @__PURE__ */ new Map(),
|
|
253
|
+
wire: void 0,
|
|
254
|
+
gen: 0,
|
|
255
|
+
owed: 0,
|
|
256
|
+
snapped: false,
|
|
257
|
+
parts: void 0,
|
|
258
|
+
queue: [],
|
|
259
|
+
running: false,
|
|
260
|
+
lastViewAt: void 0,
|
|
261
|
+
rewatchQueued: false
|
|
262
|
+
};
|
|
263
|
+
topics.set(topic, w);
|
|
264
|
+
}
|
|
265
|
+
const local = {
|
|
266
|
+
keys: opts.keys ? new Set(opts.keys) : void 0,
|
|
267
|
+
onView,
|
|
268
|
+
primed: false
|
|
269
|
+
};
|
|
270
|
+
const before = w.gen === 0 ? void 0 : w.wire;
|
|
271
|
+
w.locals.add(local);
|
|
272
|
+
if (w.gen > 0 && w.snapped && sameKeys(unionOf(w), before)) {
|
|
273
|
+
w.queue.push({
|
|
274
|
+
changed: /* @__PURE__ */ new Set(),
|
|
275
|
+
vseq: void 0,
|
|
276
|
+
burst: false,
|
|
277
|
+
gen: w.gen
|
|
278
|
+
});
|
|
279
|
+
drain(w);
|
|
280
|
+
} else rewatchSoon(w);
|
|
281
|
+
const mine = w;
|
|
282
|
+
return () => {
|
|
283
|
+
if (!mine.locals.delete(local) || topics.get(topic) !== mine) return;
|
|
284
|
+
if (mine.locals.size > 0) {
|
|
285
|
+
rewatchSoon(mine);
|
|
286
|
+
return;
|
|
287
|
+
}
|
|
288
|
+
topics.delete(topic);
|
|
289
|
+
seen.delete(topic);
|
|
290
|
+
if (mine.gen > 0) o.send({
|
|
291
|
+
v: 1,
|
|
292
|
+
t: "unwatch",
|
|
293
|
+
p: { topic }
|
|
294
|
+
});
|
|
295
|
+
};
|
|
296
|
+
},
|
|
297
|
+
receive(msg) {
|
|
298
|
+
const w = topics.get(msg.p.topic);
|
|
299
|
+
if (!w) return;
|
|
300
|
+
o.delivered();
|
|
301
|
+
const t = o.now();
|
|
302
|
+
if (msg.p.vseq === 1 && w.owed > 0) w.owed--;
|
|
303
|
+
const current = w.owed === 0;
|
|
304
|
+
const changed = w.parts ?? /* @__PURE__ */ new Set();
|
|
305
|
+
for (const k of msg.p.cleared ?? []) if (w.values.delete(k)) changed.add(k);
|
|
306
|
+
for (const v of msg.p.values) {
|
|
307
|
+
if (w.wire && !w.wire.has(v.key)) continue;
|
|
308
|
+
w.values.set(v.key, {
|
|
309
|
+
version: v.version,
|
|
310
|
+
value: v.value
|
|
311
|
+
});
|
|
312
|
+
changed.add(v.key);
|
|
313
|
+
}
|
|
314
|
+
if (msg.p.more) {
|
|
315
|
+
w.parts = changed;
|
|
316
|
+
return;
|
|
317
|
+
}
|
|
318
|
+
w.parts = void 0;
|
|
319
|
+
const burst = o.intervalMs === 0 || w.lastViewAt !== void 0 && t - w.lastViewAt < o.intervalMs;
|
|
320
|
+
w.lastViewAt = t;
|
|
321
|
+
if (current) w.snapped = true;
|
|
322
|
+
w.queue.push({
|
|
323
|
+
changed,
|
|
324
|
+
vseq: current ? msg.p.vseq : void 0,
|
|
325
|
+
burst,
|
|
326
|
+
gen: w.gen
|
|
327
|
+
});
|
|
328
|
+
drain(w);
|
|
329
|
+
},
|
|
330
|
+
reattach() {
|
|
331
|
+
seen.clear();
|
|
332
|
+
for (const w of topics.values()) {
|
|
333
|
+
w.owed = 0;
|
|
334
|
+
w.parts = void 0;
|
|
335
|
+
w.lastViewAt = void 0;
|
|
336
|
+
if (w.locals.size > 0) sendWatch(w);
|
|
337
|
+
}
|
|
338
|
+
},
|
|
339
|
+
takeSeen() {
|
|
340
|
+
const out = [...seen].map(([topic, vseq]) => ({
|
|
341
|
+
topic,
|
|
342
|
+
vseq
|
|
343
|
+
}));
|
|
344
|
+
seen.clear();
|
|
345
|
+
return out;
|
|
346
|
+
}
|
|
347
|
+
};
|
|
348
|
+
}
|
|
349
|
+
/** Two key sets with the same keys; `undefined` is every key. */
|
|
350
|
+
function sameKeys(a, b) {
|
|
351
|
+
if (!a || !b) return a === b;
|
|
352
|
+
if (a.size !== b.size) return false;
|
|
353
|
+
for (const k of a) if (!b.has(k)) return false;
|
|
354
|
+
return true;
|
|
355
|
+
}
|
|
356
|
+
/** True for anything with a `then` method. @internal */
|
|
357
|
+
function isThenable(x) {
|
|
358
|
+
return (typeof x === "object" || typeof x === "function") && x !== null && typeof x.then === "function";
|
|
359
|
+
}
|
|
149
360
|
/** Wraps a hand-written check as a Standard Schema that passes valid values through unchanged. */
|
|
150
361
|
function systemSchema(check) {
|
|
151
362
|
return { "~standard": {
|
|
@@ -170,7 +381,12 @@ function majorMinor(version) {
|
|
|
170
381
|
const m = /^(\d+)\.(\d+)(?:[.+-].*)?$/.exec(version);
|
|
171
382
|
return m ? `${Number(m[1])}.${Number(m[2])}` : void 0;
|
|
172
383
|
}
|
|
173
|
-
|
|
384
|
+
function compareVersions(a, b) {
|
|
385
|
+
const [am, an] = parse(a);
|
|
386
|
+
const [bm, bn] = parse(b);
|
|
387
|
+
return am - bm || an - bn;
|
|
388
|
+
}
|
|
389
|
+
var PROTOCOL, isRecord$1, isOptionalString, ERROR_CODES, SYSTEM_TOPIC_PREFIX, MAX_BLOCK_MS, HOST_TO_ISOLATE, isHostToIsolate, BUS_TO_ISOLATE, isBusToIsolate, point, BusError, isObject, issue, checks, stringOrNull, parse, INSPECT_MESSAGES, INSPECT_TO_ISOLATE, isInspectToIsolate;
|
|
174
390
|
var init_dist = __esmMin((() => {
|
|
175
391
|
PROTOCOL = {
|
|
176
392
|
v: 1,
|
|
@@ -228,7 +444,8 @@ var init_dist = __esmMin((() => {
|
|
|
228
444
|
BUS_TO_ISOLATE = {
|
|
229
445
|
deliver: true,
|
|
230
446
|
reset: true,
|
|
231
|
-
result: true
|
|
447
|
+
result: true,
|
|
448
|
+
view: true
|
|
232
449
|
};
|
|
233
450
|
isBusToIsolate = envelopeGuard(BUS_TO_ISOLATE);
|
|
234
451
|
point = ({ offset, seq, partial }) => partial ? {
|
|
@@ -281,6 +498,12 @@ var init_dist = __esmMin((() => {
|
|
|
281
498
|
})),
|
|
282
499
|
retention: { kind: "compacted" }
|
|
283
500
|
});
|
|
501
|
+
parse = (v) => {
|
|
502
|
+
const mm = majorMinor(v);
|
|
503
|
+
if (mm === void 0) throw new TypeError(`not a version: ${JSON.stringify(v)}`);
|
|
504
|
+
const [major = 0, minor = 0] = mm.split(".").map(Number);
|
|
505
|
+
return [major, minor];
|
|
506
|
+
};
|
|
284
507
|
INSPECT_MESSAGES = {
|
|
285
508
|
snapshot: true,
|
|
286
509
|
event: true,
|
|
@@ -296,6 +519,55 @@ var init_dist = __esmMin((() => {
|
|
|
296
519
|
};
|
|
297
520
|
isInspectToIsolate = envelopeGuard(INSPECT_TO_ISOLATE);
|
|
298
521
|
}));
|
|
522
|
+
// package.json
|
|
523
|
+
var version = "0.2.0-next.0";
|
|
524
|
+
// src/bus/stamped.ts
|
|
525
|
+
init_dist();
|
|
526
|
+
/** The release that changed `publish`'s shape. */
|
|
527
|
+
const NEW_PUBLISH = "0.2";
|
|
528
|
+
/**
|
|
529
|
+
* Does an isolate stamped `requires` get 0.1's `publish`? Only when it was
|
|
530
|
+
* built before 0.2 and this runtime is 0.2 or later. An unstamped isolate gets
|
|
531
|
+
* the current shape.
|
|
532
|
+
* @param own This runtime's `major.minor`. Default: from the package version.
|
|
533
|
+
*/
|
|
534
|
+
function usesOldPublish(requires, own = majorMinor(version) ?? "0.0") {
|
|
535
|
+
if (requires === void 0 || majorMinor(requires) === void 0) return false;
|
|
536
|
+
return compareVersions(requires, NEW_PUBLISH) < 0 && compareVersions(own, NEW_PUBLISH) >= 0;
|
|
537
|
+
}
|
|
538
|
+
/** What the first old-shape call logs, with `KernelConfig.dev` set. */
|
|
539
|
+
const OLD_PUBLISH_WARNING = "Isoloop: this isolate was built for 0.1 and calls ctx.bus.publish(topic, { value, key }). Since 0.2 it is publish(topic, value, { key }); the old shape stops working in 0.3. Rebuild the isolate on 0.2 after running npx isoloop@latest migrate.";
|
|
540
|
+
/**
|
|
541
|
+
* `bus` with 0.1's `publish(topic, { value, key? })`, the rest unchanged. The
|
|
542
|
+
* first `publish` call runs `warn`, when given.
|
|
543
|
+
*/
|
|
544
|
+
function withOldPublish(bus, warn) {
|
|
545
|
+
let warned = false;
|
|
546
|
+
return {
|
|
547
|
+
...bus,
|
|
548
|
+
publish(topic, rec) {
|
|
549
|
+
if (!warned) {
|
|
550
|
+
warned = true;
|
|
551
|
+
warn?.();
|
|
552
|
+
}
|
|
553
|
+
const r = rec;
|
|
554
|
+
if (typeof r !== "object" || r === null) return Promise.reject(/* @__PURE__ */ new TypeError("publish(topic, rec): rec must be { value, key? }"));
|
|
555
|
+
const key = Reflect.get(r, "key");
|
|
556
|
+
return bus.publish(topic, Reflect.get(r, "value"), typeof key === "string" ? { key } : {});
|
|
557
|
+
}
|
|
558
|
+
};
|
|
559
|
+
}
|
|
560
|
+
/**
|
|
561
|
+
* Gives `ctx` the shapes its isolate was built for, before `start` runs: today
|
|
562
|
+
* 0.1's `publish` for an isolate stamped below 0.2.
|
|
563
|
+
* @param dev `KernelConfig.dev`: the first old-shape call logs {@link OLD_PUBLISH_WARNING}.
|
|
564
|
+
* @param own This runtime's `major.minor`. Default: from the package version.
|
|
565
|
+
*/
|
|
566
|
+
function applyStamp(ctx, requires, dev, own) {
|
|
567
|
+
if (!usesOldPublish(requires, own)) return;
|
|
568
|
+
const warn = dev ? () => console.warn(OLD_PUBLISH_WARNING) : void 0;
|
|
569
|
+
Reflect.set(ctx, "bus", withOldPublish(ctx.bus, warn));
|
|
570
|
+
}
|
|
299
571
|
// src/pools/busy.ts
|
|
300
572
|
function createBusy(now = () => performance.now()) {
|
|
301
573
|
let frame = null;
|
|
@@ -610,7 +882,6 @@ var init_busy = __esmMin((() => {
|
|
|
610
882
|
BUSY = Symbol.for("isoloop.busy");
|
|
611
883
|
}));
|
|
612
884
|
// src/pools/freeze.ts
|
|
613
|
-
init_dist();
|
|
614
885
|
init_busy();
|
|
615
886
|
/** Constructors whose `prototype` and statics are frozen, if this engine has them. */
|
|
616
887
|
const NAMES = [
|
|
@@ -950,6 +1221,7 @@ function createInspectorClient() {
|
|
|
950
1221
|
}
|
|
951
1222
|
var failure, endedError;
|
|
952
1223
|
var init_inspector = __esmMin((() => {
|
|
1224
|
+
init_dist();
|
|
953
1225
|
failure = (r) => r.ok ? void 0 : new BusError(r.error);
|
|
954
1226
|
endedError = () => /* @__PURE__ */ new Error("inspector: its port has ended; controls no longer act");
|
|
955
1227
|
}));
|
|
@@ -993,14 +1265,52 @@ function createBusClient(first, o = {}) {
|
|
|
993
1265
|
send(withId);
|
|
994
1266
|
});
|
|
995
1267
|
};
|
|
996
|
-
|
|
997
|
-
|
|
998
|
-
|
|
999
|
-
|
|
1000
|
-
|
|
1001
|
-
|
|
1002
|
-
|
|
1268
|
+
/** Sends a `commits` with these offsets and every `seen` still pending. */
|
|
1269
|
+
const sendCommits = (offsets) => {
|
|
1270
|
+
clearSeenTimer();
|
|
1271
|
+
const seen = watches.takeSeen();
|
|
1272
|
+
if (offsets.length === 0 && seen.length === 0) return;
|
|
1273
|
+
send({
|
|
1274
|
+
v: 1,
|
|
1275
|
+
t: "commits",
|
|
1276
|
+
p: seen.length > 0 ? {
|
|
1277
|
+
offsets,
|
|
1278
|
+
seen
|
|
1279
|
+
} : { offsets }
|
|
1280
|
+
});
|
|
1281
|
+
};
|
|
1282
|
+
const intervalMs = o.commitIntervalMs ?? 100;
|
|
1283
|
+
let seenTimer;
|
|
1284
|
+
let seenQueued = false;
|
|
1285
|
+
const clearSeenTimer = () => {
|
|
1286
|
+
if (seenTimer !== void 0) clearTimeout(seenTimer);
|
|
1287
|
+
seenTimer = void 0;
|
|
1288
|
+
seenQueued = false;
|
|
1289
|
+
};
|
|
1290
|
+
const watches = createWatches({
|
|
1291
|
+
send,
|
|
1292
|
+
call: (msg) => call(msg),
|
|
1293
|
+
now,
|
|
1294
|
+
onError: (err) => o.onError?.(err),
|
|
1295
|
+
intervalMs,
|
|
1296
|
+
flushNow: () => sendCommits(committer.take() ?? []),
|
|
1297
|
+
flushSoon: () => {
|
|
1298
|
+
if (intervalMs === 0) {
|
|
1299
|
+
if (seenQueued) return;
|
|
1300
|
+
seenQueued = true;
|
|
1301
|
+
queueMicrotask(() => {
|
|
1302
|
+
if (seenQueued) sendCommits(committer.take() ?? []);
|
|
1303
|
+
});
|
|
1304
|
+
return;
|
|
1305
|
+
}
|
|
1306
|
+
seenTimer ??= setTimeout(() => sendCommits(committer.take() ?? []), intervalMs);
|
|
1003
1307
|
},
|
|
1308
|
+
delivered: () => {
|
|
1309
|
+
lastDelivery = now();
|
|
1310
|
+
}
|
|
1311
|
+
});
|
|
1312
|
+
const committer = createCommitter({
|
|
1313
|
+
send: sendCommits,
|
|
1004
1314
|
...o.commitIntervalMs === void 0 ? {} : { intervalMs: o.commitIntervalMs },
|
|
1005
1315
|
...o.credit === void 0 ? {} : { credit: o.credit }
|
|
1006
1316
|
});
|
|
@@ -1094,6 +1404,9 @@ function createBusClient(first, o = {}) {
|
|
|
1094
1404
|
}
|
|
1095
1405
|
return;
|
|
1096
1406
|
}
|
|
1407
|
+
case "view":
|
|
1408
|
+
watches.receive(msg);
|
|
1409
|
+
return;
|
|
1097
1410
|
default: return msg;
|
|
1098
1411
|
}
|
|
1099
1412
|
};
|
|
@@ -1106,14 +1419,14 @@ function createBusClient(first, o = {}) {
|
|
|
1106
1419
|
listen(first);
|
|
1107
1420
|
return {
|
|
1108
1421
|
client: {
|
|
1109
|
-
async publish(topic,
|
|
1422
|
+
async publish(topic, value, opts = {}) {
|
|
1110
1423
|
const p = {
|
|
1111
1424
|
topic: topic.name,
|
|
1112
|
-
value
|
|
1425
|
+
value,
|
|
1113
1426
|
epoch,
|
|
1114
1427
|
seq: ++seq
|
|
1115
1428
|
};
|
|
1116
|
-
if (
|
|
1429
|
+
if (opts.key !== void 0) p.key = opts.key;
|
|
1117
1430
|
check(await call({
|
|
1118
1431
|
v: 1,
|
|
1119
1432
|
t: "publish",
|
|
@@ -1192,6 +1505,9 @@ function createBusClient(first, o = {}) {
|
|
|
1192
1505
|
const d = delivered.get(record);
|
|
1193
1506
|
if (d === void 0) throw new Error("commit: this record was not delivered to this isolate");
|
|
1194
1507
|
commit(d);
|
|
1508
|
+
},
|
|
1509
|
+
watch(topic, onView, opts) {
|
|
1510
|
+
return watches.watch(topic.name, onView, opts);
|
|
1195
1511
|
}
|
|
1196
1512
|
},
|
|
1197
1513
|
idle: () => ({
|
|
@@ -1206,7 +1522,7 @@ function createBusClient(first, o = {}) {
|
|
|
1206
1522
|
listen(next);
|
|
1207
1523
|
const unanswered = [...pending.values()];
|
|
1208
1524
|
for (const p of unanswered) {
|
|
1209
|
-
if (p.msg.t !== "subscribe") continue;
|
|
1525
|
+
if (p.msg.t !== "subscribe" && p.msg.t !== "watch") continue;
|
|
1210
1526
|
pending.delete(p.msg.id);
|
|
1211
1527
|
p.settle({
|
|
1212
1528
|
ok: true,
|
|
@@ -1214,16 +1530,19 @@ function createBusClient(first, o = {}) {
|
|
|
1214
1530
|
});
|
|
1215
1531
|
}
|
|
1216
1532
|
committer.take();
|
|
1533
|
+
clearSeenTimer();
|
|
1217
1534
|
for (const sub of subs.values()) {
|
|
1218
1535
|
sub.queue.length = 0;
|
|
1219
1536
|
sub.highest = -1;
|
|
1220
1537
|
sendSubscribe(sub);
|
|
1221
1538
|
}
|
|
1222
|
-
|
|
1539
|
+
watches.reattach();
|
|
1540
|
+
for (const p of unanswered) if (p.msg.t !== "subscribe" && p.msg.t !== "watch") send(p.msg);
|
|
1223
1541
|
},
|
|
1224
1542
|
async close() {
|
|
1225
1543
|
const offsets = committer.take();
|
|
1226
1544
|
committer.dispose();
|
|
1545
|
+
clearSeenTimer();
|
|
1227
1546
|
if (offsets) await call({
|
|
1228
1547
|
v: 1,
|
|
1229
1548
|
t: "commits",
|
|
@@ -1232,9 +1551,6 @@ function createBusClient(first, o = {}) {
|
|
|
1232
1551
|
}
|
|
1233
1552
|
};
|
|
1234
1553
|
}
|
|
1235
|
-
function isThenable(x) {
|
|
1236
|
-
return (typeof x === "object" || typeof x === "function") && x !== null && typeof x.then === "function";
|
|
1237
|
-
}
|
|
1238
1554
|
var randomEpoch, check, REQUEST_TIMEOUT_MS;
|
|
1239
1555
|
var init_bus_client = __esmMin((() => {
|
|
1240
1556
|
init_dist();
|
|
@@ -2702,8 +3018,6 @@ var init_render = __esmMin((() => {
|
|
|
2702
3018
|
shared = null;
|
|
2703
3019
|
headListeners = /* @__PURE__ */ new Set();
|
|
2704
3020
|
}));
|
|
2705
|
-
// package.json
|
|
2706
|
-
var version = "0.1.0";
|
|
2707
3021
|
// src/isolate/vanilla.ts
|
|
2708
3022
|
var vanilla_exports = /* @__PURE__ */ __exportAll({
|
|
2709
3023
|
REQUIRES: () => REQUIRES,
|
|
@@ -2724,7 +3038,7 @@ var vanilla_exports = /* @__PURE__ */ __exportAll({
|
|
|
2724
3038
|
* start(ctx) {
|
|
2725
3039
|
* const button = document.createElement('button');
|
|
2726
3040
|
* button.textContent = 'Add one';
|
|
2727
|
-
* button.addEventListener('click', () => void ctx.bus.publish(count,
|
|
3041
|
+
* button.addEventListener('click', () => void ctx.bus.publish(count, 1));
|
|
2728
3042
|
* ctx.root?.append(button);
|
|
2729
3043
|
* },
|
|
2730
3044
|
* });
|
|
@@ -3010,13 +3324,14 @@ async function boot(m, init) {
|
|
|
3010
3324
|
const start = main.start;
|
|
3011
3325
|
if (typeof start === "function") {
|
|
3012
3326
|
const requires = "requires" in main ? main.requires : void 0;
|
|
3013
|
-
await startIsolate(m, runtime, (ctx) => Reflect.apply(start, main, [ctx]), typeof requires === "string" ? requires : void 0);
|
|
3327
|
+
await startIsolate(m, runtime, (ctx) => Reflect.apply(start, main, [ctx]), typeof requires === "string" ? requires : void 0, init.dev === true);
|
|
3014
3328
|
return;
|
|
3015
3329
|
}
|
|
3016
3330
|
}
|
|
3017
3331
|
fatal(m, `${init.src} has no default export with start(ctx)`);
|
|
3018
3332
|
}
|
|
3019
|
-
async function startIsolate(m, runtime, start, requires) {
|
|
3333
|
+
async function startIsolate(m, runtime, start, requires, dev) {
|
|
3334
|
+
applyStamp(runtime.ctx, requires, dev);
|
|
3020
3335
|
m.receive = (msg) => {
|
|
3021
3336
|
runtime.receive(msg);
|
|
3022
3337
|
};
|
package/dist/index.d.ts
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
import { a as defineIsolate, c as ScrollIntoViewOptionsLike, d as
|
|
1
|
+
import { a as defineIsolate, c as ScrollIntoViewOptionsLike, d as RequestOptions, f as SubscribeOptions, i as REQUIRES, l as BusClient, m as IsolateInspector, n as IsolateDefinition, o as HostApi, p as WatchOptions, r as IsolateRoot, s as HostCallError, t as IsolateContext, u as OnView } from "./vanilla-Ddt7U5Xp.js";
|
|
2
2
|
import { BusError } from "@isoloop/protocol";
|
|
3
|
-
export { type BusClient, BusError, type HostApi, HostCallError, type IsolateContext, type IsolateDefinition, type IsolateInspector, type IsolateRoot, REQUIRES, type RequestOptions, type ScrollIntoViewOptionsLike, type SubscribeOptions, defineIsolate };
|
|
3
|
+
export { type BusClient, BusError, type HostApi, HostCallError, type IsolateContext, type IsolateDefinition, type IsolateInspector, type IsolateRoot, type OnView, REQUIRES, type RequestOptions, type ScrollIntoViewOptionsLike, type SubscribeOptions, type WatchOptions, defineIsolate };
|
package/dist/index.js
CHANGED
|
@@ -21,7 +21,7 @@ var HostCallError = class extends Error {
|
|
|
21
21
|
*
|
|
22
22
|
* @internal
|
|
23
23
|
*/
|
|
24
|
-
const REQUIRES = majorMinor("0.
|
|
24
|
+
const REQUIRES = majorMinor("0.2.0-next.0") ?? "0.0";
|
|
25
25
|
/**
|
|
26
26
|
* Types an isolate module's default export and stamps it with the version
|
|
27
27
|
* it was built against.
|
|
@@ -36,7 +36,7 @@ const REQUIRES = majorMinor("0.1.0") ?? "0.0";
|
|
|
36
36
|
* start(ctx) {
|
|
37
37
|
* const button = document.createElement('button');
|
|
38
38
|
* button.textContent = 'Add one';
|
|
39
|
-
* button.addEventListener('click', () => void ctx.bus.publish(count,
|
|
39
|
+
* button.addEventListener('click', () => void ctx.bus.publish(count, 1));
|
|
40
40
|
* ctx.root?.append(button);
|
|
41
41
|
* },
|
|
42
42
|
* });
|
package/dist/testing.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { o as HostApi, t as IsolateContext } from "./vanilla-
|
|
1
|
+
import { o as HostApi, t as IsolateContext } from "./vanilla-Ddt7U5Xp.js";
|
|
2
2
|
import { ShutdownReason } from "@isoloop/protocol";
|
|
3
3
|
/** `createTestContext`'s options. */
|
|
4
4
|
export type TestContextOptions = {
|
|
@@ -22,9 +22,15 @@ export type TestContext = {
|
|
|
22
22
|
error: unknown;
|
|
23
23
|
fatal: boolean;
|
|
24
24
|
}[];
|
|
25
|
-
/**
|
|
25
|
+
/**
|
|
26
|
+
* Delivers a record to every subscription on `topic`, in order, and passes
|
|
27
|
+
* its value to every watch of its key (`null` without a key) as one view.
|
|
28
|
+
*/
|
|
26
29
|
emit: (topic: string, value: unknown, key?: string) => void;
|
|
27
|
-
/**
|
|
30
|
+
/**
|
|
31
|
+
* Runs `onReset` of every subscription on `topic`, and empties its watches'
|
|
32
|
+
* views, as when a bus restart loses a memory topic.
|
|
33
|
+
*/
|
|
28
34
|
reset: (topic: string) => void;
|
|
29
35
|
/** Every `publish`, in order. */
|
|
30
36
|
readonly published: {
|
|
@@ -37,6 +43,11 @@ export type TestContext = {
|
|
|
37
43
|
topic: string;
|
|
38
44
|
from: string;
|
|
39
45
|
}[];
|
|
46
|
+
/** Open watches, by topic and keys (`undefined` for every key). @experimental */
|
|
47
|
+
watches: () => {
|
|
48
|
+
topic: string;
|
|
49
|
+
keys: (string | null)[] | undefined;
|
|
50
|
+
}[];
|
|
40
51
|
/** Runs the `onShutdown` handlers, as the runtime does on `stop`. `reason` defaults to `'stop'`. */
|
|
41
52
|
shutdown: (reason?: ShutdownReason) => Promise<void>;
|
|
42
53
|
};
|
package/dist/testing.js
CHANGED
|
@@ -16,20 +16,30 @@ const ZERO = {
|
|
|
16
16
|
*/
|
|
17
17
|
function createTestContext(o = {}) {
|
|
18
18
|
const subs = /* @__PURE__ */ new Set();
|
|
19
|
+
const watches = /* @__PURE__ */ new Set();
|
|
20
|
+
/** The newest value per key of each topic emitted to, for watches. */
|
|
21
|
+
const latest = /* @__PURE__ */ new Map();
|
|
22
|
+
const viewOf = (w) => {
|
|
23
|
+
const all = latest.get(w.topic) ?? /* @__PURE__ */ new Map();
|
|
24
|
+
if (!w.keys) return new Map(all);
|
|
25
|
+
const view = /* @__PURE__ */ new Map();
|
|
26
|
+
for (const k of w.keys) if (all.has(k)) view.set(k, all.get(k));
|
|
27
|
+
return view;
|
|
28
|
+
};
|
|
19
29
|
const published = [];
|
|
20
30
|
const shutdownFns = [];
|
|
21
31
|
const errors = [];
|
|
22
32
|
let offset = 0;
|
|
23
33
|
let portal;
|
|
24
34
|
const bus = {
|
|
25
|
-
publish(topic,
|
|
26
|
-
published.push(
|
|
35
|
+
publish(topic, value, opts = {}) {
|
|
36
|
+
published.push(opts.key === void 0 ? {
|
|
27
37
|
topic: topic.name,
|
|
28
|
-
value
|
|
38
|
+
value
|
|
29
39
|
} : {
|
|
30
40
|
topic: topic.name,
|
|
31
|
-
key:
|
|
32
|
-
value
|
|
41
|
+
key: opts.key,
|
|
42
|
+
value
|
|
33
43
|
});
|
|
34
44
|
return Promise.resolve();
|
|
35
45
|
},
|
|
@@ -57,7 +67,23 @@ function createTestContext(o = {}) {
|
|
|
57
67
|
});
|
|
58
68
|
},
|
|
59
69
|
reply: () => Promise.resolve(),
|
|
60
|
-
commit: () => {}
|
|
70
|
+
commit: () => {},
|
|
71
|
+
watch(topic, onView, wo = {}) {
|
|
72
|
+
const w = {
|
|
73
|
+
topic: topic.name,
|
|
74
|
+
keys: wo.keys ? new Set(wo.keys) : void 0,
|
|
75
|
+
onView
|
|
76
|
+
};
|
|
77
|
+
watches.add(w);
|
|
78
|
+
queueMicrotask(() => {
|
|
79
|
+
if (!watches.has(w)) return;
|
|
80
|
+
const view = viewOf(w);
|
|
81
|
+
w.onView(view, new Set(view.keys()));
|
|
82
|
+
});
|
|
83
|
+
return () => {
|
|
84
|
+
watches.delete(w);
|
|
85
|
+
};
|
|
86
|
+
}
|
|
61
87
|
};
|
|
62
88
|
const host = {
|
|
63
89
|
navigate: () => Promise.resolve(true),
|
|
@@ -124,14 +150,30 @@ function createTestContext(o = {}) {
|
|
|
124
150
|
...key === void 0 ? {} : { key }
|
|
125
151
|
};
|
|
126
152
|
for (const s of subs) if (s.topic === topic) s.fn(rec);
|
|
153
|
+
const k = key ?? null;
|
|
154
|
+
let values = latest.get(topic);
|
|
155
|
+
if (!values) latest.set(topic, values = /* @__PURE__ */ new Map());
|
|
156
|
+
values.set(k, value);
|
|
157
|
+
for (const w of watches) if (w.topic === topic && (!w.keys || w.keys.has(k))) w.onView(viewOf(w), /* @__PURE__ */ new Set([k]));
|
|
127
158
|
},
|
|
128
159
|
reset(topic) {
|
|
129
160
|
for (const s of subs) if (s.topic === topic) s.opts.onReset?.();
|
|
161
|
+
const lost = latest.get(topic);
|
|
162
|
+
latest.delete(topic);
|
|
163
|
+
for (const w of watches) {
|
|
164
|
+
if (w.topic !== topic) continue;
|
|
165
|
+
const changed = new Set([...lost?.keys() ?? []].filter((k) => !w.keys || w.keys.has(k)));
|
|
166
|
+
if (changed.size > 0) w.onView(viewOf(w), changed);
|
|
167
|
+
}
|
|
130
168
|
},
|
|
131
169
|
subscriptions: () => [...subs].map((s) => ({
|
|
132
170
|
topic: s.topic,
|
|
133
171
|
from: s.opts.from ?? "latest"
|
|
134
172
|
})),
|
|
173
|
+
watches: () => [...watches].map((w) => ({
|
|
174
|
+
topic: w.topic,
|
|
175
|
+
keys: w.keys ? [...w.keys] : void 0
|
|
176
|
+
})),
|
|
135
177
|
async shutdown(reason = "stop") {
|
|
136
178
|
await Promise.allSettled(shutdownFns.map(async (fn) => fn(reason)));
|
|
137
179
|
}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { ErrorCode, InspectMessage, InspectRecord, InspectSnapshot, Rect, ShutdownReason, StartPosition, TopicRef, TypedRecord } from "@isoloop/protocol";
|
|
1
|
+
import { ErrorCode, InspectMessage, InspectRecord, InspectSnapshot, OnView, OnView as OnView$1, Rect, ShutdownReason, StartPosition, TopicRef, TypedRecord, WatchOptions, WatchOptions as WatchOptions$1 } from "@isoloop/protocol";
|
|
2
2
|
/**
|
|
3
3
|
* The inspection feed and its controls, as an isolate with `inspect: true`
|
|
4
4
|
* sees them: `ctx.inspector`, on a port of its own to the host. It's
|
|
@@ -105,17 +105,18 @@ type SubscribeOptions = {
|
|
|
105
105
|
/** An isolate's side of the bus: `ctx.bus`, on its own port to the bus worker. */
|
|
106
106
|
interface BusClient {
|
|
107
107
|
/**
|
|
108
|
-
* Appends a record to a topic
|
|
108
|
+
* Appends a record to a topic: `publish(count, 1)`, or
|
|
109
|
+
* `publish(prices, 102, { key: 'AAPL' })` with a key.
|
|
109
110
|
* @param topic The topic, from the contracts.
|
|
110
|
-
* @param
|
|
111
|
+
* @param value The record's value, checked against the topic's schema.
|
|
112
|
+
* @param opts The record's `key`. Default: none.
|
|
111
113
|
* @returns Resolves once the broker has appended the record.
|
|
112
114
|
* @throws Rejects with a `BusError`, for example `forbidden` without a
|
|
113
115
|
* publish grant, `invalid` when the value fails the schema, or
|
|
114
116
|
* `bus_unavailable` while 1,000 calls wait for an answer.
|
|
115
117
|
*/
|
|
116
|
-
publish<In>(topic: TopicRef<In, any, any, any>,
|
|
118
|
+
publish<In>(topic: TopicRef<In, any, any, any>, value: In, opts?: {
|
|
117
119
|
key?: string;
|
|
118
|
-
value: In;
|
|
119
120
|
}): Promise<void>;
|
|
120
121
|
/**
|
|
121
122
|
* Delivers a topic's records to `fn`, one at a time, in order: the next
|
|
@@ -159,6 +160,25 @@ interface BusClient {
|
|
|
159
160
|
* @experimental
|
|
160
161
|
*/
|
|
161
162
|
commit(record: TypedRecord<unknown>): void;
|
|
163
|
+
/**
|
|
164
|
+
* A watcher skips values; use `subscribe` to get every record.
|
|
165
|
+
*
|
|
166
|
+
* Reads a topic's current value per key, not its stream: `onView` gets a
|
|
167
|
+
* snapshot first, the newest retained value of each key it watches, then
|
|
168
|
+
* the keys that changed. When several values arrive for one key between
|
|
169
|
+
* two views, only the newest is passed on. Records with no key share the
|
|
170
|
+
* key `null`. A key leaves the view when retention removes its newest
|
|
171
|
+
* record. Several watches on one topic share one watch on the bus, so
|
|
172
|
+
* each component can have its own. Needs the subscribe grant for the
|
|
173
|
+
* topic; a refused watch is reported as a non-fatal error.
|
|
174
|
+
* @param topic The topic, from the contracts.
|
|
175
|
+
* @param onView Called with the whole view and the keys that changed; the
|
|
176
|
+
* next view waits until it returns or its promise settles.
|
|
177
|
+
* @param opts The keys to watch. Default: every key.
|
|
178
|
+
* @returns The function that ends this watch.
|
|
179
|
+
* @experimental
|
|
180
|
+
*/
|
|
181
|
+
watch<Out>(topic: TopicRef<any, Out, any, any>, onView: OnView$1<Out>, opts?: WatchOptions$1): () => void;
|
|
162
182
|
}
|
|
163
183
|
/** A host call (`ctx.host`) that failed, with an error code to branch on. */
|
|
164
184
|
declare class HostCallError extends Error {
|
|
@@ -349,11 +369,11 @@ declare const REQUIRES: string;
|
|
|
349
369
|
* start(ctx) {
|
|
350
370
|
* const button = document.createElement('button');
|
|
351
371
|
* button.textContent = 'Add one';
|
|
352
|
-
* button.addEventListener('click', () => void ctx.bus.publish(count,
|
|
372
|
+
* button.addEventListener('click', () => void ctx.bus.publish(count, 1));
|
|
353
373
|
* ctx.root?.append(button);
|
|
354
374
|
* },
|
|
355
375
|
* });
|
|
356
376
|
* ```
|
|
357
377
|
*/
|
|
358
378
|
declare function defineIsolate<Root = IsolateRoot>(def: IsolateDefinition<Root>): IsolateDefinition<Root>;
|
|
359
|
-
export { defineIsolate as a, ScrollIntoViewOptionsLike as c,
|
|
379
|
+
export { defineIsolate as a, ScrollIntoViewOptionsLike as c, RequestOptions as d, SubscribeOptions as f, REQUIRES as i, BusClient as l, IsolateInspector as m, IsolateDefinition as n, HostApi as o, WatchOptions as p, IsolateRoot as r, HostCallError as s, IsolateContext as t, OnView as u };
|
package/dist/worker.js
CHANGED
|
@@ -146,6 +146,217 @@ function createCommitter(o) {
|
|
|
146
146
|
}
|
|
147
147
|
};
|
|
148
148
|
}
|
|
149
|
+
/** Makes the watches of one bus connection. @internal */
|
|
150
|
+
function createWatches(o) {
|
|
151
|
+
const topics = /* @__PURE__ */ new Map();
|
|
152
|
+
/** The highest settled view per topic, not yet sent. */
|
|
153
|
+
const seen = /* @__PURE__ */ new Map();
|
|
154
|
+
const unionOf = (w) => {
|
|
155
|
+
const union = /* @__PURE__ */ new Set();
|
|
156
|
+
for (const l of w.locals) {
|
|
157
|
+
if (!l.keys) return void 0;
|
|
158
|
+
for (const k of l.keys) union.add(k);
|
|
159
|
+
}
|
|
160
|
+
return union;
|
|
161
|
+
};
|
|
162
|
+
const sendWatch = (w) => {
|
|
163
|
+
const union = unionOf(w);
|
|
164
|
+
w.wire = union;
|
|
165
|
+
if (union) {
|
|
166
|
+
for (const k of w.values.keys()) if (!union.has(k)) w.values.delete(k);
|
|
167
|
+
}
|
|
168
|
+
const gen = ++w.gen;
|
|
169
|
+
w.owed++;
|
|
170
|
+
seen.delete(w.topic);
|
|
171
|
+
const p = { topic: w.topic };
|
|
172
|
+
if (union) p.keys = [...union];
|
|
173
|
+
if (w.values.size > 0) p.have = [...w.values].map(([k, v]) => [k, v.version]);
|
|
174
|
+
o.call({
|
|
175
|
+
v: 1,
|
|
176
|
+
t: "watch",
|
|
177
|
+
p
|
|
178
|
+
}).then((r) => {
|
|
179
|
+
if (r.ok) return;
|
|
180
|
+
if (topics.get(w.topic) === w && w.gen === gen) w.owed = Math.max(0, w.owed - 1);
|
|
181
|
+
o.onError(new BusError(r.error));
|
|
182
|
+
});
|
|
183
|
+
};
|
|
184
|
+
/** Sends one `watch` for the changes of this task: mounts and unmounts come in groups. */
|
|
185
|
+
const rewatchSoon = (w) => {
|
|
186
|
+
if (w.rewatchQueued) return;
|
|
187
|
+
w.rewatchQueued = true;
|
|
188
|
+
queueMicrotask(() => {
|
|
189
|
+
w.rewatchQueued = false;
|
|
190
|
+
if (topics.get(w.topic) !== w || w.locals.size === 0) return;
|
|
191
|
+
if (w.gen === 0 || !sameKeys(unionOf(w), w.wire)) sendWatch(w);
|
|
192
|
+
});
|
|
193
|
+
};
|
|
194
|
+
const viewFor = (w, l) => {
|
|
195
|
+
const view = /* @__PURE__ */ new Map();
|
|
196
|
+
if (l.keys) for (const k of l.keys) {
|
|
197
|
+
const v = w.values.get(k);
|
|
198
|
+
if (v) view.set(k, v.value);
|
|
199
|
+
}
|
|
200
|
+
else for (const [k, v] of w.values) view.set(k, v.value);
|
|
201
|
+
return view;
|
|
202
|
+
};
|
|
203
|
+
const settle = (w, item) => {
|
|
204
|
+
if (item.vseq === void 0 || topics.get(w.topic) !== w || item.gen !== w.gen) return;
|
|
205
|
+
seen.set(w.topic, Math.max(seen.get(w.topic) ?? 0, item.vseq));
|
|
206
|
+
if (item.burst) o.flushNow();
|
|
207
|
+
else o.flushSoon();
|
|
208
|
+
};
|
|
209
|
+
const drain = async (w) => {
|
|
210
|
+
if (w.running) return;
|
|
211
|
+
w.running = true;
|
|
212
|
+
while (w.queue.length > 0 && topics.get(w.topic) === w) {
|
|
213
|
+
const item = w.queue.shift();
|
|
214
|
+
if (!item) break;
|
|
215
|
+
const waits = [];
|
|
216
|
+
for (const l of w.locals) {
|
|
217
|
+
let changed;
|
|
218
|
+
if (l.keys) {
|
|
219
|
+
changed = /* @__PURE__ */ new Set();
|
|
220
|
+
for (const k of item.changed) if (l.keys.has(k)) changed.add(k);
|
|
221
|
+
} else changed = item.changed;
|
|
222
|
+
if (!l.primed) {
|
|
223
|
+
l.primed = true;
|
|
224
|
+
const view = viewFor(w, l);
|
|
225
|
+
changed = /* @__PURE__ */ new Set([...changed, ...view.keys()]);
|
|
226
|
+
waits.push(...run(l, view, changed));
|
|
227
|
+
} else if (changed.size > 0) waits.push(...run(l, viewFor(w, l), changed));
|
|
228
|
+
}
|
|
229
|
+
if (waits.length > 0) await Promise.allSettled(waits);
|
|
230
|
+
settle(w, item);
|
|
231
|
+
}
|
|
232
|
+
w.running = false;
|
|
233
|
+
};
|
|
234
|
+
const run = (l, view, changed) => {
|
|
235
|
+
try {
|
|
236
|
+
const done = l.onView(view, changed);
|
|
237
|
+
if (isThenable(done)) return [done.then(void 0, (err) => {
|
|
238
|
+
o.onError(err);
|
|
239
|
+
})];
|
|
240
|
+
} catch (err) {
|
|
241
|
+
o.onError(err);
|
|
242
|
+
}
|
|
243
|
+
return [];
|
|
244
|
+
};
|
|
245
|
+
return {
|
|
246
|
+
watch(topic, onView, opts = {}) {
|
|
247
|
+
let w = topics.get(topic);
|
|
248
|
+
if (!w) {
|
|
249
|
+
w = {
|
|
250
|
+
topic,
|
|
251
|
+
locals: /* @__PURE__ */ new Set(),
|
|
252
|
+
values: /* @__PURE__ */ new Map(),
|
|
253
|
+
wire: void 0,
|
|
254
|
+
gen: 0,
|
|
255
|
+
owed: 0,
|
|
256
|
+
snapped: false,
|
|
257
|
+
parts: void 0,
|
|
258
|
+
queue: [],
|
|
259
|
+
running: false,
|
|
260
|
+
lastViewAt: void 0,
|
|
261
|
+
rewatchQueued: false
|
|
262
|
+
};
|
|
263
|
+
topics.set(topic, w);
|
|
264
|
+
}
|
|
265
|
+
const local = {
|
|
266
|
+
keys: opts.keys ? new Set(opts.keys) : void 0,
|
|
267
|
+
onView,
|
|
268
|
+
primed: false
|
|
269
|
+
};
|
|
270
|
+
const before = w.gen === 0 ? void 0 : w.wire;
|
|
271
|
+
w.locals.add(local);
|
|
272
|
+
if (w.gen > 0 && w.snapped && sameKeys(unionOf(w), before)) {
|
|
273
|
+
w.queue.push({
|
|
274
|
+
changed: /* @__PURE__ */ new Set(),
|
|
275
|
+
vseq: void 0,
|
|
276
|
+
burst: false,
|
|
277
|
+
gen: w.gen
|
|
278
|
+
});
|
|
279
|
+
drain(w);
|
|
280
|
+
} else rewatchSoon(w);
|
|
281
|
+
const mine = w;
|
|
282
|
+
return () => {
|
|
283
|
+
if (!mine.locals.delete(local) || topics.get(topic) !== mine) return;
|
|
284
|
+
if (mine.locals.size > 0) {
|
|
285
|
+
rewatchSoon(mine);
|
|
286
|
+
return;
|
|
287
|
+
}
|
|
288
|
+
topics.delete(topic);
|
|
289
|
+
seen.delete(topic);
|
|
290
|
+
if (mine.gen > 0) o.send({
|
|
291
|
+
v: 1,
|
|
292
|
+
t: "unwatch",
|
|
293
|
+
p: { topic }
|
|
294
|
+
});
|
|
295
|
+
};
|
|
296
|
+
},
|
|
297
|
+
receive(msg) {
|
|
298
|
+
const w = topics.get(msg.p.topic);
|
|
299
|
+
if (!w) return;
|
|
300
|
+
o.delivered();
|
|
301
|
+
const t = o.now();
|
|
302
|
+
if (msg.p.vseq === 1 && w.owed > 0) w.owed--;
|
|
303
|
+
const current = w.owed === 0;
|
|
304
|
+
const changed = w.parts ?? /* @__PURE__ */ new Set();
|
|
305
|
+
for (const k of msg.p.cleared ?? []) if (w.values.delete(k)) changed.add(k);
|
|
306
|
+
for (const v of msg.p.values) {
|
|
307
|
+
if (w.wire && !w.wire.has(v.key)) continue;
|
|
308
|
+
w.values.set(v.key, {
|
|
309
|
+
version: v.version,
|
|
310
|
+
value: v.value
|
|
311
|
+
});
|
|
312
|
+
changed.add(v.key);
|
|
313
|
+
}
|
|
314
|
+
if (msg.p.more) {
|
|
315
|
+
w.parts = changed;
|
|
316
|
+
return;
|
|
317
|
+
}
|
|
318
|
+
w.parts = void 0;
|
|
319
|
+
const burst = o.intervalMs === 0 || w.lastViewAt !== void 0 && t - w.lastViewAt < o.intervalMs;
|
|
320
|
+
w.lastViewAt = t;
|
|
321
|
+
if (current) w.snapped = true;
|
|
322
|
+
w.queue.push({
|
|
323
|
+
changed,
|
|
324
|
+
vseq: current ? msg.p.vseq : void 0,
|
|
325
|
+
burst,
|
|
326
|
+
gen: w.gen
|
|
327
|
+
});
|
|
328
|
+
drain(w);
|
|
329
|
+
},
|
|
330
|
+
reattach() {
|
|
331
|
+
seen.clear();
|
|
332
|
+
for (const w of topics.values()) {
|
|
333
|
+
w.owed = 0;
|
|
334
|
+
w.parts = void 0;
|
|
335
|
+
w.lastViewAt = void 0;
|
|
336
|
+
if (w.locals.size > 0) sendWatch(w);
|
|
337
|
+
}
|
|
338
|
+
},
|
|
339
|
+
takeSeen() {
|
|
340
|
+
const out = [...seen].map(([topic, vseq]) => ({
|
|
341
|
+
topic,
|
|
342
|
+
vseq
|
|
343
|
+
}));
|
|
344
|
+
seen.clear();
|
|
345
|
+
return out;
|
|
346
|
+
}
|
|
347
|
+
};
|
|
348
|
+
}
|
|
349
|
+
/** Two key sets with the same keys; `undefined` is every key. */
|
|
350
|
+
function sameKeys(a, b) {
|
|
351
|
+
if (!a || !b) return a === b;
|
|
352
|
+
if (a.size !== b.size) return false;
|
|
353
|
+
for (const k of a) if (!b.has(k)) return false;
|
|
354
|
+
return true;
|
|
355
|
+
}
|
|
356
|
+
/** True for anything with a `then` method. @internal */
|
|
357
|
+
function isThenable(x) {
|
|
358
|
+
return (typeof x === "object" || typeof x === "function") && x !== null && typeof x.then === "function";
|
|
359
|
+
}
|
|
149
360
|
/** Wraps a hand-written check as a Standard Schema that passes valid values through unchanged. */
|
|
150
361
|
function systemSchema(check) {
|
|
151
362
|
return { "~standard": {
|
|
@@ -170,7 +381,12 @@ function majorMinor(version) {
|
|
|
170
381
|
const m = /^(\d+)\.(\d+)(?:[.+-].*)?$/.exec(version);
|
|
171
382
|
return m ? `${Number(m[1])}.${Number(m[2])}` : void 0;
|
|
172
383
|
}
|
|
173
|
-
|
|
384
|
+
function compareVersions(a, b) {
|
|
385
|
+
const [am, an] = parse(a);
|
|
386
|
+
const [bm, bn] = parse(b);
|
|
387
|
+
return am - bm || an - bn;
|
|
388
|
+
}
|
|
389
|
+
var PROTOCOL, isRecord$1, isOptionalString, ERROR_CODES, SYSTEM_TOPIC_PREFIX, MAX_BLOCK_MS, HOST_TO_ISOLATE, isHostToIsolate, BUS_TO_ISOLATE, isBusToIsolate, point, BusError, isObject, issue, checks, stringOrNull, parse, INSPECT_MESSAGES, INSPECT_TO_ISOLATE, isInspectToIsolate;
|
|
174
390
|
var init_dist = __esmMin((() => {
|
|
175
391
|
PROTOCOL = {
|
|
176
392
|
v: 1,
|
|
@@ -228,7 +444,8 @@ var init_dist = __esmMin((() => {
|
|
|
228
444
|
BUS_TO_ISOLATE = {
|
|
229
445
|
deliver: true,
|
|
230
446
|
reset: true,
|
|
231
|
-
result: true
|
|
447
|
+
result: true,
|
|
448
|
+
view: true
|
|
232
449
|
};
|
|
233
450
|
isBusToIsolate = envelopeGuard(BUS_TO_ISOLATE);
|
|
234
451
|
point = ({ offset, seq, partial }) => partial ? {
|
|
@@ -281,6 +498,12 @@ var init_dist = __esmMin((() => {
|
|
|
281
498
|
})),
|
|
282
499
|
retention: { kind: "compacted" }
|
|
283
500
|
});
|
|
501
|
+
parse = (v) => {
|
|
502
|
+
const mm = majorMinor(v);
|
|
503
|
+
if (mm === void 0) throw new TypeError(`not a version: ${JSON.stringify(v)}`);
|
|
504
|
+
const [major = 0, minor = 0] = mm.split(".").map(Number);
|
|
505
|
+
return [major, minor];
|
|
506
|
+
};
|
|
284
507
|
INSPECT_MESSAGES = {
|
|
285
508
|
snapshot: true,
|
|
286
509
|
event: true,
|
|
@@ -296,6 +519,56 @@ var init_dist = __esmMin((() => {
|
|
|
296
519
|
};
|
|
297
520
|
isInspectToIsolate = envelopeGuard(INSPECT_TO_ISOLATE);
|
|
298
521
|
}));
|
|
522
|
+
// package.json
|
|
523
|
+
var version = "0.2.0-next.0";
|
|
524
|
+
// src/bus/stamped.ts
|
|
525
|
+
/**
|
|
526
|
+
* Does an isolate stamped `requires` get 0.1's `publish`? Only when it was
|
|
527
|
+
* built before 0.2 and this runtime is 0.2 or later. An unstamped isolate gets
|
|
528
|
+
* the current shape.
|
|
529
|
+
* @param own This runtime's `major.minor`. Default: from the package version.
|
|
530
|
+
*/
|
|
531
|
+
function usesOldPublish(requires, own = majorMinor(version) ?? "0.0") {
|
|
532
|
+
if (requires === void 0 || majorMinor(requires) === void 0) return false;
|
|
533
|
+
return compareVersions(requires, NEW_PUBLISH) < 0 && compareVersions(own, NEW_PUBLISH) >= 0;
|
|
534
|
+
}
|
|
535
|
+
/**
|
|
536
|
+
* `bus` with 0.1's `publish(topic, { value, key? })`, the rest unchanged. The
|
|
537
|
+
* first `publish` call runs `warn`, when given.
|
|
538
|
+
*/
|
|
539
|
+
function withOldPublish(bus, warn) {
|
|
540
|
+
let warned = false;
|
|
541
|
+
return {
|
|
542
|
+
...bus,
|
|
543
|
+
publish(topic, rec) {
|
|
544
|
+
if (!warned) {
|
|
545
|
+
warned = true;
|
|
546
|
+
warn?.();
|
|
547
|
+
}
|
|
548
|
+
const r = rec;
|
|
549
|
+
if (typeof r !== "object" || r === null) return Promise.reject(/* @__PURE__ */ new TypeError("publish(topic, rec): rec must be { value, key? }"));
|
|
550
|
+
const key = Reflect.get(r, "key");
|
|
551
|
+
return bus.publish(topic, Reflect.get(r, "value"), typeof key === "string" ? { key } : {});
|
|
552
|
+
}
|
|
553
|
+
};
|
|
554
|
+
}
|
|
555
|
+
/**
|
|
556
|
+
* Gives `ctx` the shapes its isolate was built for, before `start` runs: today
|
|
557
|
+
* 0.1's `publish` for an isolate stamped below 0.2.
|
|
558
|
+
* @param dev `KernelConfig.dev`: the first old-shape call logs {@link OLD_PUBLISH_WARNING}.
|
|
559
|
+
* @param own This runtime's `major.minor`. Default: from the package version.
|
|
560
|
+
*/
|
|
561
|
+
function applyStamp(ctx, requires, dev, own) {
|
|
562
|
+
if (!usesOldPublish(requires, own)) return;
|
|
563
|
+
const warn = dev ? () => console.warn(OLD_PUBLISH_WARNING) : void 0;
|
|
564
|
+
Reflect.set(ctx, "bus", withOldPublish(ctx.bus, warn));
|
|
565
|
+
}
|
|
566
|
+
var NEW_PUBLISH, OLD_PUBLISH_WARNING;
|
|
567
|
+
var init_stamped = __esmMin((() => {
|
|
568
|
+
init_dist();
|
|
569
|
+
NEW_PUBLISH = "0.2";
|
|
570
|
+
OLD_PUBLISH_WARNING = "Isoloop: this isolate was built for 0.1 and calls ctx.bus.publish(topic, { value, key }). Since 0.2 it is publish(topic, value, { key }); the old shape stops working in 0.3. Rebuild the isolate on 0.2 after running npx isoloop@latest migrate.";
|
|
571
|
+
}));
|
|
299
572
|
// src/pools/busy.ts
|
|
300
573
|
function createBusy(now = () => performance.now()) {
|
|
301
574
|
let frame = null;
|
|
@@ -975,14 +1248,52 @@ function createBusClient(first, o = {}) {
|
|
|
975
1248
|
send(withId);
|
|
976
1249
|
});
|
|
977
1250
|
};
|
|
978
|
-
|
|
979
|
-
|
|
980
|
-
|
|
981
|
-
|
|
982
|
-
|
|
983
|
-
|
|
984
|
-
|
|
1251
|
+
/** Sends a `commits` with these offsets and every `seen` still pending. */
|
|
1252
|
+
const sendCommits = (offsets) => {
|
|
1253
|
+
clearSeenTimer();
|
|
1254
|
+
const seen = watches.takeSeen();
|
|
1255
|
+
if (offsets.length === 0 && seen.length === 0) return;
|
|
1256
|
+
send({
|
|
1257
|
+
v: 1,
|
|
1258
|
+
t: "commits",
|
|
1259
|
+
p: seen.length > 0 ? {
|
|
1260
|
+
offsets,
|
|
1261
|
+
seen
|
|
1262
|
+
} : { offsets }
|
|
1263
|
+
});
|
|
1264
|
+
};
|
|
1265
|
+
const intervalMs = o.commitIntervalMs ?? 100;
|
|
1266
|
+
let seenTimer;
|
|
1267
|
+
let seenQueued = false;
|
|
1268
|
+
const clearSeenTimer = () => {
|
|
1269
|
+
if (seenTimer !== void 0) clearTimeout(seenTimer);
|
|
1270
|
+
seenTimer = void 0;
|
|
1271
|
+
seenQueued = false;
|
|
1272
|
+
};
|
|
1273
|
+
const watches = createWatches({
|
|
1274
|
+
send,
|
|
1275
|
+
call: (msg) => call(msg),
|
|
1276
|
+
now,
|
|
1277
|
+
onError: (err) => o.onError?.(err),
|
|
1278
|
+
intervalMs,
|
|
1279
|
+
flushNow: () => sendCommits(committer.take() ?? []),
|
|
1280
|
+
flushSoon: () => {
|
|
1281
|
+
if (intervalMs === 0) {
|
|
1282
|
+
if (seenQueued) return;
|
|
1283
|
+
seenQueued = true;
|
|
1284
|
+
queueMicrotask(() => {
|
|
1285
|
+
if (seenQueued) sendCommits(committer.take() ?? []);
|
|
1286
|
+
});
|
|
1287
|
+
return;
|
|
1288
|
+
}
|
|
1289
|
+
seenTimer ??= setTimeout(() => sendCommits(committer.take() ?? []), intervalMs);
|
|
985
1290
|
},
|
|
1291
|
+
delivered: () => {
|
|
1292
|
+
lastDelivery = now();
|
|
1293
|
+
}
|
|
1294
|
+
});
|
|
1295
|
+
const committer = createCommitter({
|
|
1296
|
+
send: sendCommits,
|
|
986
1297
|
...o.commitIntervalMs === void 0 ? {} : { intervalMs: o.commitIntervalMs },
|
|
987
1298
|
...o.credit === void 0 ? {} : { credit: o.credit }
|
|
988
1299
|
});
|
|
@@ -1076,6 +1387,9 @@ function createBusClient(first, o = {}) {
|
|
|
1076
1387
|
}
|
|
1077
1388
|
return;
|
|
1078
1389
|
}
|
|
1390
|
+
case "view":
|
|
1391
|
+
watches.receive(msg);
|
|
1392
|
+
return;
|
|
1079
1393
|
default: return msg;
|
|
1080
1394
|
}
|
|
1081
1395
|
};
|
|
@@ -1088,14 +1402,14 @@ function createBusClient(first, o = {}) {
|
|
|
1088
1402
|
listen(first);
|
|
1089
1403
|
return {
|
|
1090
1404
|
client: {
|
|
1091
|
-
async publish(topic,
|
|
1405
|
+
async publish(topic, value, opts = {}) {
|
|
1092
1406
|
const p = {
|
|
1093
1407
|
topic: topic.name,
|
|
1094
|
-
value
|
|
1408
|
+
value,
|
|
1095
1409
|
epoch,
|
|
1096
1410
|
seq: ++seq
|
|
1097
1411
|
};
|
|
1098
|
-
if (
|
|
1412
|
+
if (opts.key !== void 0) p.key = opts.key;
|
|
1099
1413
|
check(await call({
|
|
1100
1414
|
v: 1,
|
|
1101
1415
|
t: "publish",
|
|
@@ -1174,6 +1488,9 @@ function createBusClient(first, o = {}) {
|
|
|
1174
1488
|
const d = delivered.get(record);
|
|
1175
1489
|
if (d === void 0) throw new Error("commit: this record was not delivered to this isolate");
|
|
1176
1490
|
commit(d);
|
|
1491
|
+
},
|
|
1492
|
+
watch(topic, onView, opts) {
|
|
1493
|
+
return watches.watch(topic.name, onView, opts);
|
|
1177
1494
|
}
|
|
1178
1495
|
},
|
|
1179
1496
|
idle: () => ({
|
|
@@ -1188,7 +1505,7 @@ function createBusClient(first, o = {}) {
|
|
|
1188
1505
|
listen(next);
|
|
1189
1506
|
const unanswered = [...pending.values()];
|
|
1190
1507
|
for (const p of unanswered) {
|
|
1191
|
-
if (p.msg.t !== "subscribe") continue;
|
|
1508
|
+
if (p.msg.t !== "subscribe" && p.msg.t !== "watch") continue;
|
|
1192
1509
|
pending.delete(p.msg.id);
|
|
1193
1510
|
p.settle({
|
|
1194
1511
|
ok: true,
|
|
@@ -1196,16 +1513,19 @@ function createBusClient(first, o = {}) {
|
|
|
1196
1513
|
});
|
|
1197
1514
|
}
|
|
1198
1515
|
committer.take();
|
|
1516
|
+
clearSeenTimer();
|
|
1199
1517
|
for (const sub of subs.values()) {
|
|
1200
1518
|
sub.queue.length = 0;
|
|
1201
1519
|
sub.highest = -1;
|
|
1202
1520
|
sendSubscribe(sub);
|
|
1203
1521
|
}
|
|
1204
|
-
|
|
1522
|
+
watches.reattach();
|
|
1523
|
+
for (const p of unanswered) if (p.msg.t !== "subscribe" && p.msg.t !== "watch") send(p.msg);
|
|
1205
1524
|
},
|
|
1206
1525
|
async close() {
|
|
1207
1526
|
const offsets = committer.take();
|
|
1208
1527
|
committer.dispose();
|
|
1528
|
+
clearSeenTimer();
|
|
1209
1529
|
if (offsets) await call({
|
|
1210
1530
|
v: 1,
|
|
1211
1531
|
t: "commits",
|
|
@@ -1214,9 +1534,6 @@ function createBusClient(first, o = {}) {
|
|
|
1214
1534
|
}
|
|
1215
1535
|
};
|
|
1216
1536
|
}
|
|
1217
|
-
function isThenable(x) {
|
|
1218
|
-
return (typeof x === "object" || typeof x === "function") && x !== null && typeof x.then === "function";
|
|
1219
|
-
}
|
|
1220
1537
|
var randomEpoch, check, REQUEST_TIMEOUT_MS;
|
|
1221
1538
|
var init_bus_client = __esmMin((() => {
|
|
1222
1539
|
init_dist();
|
|
@@ -2685,8 +3002,6 @@ var init_render = __esmMin((() => {
|
|
|
2685
3002
|
shared = null;
|
|
2686
3003
|
headListeners = /* @__PURE__ */ new Set();
|
|
2687
3004
|
}));
|
|
2688
|
-
// package.json
|
|
2689
|
-
var version = "0.1.0";
|
|
2690
3005
|
// src/isolate/vanilla.ts
|
|
2691
3006
|
var vanilla_exports = /* @__PURE__ */ __exportAll({
|
|
2692
3007
|
REQUIRES: () => REQUIRES,
|
|
@@ -2707,7 +3022,7 @@ var vanilla_exports = /* @__PURE__ */ __exportAll({
|
|
|
2707
3022
|
* start(ctx) {
|
|
2708
3023
|
* const button = document.createElement('button');
|
|
2709
3024
|
* button.textContent = 'Add one';
|
|
2710
|
-
* button.addEventListener('click', () => void ctx.bus.publish(count,
|
|
3025
|
+
* button.addEventListener('click', () => void ctx.bus.publish(count, 1));
|
|
2711
3026
|
* ctx.root?.append(button);
|
|
2712
3027
|
* },
|
|
2713
3028
|
* });
|
|
@@ -2966,13 +3281,14 @@ async function boot(m, init) {
|
|
|
2966
3281
|
const start = main.start;
|
|
2967
3282
|
if (typeof start === "function") {
|
|
2968
3283
|
const requires = "requires" in main ? main.requires : void 0;
|
|
2969
|
-
await startIsolate(m, runtime, (ctx) => Reflect.apply(start, main, [ctx]), typeof requires === "string" ? requires : void 0);
|
|
3284
|
+
await startIsolate(m, runtime, (ctx) => Reflect.apply(start, main, [ctx]), typeof requires === "string" ? requires : void 0, init.dev === true);
|
|
2970
3285
|
return;
|
|
2971
3286
|
}
|
|
2972
3287
|
}
|
|
2973
3288
|
fatal(m, `${init.src} has no default export with start(ctx)`);
|
|
2974
3289
|
}
|
|
2975
|
-
async function startIsolate(m, runtime, start, requires) {
|
|
3290
|
+
async function startIsolate(m, runtime, start, requires, dev) {
|
|
3291
|
+
applyStamp(runtime.ctx, requires, dev);
|
|
2976
3292
|
m.receive = (msg) => {
|
|
2977
3293
|
runtime.receive(msg);
|
|
2978
3294
|
};
|
|
@@ -3036,6 +3352,7 @@ function startOnce() {
|
|
|
3036
3352
|
var mode, members, postWorker, memberPost, fatal, beating, STARTED;
|
|
3037
3353
|
var init_bootstrap = __esmMin((() => {
|
|
3038
3354
|
init_dist();
|
|
3355
|
+
init_stamped();
|
|
3039
3356
|
init_busy();
|
|
3040
3357
|
init_freeze();
|
|
3041
3358
|
mode = null;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@isoloop/runtime",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.2.0-next.0",
|
|
4
4
|
"description": "The Isoloop runtime: write a part of your app as an isolate in a worker, with a UI or headless, in plain TypeScript.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"isoloop",
|
|
@@ -43,8 +43,8 @@
|
|
|
43
43
|
}
|
|
44
44
|
},
|
|
45
45
|
"dependencies": {
|
|
46
|
-
"@isoloop/dom": "0.
|
|
47
|
-
"@isoloop/protocol": "0.
|
|
46
|
+
"@isoloop/dom": "0.2.0-next.0",
|
|
47
|
+
"@isoloop/protocol": "0.2.0-next.0"
|
|
48
48
|
},
|
|
49
49
|
"devDependencies": {
|
|
50
50
|
"typescript": "7.0.2"
|