Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit b5490bb

Browse files
authored
save a plain version of yjs content (#572)
1 parent e383874 commit b5490bb

File tree

4 files changed

+49
-19
lines changed

4 files changed

+49
-19
lines changed

api/src/cli.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ program
1515
.action(function (repoPath) {
1616
console.log("repoPath", repoPath);
1717
// start the server
18-
startServer({ port: 4001, blobDir: repoPath });
18+
startServer({ port: 4001, repoDir: repoPath });
1919
});
2020

2121
program.parse(process.argv);

api/src/run.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { startServer } from "./server";
22

3-
const blobDir = `${process.cwd()}/example-repo`;
4-
console.log("blobDir", blobDir);
3+
const repoDir = `${process.cwd()}/example-repo`;
4+
console.log("repoDir", repoDir);
55

6-
startServer({ port: 4000, blobDir });
6+
startServer({ port: 4000, repoDir });

api/src/server.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import { bindState, writeState } from "./yjs/yjs-blob";
1010
import cors from "cors";
1111
import { createSpawnerRouter, router } from "./spawner/trpc";
1212

13-
export async function startServer({ port, blobDir }) {
13+
export async function startServer({ port, repoDir }) {
1414
console.log("starting server ..");
1515
const app = express();
1616
app.use(express.json({ limit: "20mb" }));
@@ -39,7 +39,7 @@ export async function startServer({ port, blobDir }) {
3939

4040
wss.on("connection", (...args) =>
4141
createSetupWSConnection(
42-
(doc, repoId) => bindState(doc, repoId, blobDir),
42+
(doc, repoId) => bindState(doc, repoId, repoDir),
4343
writeState
4444
)(...args)
4545
);

api/src/yjs/yjs-blob.ts

Lines changed: 43 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)