@krate/runtime 0.5.0-beta.3 → 0.5.0-rc.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.
@@ -140,6 +140,24 @@ describe('hydration bootstrap', () => {
140
140
  await tick();
141
141
  expect(tbody.textContent).toBe('off7');
142
142
  });
143
+ it('renders bigint, and primitives inside arrays, as text in a content slot', async () => {
144
+ document.body.innerHTML = '<div id="root"><p><!--k:v-->0<!--/k:v--></p></div>';
145
+ new Function(loadBootstrap())();
146
+ const w = window;
147
+ const [val, setVal] = createSignal(5n / 2n);
148
+ w.kbindContent('v', () => val());
149
+ const p = document.querySelector('p');
150
+ expect(w.__krateErrs).toEqual([]);
151
+ await tick();
152
+ expect(p.textContent).toBe('2');
153
+ setVal(['a', 3, 7n, null, false]);
154
+ await tick();
155
+ expect(w.__krateErrs).toEqual([]);
156
+ expect(p.textContent).toBe('a37');
157
+ setVal(12345678901234567890n);
158
+ await tick();
159
+ expect(p.textContent).toBe('12345678901234567890');
160
+ });
143
161
  it('finds nodes of a branch swapped out before its binding ran', async () => {
144
162
  document.body.innerHTML =
145
163
  '<div id="root"><p><!--k:c--><button data-k="k:b">ssr</button><!--/k:c--></p>' +
package/dist/suspense.js CHANGED
@@ -23,7 +23,7 @@
23
23
  * content keeps showing it when a transition update suspends it, and the
24
24
  * transition stays pending until that work and any async action settle.
25
25
  */
26
- import { createSignal, createEffect, onCleanup, krateGetOwner, krateSetOwner } from './signal.js';
26
+ import { createSignal, createEffect, onCleanup, krateGetOwner, krateSetOwner, krateOnDispose, krateCurrentScope } from './signal.js';
27
27
  const tracked = new WeakSet();
28
28
  export function isThenable(value) {
29
29
  return (value != null &&
@@ -369,6 +369,9 @@ class BoundaryController {
369
369
  this.pending = new Set();
370
370
  this.holds = 0;
371
371
  this.scheduled = false;
372
+ /** Route scope the boundary belongs to, and whether its page is gone. */
373
+ this.route = krateCurrentScope();
374
+ this.dead = false;
372
375
  this.throttle = null;
373
376
  this.id = id;
374
377
  this.anchor = anchor;
@@ -420,6 +423,10 @@ class BoundaryController {
420
423
  });
421
424
  }
422
425
  update() {
426
+ // A boundary of a page that was navigated away from must not touch the DOM
427
+ // or the slot state of the page that replaced it.
428
+ if (this.dead)
429
+ return;
423
430
  const want = this.pending.size === 0 && this.holds === 0;
424
431
  if (want === this.content)
425
432
  return;
@@ -609,6 +616,23 @@ export function registerBoundary(boundary) {
609
616
  export function resetBoundaries() {
610
617
  boundaries.clear();
611
618
  }
619
+ // Suspended component runs and compiled boundaries are retried when their
620
+ // promise settles, which can be after the user left the page: a retry would
621
+ // mount the effects of a page that is gone. A page teardown (soft navigation)
622
+ // marks the ones it removed dead.
623
+ const liveRuns = new Set();
624
+ krateOnDispose((gone) => {
625
+ for (const run of liveRuns) {
626
+ if (gone === null || gone(run.route))
627
+ run.dead = true;
628
+ }
629
+ for (const [id, b] of Array.from(boundaries)) {
630
+ if (b instanceof BoundaryController && (gone === null || gone(b.route))) {
631
+ b.dead = true;
632
+ boundaries.delete(id);
633
+ }
634
+ }
635
+ });
612
636
  /**
613
637
  * Suspends `boundary` on `t`. Inside a transition an already revealed
614
638
  * boundary keeps its content and the transition waits instead; with no
@@ -634,10 +658,15 @@ export function currentBoundary() {
634
658
  */
635
659
  export function __krateBoundary(id, fn) {
636
660
  const boundary = id ? getBoundary(id) : null;
637
- const state = { boundary, scope: { list: [], i: 0 }, disposers: [] };
661
+ const state = { boundary, scope: { list: [], i: 0 }, disposers: [], route: krateCurrentScope(), dead: false };
638
662
  const errOwner = errorOwner();
639
663
  boundary?.hold();
640
664
  const attempt = () => {
665
+ // The page was left while the promise was pending: do not mount it now.
666
+ if (state.dead) {
667
+ liveRuns.delete(state);
668
+ return;
669
+ }
641
670
  state.scope.i = 0;
642
671
  const prev = currentRun;
643
672
  currentRun = state;
@@ -649,6 +678,7 @@ export function __krateBoundary(id, fn) {
649
678
  withReadScope(state.scope, fn);
650
679
  currentRun = prev;
651
680
  krateSetOwner(owner);
681
+ liveRuns.delete(state);
652
682
  boundary?.release();
653
683
  }
654
684
  catch (error) {
@@ -657,9 +687,11 @@ export function __krateBoundary(id, fn) {
657
687
  for (const dispose of state.disposers.splice(0))
658
688
  dispose();
659
689
  if (isSuspenseSignal(error)) {
690
+ liveRuns.add(state);
660
691
  error.thenable.then(attempt, attempt);
661
692
  return;
662
693
  }
694
+ liveRuns.delete(state);
663
695
  boundary?.release();
664
696
  reportSuspenseError(error, boundary, errOwner);
665
697
  }
@@ -1,5 +1,5 @@
1
1
  import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
2
- import { createSignal, createEffect } from './signal.js';
2
+ import { createSignal, createEffect, disposeAll, krateDisposeExcept, krateEnterScope, krateLeaveScope } from './signal.js';
3
3
  import { createContext } from './context.js';
4
4
  import { createResource } from './resource.js';
5
5
  import { __krateBoundary, __krateLazy, __krateLazyPayload, __krateRead, __krateTransitionStarter, __krateUse, createBoundary, createDeferredValue, createExternalStore, getBoundary, isInTransition, readValue, registerBoundary, resetBoundaries, runAsyncTransition, setFallbackThrottle, setLazyChunkImporter, setSuspenseErrorHandler, startTransition, trackThenable, isSuspenseSignal, } from './suspense.js';
@@ -516,3 +516,41 @@ function installFindSlot(html) {
516
516
  return null;
517
517
  };
518
518
  }
519
+ describe('a page left while a boundary is suspended', () => {
520
+ it('does not retry the suspended component when its promise settles', async () => {
521
+ const { b, shown } = testBoundary('gone', false);
522
+ const d = deferred();
523
+ let runs = 0;
524
+ let effects = 0;
525
+ runIn(b, () => {
526
+ runs++;
527
+ __krateUse(() => __krateRead(d.promise));
528
+ createEffect(() => void effects++);
529
+ });
530
+ expect(runs).toBe(1);
531
+ disposeAll();
532
+ d.resolve('late');
533
+ await flush();
534
+ // Neither the component body (with its effects) nor the reveal ran.
535
+ expect(runs).toBe(1);
536
+ expect(effects).toBe(0);
537
+ expect(shown).toEqual([]);
538
+ });
539
+ it('still retries after a soft navigation that kept the layout owning the run', async () => {
540
+ krateEnterScope('_lkeep_');
541
+ const { b, shown } = testBoundary('kept', false);
542
+ const d = deferred();
543
+ let runs = 0;
544
+ runIn(b, () => {
545
+ runs++;
546
+ __krateUse(() => __krateRead(d.promise));
547
+ });
548
+ krateLeaveScope();
549
+ krateDisposeExcept(['_lkeep_']);
550
+ d.resolve('ok');
551
+ await flush();
552
+ expect(runs).toBe(2);
553
+ expect(shown).toEqual(['content']);
554
+ disposeAll();
555
+ });
556
+ });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@krate/runtime",
3
- "version": "0.5.0-beta.3",
3
+ "version": "0.5.0-rc.1",
4
4
  "description": "Client-side runtime for Krate - signals, hydration, SPA router, resources, and context.",
5
5
  "license": "Apache-2.0",
6
6
  "type": "module",