This commit is contained in:
Rizky 2023-10-28 08:54:50 +07:00
parent 058c84ad23
commit 0fc538b14e
7 changed files with 43 additions and 29 deletions

View File

@ -3,7 +3,7 @@ export const SyncActionDefinition = {
"list": "0", "list": "0",
"group": "1", "group": "1",
"load": "2", "load": "2",
"js": "3" "update": "3"
}, },
"comp": { "comp": {
"new": "4", "new": "4",
@ -30,7 +30,7 @@ export const SyncActionPaths = {
"0": "site.list", "0": "site.list",
"1": "site.group", "1": "site.group",
"2": "site.load", "2": "site.load",
"3": "site.js", "3": "site.update",
"4": "comp.new", "4": "comp.new",
"5": "comp.list", "5": "comp.list",
"6": "comp.group", "6": "comp.group",

View File

@ -23,10 +23,14 @@ export const SyncActions = {
({}) as Record<string, { id: string; name: string; domain: string }>, ({}) as Record<string, { id: string; name: string; domain: string }>,
group: async () => ({}) as ReturnType<typeof site_group>, group: async () => ({}) as ReturnType<typeof site_group>,
load: async (id: string) => ({}) as ESite | void, load: async (id: string) => ({}) as ESite | void,
js: async ( update: async (
id: string, id: string,
js_compressed: Uint8Array, site: Partial<
built_compressed: Uint8Array Omit<ESite, "js" | "js_compiled"> & {
js: Buffer;
js_compiled: Buffer;
}
>
) => {}, ) => {},
}, },
comp: { comp: {

View File

@ -1,6 +1,6 @@
export * from "./activity"; export * from "./activity";
export * from "./client_info"; export * from "./client_info";
export * from "./site_js"; export * from "./site_update";
export * from "./site_load"; export * from "./site_load";
export * from "./site_group"; export * from "./site_group";
export * from "./page_load"; export * from "./page_load";

View File

@ -7,21 +7,24 @@ import { conns } from "../entity/conn";
import { sendWS } from "../sync-handler"; import { sendWS } from "../sync-handler";
const decoder = new TextDecoder(); const decoder = new TextDecoder();
export const site_js: SAction["site"]["js"] = async function ( export const site_update: SAction["site"]["update"] = async function (
this: SyncConnection, this: SyncConnection,
id: string, id: string,
js_compressed, site
built_compressed
) { ) {
if (validate(id)) { if (validate(id)) {
const js = decoder.decode(await gunzipAsync(js_compressed)); const updated = {} as any;
const js_compiled = decoder.decode(await gunzipAsync(built_compressed)); for (const [key, value] of Object.entries(site)) {
if (key === "js" || key === "js_compiled") {
updated[key] = decoder.decode(await gunzipAsync(value as any));
} else {
updated[key] = value;
}
}
await db.site.update({ await db.site.update({
where: { id }, where: { id },
data: { data: updated,
js,
js_compiled,
},
}); });
user.active.findAll({ site_id: id }).map((e) => { user.active.findAll({ site_id: id }).map((e) => {
@ -31,7 +34,7 @@ export const site_js: SAction["site"]["js"] = async function (
sendWS(ws, { sendWS(ws, {
type: SyncType.Event, type: SyncType.Event,
event: "site_js_updated", event: "site_js_updated",
data: { js: js_compressed, jsc: built_compressed }, data: site,
}); });
} }
}); });

View File

@ -108,12 +108,14 @@ export const edInitSync = (p: PG) => {
p.site = site; p.site = site;
p.render(); p.render();
}, },
site_js_updated(arg) { site_js_updated(site) {
const js = decoder.decode(decompress(arg.js)); for (const [k, v] of Object.entries(site)) {
const jsc = decoder.decode(decompress(arg.jsc)); if (k === "js" || k === "js_compiled") {
p.site[k] = decoder.decode(decompress(v as any));
p.site.js = js; } else {
p.site.js_compiled = jsc; (p.site as any)[k] = v;
}
}
p.render(); p.render();
}, },
async remote_svlocal(data) { async remote_svlocal(data) {

View File

@ -40,15 +40,13 @@ export const EdScriptSite = () => {
p.site.js = src; p.site.js = src;
p.site.js_compiled = built; p.site.js_compiled = built;
// todo: re-run site js
clearTimeout(local.timeout); clearTimeout(local.timeout);
local.timeout = setTimeout(async () => { local.timeout = setTimeout(async () => {
await p.sync.site.js( await p.sync.site.update(p.site.id, {
p.site.id, js: Buffer.from(compress(src)),
Buffer.from(compress(src)), js_compiled: Buffer.from(compress(built)),
Buffer.from(compress(built)) });
);
}, 1000); }, 1000);
} }
}, },

View File

@ -82,7 +82,14 @@ export const clientStartSync = async (arg: {
id: string; id: string;
sv_local: Uint8Array; sv_local: Uint8Array;
}) => void; }) => void;
site_js_updated: (arg: { js: Uint8Array; jsc: Uint8Array }) => void; site_js_updated: (
arg: Partial<
Omit<ESite, "js" | "js_compiled"> & {
js: Uint8Array;
js_compiled: Uint8Array;
}
>
) => void;
activity: (arg: { activity: (arg: {
page: Record<string, ActivityList>; page: Record<string, ActivityList>;
comp: Record<string, ActivityList>; comp: Record<string, ActivityList>;