refactor(rpc): use RPC for all client↔server communication + transport abstraction#4
Open
yann-copilot wants to merge 1 commit intostorybookjs:mainfrom
Open
Conversation
…cation - Add HighlighterTransport interface in src/client/transport.ts Decouples overlay/listener logic from the transport mechanism. Allows the highlighter to be used outside Vite DevTools (browser extension, standalone overlay, custom tooling) by providing a different transport implementation. - Replace raw import.meta.hot.on with ctx.rpc.client.register 'component-highlighter:story-created' is now a typed client RPC function instead of a raw HMR custom event. - Replace server.ws.send with ctx.rpc.broadcast Server notifies all connected DevTools clients via the RPC layer instead of bypassing it with a raw WebSocket message. - Export HighlighterTransport, CreateStoryPayload, StoryCreatedPayload from the package root for external integrations. The overlay and listeners remain transport-agnostic (mitt/DOM events only); vite-devtools.ts wires the concrete ViteDevTools-backed transport, and other contexts can plug in their own adapter.
commit: |
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.
What I did
The plugin previously had a mixed communication pattern:
create-story): correctly usedctx.rpc.call/defineRpcFunctionstory-created): was using rawserver.ws.send+import.meta.hot.on— bypassing the RPC layer entirelyThis PR fixes the inconsistency and adds a transport abstraction for future extractability.
Implementation details
HighlighterTransport interface (
src/client/transport.ts)A minimal interface that decouples the overlay/listeners from the communication mechanism:
The overlay and listeners stay pure (mitt/DOM events only). Other contexts (browser extension, standalone overlay, custom tooling) can provide their own transport adapter.
client→server: unchanged, just routed through transport
ctx.rpc.call('component-highlighter:create-story', data)server→client: now properly RPC
Before:
server.ws.send({ type: 'custom', event: '...', data })+import.meta.hot.on(...)After:
devtoolsCtx.rpc.broadcast('component-highlighter:story-created', data)ctx.rpc.client.register(defineRpcFunction({ name: 'component-highlighter:story-created', ... }))DevToolsRpcClientFunctions declaration
Added
component-highlighter:story-createdto the module augmentation so the type system knows about this client function.Exports
HighlighterTransport,CreateStoryPayload,StoryCreatedPayloadexported from package root for external integrations.Checklist
@vitejs/devtools-kitRPC consistentlycodegen/andprovider-analyzernot introduced by this PRAI disclosure