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
|
let handle
|
||||||
|
|
||||||
export function initRepo() {
|
// Define the current schema version
|
||||||
|
const CURRENT_SCHEMA_VERSION = 1;
|
||||||
|
|
||||||
|
export async function initRepo() {
|
||||||
const repo = new Repo({
|
const repo = new Repo({
|
||||||
network: [
|
network: [
|
||||||
new BrowserWebSocketClientAdapter("wss://automerge.rheinheim.fraction.ch")
|
new BrowserWebSocketClientAdapter("wss://automerge.rheinheim.fraction.ch")
|
||||||
@ -21,19 +24,35 @@ export function initRepo() {
|
|||||||
|
|
||||||
if (rootDocUrl && isValidAutomergeUrl(rootDocUrl)) {
|
if (rootDocUrl && isValidAutomergeUrl(rootDocUrl)) {
|
||||||
handle = repo.find(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 {
|
} else {
|
||||||
// Create new document if none exists
|
// Create new document with current schema version
|
||||||
handle = repo.create({
|
handle = repo.create({
|
||||||
|
schemaVersion: CURRENT_SCHEMA_VERSION,
|
||||||
shapes: []
|
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
|
document.location.hash = handle.url
|
||||||
}
|
}
|
||||||
|
|
||||||
// handle.on("change", ({doc}) => {
|
|
||||||
// console.log("Document changed!", doc)
|
|
||||||
// })
|
|
||||||
|
|
||||||
return handle
|
return handle
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -21,9 +21,9 @@ function throttle(func, limit) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 1. Init the repo & document handle
|
// Initialize the document
|
||||||
const handle = initRepo()
|
const handle = await initRepo()
|
||||||
await handle.whenReady()
|
|
||||||
|
|
||||||
// draw current state
|
// draw current state
|
||||||
createOrUpdateShapes(container, handle.docSync().shapes)
|
createOrUpdateShapes(container, handle.docSync().shapes)
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user