Skip to content

Commit 3575a68

Browse files
committed
docs(devframe): document rpc/ folder + WeakMap context pattern
Skill and rpc guide now describe the one-file-per-RPC layout under src/rpc/ and the WeakMap-based src/context.ts for sharing setup-time state across files. All three examples adopt the pattern, with streaming-chat demonstrating the WeakMap context for its shared channel + history.
1 parent 45db91f commit 3575a68

25 files changed

Lines changed: 511 additions & 339 deletions

File tree

docs/guide/rpc.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ export default defineDevframe({
5353
})
5454
```
5555

56+
Place each function in its own file under `src/rpc/`, and barrel them in `src/rpc/index.ts` as `const serverFunctions = [...] as const`. The same array feeds the [type-safe client registry](#type-safe-client-registry) and keeps registration order explicit. When per-file functions need to share setup-time state (channels, shared state handles, loaders), expose it through a `WeakMap<DevToolsNodeContext, T>` in a sibling `src/context.ts`.
57+
5658
### Naming convention
5759

5860
Scope with your devframe id and use kebab-case for the action: `my-devframe:get-modules`, `my-devframe:read-file`, `my-devframe:trigger-rebuild`.

examples/files-inspector/src/client/routes/about.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ export function About({ rpc, basePath }: { rpc: DevToolsRpcClient, basePath: str
55
const [cwd, setCwd] = useState<string>('')
66

77
useEffect(() => {
8-
rpc.call('devframe-files-inspector:get-cwd' as any).then((r: any) => {
8+
rpc.call('devframe-files-inspector:get-cwd').then((r) => {
99
setCwd(r.cwd)
1010
})
1111
}, [rpc])

examples/files-inspector/src/client/routes/home.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ export function Home({ rpc }: { rpc: DevToolsRpcClient }) {
88
async function refresh() {
99
setLoading(true)
1010
try {
11-
const result = await rpc.call('devframe-files-inspector:list-files' as any) as string[]
11+
const result = await rpc.call('devframe-files-inspector:list-files')
1212
setFiles(result)
1313
}
1414
finally {
Lines changed: 4 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
1-
import process from 'node:process'
21
import { fileURLToPath } from 'node:url'
3-
import { defineRpcFunction } from 'devframe'
42
import { defineDevframe } from 'devframe/types'
5-
import { glob } from 'tinyglobby'
3+
import { serverFunctions } from './rpc'
64

75
const BASE_PATH = '/__devframe-files-inspector/'
86
const distDir = fileURLToPath(new URL('../dist/client', import.meta.url))
@@ -18,25 +16,8 @@ export default defineDevframe({
1816
distDir,
1917
},
2018
spa: { loader: 'none' },
21-
async setup(ctx) {
22-
const targetCwd = process.env.DEVFRAME_E2E_CWD || ctx.cwd
23-
24-
ctx.rpc.register(defineRpcFunction({
25-
name: 'devframe-files-inspector:get-cwd',
26-
type: 'static',
27-
jsonSerializable: true,
28-
handler: () => ({ cwd: targetCwd }),
29-
}))
30-
31-
ctx.rpc.register(defineRpcFunction({
32-
name: 'devframe-files-inspector:list-files',
33-
type: 'query',
34-
jsonSerializable: true,
35-
handler: async () => {
36-
const files = await glob(['*'], { cwd: targetCwd, onlyFiles: true, dot: false })
37-
return files.map(f => f.replace(/\\/g, '/')).sort()
38-
},
39-
snapshot: true,
40-
}))
19+
setup(ctx) {
20+
for (const fn of serverFunctions)
21+
ctx.rpc.register(fn)
4122
},
4223
})
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import process from 'node:process'
2+
import { defineRpcFunction } from 'devframe'
3+
4+
export const getCwd = defineRpcFunction({
5+
name: 'devframe-files-inspector:get-cwd',
6+
type: 'static',
7+
jsonSerializable: true,
8+
setup: ctx => ({
9+
handler: () => ({ cwd: process.env.DEVFRAME_E2E_CWD || ctx.cwd }),
10+
}),
11+
})
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import type { RpcDefinitionsToFunctions } from 'devframe/rpc'
2+
import { getCwd } from './get-cwd'
3+
import { listFiles } from './list-files'
4+
5+
export const serverFunctions = [getCwd, listFiles] as const
6+
7+
declare module 'devframe' {
8+
interface DevToolsRpcServerFunctions extends RpcDefinitionsToFunctions<typeof serverFunctions> {}
9+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import process from 'node:process'
2+
import { defineRpcFunction } from 'devframe'
3+
import { glob } from 'tinyglobby'
4+
5+
export const listFiles = defineRpcFunction({
6+
name: 'devframe-files-inspector:list-files',
7+
type: 'query',
8+
jsonSerializable: true,
9+
snapshot: true,
10+
setup: ctx => ({
11+
handler: async () => {
12+
const cwd = process.env.DEVFRAME_E2E_CWD || ctx.cwd
13+
const files = await glob(['*'], { cwd, onlyFiles: true, dot: false })
14+
return files.map(f => f.replace(/\\/g, '/')).sort()
15+
},
16+
}),
17+
})

examples/next-runtime-snapshot/src/client/app/components/snapshot-env.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ export function SnapshotEnv() {
1515
return
1616
setLoading(true)
1717
try {
18-
const r = await rpc.call('next-runtime-snapshot:env' as any, { pattern: p }) as EnvSnapshot
18+
const r = await rpc.call('next-runtime-snapshot:env', { pattern: p })
1919
setSnap(r)
2020
}
2121
finally {

examples/next-runtime-snapshot/src/client/app/components/snapshot-memory.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ export function SnapshotMemory() {
3131
return
3232
setLoading(true)
3333
try {
34-
const r = await rpc.call('next-runtime-snapshot:memory' as any) as MemorySnapshot
34+
const r = await rpc.call('next-runtime-snapshot:memory')
3535
setSnap(r)
3636
}
3737
finally {

examples/next-runtime-snapshot/src/client/app/components/snapshot-system.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@ export function SnapshotSystem() {
1616
if (!rpc)
1717
return
1818
let active = true
19-
rpc.call('next-runtime-snapshot:system' as any).then((r: unknown) => {
19+
rpc.call('next-runtime-snapshot:system').then((r) => {
2020
if (active)
21-
setInfo(r as SystemInfo)
21+
setInfo(r)
2222
})
2323
return () => {
2424
active = false

0 commit comments

Comments
 (0)