Skip to content

Commit

Permalink
Change CRDT model to reflect board structure
Browse files Browse the repository at this point in the history
This way it might be easier later on to transition to a fully CRDT based
model, as now we can iterate all items and their fields and react to
item additions and removals.
  • Loading branch information
raimohanska committed Feb 25, 2024
1 parent 2661444 commit 5ceaad6
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
2 changes: 2 additions & 0 deletions YJS_CRDT_WIP.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@ Must-haves
- Undo buffer integration. Editor has its own local undo but we should also add the full edit as a global undo item
- Mobile check
- Manage session on the server side: terminate YJS sockets when websocket session is terminated
- APIs
- Performance testing
- Playwright tests
- Include API basic tests in Playwright tests

Nice-to-haves

Expand Down
20 changes: 17 additions & 3 deletions frontend/src/store/crdt-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,29 @@ function BoardCRDT(
console.log("YJS Provider status", event.status)
})

function getField(itemId: Id, field: string) {
return doc.getText(`items.${itemId}.${field}`)
function getField(itemId: Id, fieldName: string, create: boolean = true) {
const items = doc.getMap("items")
let item = items.get(itemId) as Y.Map<any> | undefined
if (!item) {
if (!create) throw Error(`Item ${itemId} not found`)
item = new Y.Map()
items.set(itemId, item)
}
let field = item.get(fieldName) as Y.Text | undefined
if (!field) {
if (!create) throw Error(`Field ${fieldName} not found in item ${itemId}`)
field = new Y.Text()
item.set(fieldName, field)
}
return field
}

localBoardItemEvents.forEach((event) => {
if (event.action === "item.add") {
for (const item of event.items) {
if (isTextItem(item) && item.crdt) {
getField(item.id, "text").insert(0, item.text)
// Initialize CRDT for the "text" field
getField(item.id, "text", true).insert(0, item.text)
}
}
}
Expand Down

0 comments on commit 5ceaad6

Please sign in to comment.