@moku-labs/web 2.0.1 → 2.1.1

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/index.cjs CHANGED
@@ -1420,6 +1420,22 @@ function route(pattern) {
1420
1420
  const builder = {
1421
1421
  pattern: carrier.pattern,
1422
1422
  _meta: carrier._meta,
1423
+ /**
1424
+ * The named view-transition behaviour set via `.transition()` (live read).
1425
+ *
1426
+ * @returns The transition mode, or undefined when unset (app default applies).
1427
+ */
1428
+ get _transition() {
1429
+ return carrier._transition;
1430
+ },
1431
+ /**
1432
+ * The scroll behaviour set via `.scroll()` (live read).
1433
+ *
1434
+ * @returns The scroll mode, or undefined when unset (app default applies).
1435
+ */
1436
+ get _scroll() {
1437
+ return carrier._scroll;
1438
+ },
1423
1439
  _handlers: carrier._handlers,
1424
1440
  ...createBuilderMethods(set),
1425
1441
  /**
@@ -1437,6 +1453,38 @@ function route(pattern) {
1437
1453
  meta(meta) {
1438
1454
  Object.assign(carrier._meta, meta);
1439
1455
  return builder;
1456
+ },
1457
+ /**
1458
+ * Declare the view-transition behaviour for navigations TO this route — a typed
1459
+ * framework directive read by the SPA kernel, overriding the app-wide
1460
+ * `spa.viewTransitions` default.
1461
+ *
1462
+ * @param mode - The {@link TransitionMode} (`none`/`crossfade`/`slide`/`morph`).
1463
+ * @returns The same builder for chaining.
1464
+ * @example
1465
+ * ```ts
1466
+ * route("/board/{id}/issue/{issueId}").transition("morph");
1467
+ * ```
1468
+ */
1469
+ transition(mode) {
1470
+ carrier._transition = mode;
1471
+ return builder;
1472
+ },
1473
+ /**
1474
+ * Declare the scroll behaviour for navigations TO this route — a typed framework
1475
+ * directive read by the SPA kernel, overriding the app-wide `spa.scrollRestoration`
1476
+ * default.
1477
+ *
1478
+ * @param mode - The {@link ScrollMode} (`top`/`preserve`).
1479
+ * @returns The same builder for chaining.
1480
+ * @example
1481
+ * ```ts
1482
+ * route("/board/{id}/issue/{issueId}").scroll("preserve");
1483
+ * ```
1484
+ */
1485
+ scroll(mode) {
1486
+ carrier._scroll = mode;
1487
+ return builder;
1440
1488
  }
1441
1489
  };
1442
1490
  return builder;
@@ -9161,11 +9209,12 @@ function createApi(ctx) {
9161
9209
  * Programmatically navigate to a path (client runtime; no-op without a DOM).
9162
9210
  *
9163
9211
  * @param path - Target path (pathname, optionally with search/hash).
9212
+ * @param options - Optional per-navigation overrides (e.g. `{ scroll: "preserve" }`).
9164
9213
  * @example
9165
9214
  * app.spa.navigate("/about");
9166
9215
  */
9167
- navigate(path) {
9168
- ctx.state.kernel?.processNav(path);
9216
+ navigate(path, options) {
9217
+ ctx.state.kernel?.processNav(path, options);
9169
9218
  },
9170
9219
  /**
9171
9220
  * Read the current resolved URL.
@@ -9356,13 +9405,15 @@ let commitVNodeFunction;
9356
9405
  * await loadRenderChunk();
9357
9406
  */
9358
9407
  async function loadRenderChunk() {
9359
- renderChunk ??= Promise.resolve().then(() => require("./render-DHUcHCYs.cjs"));
9408
+ renderChunk ??= Promise.resolve().then(() => require("./render-rf8FAg_Z.cjs"));
9360
9409
  commitVNodeFunction = (await renderChunk).commitVNode;
9361
9410
  }
9362
9411
  /**
9363
9412
  * Commit a {@link RenderResult} into a host: `string` → `innerHTML`, `Node` →
9364
- * `replaceChildren`, `void`/`undefined` → no-op (the render mutated the DOM itself), and
9365
- * a Preact `VNode` → committed through the lazy gate (loading it on demand if needed).
9413
+ * `replaceChildren`, `null` → Preact unmount via the lazy gate (`render(null, host)` —
9414
+ * stays re-mountable, the correct "empty" for a persistent render-island), `void`/`undefined`
9415
+ * → no-op (the render mutated the DOM itself), and a Preact `VNode` → committed through the
9416
+ * lazy gate (loading it on demand if needed).
9366
9417
  *
9367
9418
  * @param host - The island host element to render into.
9368
9419
  * @param result - The value returned by the island's `render`.
@@ -9489,6 +9540,18 @@ function buildContext(state, instance) {
9489
9540
  return instance.route.url;
9490
9541
  },
9491
9542
  /**
9543
+ * Programmatically navigate the SPA (delegates to the kernel's navigator through
9544
+ * `state.navigate` — the same decoupled seam as `island`). No-op before the kernel binds.
9545
+ *
9546
+ * @param path - The internal destination path.
9547
+ * @param options - Optional per-navigation overrides (e.g. `{ scroll: "preserve" }`).
9548
+ * @example
9549
+ * ctx.navigate(ctx.url("issue", { id, issueId }));
9550
+ */
9551
+ navigate(path, options) {
9552
+ state.navigate?.(path, options);
9553
+ },
9554
+ /**
9492
9555
  * The live per-instance state (`undefined` for legacy hooks-only islands).
9493
9556
  *
9494
9557
  * @returns The current state.
@@ -10108,20 +10171,48 @@ function prefersReducedMotion() {
10108
10171
  * Because the swap is post-fetch, doing it here also avoids the visible "scroll up,
10109
10172
  * THEN the page loads" pause that scrolling in the router (pre-fetch) produced.
10110
10173
  *
10174
+ * Named transitions (`transition.types`, e.g. `["slide"]`) are exposed to CSS two ways
10175
+ * for the transition's lifetime, so a consumer can style the motion per navigation:
10176
+ * • a `:root[data-view-transition~="slide"]` attribute (works on EVERY View-Transitions
10177
+ * engine, including those without the `types` dictionary form);
10178
+ * • the standards-track `types` option (`:active-view-transition-type(slide)`) when the
10179
+ * engine supports the `startViewTransition({ update, types })` dictionary form.
10180
+ * Empty `types` ⇒ the default root crossfade. A shared `view-transition-name` left on an
10181
+ * element across the swap (e.g. a card and the panel it opens) morphs one into the other.
10182
+ *
10111
10183
  * @param doSwap - The synchronous DOM mutation to perform.
10112
- * @param viewTransitions - Whether to wrap the swap in `startViewTransition`.
10184
+ * @param transition - The resolved {@link SwapTransition} (enabled + named types).
10113
10185
  * @param beforeCapture - Optional hook run synchronously just before the swap/capture
10114
10186
  * (e.g. scroll to the destination position).
10115
10187
  * @example
10116
- * runSwap(() => current.replaceWith(next), true, () => scrollTo({ top: 0, behavior: "instant" }));
10188
+ * runSwap(() => current.replaceWith(next), { enabled: true, types: ["slide"] });
10117
10189
  */
10118
- function runSwap(doSwap, viewTransitions, beforeCapture) {
10190
+ function runSwap(doSwap, transition, beforeCapture) {
10119
10191
  const reduced = prefersReducedMotion();
10120
10192
  const docWithVt = document;
10121
- const canUseViewTransitions = viewTransitions && !reduced && typeof docWithVt.startViewTransition === "function";
10193
+ const canUseViewTransitions = transition.enabled && !reduced && typeof docWithVt.startViewTransition === "function";
10122
10194
  beforeCapture?.();
10123
- if (canUseViewTransitions) docWithVt.startViewTransition(doSwap);
10124
- else doSwap();
10195
+ if (!canUseViewTransitions) {
10196
+ doSwap();
10197
+ return;
10198
+ }
10199
+ const root = typeof document === "undefined" ? void 0 : document.documentElement;
10200
+ const hasTypes = transition.types.length > 0;
10201
+ if (hasTypes && root) root.dataset.viewTransition = transition.types.join(" ");
10202
+ const clearMarker = () => {
10203
+ if (hasTypes && root) delete root.dataset.viewTransition;
10204
+ };
10205
+ let result;
10206
+ if (hasTypes) try {
10207
+ result = docWithVt.startViewTransition({
10208
+ update: doSwap,
10209
+ types: transition.types
10210
+ });
10211
+ } catch {
10212
+ result = docWithVt.startViewTransition(doSwap);
10213
+ }
10214
+ else result = docWithVt.startViewTransition(doSwap);
10215
+ Promise.resolve(result?.finished).then(clearMarker).catch(clearMarker);
10125
10216
  }
10126
10217
  /**
10127
10218
  * Replace the `swapSelector` region of the live document with the matching
@@ -10135,22 +10226,22 @@ function runSwap(doSwap, viewTransitions, beforeCapture) {
10135
10226
  *
10136
10227
  * @param doc - The fetched document (DOMParser-parsed) holding the new region.
10137
10228
  * @param swapSelector - CSS selector for the region to replace.
10138
- * @param viewTransitions - Whether to wrap the swap in `startViewTransition`.
10229
+ * @param transition - The resolved {@link SwapTransition} (enabled + named types).
10139
10230
  * @param onSwapped - Callback run after the DOM mutation (mount/notify/scroll).
10140
10231
  * @param beforeCapture - Optional hook run synchronously just before the swap/capture
10141
10232
  * (forwarded to {@link runSwap} — e.g. scroll to the destination position).
10142
10233
  * @returns `true` when the swap was dispatched, `false` when either document lacks the region.
10143
10234
  * @example
10144
- * swapRegion(doc, "main > section", false, () => mountNew());
10235
+ * swapRegion(doc, "main > section", { enabled: false, types: [] }, () => mountNew());
10145
10236
  */
10146
- function swapRegion(doc, swapSelector, viewTransitions, onSwapped, beforeCapture) {
10237
+ function swapRegion(doc, swapSelector, transition, onSwapped, beforeCapture) {
10147
10238
  const newContent = doc.querySelector(swapSelector);
10148
10239
  const currentContent = document.querySelector(swapSelector);
10149
10240
  if (!newContent || !currentContent) return false;
10150
10241
  runSwap(() => {
10151
10242
  currentContent.replaceWith(newContent);
10152
10243
  onSwapped();
10153
- }, viewTransitions, beforeCapture);
10244
+ }, transition, beforeCapture);
10154
10245
  return true;
10155
10246
  }
10156
10247
  /**
@@ -10307,10 +10398,25 @@ function attachRouter(handlers, navigate) {
10307
10398
  const ERROR_PREFIX$1 = "[web]";
10308
10399
  /** Last-resort `swapSelector` when neither config nor defaults supply one. */
10309
10400
  const FALLBACK_SWAP_SELECTOR = "main > section";
10401
+ /**
10402
+ * Resolve the `viewTransitions` config value into the default {@link TransitionMode}:
10403
+ * `true` → `"crossfade"`, `false` → `"none"`, a named mode → itself.
10404
+ *
10405
+ * @param raw - The raw `viewTransitions` config value (boolean or named mode).
10406
+ * @returns The resolved default transition mode.
10407
+ * @example
10408
+ * resolveDefaultTransition(true); // "crossfade"
10409
+ */
10410
+ function resolveDefaultTransition(raw) {
10411
+ if (raw === true) return "crossfade";
10412
+ if (raw === false) return "none";
10413
+ return raw;
10414
+ }
10310
10415
  /** Default SPA config (declared as a value — no inline assertion). */
10311
10416
  const defaultSpaConfig = {
10312
10417
  swapSelector: "main > section",
10313
10418
  viewTransitions: false,
10419
+ scrollRestoration: "top",
10314
10420
  progressBar: true,
10315
10421
  islands: []
10316
10422
  };
@@ -10347,9 +10453,12 @@ function resolveSpaConfig(config) {
10347
10453
  const swapSelector = config.swapSelector ?? defaultSpaConfig.swapSelector ?? FALLBACK_SWAP_SELECTOR;
10348
10454
  if (swapSelector.trim() === "") throw new Error(`${ERROR_PREFIX$1} spa.swapSelector must be a non-empty string.\n Set a CSS selector for the page region to swap (e.g. "main > section").`);
10349
10455
  if (!isValidSelector(swapSelector)) throw new Error(`${ERROR_PREFIX$1} spa.swapSelector is not a valid CSS selector: "${swapSelector}".\n Provide a syntactically valid selector.`);
10456
+ const defaultTransition = resolveDefaultTransition(config.viewTransitions ?? false);
10350
10457
  return {
10351
10458
  swapSelector,
10352
- viewTransitions: config.viewTransitions ?? false,
10459
+ viewTransitions: defaultTransition !== "none",
10460
+ defaultTransition,
10461
+ scrollRestoration: config.scrollRestoration ?? "top",
10353
10462
  progressBar: config.progressBar ?? true,
10354
10463
  islands: config.islands ?? []
10355
10464
  };
@@ -10374,6 +10483,7 @@ function createState(_ctx) {
10374
10483
  currentUrl: "",
10375
10484
  destroyRouter: null,
10376
10485
  started: false,
10486
+ navigate: null,
10377
10487
  kernel: null
10378
10488
  };
10379
10489
  }
@@ -10444,6 +10554,52 @@ function createSpaKernel(state, config, emit, deps) {
10444
10554
  const resolved = resolveSpaConfig(config);
10445
10555
  let progress;
10446
10556
  let pendingScrollToTop = true;
10557
+ let pendingTransition = resolveTransition(resolved.defaultTransition);
10558
+ /**
10559
+ * Resolve a {@link TransitionMode} into the runtime {@link SwapTransition} the swap
10560
+ * consumes: `"none"` ⇒ disabled; `"crossfade"` ⇒ enabled, no named types (default root
10561
+ * crossfade); any other mode ⇒ enabled with that name as the View-Transition type. The
10562
+ * app-wide `viewTransitions` enabled flag gates everything: when it is off AND the route
10563
+ * declares no transition, no transition runs.
10564
+ *
10565
+ * @param mode - The effective transition mode for this navigation.
10566
+ * @returns The resolved swap descriptor (enabled + named types).
10567
+ * @example
10568
+ * pendingTransition = resolveTransition("slide");
10569
+ */
10570
+ function resolveTransition(mode) {
10571
+ if (mode === "none") return {
10572
+ enabled: false,
10573
+ types: []
10574
+ };
10575
+ if (mode === "crossfade") return {
10576
+ enabled: true,
10577
+ types: []
10578
+ };
10579
+ return {
10580
+ enabled: true,
10581
+ types: [mode]
10582
+ };
10583
+ }
10584
+ /**
10585
+ * Resolve the per-navigation scroll + transition intent from the DESTINATION route,
10586
+ * falling back to the app-wide defaults. A route's `.scroll()`/`.transition()` win; an
10587
+ * explicit per-call `scrollOverride` (from `ctx.navigate`/`app.spa.navigate`) wins over
10588
+ * the route. `forwardScrollToTop` carries the navigation-type intent (forward ⇒ `true`,
10589
+ * traverse ⇒ `false`); a `"preserve"` resolution forces no scroll-to-top either way.
10590
+ *
10591
+ * @param pathname - The destination pathname (search stripped for matching).
10592
+ * @param forwardScrollToTop - Whether this nav would scroll to top by default (forward vs traverse).
10593
+ * @param scrollOverride - An explicit per-call scroll override, if any.
10594
+ * @example
10595
+ * applyNavIntent("/board/abc/issue/1", true, undefined);
10596
+ */
10597
+ const applyNavIntent = (pathname, forwardScrollToTop, scrollOverride) => {
10598
+ const route = deps.router.match(pathname.split("?")[0] ?? pathname)?.route;
10599
+ const scrollMode = scrollOverride ?? route?._scroll ?? resolved.scrollRestoration;
10600
+ pendingScrollToTop = forwardScrollToTop && scrollMode !== "preserve";
10601
+ pendingTransition = resolveTransition(route?._transition ?? resolved.defaultTransition);
10602
+ };
10447
10603
  /**
10448
10604
  * Apply the in-flight navigation's scroll intent — the swap's `beforeCapture` hook.
10449
10605
  * For a forward nav it scrolls to top BEFORE the swap (and, with view transitions on,
@@ -10509,7 +10665,7 @@ function createSpaKernel(state, config, emit, deps) {
10509
10665
  const doc = new DOMParser().parseFromString(html, "text/html");
10510
10666
  syncHead(deps.head, doc);
10511
10667
  unmountPageSpecific(state, emit);
10512
- if (!swapRegion(doc, resolved.swapSelector, resolved.viewTransitions, () => {
10668
+ if (!swapRegion(doc, resolved.swapSelector, pendingTransition, () => {
10513
10669
  const routeSlice = islandRouteContext(pathname);
10514
10670
  scanAndMount(state, emit, resolved.swapSelector, routeSlice);
10515
10671
  notifyNavEnd(state, routeSlice);
@@ -10609,7 +10765,7 @@ function createSpaKernel(state, config, emit, deps) {
10609
10765
  const commitDataRender = async (pathname, resolvedRender, signal) => {
10610
10766
  if (signal?.aborted) return;
10611
10767
  const { route, vnode, routeContext, region } = resolvedRender;
10612
- const { renderVNode } = await Promise.resolve().then(() => require("./render-DHUcHCYs.cjs"));
10768
+ const { renderVNode } = await Promise.resolve().then(() => require("./render-rf8FAg_Z.cjs"));
10613
10769
  if (signal?.aborted) return;
10614
10770
  syncDataHead(deps.head, route, routeContext);
10615
10771
  unmountPageSpecific(state, emit);
@@ -10628,7 +10784,7 @@ function createSpaKernel(state, config, emit, deps) {
10628
10784
  scanAndMount(state, emit, resolved.swapSelector, routeSlice);
10629
10785
  notifyNavEnd(state, routeSlice);
10630
10786
  };
10631
- runSwap(renderAndMount, resolved.viewTransitions, applyPendingScroll);
10787
+ runSwap(renderAndMount, pendingTransition, applyPendingScroll);
10632
10788
  state.currentUrl = pathname;
10633
10789
  progress?.done();
10634
10790
  emit("spa:navigated", { url: pathname });
@@ -10678,7 +10834,7 @@ function createSpaKernel(state, config, emit, deps) {
10678
10834
  return;
10679
10835
  }
10680
10836
  const { vnode, region } = resolvedRender;
10681
- const { renderVNode } = await Promise.resolve().then(() => require("./render-DHUcHCYs.cjs"));
10837
+ const { renderVNode } = await Promise.resolve().then(() => require("./render-rf8FAg_Z.cjs"));
10682
10838
  renderVNode(vnode, region);
10683
10839
  scanAndMount(state, emit, resolved.swapSelector, routeSlice);
10684
10840
  };
@@ -10693,17 +10849,40 @@ function createSpaKernel(state, config, emit, deps) {
10693
10849
  * (default `true`; forward navs). Traverse passes `false` to keep its restored scroll.
10694
10850
  * @param signal - Aborts when this navigation is superseded (`navEvent.signal`);
10695
10851
  * a superseded navigation never applies its swap (no stale last-write-wins).
10852
+ * @param scrollOverride - An explicit per-call scroll override (from `ctx.navigate`/
10853
+ * `app.spa.navigate`); wins over the destination route's `.scroll()` + the app default.
10696
10854
  * @returns A promise resolving once the swap (or fallback) is dispatched.
10697
10855
  * @example
10698
10856
  * await navigate("/en/world/");
10699
10857
  */
10700
- const navigate = async (pathname, scrollToTop = true, signal) => {
10701
- pendingScrollToTop = scrollToTop;
10858
+ const navigate = async (pathname, scrollToTop = true, signal, scrollOverride) => {
10859
+ applyNavIntent(pathname, scrollToTop, scrollOverride);
10702
10860
  handleStart(pathname);
10703
10861
  if (deps.router.mode() !== "ssg" && await tryDataRender(pathname, signal)) return;
10704
10862
  if (signal?.aborted) return;
10705
10863
  await performNavigation(pathname, handlers, signal);
10706
10864
  };
10865
+ /**
10866
+ * Programmatic navigation entry (`app.spa.navigate` / `ctx.navigate`). A link click or the
10867
+ * Navigation API commits the address bar before the interceptor calls {@link navigate}; a
10868
+ * programmatic call does NOT, so it must commit the URL itself first — otherwise the content
10869
+ * swaps but the address bar (and the back button / refresh / deep link) stays on the old URL.
10870
+ * `history.pushState` updates the URL WITHOUT firing a `navigate` event, so it can't
10871
+ * double-trigger the interceptor. The current scroll is saved first so a later back restores it.
10872
+ *
10873
+ * @param path - The destination path (pathname + optional search).
10874
+ * @param scroll - The optional per-call scroll override.
10875
+ * @example
10876
+ * navigateProgrammatic("/board/abc/issue/1", "preserve");
10877
+ */
10878
+ const navigateProgrammatic = (path, scroll) => {
10879
+ if (typeof document === "undefined") return;
10880
+ if (path !== currentLocationUrl()) {
10881
+ saveScrollPosition(currentLocationUrl());
10882
+ history.pushState({}, "", path);
10883
+ }
10884
+ navigate(path, true, void 0, scroll).catch(() => {});
10885
+ };
10707
10886
  return {
10708
10887
  /**
10709
10888
  * Register config islands and seed currentUrl from the document.
@@ -10714,6 +10893,9 @@ function createSpaKernel(state, config, emit, deps) {
10714
10893
  init() {
10715
10894
  for (const island of resolved.islands) registerIsland(state, island);
10716
10895
  state.currentUrl = currentLocationUrl();
10896
+ state.navigate = (path, options) => {
10897
+ navigateProgrammatic(path, options?.scroll);
10898
+ };
10717
10899
  },
10718
10900
  /**
10719
10901
  * Boot navigation interception + initial scan (throws if already started).
@@ -10744,15 +10926,16 @@ function createSpaKernel(state, config, emit, deps) {
10744
10926
  registerIsland(state, island);
10745
10927
  },
10746
10928
  /**
10747
- * Process a navigation to `path` (fetch then swap; full reload on error).
10929
+ * Process a navigation to `path` (fetch then swap; full reload on error). An optional
10930
+ * per-call `scroll` override flows through to the swap's scroll intent.
10748
10931
  *
10749
10932
  * @param path - The target path to navigate to.
10933
+ * @param options - Optional per-navigation overrides (e.g. `{ scroll: "preserve" }`).
10750
10934
  * @example
10751
10935
  * kernel.processNav("/about");
10752
10936
  */
10753
- processNav(path) {
10754
- if (typeof document === "undefined") return;
10755
- navigate(path).catch(() => {});
10937
+ processNav(path, options) {
10938
+ navigateProgrammatic(path, options?.scroll);
10756
10939
  },
10757
10940
  /**
10758
10941
  * Scan the swap region and mount islands for matching elements.
@@ -10844,6 +11027,255 @@ function disposeSpa() {
10844
11027
  }
10845
11028
  }
10846
11029
  //#endregion
11030
+ //#region src/plugins/spa/channel.ts
11031
+ /** Default keepalive interval (ms). */
11032
+ const DEFAULT_KEEPALIVE_MS = 3e4;
11033
+ /** Default first reconnect delay (ms). */
11034
+ const DEFAULT_RECONNECT_BASE_MS = 500;
11035
+ /** Default reconnect ceiling (ms). */
11036
+ const DEFAULT_RECONNECT_MAX_MS = 8e3;
11037
+ /**
11038
+ * Default frame parser — `JSON.parse`, dropping frames that fail to parse.
11039
+ *
11040
+ * @param raw - The raw text frame.
11041
+ * @returns The parsed value, or undefined on a parse error.
11042
+ * @example
11043
+ * const msg = defaultParse<BoardPatch>('{"type":"x"}');
11044
+ */
11045
+ function defaultParse(raw) {
11046
+ try {
11047
+ return JSON.parse(raw);
11048
+ } catch {
11049
+ return;
11050
+ }
11051
+ }
11052
+ /**
11053
+ * Create a live realtime channel — a single self-healing WebSocket bound to one channel
11054
+ * `id` at a time, with refcounted subscription, auto-reconnect, an optional keepalive, a
11055
+ * pre-seed buffer, and an optimistic local-echo path. See the file header for the model.
11056
+ *
11057
+ * @param options - The channel configuration (see {@link ChannelOptions}).
11058
+ * @returns The {@link Channel} control surface.
11059
+ * @example
11060
+ * const board = createChannel<BoardPatch>({ url: (id) => `wss://host/ws/board/${id}` });
11061
+ */
11062
+ function createChannel(options) {
11063
+ const parse = options.parse ?? defaultParse;
11064
+ const reconnectCfg = options.reconnect === false ? void 0 : options.reconnect ?? {};
11065
+ const baseMs = reconnectCfg?.baseMs ?? DEFAULT_RECONNECT_BASE_MS;
11066
+ const maxMs = reconnectCfg?.maxMs ?? DEFAULT_RECONNECT_MAX_MS;
11067
+ const keepEveryMs = options.keepAlive?.everyMs ?? DEFAULT_KEEPALIVE_MS;
11068
+ /** The live socket, or undefined when disconnected. */
11069
+ let socket;
11070
+ /** The id we WANT to stay subscribed to (drives reconnect); undefined ⇒ intentionally off. */
11071
+ let desiredId;
11072
+ /** Whether the consumer has seeded — gates live delivery vs. buffering. */
11073
+ let seeded = false;
11074
+ /** Frames received before {@link Channel.seed}, replayed in arrival order on seed. */
11075
+ let buffer = [];
11076
+ /** Message handlers fanned out on every delivered frame. */
11077
+ const handlers = /* @__PURE__ */ new Set();
11078
+ /** Consecutive closes since the last clean open — drives the backoff delay. */
11079
+ let reconnectAttempts = 0;
11080
+ /** Pending reconnect timer handle, or undefined when none is scheduled. */
11081
+ let reconnectTimer;
11082
+ /** Keepalive interval handle, or undefined when off. */
11083
+ let keepAliveTimer;
11084
+ /**
11085
+ * Fan a message out to every handler now, or queue it when buffering before seed.
11086
+ *
11087
+ * @param message - The message to deliver or buffer.
11088
+ * @example
11089
+ * deliver(patch);
11090
+ */
11091
+ function deliver(message) {
11092
+ if (options.bufferUntilSeed && !seeded) {
11093
+ buffer.push(message);
11094
+ return;
11095
+ }
11096
+ for (const handler of handlers) handler(message);
11097
+ }
11098
+ /**
11099
+ * Parse + route an incoming frame, dropping the keepalive reply and unparseable frames.
11100
+ *
11101
+ * @param event - The socket message event.
11102
+ * @example
11103
+ * socket.addEventListener("message", dispatch);
11104
+ */
11105
+ function dispatch(event) {
11106
+ if (typeof event.data !== "string") return;
11107
+ if (options.keepAlive?.ignore !== void 0 && event.data === options.keepAlive.ignore) return;
11108
+ const message = parse(event.data);
11109
+ if (message !== void 0) deliver(message);
11110
+ }
11111
+ /**
11112
+ * Send the keepalive frame when the socket is open (no-op otherwise).
11113
+ *
11114
+ * @example
11115
+ * ping();
11116
+ */
11117
+ function ping() {
11118
+ if (options.keepAlive && socket?.readyState === WebSocket.OPEN) socket.send(options.keepAlive.send);
11119
+ }
11120
+ /**
11121
+ * Open the live socket for {@link desiredId}, wiring delivery + auto-reconnect. Shared by
11122
+ * the first connect and every backoff retry.
11123
+ *
11124
+ * @example
11125
+ * openSocket();
11126
+ */
11127
+ function openSocket() {
11128
+ const id = desiredId;
11129
+ if (id === void 0 || typeof WebSocket === "undefined") return;
11130
+ const ws = new WebSocket(options.url(id));
11131
+ socket = ws;
11132
+ ws.addEventListener("message", dispatch);
11133
+ ws.addEventListener("open", () => {
11134
+ reconnectAttempts = 0;
11135
+ });
11136
+ ws.addEventListener("close", () => {
11137
+ if (socket !== ws || desiredId === void 0) return;
11138
+ socket = void 0;
11139
+ if (reconnectCfg) scheduleReconnect();
11140
+ });
11141
+ }
11142
+ /**
11143
+ * Schedule a reconnect to {@link desiredId} with exponential backoff (capped), unless one
11144
+ * is already pending or reconnection is off.
11145
+ *
11146
+ * @example
11147
+ * scheduleReconnect();
11148
+ */
11149
+ function scheduleReconnect() {
11150
+ if (reconnectTimer !== void 0 || desiredId === void 0) return;
11151
+ const delay = Math.min(baseMs * 2 ** reconnectAttempts, maxMs);
11152
+ reconnectAttempts += 1;
11153
+ reconnectTimer = setTimeout(() => {
11154
+ reconnectTimer = void 0;
11155
+ if (desiredId !== void 0) openSocket();
11156
+ }, delay);
11157
+ }
11158
+ /**
11159
+ * Tear down the live socket + timers and re-arm the pre-seed buffer, keeping handlers so a
11160
+ * later connect resumes delivering to them. Clearing {@link desiredId} first marks the drop
11161
+ * intentional (no reconnect).
11162
+ *
11163
+ * @example
11164
+ * teardown();
11165
+ */
11166
+ function teardown() {
11167
+ desiredId = void 0;
11168
+ if (reconnectTimer !== void 0) {
11169
+ clearTimeout(reconnectTimer);
11170
+ reconnectTimer = void 0;
11171
+ }
11172
+ if (keepAliveTimer !== void 0) {
11173
+ clearInterval(keepAliveTimer);
11174
+ keepAliveTimer = void 0;
11175
+ }
11176
+ if (socket) {
11177
+ socket.removeEventListener("message", dispatch);
11178
+ socket.close();
11179
+ socket = void 0;
11180
+ }
11181
+ seeded = false;
11182
+ buffer = [];
11183
+ }
11184
+ /**
11185
+ * Open (or switch) the socket to `id`, re-arming the keepalive + pre-seed buffer.
11186
+ *
11187
+ * @param id - The channel id to connect to.
11188
+ * @example
11189
+ * connect("board-123");
11190
+ */
11191
+ function connect(id) {
11192
+ const alive = socket?.readyState === WebSocket.CONNECTING || socket?.readyState === WebSocket.OPEN;
11193
+ if (desiredId === id && alive) return;
11194
+ teardown();
11195
+ desiredId = id;
11196
+ seeded = false;
11197
+ buffer = [];
11198
+ reconnectAttempts = 0;
11199
+ openSocket();
11200
+ if (options.keepAlive) keepAliveTimer = setInterval(ping, keepEveryMs);
11201
+ }
11202
+ return {
11203
+ /**
11204
+ * Subscribe to `id`, connecting (or switching) the single active socket. Refcounted.
11205
+ *
11206
+ * @param id - The channel id to subscribe to.
11207
+ * @param handler - Called with each delivered message.
11208
+ * @returns A function that removes this handler (last out disconnects).
11209
+ * @example
11210
+ * const off = board.subscribe(boardId, (patch) => applyPatch(ctx, patch));
11211
+ */
11212
+ subscribe(id, handler) {
11213
+ handlers.add(handler);
11214
+ connect(id);
11215
+ return () => {
11216
+ handlers.delete(handler);
11217
+ if (handlers.size === 0) teardown();
11218
+ };
11219
+ },
11220
+ /**
11221
+ * Mark `id` seeded and flush the pre-seed buffer (no-op unless it is the active id).
11222
+ *
11223
+ * @param id - The channel id whose buffer to flush.
11224
+ * @example
11225
+ * board.seed(boardId);
11226
+ */
11227
+ seed(id) {
11228
+ if (id !== desiredId || seeded) return;
11229
+ seeded = true;
11230
+ const queued = buffer;
11231
+ buffer = [];
11232
+ for (const message of queued) for (const handler of handlers) handler(message);
11233
+ },
11234
+ /**
11235
+ * Deliver a message to the local handlers immediately (optimistic echo) for the active id.
11236
+ *
11237
+ * @param id - The channel id (ignored unless it is the active id).
11238
+ * @param message - The message to deliver locally now.
11239
+ * @example
11240
+ * board.deliverLocal(boardId, { type: "issue.moved", issueId });
11241
+ */
11242
+ deliverLocal(id, message) {
11243
+ if (id !== desiredId) return;
11244
+ for (const handler of handlers) handler(message);
11245
+ },
11246
+ /**
11247
+ * Send a raw text frame over the live socket (no-op when not open).
11248
+ *
11249
+ * @param raw - The frame to send.
11250
+ * @example
11251
+ * board.send("ping");
11252
+ */
11253
+ send(raw) {
11254
+ if (socket?.readyState === WebSocket.OPEN) socket.send(raw);
11255
+ },
11256
+ /**
11257
+ * The currently-subscribed channel id, or undefined when disconnected.
11258
+ *
11259
+ * @returns The active id, or undefined.
11260
+ * @example
11261
+ * board.current();
11262
+ */
11263
+ current() {
11264
+ return desiredId;
11265
+ },
11266
+ /**
11267
+ * Close the socket, cancel reconnect, and clear all handlers.
11268
+ *
11269
+ * @example
11270
+ * board.disconnect();
11271
+ */
11272
+ disconnect() {
11273
+ handlers.clear();
11274
+ teardown();
11275
+ }
11276
+ };
11277
+ }
11278
+ //#endregion
10847
11279
  //#region src/plugins/spa/lazy-embed.ts
10848
11280
  /**
10849
11281
  * @file `lazyEmbed` island — activates the static embed facades emitted by the
@@ -13257,6 +13689,7 @@ Object.defineProperty(exports, "cloudflareBindings", {
13257
13689
  });
13258
13690
  exports.contentPlugin = contentPlugin;
13259
13691
  exports.createApp = createApp;
13692
+ exports.createChannel = createChannel;
13260
13693
  exports.createIsland = createIsland;
13261
13694
  exports.createPlugin = createPlugin;
13262
13695
  exports.createUrls = createUrls;