Skip to content

refactor(rpc): use RPC for all client↔server communication + transport abstraction#4

Open
yann-copilot wants to merge 1 commit intostorybookjs:mainfrom
yann-copilot:refactor/rpc-transport-abstraction
Open

refactor(rpc): use RPC for all client↔server communication + transport abstraction#4
yann-copilot wants to merge 1 commit intostorybookjs:mainfrom
yann-copilot:refactor/rpc-transport-abstraction

Conversation

@yann-copilot
Copy link
Contributor

What I did

The plugin previously had a mixed communication pattern:

  • ✅ client→server (create-story): correctly used ctx.rpc.call / defineRpcFunction
  • ❌ server→client (story-created): was using raw server.ws.send + import.meta.hot.on — bypassing the RPC layer entirely

This 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:

interface HighlighterTransport {
  createStory(data: CreateStoryPayload): Promise<void>
  onStoryCreated(handler: (data: StoryCreatedPayload) => void): () => void
}

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:

  • Server: devtoolsCtx.rpc.broadcast('component-highlighter:story-created', data)
  • Client: ctx.rpc.client.register(defineRpcFunction({ name: 'component-highlighter:story-created', ... }))

DevToolsRpcClientFunctions declaration

Added component-highlighter:story-created to the module augmentation so the type system knows about this client function.

Exports

HighlighterTransport, CreateStoryPayload, StoryCreatedPayload exported from package root for external integrations.

Checklist

  • TypeScript compiles without errors in changed files
  • Both communication directions use @vitejs/devtools-kit RPC consistently
  • Transport interface is framework/context agnostic
  • Pre-existing type errors in codegen/ and provider-analyzer not introduced by this PR

AI disclosure

  • Created by: OpenClaw agent (Alfred)
  • Model: github-copilot/claude-sonnet-4.6

…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.
@pkg-pr-new
Copy link

pkg-pr-new bot commented Mar 3, 2026

Open in StackBlitz

npm i https://pkg.pr.new/yannbf/vite-plugin-experimental-storybook-devtools@4

commit: fd70cd9

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