Skip to content

Quick Start

Get a Browser Lifecycle session running in under five minutes.

Full tutorial

For the complete step-by-step path with playground links, see the Tutorial.

1. Install

bash
pnpm add @jayoncode/browser-lifecycle

2. Create a session

ts
import { createBrowserLifecycle } from "@jayoncode/browser-lifecycle";

const lifecycle = createBrowserLifecycle({ autoStart: true });

Expected output: No console output yet. The session starts in the running phase.

3. React to visibility

ts
lifecycle.on("page:visible", () => {
  console.log("visible");
});

lifecycle.on("page:hidden", () => {
  console.log("hidden");
});

Switch browser tabs to trigger events. Try the Visibility Playground to inspect ordering and metadata.

4. Read snapshot state

ts
const snapshot = lifecycle.getSnapshot();
console.log(snapshot.page.visibility);

5. Clean up

ts
await lifecycle.dispose();

Common mistakes

MistakeFix
Creating multiple sessions per tabUse one lifecycle instance per tab
Forgetting dispose() on unmountAlways dispose in framework cleanup hooks
Subscribing in SSRInitialize only in browser code paths
Passing resolved config back into merge helpersUse input-shaped configuration objects

Next steps

JOC is a JayOnCode monorepo of @jayoncode/* packages. Docs sync from source via TypeDoc and sync scripts.