Router no Async
This commit is contained in:
39
sigpro2.js
39
sigpro2.js
@@ -64,8 +64,10 @@ 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; };
|
||||||
}
|
}
|
||||||
@@ -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;
|
||||||
};
|
};
|
||||||
@@ -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