Skip to content

Commit

Permalink
save to codepod.json and codepod.bin (#575)
Browse files Browse the repository at this point in the history
  • Loading branch information
lihebi authored Oct 31, 2023
1 parent 754c0c2 commit d8e5ed8
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 10 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ Step 3: launch CodePod from terminal:
```

Open this URL in your browser to see the app. The files will be saved to the
directory `/path/to/repo/.codepod/yjs.[bin|json]`.
directory `/path/to/repo/codepod.bin|json`. The `codepod.bin` is the source of
truth, and `codepod.json` is for human-readability only.

In the future, you can update the app:

Expand Down
16 changes: 7 additions & 9 deletions api/src/yjs/yjs-blob.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,11 @@ function getDebouncedCallback(key) {
function handleSaveBlob({ repoId, yDocBlob, repoDir }) {
console.log("save blob", repoId, yDocBlob.length);
// create the yjs-blob folder if not exists
const dir = `${repoDir}/.codepod`;
if (!fs.existsSync(dir)) {
fs.mkdirSync(dir, { recursive: true });
if (!fs.existsSync(repoDir)) {
fs.mkdirSync(repoDir, { recursive: true });
}
// save the blob to file system
fs.writeFileSync(`${dir}/yjs.bin`, yDocBlob);
fs.writeFileSync(`${repoDir}/codepod.bin`, yDocBlob);
}

function handleSavePlain({ repoId, ydoc, repoDir }) {
Expand All @@ -82,11 +81,10 @@ function handleSavePlain({ repoId, ydoc, repoDir }) {
resultMap: resultMap.toJSON(),
runtimeMap: runtimeMap.toJSON(),
};
const dir = `${repoDir}/.codepod`;
if (!fs.existsSync(dir)) {
fs.mkdirSync(dir, { recursive: true });
if (!fs.existsSync(repoDir)) {
fs.mkdirSync(repoDir, { recursive: true });
}
fs.writeFileSync(`${dir}/yjs.json`, JSON.stringify(plain, null, 2));
fs.writeFileSync(`${repoDir}/codepod.json`, JSON.stringify(plain, null, 2));
}

/**
Expand Down Expand Up @@ -132,7 +130,7 @@ async function loadFromFS(ydoc: Y.Doc, repoId: string, repoDir: string) {
// load from the database and write to the ydoc
console.log("=== loadFromFS");
// read the blob from file system
const binFile = `${repoDir}/.codepod/yjs.bin`;
const binFile = `${repoDir}/codepod.bin`;
if (fs.existsSync(binFile)) {
const yDocBlob = fs.readFileSync(binFile);
Y.applyUpdate(ydoc, yDocBlob);
Expand Down

0 comments on commit d8e5ed8

Please sign in to comment.