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

View File

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

View File

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

View File

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

View File

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

View File

@ -82,7 +82,14 @@ export const clientStartSync = async (arg: {
id: string;
sv_local: Uint8Array;
}) => 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: {
page: Record<string, ActivityList>;
comp: Record<string, ActivityList>;