diff --git a/src/docManager.js b/src/docManager.js index 4e0095f..a57a3a2 100644 --- a/src/docManager.js +++ b/src/docManager.js @@ -8,7 +8,10 @@ import { BrowserWebSocketClientAdapter } from "@automerge/automerge-repo-network let handle -export function initRepo() { +// Define the current schema version +const CURRENT_SCHEMA_VERSION = 1; + +export async function initRepo() { const repo = new Repo({ network: [ new BrowserWebSocketClientAdapter("wss://automerge.rheinheim.fraction.ch") @@ -21,19 +24,35 @@ export function initRepo() { if (rootDocUrl && isValidAutomergeUrl(rootDocUrl)) { handle = repo.find(rootDocUrl) + // Wait for handle to be ready + await handle.whenReady() + + // Check and upgrade schema if needed + handle.change(doc => { + if (!doc.schemaVersion) { + // Handle legacy documents (pre-versioning) + doc.schemaVersion = 1 + } + // Add future upgrade logic here + // if (doc.schemaVersion < 2) { + // // Example: upgrade shapes to include a new required field + // doc.shapes.forEach(shape => { + // shape.opacity = 1.0 // Add new field with default value + // }) + // doc.schemaVersion = 2 + // } + }) } else { - // Create new document if none exists + // Create new document with current schema version handle = repo.create({ + schemaVersion: CURRENT_SCHEMA_VERSION, shapes: [] }) - // Set the URL hash to the new document's URL + // Wait for handle to be ready + await handle.whenReady() document.location.hash = handle.url } - // handle.on("change", ({doc}) => { - // console.log("Document changed!", doc) - // }) - return handle } diff --git a/src/main.js b/src/main.js index cb5c41a..5ec625d 100644 --- a/src/main.js +++ b/src/main.js @@ -21,9 +21,9 @@ function throttle(func, limit) { } } -// 1. Init the repo & document handle -const handle = initRepo() -await handle.whenReady() + // Initialize the document +const handle = await initRepo() + // draw current state createOrUpdateShapes(container, handle.docSync().shapes)