Skip to content

Commit a96cda8

Browse files
SaKaNa-Yclaudeantfubot
authored
fix(shared-state): keep initial snapshots local (#454)
Co-authored-by: Claude Opus 5.5 <[email protected]> Co-authored-by: Anthony Fu (via agent) <[email protected]>
1 parent 705a573 commit a96cda8

3 files changed

Lines changed: 126 additions & 5 deletions

File tree

‎design/uno.config.ts‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import type { Preset } from 'unocss'
22
import { fileURLToPath } from 'node:url'
33
import { presetAnthonyDesign, resolvePrimary } from '@antfu/design/unocss'
44
import {
5+
cssIdRE,
56
defineConfig,
67
presetIcons,
78
presetWind4,
@@ -54,6 +55,14 @@ export function createDesignConfig(options: CreateDesignConfigOptions = {}) {
5455
*/
5556
content: {
5657
filesystem: [fileURLToPath(new URL('./design.ts', import.meta.url))],
58+
/**
59+
* Restates UnoCSS's default pipeline exclude. Without it, `mergeConfigs`
60+
* leaves `exclude: []`, and UnoCSS 66.11 then treats every module as
61+
* excluded, so no utilities are extracted from app sources.
62+
*/
63+
pipeline: {
64+
exclude: [cssIdRE],
65+
},
5766
},
5867
/**
5968
* Wind leaves bare `border`/`border-b` at currentColor; restore the subtle

‎packages/devframe/src/client/rpc-shared-state.test.ts‎

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { createEventEmitter } from 'devframe/utils/events'
22
import { describe, expect, it } from 'vitest'
3+
import { DEVFRAME_EVENTS } from '../events'
34
import { createRpcSharedStateClientHost } from './rpc-shared-state'
45

56
function makeFakeRpc() {
@@ -50,3 +51,106 @@ describe('client shared state', () => {
5051
expect(setCalls).toHaveLength(1) // exactly one server-state:set, not two
5152
})
5253
})
54+
55+
describe('initial shared-state snapshots', () => {
56+
function setup() {
57+
const { rpc, setCalls } = makeFakeRpc()
58+
const snapshot = Promise.withResolvers<object | undefined>()
59+
const handlers = new Map<string, (...args: any[]) => void>()
60+
rpc.isTrusted = true
61+
rpc.call = () => snapshot.promise
62+
rpc.client.register = (definition: any) => handlers.set(definition.name, definition.handler)
63+
const host = createRpcSharedStateClientHost(rpc)
64+
return { rpc, host, snapshot, handlers, setCalls }
65+
}
66+
67+
it('reads a snapshot without writing it back to the node side', async () => {
68+
const { host, snapshot, setCalls } = setup()
69+
const state = await host.get('k', { initialValue: { count: 0, localDefault: true } })
70+
snapshot.resolve({ count: 1 })
71+
await snapshot.promise
72+
expect(state.value()).toEqual({ count: 1, localDefault: true })
73+
expect(setCalls).toEqual([])
74+
state.mutate((draft) => {
75+
draft.count = 2
76+
})
77+
expect(setCalls[0]?.[1]).toEqual({ count: 2, localDefault: true })
78+
})
79+
80+
it('keeps an update received while the initial snapshot is pending', async () => {
81+
const { host, snapshot, handlers } = setup()
82+
const state = await host.get('k', { initialValue: { count: 0 } })
83+
handlers.get(DEVFRAME_EVENTS.broadcast.clientStateUpdated)!('k', { count: 2 }, 'newer')
84+
snapshot.resolve({ count: 1 })
85+
await snapshot.promise
86+
expect(state.value()).toEqual({ count: 2 })
87+
})
88+
89+
it('keeps a local write made while the initial snapshot is pending', async () => {
90+
const { host, snapshot, setCalls } = setup()
91+
const state = await host.get('k', { initialValue: { count: 0 } })
92+
state.mutate((draft) => {
93+
draft.count = 2
94+
})
95+
snapshot.resolve({ count: 1 })
96+
await snapshot.promise
97+
expect(state.value()).toEqual({ count: 2 })
98+
expect(setCalls).toHaveLength(1)
99+
})
100+
101+
it('keeps a local undo that restores the initial object while the snapshot is pending', async () => {
102+
const { host, snapshot, setCalls } = setup()
103+
const state = await host.get('k', { initialValue: { count: 0 } })
104+
const initialValue = state.value()
105+
state.mutate((draft) => {
106+
draft.count = 2
107+
})
108+
state.mutate(() => initialValue)
109+
expect(state.value()).toBe(initialValue)
110+
expect(setCalls.map(([, value]) => value)).toEqual([{ count: 2 }, { count: 0 }])
111+
112+
snapshot.resolve({ count: 1 })
113+
await snapshot.promise
114+
expect(state.value()).toBe(initialValue)
115+
expect(setCalls).toHaveLength(2)
116+
})
117+
118+
it('applies the initial snapshot after a mutation that leaves the value unchanged', async () => {
119+
const { host, snapshot, setCalls } = setup()
120+
const state = await host.get('k', { initialValue: { count: 0 } })
121+
state.mutate((draft) => {
122+
draft.count = 0
123+
})
124+
snapshot.resolve({ count: 1 })
125+
await snapshot.promise
126+
expect(state.value()).toEqual({ count: 1 })
127+
expect(setCalls).toEqual([])
128+
})
129+
130+
it('forwards a local write triggered by the initial snapshot', async () => {
131+
const { host, snapshot, setCalls } = setup()
132+
const state = await host.get('k', { initialValue: { count: 0 } })
133+
state.on('updated', (value) => {
134+
if (value.count === 1) {
135+
state.mutate((draft) => {
136+
draft.count = 2
137+
})
138+
}
139+
})
140+
snapshot.resolve({ count: 1 })
141+
await snapshot.promise
142+
expect(state.value()).toEqual({ count: 2 })
143+
expect(setCalls).toHaveLength(1)
144+
expect(setCalls[0]?.[1]).toEqual({ count: 2 })
145+
})
146+
147+
it('ignores a snapshot after its state is deleted', async () => {
148+
const { host, snapshot, setCalls } = setup()
149+
const state = await host.get('k', { initialValue: { count: 0 } })
150+
host.delete('k')
151+
snapshot.resolve({ count: 1 })
152+
await snapshot.promise
153+
expect(state.value()).toEqual({ count: 0 })
154+
expect(setCalls).toEqual([])
155+
})
156+
})

‎packages/devframe/src/client/rpc-shared-state.ts‎

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import type { RpcSharedStateGetOptions, RpcSharedStateHost } from 'devframe/types'
22
import type { SharedState, SharedStatePatch } from 'devframe/utils/shared-state'
33
import type { DevframeRpcClient } from './rpc'
4+
import { nanoid } from 'devframe/utils/nanoid'
45
import { createSharedState } from 'devframe/utils/shared-state'
56
import { DEVFRAME_EVENTS } from '../events'
67

@@ -42,10 +43,10 @@ export function createRpcSharedStateClientHost(rpc: DevframeRpcClient): RpcShare
4243
},
4344
})
4445

45-
function registerSharedState<T extends object>(key: string, state: SharedState<T>) {
46+
function registerSharedState<T extends object>(key: string, state: SharedState<T>, initialSyncId?: string) {
4647
const offs: (() => void)[] = []
4748
offs.push(state.on('updated', (fullState, patches, syncId) => {
48-
if (isStaticBackend)
49+
if (isStaticBackend || syncId === initialSyncId)
4950
return
5051
if (patches) {
5152
rpc.callEvent('devframe:rpc:server-state:patch', key, patches, syncId)
@@ -99,15 +100,22 @@ export function createRpcSharedStateClientHost(rpc: DevframeRpcClient): RpcShare
99100
sharedState.set(key, state)
100101
for (const fn of keyAddedListeners)
101102
fn(key)
103+
let stateUpdated = false
104+
const stopTrackingUpdates = state.on('updated', () => {
105+
stateUpdated = true
106+
})
107+
const initialSyncId = nanoid()
102108
rpc.call('devframe:rpc:server-state:get', key)
103109
.then((serverState) => {
104-
if (serverState !== undefined)
105-
state.mutate(() => mergeWithInitialValue(key, serverState))
110+
// A pending read must not replace a newer update or a deleted state.
111+
if (serverState !== undefined && sharedState.get(key) === state && !stateUpdated)
112+
state.mutate(() => mergeWithInitialValue(key, serverState), initialSyncId)
106113
})
107114
.catch((error) => {
108115
console.error('Error getting server state', error)
109116
})
110-
stateDisposers.set(key, registerSharedState(key, state))
117+
.finally(stopTrackingUpdates)
118+
stateDisposers.set(key, registerSharedState(key, state, initialSyncId))
111119
return state
112120
}
113121
else {

0 commit comments

Comments
 (0)