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

View File

@ -8,9 +8,6 @@ export const code_load: SAction["code"]["load"] = async function (
type type
) { ) {
const snap = await prepCodeSnapshot(site_id, "site"); 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 { validate } from "uuid";
import { ESite } from "../../../../web/src/nova/ed/logic/ed-global"; import { ESite } from "../../../../web/src/nova/ed/logic/ed-global";
import { SAction } from "../actions"; import { SAction } from "../actions";
import { prepCodeSnapshot } from "../editor/code/prep-code";
import { SyncConnection } from "../type"; 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 ( export const site_load: SAction["site"]["load"] = async function (
this: SyncConnection, this: SyncConnection,
@ -44,14 +43,8 @@ export const site_load: SAction["site"]["load"] = async function (
} }
} }
const snap = await prepCodeSnapshot(site_id, "site"); const code = 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) };
}
}
return { return {
id: site.id, id: site.id,
name: site.name, name: site.name,
@ -65,9 +58,7 @@ export const site_load: SAction["site"]["load"] = async function (
meta: undefined, meta: undefined,
entry: [], entry: [],
}, },
// code: { code_ts: code.ts,
// snapshot: compressed,
// },
}; };
} }
} }

View File

@ -21,7 +21,7 @@ export const codeBuild = async (id_site: any) => {
const src_path = code.path(id_site, "site", "src"); const src_path = code.path(id_site, "site", "src");
if (!(await existsAsync(src_path))) return; if (!(await existsAsync(src_path))) return;
if (!code.esbuild[id_site]) { 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) { 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"), res.errors.map((e) => e.text).join("\n\n"),
"site" "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();
await codeBuild(id_site); 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 = { export type CodeBuild = {
server: BuildContext | null; server: BuildContext | null;
site: BuildContext | null; site: BuildContext | null;
site_ts: number;
}; };
export type CodeMode = keyof CodeBuild; export type CodeMode = keyof CodeBuild;

View File

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

View File

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

View File

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

View File

@ -60,11 +60,13 @@ export const applyEnv = async (p: PG) => {
w.api = apiProxy(p.site.config.api_url); 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)`); const fn = new Function("callback", `import("${url}").then(callback)`);
await new Promise<void>((resolve) => { await new Promise<void>((resolve) => {
fn((exports: any) => { fn((exports: any) => {
p.site_exports = {};
for (const [k, v] of Object.entries(exports)) { for (const [k, v] of Object.entries(exports)) {
p.site_exports[k] = v;
w[k] = v; w[k] = v;
} }
resolve(); resolve();

View File

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

View File

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