wip fix script loading

This commit is contained in:
Rizky 2024-03-22 00:41:29 +07:00
parent 8b7422536c
commit dbf95954d5
14 changed files with 94 additions and 143 deletions

File diff suppressed because one or more lines are too long

View File

@ -67,12 +67,12 @@ export const SyncActions = {
bin: Uint8Array
) => ({}) as { diff: Uint8Array; sv: Uint8Array } | void,
diff_local: async (
mode: "page" | "comp" | "site" ,
mode: "page" | "comp" | "site",
id: string,
bin: Uint8Array
) => {},
sv_remote: async (
mode: "page" | "comp" | "site" ,
mode: "page" | "comp" | "site",
id: string,
sv: Uint8Array,
diff: Uint8Array
@ -86,13 +86,6 @@ export const SyncActions = {
load: async (id: string, type: "src" | "build") =>
({}) as {
id: string;
snapshot: null | Record<
string,
{
id_doc: number;
bin: Uint8Array;
}
>;
},
edit: async (
arg:

View File

@ -8,9 +8,6 @@ export const code_load: SAction["code"]["load"] = async function (
type
) {
const snap = await prepCodeSnapshot(site_id, "site");
if (snap && snap.type === "code") {
return { id: site_id, snapshot: snap.build };
}
return { id: site_id, snapshot: null };
return { id: site_id };
};

View File

@ -1,9 +1,8 @@
import { validate } from "uuid";
import { ESite } from "../../../../web/src/nova/ed/logic/ed-global";
import { SAction } from "../actions";
import { prepCodeSnapshot } from "../editor/code/prep-code";
import { SyncConnection } from "../type";
import { gzipAsync } from "../entity/zlib";
import { prepCodeSnapshot } from "../editor/code/prep-code";
export const site_load: SAction["site"]["load"] = async function (
this: SyncConnection,
@ -44,14 +43,8 @@ export const site_load: SAction["site"]["load"] = async function (
}
}
const snap = await prepCodeSnapshot(site_id, "site");
const compressed: any = {};
if (snap) {
for (const [key, value] of Object.entries(snap.build)) {
compressed[key] = { bin: await gzipAsync(value.bin) };
}
}
const code = await prepCodeSnapshot(site.id, "site");
return {
id: site.id,
name: site.name,
@ -65,9 +58,7 @@ export const site_load: SAction["site"]["load"] = async function (
meta: undefined,
entry: [],
},
// code: {
// snapshot: compressed,
// },
code_ts: code.ts,
};
}
}

View File

@ -21,7 +21,7 @@ export const codeBuild = async (id_site: any) => {
const src_path = code.path(id_site, "site", "src");
if (!(await existsAsync(src_path))) return;
if (!code.esbuild[id_site]) {
code.esbuild[id_site] = { site: null, server: null };
code.esbuild[id_site] = { site: null, server: null, site_ts: Date.now() };
}
if (!code.esbuild[id_site].server) {
@ -193,22 +193,24 @@ if (typeof global.server_hook === "function") {
res.errors.map((e) => e.text).join("\n\n"),
"site"
);
} else {
code.esbuild[id_site].site_ts = Date.now();
const client_ids = new Set<string>();
user.active.findAll({ site_id: id_site }).forEach((e) => {
client_ids.add(e.client_id);
});
client_ids.forEach((client_id) => {
const ws = conns.get(client_id)?.ws;
if (ws) {
sendWS(ws, {
type: SyncType.Event,
event: "code_changes",
data: { ts: code.esbuild[id_site].site_ts },
});
}
});
}
const client_ids = new Set<string>();
user.active.findAll({ site_id: id_site }).forEach((e) => {
client_ids.add(e.client_id);
});
client_ids.forEach((client_id) => {
const ws = conns.get(client_id)?.ws;
if (ws) {
sendWS(ws, {
type: SyncType.Event,
event: "code_changes",
});
}
});
});
},
},

View File

@ -12,4 +12,5 @@ export const prepCodeSnapshot = async (id_site: string, mode: CodeMode) => {
.await();
await codeBuild(id_site);
return { ts: code.esbuild[id_site].site_ts };
};

View File

@ -7,6 +7,7 @@ import { dirname } from "path";
export type CodeBuild = {
server: BuildContext | null;
site: BuildContext | null;
site_ts: number;
};
export type CodeMode = keyof CodeBuild;

View File

