fix prop option + frontend recursive watch

This commit is contained in:
rizrmd 2024-06-03 11:38:50 +07:00
parent c12a14bd8a
commit 1a71697266
2 changed files with 51 additions and 32 deletions

View File

@ -108,22 +108,29 @@ export const initFrontEnd = async (
ctx: build_ctx, ctx: build_ctx,
timeout: null, timeout: null,
rebuilding: false, rebuilding: false,
watch: watch(dir.data(root), async (event, filename) => { watch: watch(
const fe = code.internal.frontend[id_site]; dir.data(root),
if ( {
fe && recursive: true,
(filename?.endsWith(".tsx") || },
filename?.endsWith(".ts") || async (event, filename) => {
filename?.endsWith(".css") || const fe = code.internal.frontend[id_site];
filename?.endsWith(".html")) if (filename?.startsWith("node_modules")) return;
) { if (
if (!fe.rebuilding) { fe &&
fe.rebuilding = true; (filename?.endsWith(".tsx") ||
await fe.ctx.rebuild(); filename?.endsWith(".ts") ||
fe.rebuilding = false; filename?.endsWith(".css") ||
filename?.endsWith(".html"))
) {
if (!fe.rebuilding) {
fe.rebuilding = true;
await fe.ctx.rebuild();
fe.rebuilding = false;
}
} }
} }
}), ),
}; };
const fe = code.internal.frontend[id_site]; const fe = code.internal.frontend[id_site];
fe.rebuilding = true; fe.rebuilding = true;

View File

@ -41,6 +41,7 @@ export const EdPropInstanceOptions: FC<{
width: 0, width: 0,
}, },
options: [] as MetaOption[], options: [] as MetaOption[],
optDeps: [] as any[],
}); });
const p = useGlobal(EDGlobal, "EDITOR"); const p = useGlobal(EDGlobal, "EDITOR");
@ -116,7 +117,14 @@ export const EdPropInstanceOptions: FC<{
const resOpt = ${src.endsWith(";") ? src : `${src};`} const resOpt = ${src.endsWith(";") ? src : `${src};`}
if (typeof resOpt === 'function') local.metaFn = resOpt; if (typeof resOpt === 'function') local.metaFn = resOpt;
else local.options = resOpt; else {
if (typeof resOpt === 'object' && Array.isArray(resOpt.deps) && typeof resOpt.fn === 'function') {
local.metaFn = resOpt.fn;
local.optDeps = resOpt.deps;
} else {
local.options = resOpt;
}
}
} catch(e) { console.error(e); }`; } catch(e) { console.error(e); }`;
fn = new Function(...Object.keys(arg), "local", final); fn = new Function(...Object.keys(arg), "local", final);
fn(...Object.values(arg), local); fn(...Object.values(arg), local);
@ -127,26 +135,30 @@ export const EdPropInstanceOptions: FC<{
} else { } else {
local.options = local.loaded; local.options = local.loaded;
} }
}
if (local.metaFn && !local.loaded && !local.loading) { useEffect(() => {
local.loading = true; if (local.metaFn) {
try { if (local.metaFn && !local.loaded && !local.loading) {
const res = local.metaFn(); local.loading = true;
const callback = (e: any) => { try {
local.loading = false; const res = local.metaFn();
local.loaded = e; const callback = (e: any) => {
local.render(); local.loading = false;
}; local.loaded = e;
if (res instanceof Promise) local.render();
res.then(callback).catch((e) => { };
console.error(e); if (res instanceof Promise)
}); res.then(callback).catch((e) => {
else callback(res); console.error(e);
} catch (e) { });
console.error(e); else callback(res);
} catch (e) {
console.error(e);
}
} }
} }
} }, local.optDeps);
let evalue: any = null; let evalue: any = null;
try { try {