Skip to content

Commit 39b11d8

Browse files
authored
fix(core): allow ctx.rpc.sharedState access during plugin setup (#195)
1 parent 602085d commit 39b11d8

File tree

3 files changed

+26
-17
lines changed

3 files changed

+26
-17
lines changed

packages/core/playground/vite.config.ts

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -147,22 +147,34 @@ export default defineConfig({
147147
initialValue: { count: 1 },
148148
})
149149

150-
setInterval(() => {
151-
counterState.mutate((current) => {
152-
current.count = (current.count + 1) % 5
153-
})
154-
const count = counterState.value().count
150+
counterState.on('updated', (newState) => {
155151
ctx.docks.update({
156152
id: 'counter',
157153
type: 'action',
158-
icon: `material-symbols:counter-${count}`,
159-
title: `Counter ${count}`,
160-
// TODO: HMR?
154+
icon: `material-symbols:counter-${newState.count}`,
155+
title: `Counter ${newState.count}`,
161156
action: ctx.utils.createSimpleClientScript(`() => {
162-
alert('Counter ${count}')
157+
alert('Counter ${newState.count}')
163158
}`),
164159
})
165-
}, 1000)
160+
})
161+
162+
// setInterval(() => {
163+
// counterState.mutate((current) => {
164+
// current.count = (current.count + 1) % 5
165+
// })
166+
// const count = counterState.value().count
167+
// ctx.docks.update({
168+
// id: 'counter',
169+
// type: 'action',
170+
// icon: `material-symbols:counter-${count}`,
171+
// title: `Counter ${count}`,
172+
// // TODO: HMR?
173+
// action: ctx.utils.createSimpleClientScript(`() => {
174+
// alert('Counter ${count}')
175+
// }`),
176+
// })
177+
// }, 1000)
166178
},
167179
},
168180
},

packages/core/src/node/__tests__/host-functions.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,12 +138,12 @@ describe('rpcFunctionsHost', () => {
138138
})).resolves.toBeUndefined()
139139
})
140140

141-
it('should throw in dev mode', async () => {
141+
it('should not throw in dev mode when rpc group is not yet set', async () => {
142142
const host = new RpcFunctionsHost({ mode: 'dev' } as DevToolsNodeContext)
143143
await expect(host.broadcast({
144144
method: 'devtoolskit:internal:terminals:updated',
145145
args: [],
146-
})).rejects.toThrow('RpcFunctionsHost] RpcGroup is not set')
146+
})).resolves.toBeUndefined()
147147
})
148148
})
149149

packages/core/src/node/host-functions.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,8 @@ export class RpcFunctionsHost extends RpcFunctionsCollectorBase<DevToolsRpcServe
4545
>(
4646
options: RpcBroadcastOptions<T, Args>,
4747
): Promise<void> {
48-
if (!this._rpcGroup) {
49-
if (this.context.mode === 'build')
50-
return
51-
throw new Error('RpcFunctionsHost] RpcGroup is not set, it likely to be an internal bug of Vite DevTools')
52-
}
48+
if (!this._rpcGroup)
49+
return
5350

5451
debugBroadcast(JSON.stringify(options.method))
5552

0 commit comments

Comments
 (0)