fix y-pojo

This commit is contained in:
Rizky 2023-11-04 03:09:16 +07:00
parent cf01c62c88
commit 763c982946
17 changed files with 120 additions and 115 deletions

View File

@ -1,4 +1,3 @@
import { syncronize } from "y-pojo";
import { IItem, MItem } from "../../web/src/utils/types/item";
import { eg } from "../ws/edit/edit-global";

View File

@ -1,4 +1,3 @@
import { syncronize } from "y-pojo";
import { MPage } from "../../web/src/utils/types/general";
import { WS_MSG_SET_PAGE, WS_MSG_SV_LOCAL } from "../../web/src/utils/types/ws";
import { eg } from "../ws/edit/edit-global";

View File

@ -3,6 +3,7 @@ import { ExecaChildProcess } from "execa";
declare global {
const Y: typeof Y;
const syncronize: typeof Y.syncronize;
}
export const glb = global as unknown as {

View File

@ -1,6 +1,5 @@
import { ServerWebSocket } from "bun";
import { validate } from "uuid";
import { syncronize } from "y-pojo";
import { WSData } from "../../../../../pkgs/core/server/create";
import {
WS_MSG_GET_COMP,

View File

@ -1,6 +1,5 @@
import { ServerWebSocket } from "bun";
import { validate } from "uuid";
import { syncronize } from "y-pojo";
import { WSData } from "../../../../../pkgs/core/server/create";
import { MPage } from "../../../../web/src/utils/types/general";
import {
@ -56,5 +55,5 @@ export const getPage = async (
type: "set_page",
changes: Y.encodeStateAsUpdate(page.doc as any).toString(),
};
sendWS(ws,JSON.stringify(sent));
sendWS(ws, JSON.stringify(sent));
};

View File

@ -0,0 +1,93 @@
import { createId } from "@paralleldrive/cuid2";
import { WebSocketHandler } from "bun";
import { WSData } from "../../../../pkgs/core/server/create";
import { WS_MSG } from "../../../web/src/utils/types/ws";
import { diffLocal } from "./action/diff-local";
import { getComp } from "./action/get-comp";
import { getPage } from "./action/get-page";
import { svLocal } from "./action/sv-local";
import { svdiffRemote } from "./action/svdiff-remote";
import { redo, undo } from "./action/undo-redo";
import { eg } from "./edit-global";
import { sendWS } from "./send";
const site = {
saveTimeout: null as any,
};
export const editHandler: WebSocketHandler<WSData> = {
open(ws) {
eg.edit.ws.set(ws, {
clientID: createId(),
});
},
async message(ws, raw) {
if (typeof raw === "string") {
try {
const msg = JSON.parse(raw) as WS_MSG;
if (msg.type === "ping") {
sendWS(ws, JSON.stringify({ type: "pong" }));
return;
}
switch (msg.type) {
case "site-js":
clearTimeout(site.saveTimeout);
site.saveTimeout = setTimeout(async () => {
const js = JSON.parse(msg.src);
await db.site.update({
where: {
id: msg.id_site,
},
data: {
js: js.src,
js_compiled: js.compiled,
},
});
}, 1000);
break;
case "get_page":
await getPage(ws, msg);
break;
case "get_comp":
await getComp(ws, msg);
break;
case "sv_local":
await svLocal(ws, msg);
break;
case "diff_local":
await diffLocal(ws, msg);
break;
case "svd_remote":
await svdiffRemote(ws, msg);
break;
case "undo":
undo(ws, msg);
break;
case "redo":
redo(ws, msg);
break;
}
} catch (e) {
console.log(e);
}
}
},
close(ws, code, reason) {
eg.edit.ws.delete(ws);
for (const page of Object.values(eg.edit.page)) {
page.ws.delete(ws);
}
for (const site of Object.values(eg.edit.site)) {
site.ws.delete(ws);
}
for (const comp of Object.values(eg.edit.comp)) {
comp.ws.delete(ws);
}
},
drain(ws) {},
};

View File

@ -1,104 +1,17 @@
import { createId } from "@paralleldrive/cuid2";
import { WebSocketHandler } from "bun";
import { WSData } from "../../../pkgs/core/server/create";
import { WS_MSG } from "../../web/src/utils/types/ws";
import { diffLocal } from "./edit/action/diff-local";
import { getComp } from "./edit/action/get-comp";
import { getPage } from "./edit/action/get-page";
import { svLocal } from "./edit/action/sv-local";
import { svdiffRemote } from "./edit/action/svdiff-remote";
import { redo, undo } from "./edit/action/undo-redo";
import { eg } from "./edit/edit-global";
import { sendWS } from "./edit/send";
import { syncHandler } from "./sync/sync-handler";
import { editHandler } from "./edit/edit-handler";
(globalThis as any).Y = Y;
eg.edit = {
site: {},
comp: {},
page: {},
ws: new WeakMap(),
};
const site = {
saveTimeout: null as any,
};
export const wsHandler: Record<string, WebSocketHandler<WSData>> = {
"/sync": syncHandler,
"/edit": {
open(ws) {
eg.edit.ws.set(ws, {
clientID: createId(),
});
},
async message(ws, raw) {
if (typeof raw === "string") {
try {
const msg = JSON.parse(raw) as WS_MSG;
if (msg.type === "ping") {
sendWS(ws, JSON.stringify({ type: "pong" }));
return;
}
switch (msg.type) {
case "site-js":
clearTimeout(site.saveTimeout);
site.saveTimeout = setTimeout(async () => {
const js = JSON.parse(msg.src);
await db.site.update({
where: {
id: msg.id_site,
},
data: {
js: js.src,
js_compiled: js.compiled,
},
});
}, 1000);
break;
case "get_page":
await getPage(ws, msg);
break;
case "get_comp":
await getComp(ws, msg);
break;
case "sv_local":
await svLocal(ws, msg);
break;
case "diff_local":
await diffLocal(ws, msg);
break;
case "svd_remote":
await svdiffRemote(ws, msg);
break;
case "undo":
undo(ws, msg);
break;
case "redo":
redo(ws, msg);
break;
}
} catch (e) {
console.log(e);
}
}
},
close(ws, code, reason) {
eg.edit.ws.delete(ws);
for (const page of Object.values(eg.edit.page)) {
page.ws.delete(ws);
}
for (const site of Object.values(eg.edit.site)) {
site.ws.delete(ws);
}
for (const comp of Object.values(eg.edit.comp)) {
comp.ws.delete(ws);
}
},
drain(ws) {},
},
"/edit": editHandler,
};

View File

@ -1,7 +1,6 @@
import { syncronize } from "y-pojo";
import { SAction } from "../actions";
import { conns } from "../entity/conn";
import { Y, docs } from "../entity/docs";
import { docs } from "../entity/docs";
import { snapshot } from "../entity/snapshot";
import { user } from "../entity/user";
import { gzipAsync } from "../entity/zlib";

View File

@ -1,15 +1,13 @@
import { TypedArray } from "yjs-types";
import { MContent } from "../../../../web/src/utils/types/general";
import { IItem, MItem } from "../../../../web/src/utils/types/item";
import { SAction } from "../actions";
import { docs } from "../entity/docs";
import { SyncConnection } from "../type";
import { syncronize } from "y-pojo";
import { MItem } from "../../../../web/src/utils/types/item";
import {
FMComponent,
FNComponent,
} from "../../../../web/src/utils/types/meta-fn";
import { MText } from "../../../../web/src/utils/types/text";
import { SAction } from "../actions";
import { docs } from "../entity/docs";
import { SyncConnection } from "../type";
export const comp_new: SAction["comp"]["new"] = async function (
this: SyncConnection,

View File

@ -1,7 +1,6 @@
import { syncronize } from "y-pojo";
import { SAction } from "../actions";
import { conns } from "../entity/conn";
import { Y, docs } from "../entity/docs";
import { docs } from "../entity/docs";
import { snapshot } from "../entity/snapshot";
import { user } from "../entity/user";
import { gzipAsync } from "../entity/zlib";
@ -13,7 +12,7 @@ export const page_load: SAction["page"]["load"] = async function (
id: string
) {
let snap = snapshot.get("page", id);
let ydoc = docs.page[id];
let ydoc = docs.page[id];
const conf = this.conf;
if (!conf) return undefined;

View File

@ -1,5 +1,5 @@
import { SAction } from "../actions";
import { Y, docs } from "../entity/docs";
import { docs } from "../entity/docs";
import { gunzipAsync } from "../entity/zlib";
import { SyncConnection } from "../type";

View File

@ -1,5 +1,5 @@
import { SAction } from "../actions";
import { Y, docs } from "../entity/docs";
import { docs } from "../entity/docs";
import { gunzipAsync, gzipAsync } from "../entity/zlib";
import { SyncConnection } from "../type";

View File

@ -1,5 +1,5 @@
import { SAction } from "../actions";
import { Y, docs } from "../entity/docs";
import { docs } from "../entity/docs";
import { gunzipAsync, gzipAsync } from "../entity/zlib";
import { SyncConnection } from "../type";

4
app/srv/y.d.ts vendored
View File

@ -1,4 +1,6 @@
import type * as Y from "yjs";
import { syncronize } from "y-pojo";
export import Doc = Y.Doc;
export import UndoManager = Y.UndoManager;
export import applyUpdate = Y.applyUpdate;
@ -7,4 +9,6 @@ export import encodeStateAsUpdate = Y.encodeStateAsUpdate;
export import Text = Y.Text;
export import Map = Y.Map;
export import Array = Y.Array;
export import syncronize = syncronize;
export as namespace Y;

View File

@ -1,7 +1,6 @@
import { parcelBuild } from "utils/parcel";
import { generateAPIFrm } from "./server/api-frm";
import { prepareApiRoutes } from "./server/api-scan";
import { createServer } from "./server/create";
import { prepareAPITypes } from "./server/prep-api-ts";
import { startDevWatcher } from "./utils/dev-watcher";
import { ensureNotRunning } from "./utils/ensure";
@ -17,6 +16,7 @@ g.status = "init";
if (!g.Y) {
g.Y = await import("yjs");
g.syncronize = (await import("y-pojo")).syncronize;
await createLogger();
g.api = {};
@ -65,7 +65,7 @@ if (!g.apiPrepared) {
if (!g.parcel) {
await parcelBuild();
}
const { createServer } = await import("./server/create");
await createServer();
g.status = "ready";

View File

@ -1,7 +1,6 @@
import { WebSocketHandler } from "bun";
import { lookup } from "mime-types";
import { createRouter } from "radix3";
import { wsHandler } from "../../../app/srv/ws/handler";
import { dir } from "../utils/dir";
import { g } from "../utils/global";
import { serveAPI } from "./serve-api";
@ -26,6 +25,7 @@ export const createServer = async () => {
g.router.insert(route.url.replace(/\*/gi, "**"), route);
}
const { wsHandler } = await import("../../../app/srv/ws/handler");
g.server = Bun.serve({
port: g.port,
websocket: {

View File

@ -1,10 +1,11 @@
import { Server, Subprocess } from "bun";
import { Logger } from "pino";
import { RadixRouter } from "radix3";
import { PrismaClient } from "../../../app/db/db";
import { syncronize } from "y-pojo";
import type * as Y from "yjs";
import { PrismaClient } from "../../../app/db/db";
type SingleRoute = {
type SingleRoute = {
url: string;
args: string[];
fn: (...arg: any[]) => Promise<any>;
@ -30,4 +31,5 @@ export const g = global as unknown as {
parcel: Subprocess;
apiPrepared: boolean;
Y: typeof Y;
syncronize: typeof syncronize;
};