Router no Async
This commit is contained in:
43
sigpro2.js
43
sigpro2.js
@@ -64,12 +64,14 @@ const SigPro = (() => {
|
|||||||
if (!Object.is(cache, next) || dirty) { cache = next; dirty = false; trackUpdate(subs, true); }
|
if (!Object.is(cache, next) || dirty) { cache = next; dirty = false; trackUpdate(subs, true); }
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
assign(e, { _deps: new Set(), _isComputed: true, _subs: subs, _deleted: false, markDirty: () => (dirty = true),
|
assign(e, {
|
||||||
stop: () => { e._deleted = true; clearDeps(e); subs.clear(); } });
|
_deps: new Set(), _isComputed: true, _subs: subs, _deleted: false, markDirty: () => (dirty = true),
|
||||||
|
stop: () => { e._deleted = true; clearDeps(e); subs.clear(); }
|
||||||
|
});
|
||||||
onUnmount(e.stop);
|
onUnmount(e.stop);
|
||||||
return () => { if (dirty) e(); trackUpdate(subs); return cache; };
|
return () => { if (dirty) e(); trackUpdate(subs); return cache; };
|
||||||
}
|
}
|
||||||
if (key) try { val = JSON.parse(localStorage.getItem(key)) ?? val; } catch(e){}
|
if (key) try { val = JSON.parse(localStorage.getItem(key)) ?? val; } catch (e) { }
|
||||||
return (...args) => {
|
return (...args) => {
|
||||||
if (args.length) {
|
if (args.length) {
|
||||||
const next = isFunc(args[0]) ? args[0](val) : args[0];
|
const next = isFunc(args[0]) ? args[0](val) : args[0];
|
||||||
@@ -104,8 +106,10 @@ const SigPro = (() => {
|
|||||||
currentOwner = prev;
|
currentOwner = prev;
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
assign(runner, { _deps: new Set(), _cleanups: new Set(), _deleted: false,
|
assign(runner, {
|
||||||
stop: () => { runner._deleted = true; clearDeps(runner); runCleanups(runner._cleanups); } });
|
_deps: new Set(), _cleanups: new Set(), _deleted: false,
|
||||||
|
stop: () => { runner._deleted = true; clearDeps(runner); runCleanups(runner._cleanups); }
|
||||||
|
});
|
||||||
onUnmount(runner.stop);
|
onUnmount(runner.stop);
|
||||||
runner(); return runner.stop;
|
runner(); return runner.stop;
|
||||||
};
|
};
|
||||||
@@ -161,7 +165,7 @@ const SigPro = (() => {
|
|||||||
Watch(() => {
|
Watch(() => {
|
||||||
const s = !!(isFunc(cond) ? cond() : cond);
|
const s = !!(isFunc(cond) ? cond() : cond);
|
||||||
if (s === last) return; last = s;
|
if (s === last) return; last = s;
|
||||||
const dispose = () => { if(view) { view.destroy(); view = null; } };
|
const dispose = () => { if (view) { view.destroy(); view = null; } };
|
||||||
if (view && !s && trans?.out) trans.out(view.container, dispose); else dispose();
|
if (view && !s && trans?.out) trans.out(view.container, dispose); else dispose();
|
||||||
const b = s ? t : f;
|
const b = s ? t : f;
|
||||||
if (b) {
|
if (b) {
|
||||||
@@ -197,29 +201,34 @@ const SigPro = (() => {
|
|||||||
|
|
||||||
// --- ROUTER SYSTEM ---
|
// --- ROUTER SYSTEM ---
|
||||||
const Router = (routes) => {
|
const Router = (routes) => {
|
||||||
const getHash = () => window.location.hash.replace(/^#/, "") || "/";
|
const getHash = () => window.location.hash.slice(1) || "/";
|
||||||
const path = $(getHash());
|
const path = $(getHash());
|
||||||
window.addEventListener("hashchange", () => path(getHash()));
|
window.addEventListener("hashchange", () => path(getHash()));
|
||||||
|
|
||||||
const outlet = Tag("div", { class: "router-outlet" });
|
const outlet = Tag("div", { class: "router-outlet" });
|
||||||
let currentView = null;
|
let currentView = null;
|
||||||
|
|
||||||
Watch([path], async () => {
|
Watch([path], () => {
|
||||||
const cur = path(), route = routes.find(r => {
|
const cur = path();
|
||||||
const p1 = r.path.split("/").filter(Boolean), p2 = cur.split("/").filter(Boolean);
|
const route = routes.find(r => {
|
||||||
return p1.length === p2.length && p1.every((p, i) => p.startsWith(":") || p === p2[i]);
|
const p1 = r.path.split("/").filter(Boolean);
|
||||||
|
const p2 = cur.split("/").filter(Boolean);
|
||||||
|
return p1.length === p2.length && p1.every((p, i) => p[0] === ":" || p === p2[i]);
|
||||||
}) || routes.find(r => r.path === "*");
|
}) || routes.find(r => r.path === "*");
|
||||||
|
|
||||||
if (route) {
|
if (route) {
|
||||||
let comp = route.component;
|
|
||||||
if (isFunc(comp) && comp.toString().includes('import')) comp = (await comp()).default;
|
|
||||||
const params = {};
|
|
||||||
route.path.split("/").filter(Boolean).forEach((p, i) => { if (p.startsWith(":")) params[p.slice(1)] = cur.split("/").filter(Boolean)[i]; });
|
|
||||||
currentView?.destroy();
|
currentView?.destroy();
|
||||||
|
const params = {};
|
||||||
|
route.path.split("/").filter(Boolean).forEach((p, i) => {
|
||||||
|
if (p[0] === ":") params[p.slice(1)] = cur.split("/").filter(Boolean)[i];
|
||||||
|
});
|
||||||
|
|
||||||
Router.params(params);
|
Router.params(params);
|
||||||
currentView = Render(() => isFunc(comp) ? comp(params) : comp);
|
currentView = Render(() => isFunc(route.component) ? route.component(params) : route.component);
|
||||||
outlet.appendChild(currentView.container);
|
outlet.replaceChildren(currentView.container);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
return outlet;
|
return outlet;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user