mirror of
https://github.com/vercel-labs/scriptc.git
synced 2026-10-31 07:23:06 +08:00
- The ratified rename rider (world-unification §7 lane 5): "DOM" collided with the browser DOM and, post-lane-1, the structure is no longer a document tree. Comments and doc prose now say "the checked-dynamic tree" for the noun and "dyn" adjectivally; runtime symbol names (scr_dyn_*), the error.domClone libCall, and the real web DOMException family are unchanged. - The few user-facing strings carrying the nickname move with it (the structuredClone cyclic message, the dc subscriber/publish fences, two emitter ICE texts); diagnostics snapshots regenerated (96 pass). One private compiler constant (DOM_PROTO_METHOD_NAMES -> DYN_PROTO_METHOD_NAMES) rides along. - Mechanical sweep, 142 files, comments/prose only — no behavior change.
58 lines
2.2 KiB
TypeScript
58 lines
2.2 KiB
TypeScript
// node:diagnostics_channel — the pub/sub core: channel identity by name,
|
|
// name/hasSubscribers reads, module-level and channel-level subscribe/
|
|
// unsubscribe (one subscriber list — Node's), publish over string and
|
|
// object messages, the mid-publish self-unsubscribe (the snapshot rule:
|
|
// siblings still fire this round), and the false answers (unsubscribe of
|
|
// a never-subscribed fn, hasSubscribers of an unknown channel).
|
|
import { channel, hasSubscribers, subscribe, unsubscribe } from "node:diagnostics_channel";
|
|
import * as dc from "diagnostics_channel";
|
|
|
|
const ch = channel("corpus:one");
|
|
console.log(ch.name);
|
|
console.log(ch.hasSubscribers, hasSubscribers("corpus:one"), hasSubscribers("corpus:never"));
|
|
|
|
// channel() interns by name: the module-level subscribe lands on the SAME
|
|
// subscriber list the channel handle publishes to.
|
|
const seen: string[] = [];
|
|
const onMessage = (message: unknown, name: string): void => {
|
|
seen.push(`${name}=${String(message)}`);
|
|
};
|
|
subscribe("corpus:one", onMessage);
|
|
console.log(ch.hasSubscribers);
|
|
ch.publish("hello");
|
|
ch.publish("again");
|
|
console.log(seen.join(","));
|
|
|
|
// unsubscribe answers whether it removed anything.
|
|
const other = (message: unknown, name: string): void => {
|
|
console.log(`other saw ${name}`);
|
|
};
|
|
console.log(unsubscribe("corpus:one", other));
|
|
console.log(ch.unsubscribe(onMessage));
|
|
console.log(ch.hasSubscribers);
|
|
ch.publish("nobody");
|
|
console.log(seen.join(","));
|
|
|
|
// Object messages cross as dyn values; the subscriber reads them back.
|
|
const obj = dc.channel("corpus:two");
|
|
obj.subscribe((message: unknown, name: string): void => {
|
|
const m = message as { foo: string; n: number };
|
|
console.log(`${name}: ${m.foo} ${m.n}`);
|
|
});
|
|
obj.publish({ foo: "bar", n: 42 });
|
|
|
|
// The snapshot rule: a subscriber unsubscribing itself mid-publish still
|
|
// lets its sibling fire this round, and is gone the next.
|
|
const tri = channel("corpus:three");
|
|
const first = (message: unknown, name: string): void => {
|
|
console.log(`first ${String(message)}`);
|
|
tri.unsubscribe(first);
|
|
};
|
|
const second = (message: unknown, name: string): void => {
|
|
console.log(`second ${String(message)}`);
|
|
};
|
|
tri.subscribe(first);
|
|
tri.subscribe(second);
|
|
tri.publish("round1");
|
|
tri.publish("round2");
|