|
| 1 | +// The Capacitor inspector on the devframe server (`devframe.ts`): the |
| 2 | +// `capacitor` shared state with patches, and its push and forget. |
| 3 | + |
| 4 | +import { createSharedState } from 'devframe/utils/shared-state'; |
| 5 | +import type { ResolvedPangularConfig } from './config.ts'; |
| 6 | +import { |
| 7 | + mergeCapacitorReport, |
| 8 | + patchCapacitorState, |
| 9 | + syncCapacitorState, |
| 10 | + toCapacitorReport, |
| 11 | + type CapacitorPage, |
| 12 | + type CapacitorState, |
| 13 | +} from './rpc/capacitor-tools.ts'; |
| 14 | +import { trackPageSessions } from './rpc/page-sessions.ts'; |
| 15 | +import type { DevframeNodeContext, Owns, PangularScope, Register } from './devframe-env.ts'; |
| 16 | + |
| 17 | +export async function registerCapacitor(env: { |
| 18 | + ctx: DevframeNodeContext; |
| 19 | + my: PangularScope; |
| 20 | + config: ResolvedPangularConfig; |
| 21 | + register: Register; |
| 22 | + owns: Owns; |
| 23 | + mayForget: Owns; |
| 24 | +}) { |
| 25 | + const { ctx, my, config, register, owns, mayForget } = env; |
| 26 | + const limits = config.limits; |
| 27 | + const capacitorPages = new Map<string, CapacitorPage>(); |
| 28 | + // With patches on, a push sends the panel what changed, not every page: |
| 29 | + // devframe's own shared states send the whole value on each change. |
| 30 | + const capacitorShared = await my.rpc.sharedState('capacitor', { |
| 31 | + sharedState: createSharedState<CapacitorState>({ |
| 32 | + initialValue: { pages: {} }, |
| 33 | + enablePatches: true, |
| 34 | + }), |
| 35 | + }); |
| 36 | + const applyCapacitor = () => |
| 37 | + capacitorShared.mutate((draft) => syncCapacitorState(draft, capacitorPages)); |
| 38 | + const capacitorSessions = trackPageSessions(ctx.rpc, (pageIds) => { |
| 39 | + let removed = false; |
| 40 | + for (const pageId of pageIds) removed = capacitorPages.delete(pageId) || removed; |
| 41 | + if (removed) applyCapacitor(); |
| 42 | + }); |
| 43 | + |
| 44 | + register({ |
| 45 | + name: 'push-capacitor', |
| 46 | + type: 'action', |
| 47 | + jsonSerializable: true, |
| 48 | + handler: (message: unknown) => { |
| 49 | + const report = toCapacitorReport(message); |
| 50 | + if (!report || !owns(report.pageId)) return { known: true }; |
| 51 | + capacitorSessions.bind(report.pageId); |
| 52 | + const answer = mergeCapacitorReport(capacitorPages, report, limits.capacitorCalls); |
| 53 | + if (answer.known) { |
| 54 | + capacitorShared.mutate((draft) => patchCapacitorState(draft, capacitorPages, report)); |
| 55 | + } |
| 56 | + return answer; |
| 57 | + }, |
| 58 | + }); |
| 59 | + |
| 60 | + register({ |
| 61 | + name: 'forget-capacitor-page', |
| 62 | + type: 'action', |
| 63 | + jsonSerializable: true, |
| 64 | + handler: (pageId: string) => { |
| 65 | + if (!mayForget(pageId)) return; |
| 66 | + capacitorSessions.unbind(pageId); |
| 67 | + if (capacitorPages.delete(pageId)) applyCapacitor(); |
| 68 | + }, |
| 69 | + }); |
| 70 | + |
| 71 | + return { capacitorPages, capacitorShared, applyCapacitor }; |
| 72 | +} |
0 commit comments