@vitejs/devtools-kit 0.1.10 → 0.1.11

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@vitejs/devtools-kit",
3
3
  "type": "module",
4
- "version": "0.1.10",
4
+ "version": "0.1.11",
5
5
  "description": "Vite DevTools Kit",
6
6
  "author": "VoidZero Inc.",
7
7
  "license": "MIT",
@@ -40,7 +40,7 @@
40
40
  "dependencies": {
41
41
  "birpc": "^4.0.0",
42
42
  "ohash": "^2.0.11",
43
- "@vitejs/devtools-rpc": "0.1.10"
43
+ "@vitejs/devtools-rpc": "0.1.11"
44
44
  },
45
45
  "devDependencies": {
46
46
  "human-id": "^4.1.3",
@@ -85,6 +85,57 @@ defineRpcFunction({
85
85
  })
86
86
  ```
87
87
 
88
+ ## Sharing State Across RPC Functions
89
+
90
+ When multiple RPC functions need shared plugin state (manager instances, options, cached data), use a `WeakMap<DevToolsNodeContext, T>` with get/set helpers instead of mutating the context object:
91
+
92
+ ```ts
93
+ // src/node/rpc/context.ts
94
+ import type { DevToolsNodeContext } from '@vitejs/devtools-kit'
95
+
96
+ interface MyPluginContext {
97
+ dataDir: string
98
+ manager: DataManager
99
+ }
100
+
101
+ const pluginContext = new WeakMap<DevToolsNodeContext, MyPluginContext>()
102
+
103
+ export function getPluginContext(ctx: DevToolsNodeContext): MyPluginContext {
104
+ const value = pluginContext.get(ctx)
105
+ if (!value)
106
+ throw new Error('Plugin context not initialized')
107
+ return value
108
+ }
109
+
110
+ export function setPluginContext(ctx: DevToolsNodeContext, value: MyPluginContext) {
111
+ pluginContext.set(ctx, value)
112
+ }
113
+ ```
114
+
115
+ Initialize in plugin setup, access in RPC functions:
116
+
117
+ ```ts
118
+ // Plugin setup
119
+ devtools: {
120
+ setup(ctx) {
121
+ setPluginContext(ctx, { dataDir: resolve(ctx.cwd, 'data'), manager: new DataManager() })
122
+ rpcFunctions.forEach(fn => ctx.rpc.register(fn))
123
+ },
124
+ }
125
+
126
+ // RPC function
127
+ const getData = defineRpcFunction({
128
+ name: 'my-plugin:get-data',
129
+ type: 'query',
130
+ setup: (ctx) => {
131
+ const { manager } = getPluginContext(ctx)
132
+ return {
133
+ handler: async () => manager.getData(),
134
+ }
135
+ },
136
+ })
137
+ ```
138
+
88
139
  ## Broadcasting Patterns
89
140
 
90
141
  ### Basic Broadcast