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

56 lines
1.2 KiB
TypeScript

import { syncronize } from "y-pojo";
import { SAction } from "../actions";
import { Y, docs } from "../entity/docs";
import { snapshot } from "../entity/snapshot";
import { ActionCtx } from "../type";
export const page_load: SAction["page"]["load"] = async function (
this: ActionCtx,
id: string
) {
let ss = snapshot.get("page", id);
let ydoc = docs.page[id];
if (!ss || !ydoc) {
const page = await db.page.findFirst({ where: { id } });
if (page) {
const doc = new Y.Doc();
let root = doc.getMap("map");
syncronize(root, { id, root: page.content_tree });
const um = new Y.UndoManager(root, { ignoreRemoteMapChanges: true });
docs.page[id] = {
doc: doc as any,
id,
um,
};
const bin = Y.encodeStateAsUpdate(doc);
snapshot.set({
bin,
id,
type: "page",
name: page.name,
ts: Date.now(),
url: page.url,
});
return {
id: id,
url: page.url,
name: page.name,
snapshot: bin,
};
}
}
if (ss) {
return {
id: ss.id,
url: ss.url,
name: ss.name,
snapshot: ss.bin,
};
}
};