@ -24,17 +24,7 @@ export const EmptySite = {
meta: undefined as void | Record<string, IMeta>,
entry: [] as string[],
},
// code: {
// snapshot: {} as
// | undefined
// | Record<
// string,
// {
// id_doc: number;
// bin: Uint8Array;
// }
// >,
// },
code_ts: 0,
};
export type ESite = typeof EmptySite;
@ -159,6 +149,7 @@ export const EDGlobal = {
sync: null as null | Awaited<ReturnType<typeof clientStartSync>>,
sync_assigned: false,
site: deepClone(EmptySite),
site_exports: {} as Record<string, any>,
site_dts: "",
script: {
site_types: {} as Record<string, string>,

View File

@ -182,19 +182,19 @@ export const edInitSync = (p: PG) => {
}
p.render();
},
async code_changes() {
async code_changes({ ts }) {
const w = window as any;
const url = `/prod/${
p.site.id
}/_prasi/code/index.js?ts=${Date.now()}`;
const url = `/prod/${p.site.id}/_prasi/code/index.js?ts=${ts}`;
const fn = new Function(
"callback",
`import("${url}").then(callback)`
);
await new Promise<void>((resolve) => {
fn((exports: any) => {
p.site_exports = {};
for (const [k, v] of Object.entries(exports)) {
p.site_exports[k] = v;
w[k] = v;
}
resolve();

View File

@ -110,6 +110,10 @@ export const EdScriptMonaco: FC<{}> = () => {
}
}
for (const [k, v] of Object.entries(p.site_exports)) {
values[k] = v;
}
let component = { id: "", props: {} as Record<string, FNCompDef> };
if (meta?.item.component?.id && meta.item.component.props) {
component.id = meta.item.component.id;

View File

@ -46,14 +46,16 @@ export const Root = () => {
base.route.router = router;
base.route.pages = pages;
const site_exports = await import(
`${w._prasi.basepath}/_prasi/code/index.js`.replace("//", "/")
);
if (site_exports) {
for (const [k, v] of Object.entries(site_exports)) {
(window as any)[k] = v;
}
}
const url = `${w._prasi.basepath}/_prasi/code/index.js`;
const fn = new Function("callback", `import("${url}").then(callback)`);
await new Promise<void>((resolve) => {
fn((exports: any) => {
for (const [k, v] of Object.entries(exports)) {
(w as any)[k] = v;
}
resolve();
});
});
render();
});

View File

@ -60,11 +60,13 @@ export const applyEnv = async (p: PG) => {
w.api = apiProxy(p.site.config.api_url);
}
const url = `/prod/${p.site.id}/_prasi/code/index.js`;
const url = `/prod/${p.site.id}/_prasi/code/index.js?ts=${p.site.code_ts}`;
const fn = new Function("callback", `import("${url}").then(callback)`);
await new Promise<void>((resolve) => {
fn((exports: any) => {
p.site_exports = {};
for (const [k, v] of Object.entries(exports)) {
p.site_exports[k] = v;
w[k] = v;
}
resolve();

View File

@ -2,50 +2,17 @@ import { VG } from "../render/global";
import { viLoadLegacy } from "./load-legacy";
export const viLoad = (vi: VG, arg: { site_id: string; api_url: string }) => {
vi.status = "loading";
if (vi.on_status_changes) {
vi.on_status_changes(vi.status);
}
vi.site.id = arg.site_id;
vi.site.api_url = arg.api_url;
vi.status = "loading";
if (vi.on_status_changes) {
vi.on_status_changes(vi.status);
}
vi.site.id = arg.site_id;
vi.site.api_url = arg.api_url;
if (!vi.site.api && !vi.site.db) {
if (vi.site.code.mode === "old") {
viLoadLegacy({
site: {
id: vi.site.id,
api_url: vi.site.api_url,
api: {
get() {
return vi.site.api;
},
set(val) {
vi.site.api = val;
},
},
db: {
get() {
return vi.site.db;
},
set(val) {
vi.site.db = val;
},
},
},
render: vi.render,
}).then(() => {
vi.status = "ready";
if (vi.on_status_changes) {
vi.on_status_changes(vi.status);
}
vi.render();
});
} else {
}
} else {
vi.status = "ready";
if (vi.on_status_changes) {
vi.on_status_changes(vi.status);
}
}
if (!vi.site.api && !vi.site.db) {
vi.status = "ready";
if (vi.on_status_changes) {
vi.on_status_changes(vi.status);
}
}
};

View File

@ -91,7 +91,7 @@ export const clientStartSync = async (arg: {
}
>
) => void;
code_changes: () => void;
code_changes: (arg: { ts: number }) => void;
disconnected: () => { reconnect: boolean };
opened: () => void;
shakehand: (client_id: string) => void;