kea-disposables
CLAUDE.md0 starsChanged 3 months agoMITView on GitHub
kea-disposables — a Kea plugin that gives every mounted logic a cache.disposables manager for useEffect-style resource cleanup, plus automatic pause/resume of that work while the browser tab is hidden. Extracted from frontend/src/kea-disposables.ts in PostHog/posthog, which remains its largest consumer. - One DisposablesManager per logic, stored on logic.cache.disposables in the plugin's afterMount, torn down in beforeUnmount — but only when logic.isMounted() has gone false, because kea reference-counts mounts. - Every manager is also registered in a module-level globalVisibilityState.allManagers set. A single…
The file
We split this CLAUDE.md into the rules it gives an agent
2 rules. About 0 are common, 1 is distinctive, and 1 is very specific to this project.
≈0
Common · estimate
Many files like this have a rule of this kind.
≈1
Distinctive · estimate
This author's own choices. Worth a look.
1
Very specific
Tied to their project's names, files or tools. Probably skip.
- Common
- Distinctive
- Very specific
- Very specific: line 28
Every manager is also registered in a module-level globalVisibilityState.allManagers set.
- Distinctive: line 75
No runtime dependencies, and it should stay that way.
≈ “Common” is an estimate: at least 1 in 5 of the 26,980 CLAUDE.md files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.
Files
CLAUDE.md
kea-disposables — a Kea plugin that gives every mounted logic a
cache.disposables manager for useEffect-style resource cleanup, plus automatic pause/resume of
that work while the browser tab is hidden.
Extracted from frontend/src/kea-disposables.ts in
PostHog/posthog, which remains its largest consumer.
Layout
src/index.ts the entire plugin — one file, no internal modules
tests/ vitest suite (jsdom by default)
helpers.ts context reset, document.hidden override, counters
disposables.test.ts unit behaviour
integration.test.ts a real polling logic driven with fake timers
builder.test.ts the disposables() builder + context-teardown behaviour
no-dom.test.ts `@vitest-environment node` — pins SSR safety
scripts/smoke.mjs loads dist/ (ESM + CJS) in plain node after a buildMental model
- One
DisposablesManagerper logic, stored onlogic.cache.disposablesin the plugin'safterMount, torn down inbeforeUnmount— but only whenlogic.isMounted()has gone false, because kea reference-counts mounts. - Every manager is also registered in a module-level
globalVisibilityState.allManagersset. A singlevisibilitychangelistener ondocumentdrives pause/resume across all of them, attached on the first manager and detached when the set empties. - Pausing runs each entry's cleanup; resuming re-runs its
setup. Setup functions must be safe to run more than once — that is the core contract this plugin imposes on callers. disposables()is a thin logic builder overadd, composed onto kea's coreafterMountbuilder. That ordering is load-bearing and verified by test: the plugin'safterMountevent runs before the coreafterMountbuilder callbacks, so the manager is already on the cache.
Invariants worth not breaking
Each of these has a test; if you change the behaviour, change the test deliberately rather than adjusting it to match.
addwhile hidden defers setup. It registers the entry with a no-op cleanup and lets the next resume run setup. Without this, async work that re-arms its own timer in afinallystarts a live timer on a background tab and defeats the whole pause. (tests/integration.test.ts)addanddisposeare no-ops after unmount, and the manager is never nulled off the cache. Continuations that outlive the logic call them unconditionally; a null there would throw aTypeErrorout of afinallyinstead of doing nothing.isDisposedis set before the cleanups run, so anything a cleanup wakes up sees an inert manager.- A remount replaces a disposed manager. A logic whose built instance is retained keeps its
cache, so leaving the disposed manager in place would make every subsequent
adda silent no-op. pauseOnPageHidden: falseentries are skipped by both pause and resume. Resuming one would leave two live listeners.- Setup/cleanup errors are caught and logged, never rethrown, and a setup that fails on resume gets a no-op cleanup so the stale one can't run.
beforeCloseContextdisposes everything.resetContext()drops logics from the store without unmounting them, sobeforeUnmountnever fires; without this hook every timer and listener alive at reset time leaks for the rest of the page's life.
Conventions
- npm, not pnpm:
changeset publishpicks its registry-query tool from the repo's package manager, and the pnpm path crashed in CI. Keeppackage-lock.jsonas the only lockfile. - oxlint + oxfmt (
npm run lint/npm run format). Single quotes, no semicolons, 2-space indent — oxfmt is opinionated about the indent and does not take a width option. - The peer range is
kea >= 3, matching every first-party kea plugin; CI runs the suite against kea 3.0 (the floor), 3 stable, and the v4nextprerelease. Don't use kea 4-only APIs. - Plugin
namemust be unique across activated plugins — kea throws on a duplicate. We claimdisposables. - There is no supported way to type
logic.cache.disposablesglobally: kea declarescacheasRecord<string, any>, which an interface augmentation cannot narrow.DisposablesCacheandgetDisposables()are the workarounds; no first-party kea plugin does better. - The package is dual ESM + CJS with types for both.
npx --no-install publintandnpx --no-install attw --pack .must stay clean; CI enforces both. - No runtime dependencies, and it should stay that way.
- Nothing may touch
documentunguarded — usehasDocument()/isPageHidden().
Releasing
Changesets. npx changeset with every user-facing change; merging the generated "version
packages" PR publishes to npm via OIDC. See CONTRIBUTING.md.
Does it work?
Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.
No reviews yet
People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.
- It worked
- Partly
- It didn't
Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.
Your trail
Files you open will collect here, so you can find them again.
Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.
Now do this for your own.
Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.

