prasi-bun/app/srv/ws/sync/actions/yjs_sv_remote.ts

26 lines
707 B
TypeScript

import { SAction } from "../actions";
import { docs } from "../entity/docs";
import { gunzipAsync, gzipAsync } from "../entity/zlib";
import { SyncConnection } from "../type";
export const yjs_sv_remote: SAction["yjs"]["sv_remote"] = async function (
this: SyncConnection,
mode,
id,
sv,
diff
) {
if (!docs[mode][id]) {
console.log(`sv_remote not found`, mode, id);
return;
}
let doc = null;
if (mode !== "code") doc = docs[mode][id].doc;
else {
doc = docs.code[id].build.site;
}
const diff_local = Y.encodeStateAsUpdate(doc as any, await gunzipAsync(sv));
Y.applyUpdate(doc as any, await gunzipAsync(diff), "local");
return { diff: await gzipAsync(diff_local) };
};