2 Swap Lifecycle
Nate edited this page 2026-11-21 20:21:39 -08:00

The swap lifecycle (the part that bites)

Navigation does not reload the page. swap.js fires, in order:

  1. evesynapse:content-will-swap on document: teardown time.
  2. #main-content is replaced.
  3. evesynapse:content-ready on document, with detail.root set to the new fragment.

Rules that follow from this:

  • Widgets subscribe, they don't run at parse time. Use EveSynapseContent.onReady(fn); fn(root) runs once for the first paint and again for every swap, with the fresh fragment as root. Initialize only what's inside root.
  • Initializers must be idempotent and root-scoped. Running twice over the same markup must be harmless; query inside root, never the whole document, for widget elements.
  • Element bindings are safe; global ones are on you. Listeners on elements inside the fragment die with it. Anything bound to document/window, and every timer, must be guarded against double-binding or torn down on content-will-swap (the countdown ticker and the page-sync loop are the worked examples in app.js).
  • Shell code binds once and never re-runs: if it needs to react to new content (the modal closer, the character-selector pruner), it listens for the lifecycle events itself.
  • A dialog open inside content being swapped out closes with it.
  • The swapper never strands a page without its assets. A swap adopts the incoming page's stylesheet and any page scripts the shell hasn't loaded (in document order, against the swapped-in content, before content-ready), and prunes the stylesheet again when a later page doesn't carry it. Only an asset that fails to load falls back to a full navigation: plus the pages whose JS holds state (the fitting simulator and saved fits always load whole regardless). That's what makes per-surface files safe: gate the file in base.html and the swap brings it along.