@@ -38,10 +38,10 @@ function getDebouncedCallback(key) {
38
38
cb ( ) ;
39
39
} ,
40
40
// write if no new activity in 10s
41
- 10000 ,
41
+ 1000 ,
42
42
{
43
43
// write at least every 20s
44
- maxWait : 20000 ,
44
+ maxWait : 5000 ,
45
45
}
46
46
)
47
47
) ;
@@ -50,21 +50,50 @@ function getDebouncedCallback(key) {
50
50
return debounceRegistry . get ( key ) ;
51
51
}
52
52
53
- async function handleSaveBlob ( { repoId, yDocBlob, blobDir } ) {
53
+ function handleSaveBlob ( { repoId, yDocBlob, repoDir } ) {
54
54
console . log ( "save blob" , repoId , yDocBlob . length ) ;
55
55
// 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 } ) ;
58
59
}
59
60
// 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 ) ) ;
61
90
}
62
91
63
92
/**
64
93
* This function is called when setting up the WS connection, after the loadFromCodePod step.
65
94
* TODO need to make sure this is only called once per repo, regardless of how many users are connected later.
66
95
*/
67
- function setupObserversToDB ( ydoc : Y . Doc , repoId : string , blobDir : string ) {
96
+ function setupObserversToDB ( ydoc : Y . Doc , repoId : string , repoDir : string ) {
68
97
console . log ( "setupObserversToDB for repo" , repoId ) ;
69
98
// just observe and save the entire doc
70
99
function observer ( _ , transaction ) {
@@ -79,7 +108,8 @@ function setupObserversToDB(ydoc: Y.Doc, repoId: string, blobDir: string) {
79
108
// FIXME it may be too expensive to update the entire doc.
80
109
// FIXME history is discarded
81
110
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 } ) ;
83
113
} ) ;
84
114
}
85
115
const rootMap = ydoc . getMap ( "rootMap" ) ;
@@ -98,11 +128,11 @@ function setupObserversToDB(ydoc: Y.Doc, repoId: string, blobDir: string) {
98
128
/**
99
129
* This function is called when setting up the WS connection, as a first step.
100
130
*/
101
- async function loadFromFS ( ydoc : Y . Doc , repoId : string , blobDir : string ) {
131
+ async function loadFromFS ( ydoc : Y . Doc , repoId : string , repoDir : string ) {
102
132
// load from the database and write to the ydoc
103
133
console . log ( "=== loadFromFS" ) ;
104
134
// read the blob from file system
105
- const binFile = `${ blobDir } /yjs.bin` ;
135
+ const binFile = `${ repoDir } /.codepod /yjs.bin` ;
106
136
if ( fs . existsSync ( binFile ) ) {
107
137
const yDocBlob = fs . readFileSync ( binFile ) ;
108
138
Y . applyUpdate ( ydoc , yDocBlob ) ;
@@ -121,11 +151,11 @@ async function loadFromFS(ydoc: Y.Doc, repoId: string, blobDir: string) {
121
151
}
122
152
}
123
153
124
- export async function bindState ( doc : Y . Doc , repoId : string , blobDir : string ) {
154
+ export async function bindState ( doc : Y . Doc , repoId : string , repoDir : string ) {
125
155
// Load persisted document state from the database.
126
- await loadFromFS ( doc , repoId , blobDir ) ;
156
+ await loadFromFS ( doc , repoId , repoDir ) ;
127
157
// Observe changes and write to the database.
128
- setupObserversToDB ( doc , repoId , blobDir ) ;
158
+ setupObserversToDB ( doc , repoId , repoDir ) ;
129
159
// setupObserversToRuntime(doc, repoId);
130
160
// reset runtime status
131
161
// clear runtimeMap status/commands but keep the ID
0 commit comments