* feat(scene-graph): let a SceneGraph take its ID generator The constructor accepts an ID generator, used for the root node and for nodes, variables, and collections created later; generated IDs skip any node, variable, collection, or mode ID already in the graph. Refs #770 Signed-off-by: Marc Went <marc@went.io> * perf(scene-graph): check mode IDs without allocating per created entity Generating an ID spread every collection into a new array and scanned it for each candidate, which runs on every createNode. Check the node, variable, and collection maps first and walk the modes with an early exit. An index of mode IDs is not kept because history snapshots, transfer, and undo replace collection maps and edit modes in place, which would leave it stale. * test(scene-graph): cover injected ID generators and collision skipping * docs: note the SceneGraph ID generator in the changelog * test(core): keep reopened .fig GUIDs when a sibling is inserted before them Refs #770 * test(collab): cover concurrent additions, same-property writes, and delete versus edit Move the synced-store harness to tests/helpers/collab so the new cases live in tests/app/collab, and let it bind graph events as a collab session does. The cases edit both peers while disconnected and check the converged graphs. Refs #770 * fix(scene-graph): keep a collection's IDs apart and stop on an exhausted generator createCollection asks for the collection and default mode IDs before registering either, so a generator repeating a candidate gave both the same ID. A generator that only returned taken IDs looped forever, even in the constructor; it now throws after a bounded number of attempts. * test: require the inserted GUID and a real disconnect in new tests The GUID test passed even if export dropped the inserted layer, and the concurrent-edit helper would have tested sequential sync had its peers not been disconnectable. * feat(scene-graph): create variable modes through the graph's ID generator The editor minted added and duplicated modes as mode:<random hex>, so an injected generator governed every graph entity except modes added after a collection's first. SceneGraph.createMode mints the ID and adds the mode; undo and redo replay it with addMode. * test(core): type the reopened .fig buffer in the GUID test * refactor(scene-graph)!: share random helpers and one component property ID randomHex, randomInt, and randomIndex move from @open-pencil/core/random to @open-pencil/scene-graph/random, so format packages can use them: design-jsx and the MCP test server dropped private copies. Component property IDs, written as prop:<random hex> in six places across Core and design-jsx, come from createComponentPropertyId. * docs: point the randomness rule at the shared ID and random helpers --------- Signed-off-by: Marc Went <marc@went.io> Co-authored-by: Marc Went <marc@went.io>
143 lines
4 KiB
TypeScript
143 lines
4 KiB
TypeScript
import * as Y from 'yjs'
|
|
|
|
import { SceneGraph } from '@open-pencil/scene-graph'
|
|
|
|
import {
|
|
bindCollabGraphEvents,
|
|
createYjsGraphSync,
|
|
registerYjsObservers
|
|
} from '@/app/collab/yjs-sync'
|
|
import { createEditorStore } from '@/app/editor/session'
|
|
|
|
import { connectYDocs } from '#tests/helpers/yjs'
|
|
|
|
/** Host and peer editor stores whose Yjs documents sync through the app's collab code. */
|
|
export type SyncedStores = ReturnType<typeof createSyncedStores>
|
|
|
|
export type SyncedStoreOptions = {
|
|
hostDoc?: Y.Doc
|
|
peerDoc?: Y.Doc
|
|
connectImmediately?: boolean
|
|
/** Sync local graph edits to Yjs through graph events, as a collab session does. */
|
|
bindGraphEvents?: boolean
|
|
}
|
|
|
|
export function createSyncedStores(options: SyncedStoreOptions = {}) {
|
|
const hostStore = createEditorStore(new SceneGraph())
|
|
const peerStore = createEditorStore(new SceneGraph())
|
|
const hostDoc = options.hostDoc ?? new Y.Doc()
|
|
const peerDoc = options.peerDoc ?? new Y.Doc()
|
|
const hostNodes = hostDoc.getMap<Y.Map<unknown>>('nodes')
|
|
const peerNodes = peerDoc.getMap<Y.Map<unknown>>('nodes')
|
|
const hostImages = hostDoc.getMap<Uint8Array>('images')
|
|
const peerImages = peerDoc.getMap<Uint8Array>('images')
|
|
let hostSuppressYjsEvents = false
|
|
let peerSuppressYjsEvents = false
|
|
let hostSuppressGraphSync = false
|
|
let peerSuppressGraphSync = false
|
|
|
|
const hostSync = createYjsGraphSync({
|
|
getStore: () => hostStore,
|
|
getYdoc: () => hostDoc,
|
|
getYnodes: () => hostNodes,
|
|
getYimages: () => hostImages,
|
|
setSuppressYjsEvents: (value) => {
|
|
hostSuppressYjsEvents = value
|
|
}
|
|
})
|
|
const peerSync = createYjsGraphSync({
|
|
getStore: () => peerStore,
|
|
getYdoc: () => peerDoc,
|
|
getYnodes: () => peerNodes,
|
|
getYimages: () => peerImages,
|
|
setSuppressYjsEvents: (value) => {
|
|
peerSuppressYjsEvents = value
|
|
}
|
|
})
|
|
|
|
registerYjsObservers({
|
|
store: hostStore,
|
|
ynodes: hostNodes,
|
|
yimages: hostImages,
|
|
getSuppressYjsEvents: () => hostSuppressYjsEvents,
|
|
setSuppressGraphSync: (value) => {
|
|
hostSuppressGraphSync = value
|
|
},
|
|
applyYjsToGraph: hostSync.applyYjsToGraph
|
|
})
|
|
registerYjsObservers({
|
|
store: peerStore,
|
|
ynodes: peerNodes,
|
|
yimages: peerImages,
|
|
getSuppressYjsEvents: () => peerSuppressYjsEvents,
|
|
setSuppressGraphSync: (value) => {
|
|
peerSuppressGraphSync = value
|
|
},
|
|
applyYjsToGraph: peerSync.applyYjsToGraph
|
|
})
|
|
|
|
const unbindGraphEvents = options.bindGraphEvents
|
|
? [
|
|
bindCollabGraphEvents({
|
|
store: hostStore,
|
|
getYdoc: () => hostDoc,
|
|
getYnodes: () => hostNodes,
|
|
getSuppressGraphSync: () => hostSuppressGraphSync,
|
|
setSuppressYjsEvents: (value) => {
|
|
hostSuppressYjsEvents = value
|
|
},
|
|
syncNodeToYjs: hostSync.syncNodeToYjs
|
|
}),
|
|
bindCollabGraphEvents({
|
|
store: peerStore,
|
|
getYdoc: () => peerDoc,
|
|
getYnodes: () => peerNodes,
|
|
getSuppressGraphSync: () => peerSuppressGraphSync,
|
|
setSuppressYjsEvents: (value) => {
|
|
peerSuppressYjsEvents = value
|
|
},
|
|
syncNodeToYjs: peerSync.syncNodeToYjs
|
|
})
|
|
]
|
|
: []
|
|
|
|
const disconnectYDocs =
|
|
options.connectImmediately === false ? undefined : connectYDocs(hostDoc, peerDoc)
|
|
|
|
return {
|
|
hostStore,
|
|
peerStore,
|
|
hostSync,
|
|
peerSync,
|
|
hostDoc,
|
|
peerDoc,
|
|
disconnectYDocs,
|
|
get hostSuppressGraphSync() {
|
|
return hostSuppressGraphSync
|
|
},
|
|
get peerSuppressGraphSync() {
|
|
return peerSuppressGraphSync
|
|
},
|
|
cleanup: () => {
|
|
for (const unbind of unbindGraphEvents) unbind()
|
|
hostStore.preparationController.dispose()
|
|
peerStore.preparationController.dispose()
|
|
disconnectYDocs?.()
|
|
hostDoc.destroy()
|
|
peerDoc.destroy()
|
|
}
|
|
}
|
|
}
|
|
|
|
export async function withSyncedStores(
|
|
run: (stores: SyncedStores) => void | Promise<void>,
|
|
options: SyncedStoreOptions = {}
|
|
) {
|
|
const stores = createSyncedStores(options)
|
|
try {
|
|
await run(stores)
|
|
} finally {
|
|
stores.cleanup()
|
|
}
|
|
}
|