Reduce duplication in Tasks API layer#805
Open
EhabY wants to merge 2 commits intorefactor-tasks-webviewfrom
Open
Reduce duplication in Tasks API layer#805EhabY wants to merge 2 commits intorefactor-tasks-webviewfrom
EhabY wants to merge 2 commits intorefactor-tasks-webviewfrom
Conversation
Replace per-definition requestHandler/commandHandler wrappers with buildRequestHandlers and buildCommandHandlers that enforce compile-time completeness over the full API surface. - Add kind discriminant to RequestDef, CommandDef, and NotificationDef interfaces and their factory functions - Add RequestHandlerMap and CommandHandlerMap mapped types that require a handler for every definition of the corresponding kind - Add buildRequestHandlers and buildCommandHandlers builder functions with typed overloads for compile-time completeness checking - Replace manual method-keyed handler maps in tasksPanelProvider with builder calls using property-name keys - Replace raw sendNotification calls with a typed notify helper
Add buildApiHook and ApiHook<Api> to protocol.ts so that useTasksApi can be derived from the TasksApi definition object with zero manual sync. - Add ApiHook<Api> mapped type that derives request, command, and notification subscription signatures from an API definition - Add buildApiHook() that iterates API entries by kind at runtime, dispatching to ipc.request/command/onNotification - Replace ~60 lines of manual method listings in useTasksApi with a single buildApiHook(TasksApi, useIpc()) call - Update 5 consumer call sites from positional args to params objects - Inline intermediate const declarations in api.ts into TasksApi object - Remove unused ParamsOf and ResponseOf utility types
DanielleMaywood
approved these changes
Feb 20, 2026
| type: TasksApi.tasksUpdated.method, | ||
| data: tasks, | ||
| }); | ||
| this.notify(TasksApi.tasksUpdated, [...tasks]); |
Collaborator
There was a problem hiding this comment.
How come we're performing [...tasks] here?
If it is because tasks is readonly Task[] and tasksUpdated only accepts Task[], could we investigate having tasksUpdated accept readonly Task[]?
| ) => () => void; | ||
| }, | ||
| ): ApiHook<Api>; | ||
| export function buildApiHook( |
Collaborator
There was a problem hiding this comment.
non-blocking: thoughts on a unit test for this function? If it is sufficiently tested indirectly then no worries.
mtojek
approved these changes
Feb 20, 2026
| export function buildApiHook( | ||
| api: Record<string, { kind: string; method: string }>, | ||
| ipc: { | ||
| request: (def: { method: string }, params: unknown) => Promise<unknown>; |
| readonly kind: "request"; | ||
| readonly method: string; | ||
| /** @internal Phantom types for inference - not present at runtime */ | ||
| readonly _types?: { params: P; response: R }; |
Member
There was a problem hiding this comment.
with never?
readonly _types?: never & { params: P; response: R };
| type: TasksApi.tasksUpdated.method, | ||
| data: tasks, | ||
| }); | ||
| this.notify(TasksApi.tasksUpdated, [...tasks]); |
| private readonly requestHandlers = buildRequestHandlers(TasksApi, { | ||
| getTasks: () => this.fetchTasks(), | ||
| getTemplates: () => this.fetchTemplates(), | ||
| getTask: (p) => this.client.getTask("me", p.taskId), |
Member
There was a problem hiding this comment.
is there a risk that client will not be initialized at the moment?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
requestHandler/commandHandlerwrappers withbuildRequestHandlersandbuildCommandHandlersmapped-type builders that enforce compile-time completeness over the full API surfacebuildApiHookandApiHook<Api>so thatuseTasksApiis auto-generated from theTasksApidefinition object at runtime, eliminating ~60 lines of manual method listings that had to stay in synckinddiscriminant toRequestDef,CommandDef, andNotificationDefso builders can dispatch by kind at runtimeapi.tsdirectly into theTasksApiobject and remove unusedParamsOf/ResponseOfutility typessendNotificationcalls intasksPanelProviderwith a typednotifyhelper