fix: use proper Automerge URL format for repo.find()
The issue was that repo.find() was creating a NEW document instead of
waiting for the existing one to sync from the network.
Changes:
- Use 'automerge:{documentId}' URL format for repo.find()
- Remove try-catch that was creating new documents
- Let repo.find() properly wait for network sync
This ensures all clients use the SAME document ID and can sync in real-time.
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
parent
afa8d8498e
commit
907b96d480
|
|
@ -3,7 +3,7 @@ import { TLStoreSnapshot } from "@tldraw/tldraw"
|
|||
import { CloudflareNetworkAdapter } from "./CloudflareAdapter"
|
||||
import { useAutomergeStoreV2, useAutomergePresence } from "./useAutomergeStoreV2"
|
||||
import { TLStoreWithStatus } from "@tldraw/tldraw"
|
||||
import { Repo } from "@automerge/automerge-repo"
|
||||
import { Repo, parseAutomergeUrl, stringifyAutomergeUrl } from "@automerge/automerge-repo"
|
||||
import { DocHandle } from "@automerge/automerge-repo"
|
||||
|
||||
interface AutomergeSyncConfig {
|
||||
|
|
@ -146,18 +146,11 @@ export function useAutomergeSync(config: AutomergeSyncConfig): TLStoreWithStatus
|
|||
let handle: DocHandle<TLStoreSnapshot>
|
||||
|
||||
if (documentId) {
|
||||
// Try to find the existing document
|
||||
console.log(`🔍 Attempting to find document ${documentId}`)
|
||||
try {
|
||||
handle = await repo.find<TLStoreSnapshot>(documentId as any)
|
||||
console.log(`✅ Found document handle: ${documentId}`)
|
||||
} catch (error) {
|
||||
// Document not available yet - this can happen when it exists on server but not locally
|
||||
console.log(`📝 Document ${documentId} not immediately available, creating new handle`)
|
||||
// Create a new handle - the sync will handle merging with server state
|
||||
handle = repo.create<TLStoreSnapshot>()
|
||||
console.log(`📝 Created new handle ${handle.documentId}, will sync with server`)
|
||||
}
|
||||
// Convert document ID to Automerge URL format and find it
|
||||
console.log(`🔍 Finding document ${documentId} via network sync`)
|
||||
const docUrl = `automerge:${documentId}` as const
|
||||
handle = await repo.find<TLStoreSnapshot>(docUrl as any)
|
||||
console.log(`✅ Got handle for document: ${handle.documentId}, isReady: ${handle.isReady()}`)
|
||||
} else {
|
||||
// Create a new document and register its ID with the server
|
||||
handle = repo.create<TLStoreSnapshot>()
|
||||
|
|
|
|||
Loading…
Reference in New Issue