[discuss] Per-view tRPC instances for DocumentDB webviews (remove ctx casts)
Nobody has claimed this yet.
Assessment
- Difficulty
- 5/5
- Estimated time
- Over a week
- Newbie friendliness
- 30/100
- Issue type
- Refactor
- Clarity
- Needs clarification
- Activity status
- Quiet
- Tech stack
- typescript
- Domain
- devtools
Research direction
Start with src/webviews/_integration/trpc.ts, then inspect the affected view routers and controllers that use the shared instance and context casts. Compare the per-view integration shape referenced from vscode-cosmosdb. This discussion is not ready for implementation until the instance strategy and actionContext modelling are decided; done would be a recorded decision and consistent treatment across the affected views.
Written by the indexing model from the issue text.
Description
Context
@microsoft/vscode-ext-webview supports either one shared tRPC instance for all webviews or one instance per webview (initWebviewTrpc<ViewContext>()). The DocumentDB extension currently uses a single shared instance bound to the framework BaseRouterContext:
// src/webviews/_integration/trpc.ts
const trpc = initWebviewTrpc<FrameworkBaseRouterContext>();
Because that one instance serves CollectionView, DocumentView, QueryInsights, IndexView, etc., two consumer-side patterns fall out of the design (surfaced during review of #795):
- Per-procedure narrowing cast — every procedure re-narrows
ctxto its view type:const myCtx = ctx as RouterContext; // ~30 sites Omiton the root context — controllers build the initial context, butactionContextis injected per-call by the telemetry runner, so the root object can't be a fullRouterContext:const trpcContext: Omit<RouterContext, 'actionContext'> = { ... };
Neither is a package limitation — both are how this extension wires the package. vscode-cosmosdb avoids the cast by building a separate instance per webview, each initTRPC.context<QueryEditorRouterContext>().
Proposal
Move DocumentDB to per-view tRPC instances — one initWebviewTrpc<ThisViewContext>() per webview — so ctx is already the precise per-view type inside procedures.
Pros
- Removes ~30
ctx as RouterContextcasts. Procedures readctx.sessionId,ctx.actionContext, etc. directly, fully typed. - Type safety instead of casts. A wrong field access becomes a compile error rather than being masked by the
ascast. - Matches the framework's intended usage and the
vscode-cosmosdbshape, making the two extensions' integration layers converge (easier shared learnings / future extraction). - Clearer per-view boundaries — each view owns its context type end to end; no giant shared
BaseRouterContextunion of concerns.
Cons
- More wiring / boilerplate. N instances instead of one: each needs its own
publicProcedure/router/createCallerFactory(and the telemetry middleware applied per instance). The framework note warns against hiding the.use()chain behind a generic helper (it collapses tRPC'sProcedureBuilderinference toany), so the wiring is repeated per instance by design. - Circular-import care. The current single-leaf
trpc.tsdeliberately breaks anappRouter -> per-view router -> appRoutercycle. Per-view instances need the same discipline replicated N times. - Does not remove the
Omit(or optional) on the root context.actionContextis still injected per call, so the controller-built root context still lacks it regardless of instance count. This refactor fixes the cast, not the injected-field modelling. - Churn for limited functional gain. It's an ergonomics/typing improvement, not a behavior change; touches every router + controller.
Decision needed
- Do we want per-view instances (remove casts) vs. keep the single instance (one cast pattern, less wiring)?
- Independently: how to model the injected
actionContexton the root context — keepOmit<RouterContext, 'actionContext'>, make it optional, or aWithActionContext<T>wrapper at read sites?
Follow-up to #795 (kept out of that PR to stay scoped to the package API change).
- Dominant language
- TypeScript
- Stars
- 31
- Forks
- 22
- Avg merge
- 2d 20h
- Merged PRs (30d)
- 21
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from microsoft/vscode-documentdb
-
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
microsoft/vscode-documentdb#885 ·
-
bug-bash
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
microsoft/vscode-documentdb#875 · 1 reaction ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 82/100
microsoft/vscode-documentdb#831 · 1 comment ·
-
documentation needs-triage P3
Difficulty 2/5 1-3 hours Newbie friendliness 70/100
microsoft/vscode-documentdb#643 ·
-
user feedback
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
microsoft/vscode-documentdb#535 · 1 comment · 2 reactions ·
All issues in microsoft/vscode-documentdb
Similar issues
-
Difficulty 2/5 1-3 hours Newbie friendliness 84/100
Eynzof/Hermes-CN-Desktop#610 ·
-
bug clawsweeper:linked-pr-open clawsweeper:needs-live-repro clawsweeper:no-new-fix-pr impact:message-loss issue-rating: 🐚 platinum hermit P2 regression
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
-
enhancement
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
-
calcite-components needs triage refactor
Difficulty 2/5 1-3 hours Newbie friendliness 75/100
Esri/calcite-design-system#15203 ·
-
Difficulty 1/5 Under an hour Newbie friendliness 78/100
fullcalendar/fullcalendar#8106 ·