codepod-io / codepod

Codepod IDE: Scalable Interactive Coding
https://codepod.io
MIT License
75 stars 15 forks source link

use root YMap #443

Closed lihebi closed 1 year ago

lihebi commented 1 year ago

Instead of using four top-level YMaps (nodesMap, edgesMap, codeMap, richMap), this PR uses a root YMap rootMap, so that all fields are removable for possible future schema updates.

Before:

const nodesMap = ydoc.getMap<ReactflowNode<NodeData>>("nodesMap")
const edgesMap = ydoc.getMap<ReactflowEdge>("edgesMap")
const codeMap = ydoc.getMap<Y.Text>("codeMap")
const richMap = ydoc.getMap<Y.XmlFragment>("richMap")

After:

// rootMap
const rootMap = ydoc.getMap("rootMap");
// nested maps
const nodesMap = rootMap.get("nodesMap") as Y.Map<ReactflowNode<NodeData>>;
const edgesMap = rootMap.get("edgesMap") as Y.Map<ReactflowEdge>;
const codeMap = rootMap.get("codeMap") as Y.Map<Y.Text>;
const richMap = rootMap.get("richMap") as Y.Map<Y.XmlFragment>;

Also added four helper store.functions:

store.getNodesMap() {
  return get().ydoc.getMap("rootMap").get("nodesMap") as Y.Map<Node<NodeData>>;
},
store.getEdgesMap() {...}
store.getCodeMap() {...}
store.getRichMap() {...}

Ref from Yjs document:

Consider using a single top-level YMap: Top-level shared types cannot be deleted, so you may want to structure all your data in a single top-level YMap, eg. yDoc.getMap('data').get('page-1').