Added document version number, make startup work via await
This commit is contained in:
parent
ab409780d6
commit
57e3c9989e
@ -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
|
||||
}
|
||||
|
||||
|
||||
@ -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)
|
||||
|
||||
Loading…
Reference in New Issue
Block a user