@@ -38,10 +38,10 @@ function getDebouncedCallback(key) {
3838 cb ( ) ;
3939 } ,
4040 // write if no new activity in 10s
41- 10000 ,
41+ 1000 ,
4242 {
4343 // write at least every 20s
44- maxWait : 20000 ,
44+ maxWait : 5000 ,
4545 }
4646 )
4747 ) ;
@@ -50,21 +50,50 @@ function getDebouncedCallback(key) {
5050 return debounceRegistry . get ( key ) ;
5151}
5252
53- async function handleSaveBlob ( { repoId, yDocBlob, blobDir } ) {
53+ function handleSaveBlob ( { repoId, yDocBlob, repoDir } ) {
5454 console . log ( "save blob" , repoId , yDocBlob . length ) ;
5555 // create the yjs-blob folder if not exists
56- if ( ! fs . existsSync ( blobDir ) ) {
57- fs . mkdirSync ( blobDir ) ;
56+ const dir = `${ repoDir } /.codepod` ;
57+ if ( ! fs . existsSync ( dir ) ) {
58+ fs . mkdirSync ( dir , { recursive : true } ) ;
5859 }
5960 // save the blob to file system
60- fs . writeFileSync ( `${ blobDir } /yjs.bin` , yDocBlob ) ;
61+ fs . writeFileSync ( `${ dir } /yjs.bin` , yDocBlob ) ;
62+ }
63+
64+ function handleSavePlain ( { repoId, ydoc, repoDir } ) {
65+ console . log ( "save plain" , repoId ) ;
66+ // save the plain to file system
67+ const rootMap = ydoc . getMap ( "rootMap" ) ;
68+ const nodesMap = rootMap . get ( "nodesMap" ) as Y . Map < any > ;
69+ const edgesMap = rootMap . get ( "edgesMap" ) as Y . Map < any > ;
70+ const codeMap = rootMap . get ( "codeMap" ) as Y . Map < Y . Text > ;
71+ const richMap = rootMap . get ( "richMap" ) as Y . Map < Y . XmlFragment > ;
72+ const resultMap = rootMap . get ( "resultMap" ) as Y . Map < any > ;
73+ const runtimeMap = rootMap . get ( "runtimeMap" ) as Y . Map < any > ;
74+ const metaMap = rootMap . get ( "metaMap" ) as Y . Map < any > ;
75+ const plain = {
76+ lastUpdate : new Date ( ) . toISOString ( ) ,
77+ metaMap : metaMap . toJSON ( ) ,
78+ nodesMap : nodesMap . toJSON ( ) ,
79+ edgesMap : edgesMap . toJSON ( ) ,
80+ codeMap : codeMap . toJSON ( ) ,
81+ richMap : richMap . toJSON ( ) ,
82+ resultMap : resultMap . toJSON ( ) ,
83+ runtimeMap : runtimeMap . toJSON ( ) ,
84+ } ;
85+ const dir = `${ repoDir } /.codepod` ;
86+ if ( ! fs . existsSync ( dir ) ) {
87+ fs . mkdirSync ( dir , { recursive : true } ) ;
88+ }
89+ fs . writeFileSync ( `${ dir } /yjs.json` , JSON . stringify ( plain , null , 2 ) ) ;
6190}
6291
6392/**
6493 * This function is called when setting up the WS connection, after the loadFromCodePod step.
6594 * TODO need to make sure this is only called once per repo, regardless of how many users are connected later.
6695 */
67- function setupObserversToDB ( ydoc : Y . Doc , repoId : string , blobDir : string ) {
96+ function setupObserversToDB ( ydoc : Y . Doc , repoId : string , repoDir : string ) {
6897 console . log ( "setupObserversToDB for repo" , repoId ) ;
6998 // just observe and save the entire doc
7099 function observer ( _ , transaction ) {
@@ -79,7 +108,8 @@ function setupObserversToDB(ydoc: Y.Doc, repoId: string, blobDir: string) {
79108 // FIXME it may be too expensive to update the entire doc.
80109 // FIXME history is discarded
81110 const update = Y . encodeStateAsUpdate ( ydoc ) ;
82- handleSaveBlob ( { repoId, yDocBlob : Buffer . from ( update ) , blobDir } ) ;
111+ handleSaveBlob ( { repoId, yDocBlob : Buffer . from ( update ) , repoDir } ) ;
112+ handleSavePlain ( { repoId, ydoc, repoDir } ) ;
83113 } ) ;
84114 }
85115 const rootMap = ydoc . getMap ( "rootMap" ) ;
@@ -98,11 +128,11 @@ function setupObserversToDB(ydoc: Y.Doc, repoId: string, blobDir: string) {
98128/**
99129 * This function is called when setting up the WS connection, as a first step.
100130 */
101- async function loadFromFS ( ydoc : Y . Doc , repoId : string , blobDir : string ) {
131+ async function loadFromFS ( ydoc : Y . Doc , repoId : string , repoDir : string ) {
102132 // load from the database and write to the ydoc
103133 console . log ( "=== loadFromFS" ) ;
104134 // read the blob from file system
105- const binFile = `${ blobDir } /yjs.bin` ;
135+ const binFile = `${ repoDir } /.codepod /yjs.bin` ;
106136 if ( fs . existsSync ( binFile ) ) {
107137 const yDocBlob = fs . readFileSync ( binFile ) ;
108138 Y . applyUpdate ( ydoc , yDocBlob ) ;
@@ -121,11 +151,11 @@ async function loadFromFS(ydoc: Y.Doc, repoId: string, blobDir: string) {
121151 }
122152}
123153
124- export async function bindState ( doc : Y . Doc , repoId : string , blobDir : string ) {
154+ export async function bindState ( doc : Y . Doc , repoId : string , repoDir : string ) {
125155 // Load persisted document state from the database.
126- await loadFromFS ( doc , repoId , blobDir ) ;
156+ await loadFromFS ( doc , repoId , repoDir ) ;
127157 // Observe changes and write to the database.
128- setupObserversToDB ( doc , repoId , blobDir ) ;
158+ setupObserversToDB ( doc , repoId , repoDir ) ;
129159 // setupObserversToRuntime(doc, repoId);
130160 // reset runtime status
131161 // clear runtimeMap status/commands but keep the ID
0 commit comments