You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/kit/index.md
+85Lines changed: 85 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -19,6 +19,8 @@ DevTools Kit offers a complete toolkit for building DevTools integrations:
19
19
20
20
-**🔌 [Built-in RPC Layer](#remote-procedure-calls-rpc)**: Type-safe bidirectional communication between your Node.js server and browser clients, eliminating the need to set up WebSocket connections or message passing manually
21
21
22
+
-**🔗 [Shared State](#shared-state)**: Share data between server and client with automatic synchronization
23
+
22
24
-**🌐 Isomorphic Views Hosting**: Write your UI once and deploy it anywhere—as embedded floating panels, browser extension panels, standalone webpages, or even deployable SPAs for sharing build snapshots (work in progress).
23
25
24
26
## Why DevTools Kit?
@@ -418,6 +420,89 @@ rpc.client.register({
418
420
})
419
421
```
420
422
423
+
### Shared State
424
+
425
+
The DevTools Kit provides a built-in shared state system that enables you to share data between the server and client with automatic synchronization.
426
+
427
+
On the server side, you can get the shared state using `ctx.rpc.sharedState.get(name, options)`:
428
+
429
+
```ts {6-10}
430
+
exportdefaultfunction myPlugin():Plugin {
431
+
return {
432
+
name: 'my-plugin',
433
+
devtools: {
434
+
async setup(ctx) {
435
+
// Get the shared state
436
+
const state =awaitctx.rpc.sharedState.get('my-plugin:state', {
// Mutate the shared state, changes will be automatically synchronized to all the connected clients
448
+
state.mutate((state) => {
449
+
state.count+=1
450
+
})
451
+
}, 1000)
452
+
},
453
+
},
454
+
}
455
+
}
456
+
```
457
+
458
+
<details>
459
+
<summary>Type-safe shared state</summary>
460
+
461
+
The shared state is type-safe, you can get the state with the type of the initial value. To do so, you need to extend the `DevToolsRpcSharedStates` interface in your plugin's type definitions.
462
+
463
+
```ts [src/types.ts]
464
+
import'@vitejs/devtools-kit'
465
+
466
+
declaremodule'@vitejs/devtools-kit' {
467
+
interfaceDevToolsRpcSharedStates {
468
+
'my-plugin:state': { count:number, name:string }
469
+
}
470
+
}
471
+
```
472
+
473
+
</details>
474
+
475
+
On the client side, you can get the shared state using `client.rpc.sharedState.get(name)`:
// Now the `state` ref will be updated automatically when the shared state changes
504
+
```
505
+
421
506
## References
422
507
423
508
The docs might not cover all the details, please help us to improve it by submitting PRs. And in the meantime, you can refer to the following existing DevTools integrations for reference (but note they might not always be up to date with the latest API changes):
0 commit comments