Skip to content

feat(agera): export createScope to stop a deferred scope whose body threw - #309

Merged
dangreen merged 1 commit into
mainfrom
feat/agera-create-scope
Oct 25, 2026
Merged

dangreen merged 1 commit into
mainfrom
feat/agera-create-scope

Conversation

@dangreen

Copy link
Copy Markdown
Member

Why

deferScope hands its scope back only when the body returns. A body that throws loses the handle, and what it created before the throw stays linked to the stores it read, with nothing left to stop it. The upcoming @nanoviews/server renders a whole app in a deferred scope and stops it as soon as the markup is written, so a failed request would leave its computeds and effects subscribed to every store that outlives the request. Today it catches the error inside the body to get the handle back, stops the scope and throws the error again.

With createScope the render runs in a scope created under one made beforehand, and stopping that one stops the render either way:

const scope = createScope()

try {
  deferScope(render, scope)
} finally {
  stopScope(scope)
}

What

  • agera exports createScope(parent?), and kida and @nano_kit/store re-export it. It is the function deferScope and boundDeferScope already make their scopes with, so a bundle that uses either gets no new code.
  • It takes and returns a DeferredScope, the handle the other scope functions work with. The parent of deferScope is a DeferredScope now instead of a ReactiveNode: nothing in this repo or in nanoviews passes one, and the scope functions are @internal.
  • Two specs: a scope whose body threw is stopped with the scope it was created under, and a scope from createScope is stopped with its parent.

Size

Entry Before After Limit
agera All publics (Gzip) 3423 B 3430 B 3.45 kB
agera All publics (Brotli) 3157 B 3169 B 3.2 kB
kida All publics (Gzip) 5414 B 5418 B 5.45 kB
kida All publics (Brotli) 5020 B 5022 B 5.05 kB
@nano_kit/store All publics (Gzip) 6846 B 6849 B 6.85 kB
@nano_kit/store All publics (Brotli) 6298 B 6346 B 6.3 → 6.35 kB

Only the name of the export is new. The Brotli size of @nano_kit/store moves by 48 B where its Gzip size moves by 3 B, which is compression noise, but it no longer fits the old limit. Signal, Minimal set and Popular set are unchanged in every package.

Verification

oxlint, tsc --noEmit and vitest are clean in agera. tsc --noEmit passes in kida, @nano_kit/store, @nano_kit/query and @nano_kit/devtools, and size-limit passes in agera, kida and @nano_kit/store.

… threw

`deferScope` hands its scope back only when the body returns, so a body that throws leaves what it created linked to the stores it read. A scope from `createScope`, given to `deferScope` as the parent, stops it either way. The `parent` of `deferScope` is a scope handle now, as `createScope` returns.
@codecov

codecov Bot commented Oct 25, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 83.60%. Comparing base (aa15c29) to head (5101768).

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #309      +/-   ##
==========================================
- Coverage   83.62%   83.60%   -0.03%     
==========================================
  Files         228      228              
  Lines        4849     4849              
  Branches     1074     1074              
==========================================
- Hits         4055     4054       -1     
  Misses        553      553              
- Partials      241      242       +1     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@dangreen
dangreen merged commit 6402337 into main Oct 25, 2026
11 checks passed
@dangreen
dangreen deleted the feat/agera-create-scope branch October 25, 2026 18:55
@github-actions github-actions Bot mentioned this pull request Oct 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant