-
Notifications
You must be signed in to change notification settings - Fork 57
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Manage CRDT local persistence and connection in crdt-store
- Loading branch information
1 parent
070f1ef
commit f412a87
Showing
9 changed files
with
92 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -383,6 +383,7 @@ export const BoardView = ({ | |
dispatch, | ||
toolController, | ||
accessLevel, | ||
boardStore, | ||
}} | ||
/> | ||
) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import { IndexeddbPersistence } from "y-indexeddb" | ||
import { WebsocketProvider } from "y-websocket" | ||
import * as Y from "yjs" | ||
import { Id } from "../../../common/src/domain" | ||
import { ServerConnection, WS_ROOT } from "./server-connection" | ||
import * as L from "lonna" | ||
|
||
type BoardCRDT = ReturnType<typeof BoardCRDT> | ||
|
||
function BoardCRDT(boardId: Id, online: L.Property<boolean>) { | ||
const doc = new Y.Doc() | ||
|
||
const persistence = new IndexeddbPersistence(`b/${boardId}`, doc) | ||
|
||
persistence.on("synced", () => { | ||
console.log("CRDT data from indexedDB is loaded") | ||
}) | ||
|
||
const provider = new WebsocketProvider(`${WS_ROOT}/socket/yjs`, `board/${boardId}`, doc, { | ||
connect: online.get(), | ||
}) | ||
|
||
online.onChange((c) => (c ? provider.connect() : provider.disconnect())) | ||
|
||
provider.on("status", (event: any) => { | ||
console.log("YJS Provider status", event.status) | ||
}) | ||
|
||
function getField(itemId: Id, field: string) { | ||
return doc.getText(`items.${itemId}.${field}`) | ||
} | ||
|
||
return { | ||
doc, | ||
getField, | ||
awareness: provider.awareness, | ||
} | ||
} | ||
|
||
export type CRDTStore = ReturnType<typeof CRDTStore> | ||
export function CRDTStore(online: L.Property<boolean>) { | ||
const boards = new Map<Id, BoardCRDT>() | ||
function getBoardCrdt(boardId: Id): BoardCRDT { | ||
let doc = boards.get(boardId) | ||
if (!doc) { | ||
doc = BoardCRDT(boardId, online) | ||
} | ||
return doc | ||
} | ||
return { | ||
getBoardCrdt, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4725,6 +4725,13 @@ lib0@^0.2.31, lib0@^0.2.42, lib0@^0.2.52, lib0@^0.2.85, lib0@^0.2.86: | |
dependencies: | ||
isomorphic.js "^0.2.4" | ||
|
||
lib0@^0.2.74: | ||
version "0.2.89" | ||
resolved "https://registry.yarnpkg.com/lib0/-/lib0-0.2.89.tgz#f695ba69be34e28f73b3eeb5da92006f3897a470" | ||
integrity sha512-5j19vcCjsQhvLG6mcDD+nprtJUCbmqLz5Hzt5xgi9SV6RIW/Dty7ZkVZHGBuPOADMKjQuKDvuQTH495wsmw8DQ== | ||
dependencies: | ||
isomorphic.js "^0.2.4" | ||
|
||
[email protected]: | ||
version "3.1.1" | ||
resolved "https://registry.yarnpkg.com/lie/-/lie-3.1.1.tgz#9a436b2cc7746ca59de7a41fa469b3efb76bd87e" | ||
|
@@ -7822,6 +7829,13 @@ xtend@^4.0.0, xtend@^4.0.2, xtend@~4.0.0: | |
resolved "https://registry.yarnpkg.com/xtend/-/xtend-4.0.2.tgz#bb72779f5fa465186b1f438f674fa347fdb5db54" | ||
integrity sha512-LKYU1iAXJXUgAXn9URjiu+MWhyUXHsvfp7mcuYm9dSUKK0/CjtrUwFAxD82/mCWbtLsGjFIad0wIsod4zrTAEQ== | ||
|
||
y-indexeddb@^9.0.12: | ||
version "9.0.12" | ||
resolved "https://registry.yarnpkg.com/y-indexeddb/-/y-indexeddb-9.0.12.tgz#73657f31d52886d7532256610babf5cca4ad5e58" | ||
integrity sha512-9oCFRSPPzBK7/w5vOkJBaVCQZKHXB/v6SIT+WYhnJxlEC61juqG0hBrAf+y3gmSMLFLwICNH9nQ53uscuse6Hg== | ||
dependencies: | ||
lib0 "^0.2.74" | ||
|
||
y-leveldb@^0.1.0: | ||
version "0.1.2" | ||
resolved "https://registry.yarnpkg.com/y-leveldb/-/y-leveldb-0.1.2.tgz#43f6c5004b6891b57926d8a1e0eb0c883003e34b" | ||
|