Include fx, req, recover if in actual when
All checks were successful
Deploy Docs to Synology / deploy (push) Successful in 8s

This commit is contained in:
2026-04-22 21:39:07 +02:00
parent f05511dad8
commit 482ff19adb
6 changed files with 433 additions and 151 deletions

137
dist/sigpro.esm.js vendored
View File

@@ -231,15 +231,24 @@ var watch = (sources, cb) => {
effect(); effect();
return () => dispose(effect); return () => dispose(effect);
}; };
var cleanupNode = (node) => { var cleanupNode = (node, skipLeave = false) => {
if (!node)
return;
if (node._cleanups) { if (node._cleanups) {
node._cleanups.forEach((fn) => fn()); node._cleanups.forEach((fn) => fn());
node._cleanups.clear(); node._cleanups.clear();
} }
if (node._ownerEffect) if (node._ownerEffect)
dispose(node._ownerEffect); dispose(node._ownerEffect);
if (!skipLeave && node._sig_leave) {
return node._sig_leave(() => {
if (node.childNodes) if (node.childNodes)
node.childNodes.forEach(cleanupNode); node.childNodes.forEach((n) => cleanupNode(n, true));
node.remove();
});
}
if (node.childNodes)
node.childNodes.forEach((n) => cleanupNode(n, false));
}; };
var DANGEROUS_PROTOCOL = /^\s*(javascript|data|vbscript):/i; var DANGEROUS_PROTOCOL = /^\s*(javascript|data|vbscript):/i;
var isDangerousAttr = (key) => key === "src" || key === "href" || key.startsWith("on"); var isDangerousAttr = (key) => key === "src" || key === "href" || key.startsWith("on");
@@ -410,44 +419,61 @@ var render = (renderFn) => {
destroy: () => { destroy: () => {
cleanups.forEach((fn) => fn()); cleanups.forEach((fn) => fn());
cleanupNode(container); cleanupNode(container);
if (!container._sig_leave)
container.remove(); container.remove();
} }
}; };
}; };
var when = (cond, render2, { enter, leave } = {}) => { var when = (cond, SIP, NOP = null) => {
const wrap = h("div", { style: "display:contents" }); const anchor = doc.createTextNode("");
let view = null; const root = h("div", { style: "display:contents" }, [anchor]);
const wait = (el, cb) => { let currentView = null;
if (!el) watch(() => !!(isFunc(cond) ? cond() : cond), (show) => {
return cb(); if (currentView) {
let done = false; currentView.destroy();
const finish = () => !done && (done = true, cb()); currentView = null;
el.addEventListener("transitionend", finish, { once: true });
el.addEventListener("animationend", finish, { once: true });
setTimeout(finish, 500);
};
watch(cond, (on) => {
if (on && !view) {
const el = (view = render2(render2)).container.firstChild;
wrap.appendChild(view.container);
if (enter && el) {
el.classList.add(enter);
el.clientTop;
el.classList.add(enter + "-active");
wait(el, () => el.classList.remove(enter, enter + "-active"));
}
} else if (!on && view) {
const el = view.container.firstChild;
const destroyView = () => (view.destroy(), view = null);
if (leave && el) {
el.classList.add(leave);
wait(el, destroyView);
} else {
destroyView();
} }
const content = show ? SIP : NOP;
if (content) {
currentView = render(() => isFunc(content) ? content() : content);
root.insertBefore(currentView.container, anchor);
} }
}); });
return onUnmount(() => view?.destroy()), wrap; onUnmount(() => currentView?.destroy());
return root;
};
var fx = ({ name, duration = 200, scale, slide, rotate, blur }, child) => {
const el = typeof child === "function" ? child() : child;
if (!(el instanceof Node))
return el;
if (name) {
el.style.animation = `${name}-in ${duration}ms`;
el._sig_leave = (done) => {
el.style.animation = `${name}-out ${duration}ms`;
el.addEventListener("animationend", done, { once: true });
};
return el;
}
const hasTransform = scale || slide || rotate || blur;
el.style.transition = hasTransform ? `all ${duration}ms` : "";
el.style.opacity = "0";
if (scale)
el.style.transform = "scale(0.95)";
if (slide)
el.style.transform = "translateY(-10px)";
if (rotate)
el.style.transform = "rotate(-2deg)";
if (blur)
el.style.filter = "blur(4px)";
requestAnimationFrame(() => {
el.style.opacity = "1";
el.style.transform = scale || slide || rotate || blur ? "" : "none";
});
el._sig_leave = (done) => {
el.style.opacity = "0";
el.addEventListener("transitionend", done, { once: true });
};
return el;
}; };
var each = (src, itemFn, keyFn) => { var each = (src, itemFn, keyFn) => {
const anchor = doc.createTextNode(""); const anchor = doc.createTextNode("");
@@ -514,6 +540,47 @@ router.params = $({});
router.to = (p) => window.location.hash = p.replace(/^#?\/?/, "#/"); router.to = (p) => window.location.hash = p.replace(/^#?\/?/, "#/");
router.back = () => window.history.back(); router.back = () => window.history.back();
router.path = () => window.location.hash.replace(/^#/, "") || "/"; router.path = () => window.location.hash.replace(/^#/, "") || "/";
var req = ({ url, method = "GET", headers = {} }) => {
const loading = $(false);
const error = $(null);
const data = $(null);
let controller = null;
let timeoutId = null;
const run = async (body = null) => {
controller?.abort();
clearTimeout(timeoutId);
controller = new AbortController;
timeoutId = setTimeout(() => controller.abort(), 1e4);
loading(true);
error(null);
try {
const isFormData = body instanceof FormData;
const res = await fetch(url, {
method,
headers: isFormData ? headers : { "Content-Type": "application/json", ...headers },
body: isFormData ? body : body ? JSON.stringify(body) : undefined,
signal: controller.signal
});
const text = await res.text();
const json = text ? JSON.parse(text) : null;
if (!res.ok)
throw new Error(json?.message || res.statusText);
data(json);
return json;
} catch (e) {
if (e.name !== "AbortError")
error(e.message);
throw e;
} finally {
loading(false);
clearTimeout(timeoutId);
controller = null;
timeoutId = null;
}
};
const abort = () => controller?.abort();
return { run, abort, loading, error, data };
};
var mount = (comp, target) => { var mount = (comp, target) => {
const t = typeof target === "string" ? doc.querySelector(target) : target; const t = typeof target === "string" ? doc.querySelector(target) : target;
if (!t) if (!t)
@@ -525,7 +592,7 @@ var mount = (comp, target) => {
MOUNTED_NODES.set(t, inst); MOUNTED_NODES.set(t, inst);
return inst; return inst;
}; };
var SigPro = Object.freeze({ $, $$, watch, h, when, each, router, mount, batch }); var SigPro = Object.freeze({ $, $$, watch, h, when, each, fx, router, req, mount, batch });
if (typeof window !== "undefined") { if (typeof window !== "undefined") {
Object.assign(window, SigPro); Object.assign(window, SigPro);
"a abbr article aside audio b blockquote br button canvas caption cite code col colgroup datalist dd del details dfn dialog div dl dt em embed fieldset figcaption figure footer form h1 h2 h3 h4 h5 h6 header hr i iframe img input ins kbd label legend li main mark meter nav object ol optgroup option output p picture pre progress section select slot small source span strong sub summary sup svg table tbody td template textarea tfoot th thead time tr u ul video".split(" ").forEach((tag) => { "a abbr article aside audio b blockquote br button canvas caption cite code col colgroup datalist dd del details dfn dialog div dl dt em embed fieldset figcaption figure footer form h1 h2 h3 h4 h5 h6 header hr i iframe img input ins kbd label legend li main mark meter nav object ol optgroup option output p picture pre progress section select slot small source span strong sub summary sup svg table tbody td template textarea tfoot th thead time tr u ul video".split(" ").forEach((tag) => {
@@ -536,8 +603,10 @@ export {
when, when,
watch, watch,
router, router,
req,
mount, mount,
h, h,
fx,
each, each,
batch, batch,
$$, $$,

File diff suppressed because one or more lines are too long

137
dist/sigpro.js vendored
View File

@@ -43,8 +43,10 @@
when: () => when, when: () => when,
watch: () => watch, watch: () => watch,
router: () => router, router: () => router,
req: () => req,
mount: () => mount, mount: () => mount,
h: () => h, h: () => h,
fx: () => fx,
each: () => each, each: () => each,
batch: () => batch, batch: () => batch,
$$: () => $$, $$: () => $$,
@@ -284,15 +286,24 @@
effect(); effect();
return () => dispose(effect); return () => dispose(effect);
}; };
var cleanupNode = (node) => { var cleanupNode = (node, skipLeave = false) => {
if (!node)
return;
if (node._cleanups) { if (node._cleanups) {
node._cleanups.forEach((fn) => fn()); node._cleanups.forEach((fn) => fn());
node._cleanups.clear(); node._cleanups.clear();
} }
if (node._ownerEffect) if (node._ownerEffect)
dispose(node._ownerEffect); dispose(node._ownerEffect);
if (!skipLeave && node._sig_leave) {
return node._sig_leave(() => {
if (node.childNodes) if (node.childNodes)
node.childNodes.forEach(cleanupNode); node.childNodes.forEach((n) => cleanupNode(n, true));
node.remove();
});
}
if (node.childNodes)
node.childNodes.forEach((n) => cleanupNode(n, false));
}; };
var DANGEROUS_PROTOCOL = /^\s*(javascript|data|vbscript):/i; var DANGEROUS_PROTOCOL = /^\s*(javascript|data|vbscript):/i;
var isDangerousAttr = (key) => key === "src" || key === "href" || key.startsWith("on"); var isDangerousAttr = (key) => key === "src" || key === "href" || key.startsWith("on");
@@ -463,44 +474,61 @@
destroy: () => { destroy: () => {
cleanups.forEach((fn) => fn()); cleanups.forEach((fn) => fn());
cleanupNode(container); cleanupNode(container);
if (!container._sig_leave)
container.remove(); container.remove();
} }
}; };
}; };
var when = (cond, render2, { enter, leave } = {}) => { var when = (cond, SIP, NOP = null) => {
const wrap = h("div", { style: "display:contents" }); const anchor = doc.createTextNode("");
let view = null; const root = h("div", { style: "display:contents" }, [anchor]);
const wait = (el, cb) => { let currentView = null;
if (!el) watch(() => !!(isFunc(cond) ? cond() : cond), (show) => {
return cb(); if (currentView) {
let done = false; currentView.destroy();
const finish = () => !done && (done = true, cb()); currentView = null;
el.addEventListener("transitionend", finish, { once: true });
el.addEventListener("animationend", finish, { once: true });
setTimeout(finish, 500);
};
watch(cond, (on) => {
if (on && !view) {
const el = (view = render2(render2)).container.firstChild;
wrap.appendChild(view.container);
if (enter && el) {
el.classList.add(enter);
el.clientTop;
el.classList.add(enter + "-active");
wait(el, () => el.classList.remove(enter, enter + "-active"));
}
} else if (!on && view) {
const el = view.container.firstChild;
const destroyView = () => (view.destroy(), view = null);
if (leave && el) {
el.classList.add(leave);
wait(el, destroyView);
} else {
destroyView();
} }
const content = show ? SIP : NOP;
if (content) {
currentView = render(() => isFunc(content) ? content() : content);
root.insertBefore(currentView.container, anchor);
} }
}); });
return onUnmount(() => view?.destroy()), wrap; onUnmount(() => currentView?.destroy());
return root;
};
var fx = ({ name, duration = 200, scale, slide, rotate, blur }, child) => {
const el = typeof child === "function" ? child() : child;
if (!(el instanceof Node))
return el;
if (name) {
el.style.animation = `${name}-in ${duration}ms`;
el._sig_leave = (done) => {
el.style.animation = `${name}-out ${duration}ms`;
el.addEventListener("animationend", done, { once: true });
};
return el;
}
const hasTransform = scale || slide || rotate || blur;
el.style.transition = hasTransform ? `all ${duration}ms` : "";
el.style.opacity = "0";
if (scale)
el.style.transform = "scale(0.95)";
if (slide)
el.style.transform = "translateY(-10px)";
if (rotate)
el.style.transform = "rotate(-2deg)";
if (blur)
el.style.filter = "blur(4px)";
requestAnimationFrame(() => {
el.style.opacity = "1";
el.style.transform = scale || slide || rotate || blur ? "" : "none";
});
el._sig_leave = (done) => {
el.style.opacity = "0";
el.addEventListener("transitionend", done, { once: true });
};
return el;
}; };
var each = (src, itemFn, keyFn) => { var each = (src, itemFn, keyFn) => {
const anchor = doc.createTextNode(""); const anchor = doc.createTextNode("");
@@ -567,6 +595,47 @@
router.to = (p) => window.location.hash = p.replace(/^#?\/?/, "#/"); router.to = (p) => window.location.hash = p.replace(/^#?\/?/, "#/");
router.back = () => window.history.back(); router.back = () => window.history.back();
router.path = () => window.location.hash.replace(/^#/, "") || "/"; router.path = () => window.location.hash.replace(/^#/, "") || "/";
var req = ({ url, method = "GET", headers = {} }) => {
const loading = $(false);
const error = $(null);
const data = $(null);
let controller = null;
let timeoutId = null;
const run = async (body = null) => {
controller?.abort();
clearTimeout(timeoutId);
controller = new AbortController;
timeoutId = setTimeout(() => controller.abort(), 1e4);
loading(true);
error(null);
try {
const isFormData = body instanceof FormData;
const res = await fetch(url, {
method,
headers: isFormData ? headers : { "Content-Type": "application/json", ...headers },
body: isFormData ? body : body ? JSON.stringify(body) : undefined,
signal: controller.signal
});
const text = await res.text();
const json = text ? JSON.parse(text) : null;
if (!res.ok)
throw new Error(json?.message || res.statusText);
data(json);
return json;
} catch (e) {
if (e.name !== "AbortError")
error(e.message);
throw e;
} finally {
loading(false);
clearTimeout(timeoutId);
controller = null;
timeoutId = null;
}
};
const abort = () => controller?.abort();
return { run, abort, loading, error, data };
};
var mount = (comp, target) => { var mount = (comp, target) => {
const t = typeof target === "string" ? doc.querySelector(target) : target; const t = typeof target === "string" ? doc.querySelector(target) : target;
if (!t) if (!t)
@@ -578,7 +647,7 @@
MOUNTED_NODES.set(t, inst); MOUNTED_NODES.set(t, inst);
return inst; return inst;
}; };
var SigPro = Object.freeze({ $, $$, watch, h, when, each, router, mount, batch }); var SigPro = Object.freeze({ $, $$, watch, h, when, each, fx, router, req, mount, batch });
if (typeof window !== "undefined") { if (typeof window !== "undefined") {
Object.assign(window, SigPro); Object.assign(window, SigPro);
"a abbr article aside audio b blockquote br button canvas caption cite code col colgroup datalist dd del details dfn dialog div dl dt em embed fieldset figcaption figure footer form h1 h2 h3 h4 h5 h6 header hr i iframe img input ins kbd label legend li main mark meter nav object ol optgroup option output p picture pre progress section select slot small source span strong sub summary sup svg table tbody td template textarea tfoot th thead time tr u ul video".split(" ").forEach((tag) => { "a abbr article aside audio b blockquote br button canvas caption cite code col colgroup datalist dd del details dfn dialog div dl dt em embed fieldset figcaption figure footer form h1 h2 h3 h4 h5 h6 header hr i iframe img input ins kbd label legend li main mark meter nav object ol optgroup option output p picture pre progress section select slot small source span strong sub summary sup svg table tbody td template textarea tfoot th thead time tr u ul video".split(" ").forEach((tag) => {

2
dist/sigpro.min.js vendored

File diff suppressed because one or more lines are too long

View File

@@ -43,8 +43,10 @@
when: () => when, when: () => when,
watch: () => watch, watch: () => watch,
router: () => router, router: () => router,
req: () => req,
mount: () => mount, mount: () => mount,
h: () => h, h: () => h,
fx: () => fx,
each: () => each, each: () => each,
batch: () => batch, batch: () => batch,
$$: () => $$, $$: () => $$,
@@ -284,15 +286,24 @@
effect(); effect();
return () => dispose(effect); return () => dispose(effect);
}; };
var cleanupNode = (node) => { var cleanupNode = (node, skipLeave = false) => {
if (!node)
return;
if (node._cleanups) { if (node._cleanups) {
node._cleanups.forEach((fn) => fn()); node._cleanups.forEach((fn) => fn());
node._cleanups.clear(); node._cleanups.clear();
} }
if (node._ownerEffect) if (node._ownerEffect)
dispose(node._ownerEffect); dispose(node._ownerEffect);
if (!skipLeave && node._sig_leave) {
return node._sig_leave(() => {
if (node.childNodes) if (node.childNodes)
node.childNodes.forEach(cleanupNode); node.childNodes.forEach((n) => cleanupNode(n, true));
node.remove();
});
}
if (node.childNodes)
node.childNodes.forEach((n) => cleanupNode(n, false));
}; };
var DANGEROUS_PROTOCOL = /^\s*(javascript|data|vbscript):/i; var DANGEROUS_PROTOCOL = /^\s*(javascript|data|vbscript):/i;
var isDangerousAttr = (key) => key === "src" || key === "href" || key.startsWith("on"); var isDangerousAttr = (key) => key === "src" || key === "href" || key.startsWith("on");
@@ -463,44 +474,61 @@
destroy: () => { destroy: () => {
cleanups.forEach((fn) => fn()); cleanups.forEach((fn) => fn());
cleanupNode(container); cleanupNode(container);
if (!container._sig_leave)
container.remove(); container.remove();
} }
}; };
}; };
var when = (cond, render2, { enter, leave } = {}) => { var when = (cond, SIP, NOP = null) => {
const wrap = h("div", { style: "display:contents" }); const anchor = doc.createTextNode("");
let view = null; const root = h("div", { style: "display:contents" }, [anchor]);
const wait = (el, cb) => { let currentView = null;
if (!el) watch(() => !!(isFunc(cond) ? cond() : cond), (show) => {
return cb(); if (currentView) {
let done = false; currentView.destroy();
const finish = () => !done && (done = true, cb()); currentView = null;
el.addEventListener("transitionend", finish, { once: true });
el.addEventListener("animationend", finish, { once: true });
setTimeout(finish, 500);
};
watch(cond, (on) => {
if (on && !view) {
const el = (view = render2(render2)).container.firstChild;
wrap.appendChild(view.container);
if (enter && el) {
el.classList.add(enter);
el.clientTop;
el.classList.add(enter + "-active");
wait(el, () => el.classList.remove(enter, enter + "-active"));
}
} else if (!on && view) {
const el = view.container.firstChild;
const destroyView = () => (view.destroy(), view = null);
if (leave && el) {
el.classList.add(leave);
wait(el, destroyView);
} else {
destroyView();
} }
const content = show ? SIP : NOP;
if (content) {
currentView = render(() => isFunc(content) ? content() : content);
root.insertBefore(currentView.container, anchor);
} }
}); });
return onUnmount(() => view?.destroy()), wrap; onUnmount(() => currentView?.destroy());
return root;
};
var fx = ({ name, duration = 200, scale, slide, rotate, blur }, child) => {
const el = typeof child === "function" ? child() : child;
if (!(el instanceof Node))
return el;
if (name) {
el.style.animation = `${name}-in ${duration}ms`;
el._sig_leave = (done) => {
el.style.animation = `${name}-out ${duration}ms`;
el.addEventListener("animationend", done, { once: true });
};
return el;
}
const hasTransform = scale || slide || rotate || blur;
el.style.transition = hasTransform ? `all ${duration}ms` : "";
el.style.opacity = "0";
if (scale)
el.style.transform = "scale(0.95)";
if (slide)
el.style.transform = "translateY(-10px)";
if (rotate)
el.style.transform = "rotate(-2deg)";
if (blur)
el.style.filter = "blur(4px)";
requestAnimationFrame(() => {
el.style.opacity = "1";
el.style.transform = scale || slide || rotate || blur ? "" : "none";
});
el._sig_leave = (done) => {
el.style.opacity = "0";
el.addEventListener("transitionend", done, { once: true });
};
return el;
}; };
var each = (src, itemFn, keyFn) => { var each = (src, itemFn, keyFn) => {
const anchor = doc.createTextNode(""); const anchor = doc.createTextNode("");
@@ -567,6 +595,47 @@
router.to = (p) => window.location.hash = p.replace(/^#?\/?/, "#/"); router.to = (p) => window.location.hash = p.replace(/^#?\/?/, "#/");
router.back = () => window.history.back(); router.back = () => window.history.back();
router.path = () => window.location.hash.replace(/^#/, "") || "/"; router.path = () => window.location.hash.replace(/^#/, "") || "/";
var req = ({ url, method = "GET", headers = {} }) => {
const loading = $(false);
const error = $(null);
const data = $(null);
let controller = null;
let timeoutId = null;
const run = async (body = null) => {
controller?.abort();
clearTimeout(timeoutId);
controller = new AbortController;
timeoutId = setTimeout(() => controller.abort(), 1e4);
loading(true);
error(null);
try {
const isFormData = body instanceof FormData;
const res = await fetch(url, {
method,
headers: isFormData ? headers : { "Content-Type": "application/json", ...headers },
body: isFormData ? body : body ? JSON.stringify(body) : undefined,
signal: controller.signal
});
const text = await res.text();
const json = text ? JSON.parse(text) : null;
if (!res.ok)
throw new Error(json?.message || res.statusText);
data(json);
return json;
} catch (e) {
if (e.name !== "AbortError")
error(e.message);
throw e;
} finally {
loading(false);
clearTimeout(timeoutId);
controller = null;
timeoutId = null;
}
};
const abort = () => controller?.abort();
return { run, abort, loading, error, data };
};
var mount = (comp, target) => { var mount = (comp, target) => {
const t = typeof target === "string" ? doc.querySelector(target) : target; const t = typeof target === "string" ? doc.querySelector(target) : target;
if (!t) if (!t)
@@ -578,7 +647,7 @@
MOUNTED_NODES.set(t, inst); MOUNTED_NODES.set(t, inst);
return inst; return inst;
}; };
var SigPro = Object.freeze({ $, $$, watch, h, when, each, router, mount, batch }); var SigPro = Object.freeze({ $, $$, watch, h, when, each, fx, router, req, mount, batch });
if (typeof window !== "undefined") { if (typeof window !== "undefined") {
Object.assign(window, SigPro); Object.assign(window, SigPro);
"a abbr article aside audio b blockquote br button canvas caption cite code col colgroup datalist dd del details dfn dialog div dl dt em embed fieldset figcaption figure footer form h1 h2 h3 h4 h5 h6 header hr i iframe img input ins kbd label legend li main mark meter nav object ol optgroup option output p picture pre progress section select slot small source span strong sub summary sup svg table tbody td template textarea tfoot th thead time tr u ul video".split(" ").forEach((tag) => { "a abbr article aside audio b blockquote br button canvas caption cite code col colgroup datalist dd del details dfn dialog div dl dt em embed fieldset figcaption figure footer form h1 h2 h3 h4 h5 h6 header hr i iframe img input ins kbd label legend li main mark meter nav object ol optgroup option output p picture pre progress section select slot small source span strong sub summary sup svg table tbody td template textarea tfoot th thead time tr u ul video".split(" ").forEach((tag) => {

151
sigpro.js
View File

@@ -1,4 +1,4 @@
// sigpro 1.2.15 // sigpro 1.2.16
const isFunc = f => typeof f === "function" const isFunc = f => typeof f === "function"
const isObj = o => o && typeof o === "object" const isObj = o => o && typeof o === "object"
const isArr = Array.isArray const isArr = Array.isArray
@@ -222,14 +222,21 @@ const watch = (sources, cb) => {
return () => dispose(effect) return () => dispose(effect)
} }
const cleanupNode = node => { const cleanupNode = (node, skipLeave = false) => {
if (!node) return;
if (node._cleanups) { if (node._cleanups) {
node._cleanups.forEach(fn => fn()) node._cleanups.forEach(fn => fn());
node._cleanups.clear() node._cleanups.clear();
} }
if (node._ownerEffect) dispose(node._ownerEffect) if (node._ownerEffect) dispose(node._ownerEffect);
if (node.childNodes) node.childNodes.forEach(cleanupNode) if (!skipLeave && node._sig_leave) {
return node._sig_leave(() => {
if (node.childNodes) node.childNodes.forEach(n => cleanupNode(n, true));
node.remove();
});
} }
if (node.childNodes) node.childNodes.forEach(n => cleanupNode(n, false));
};
const DANGEROUS_PROTOCOL = /^\s*(javascript|data|vbscript):/i const DANGEROUS_PROTOCOL = /^\s*(javascript|data|vbscript):/i
const isDangerousAttr = key => key === 'src' || key === 'href' || key.startsWith('on') const isDangerousAttr = key => key === 'src' || key === 'href' || key.startsWith('on')
@@ -398,48 +405,70 @@ const render = renderFn => {
destroy: () => { destroy: () => {
cleanups.forEach(fn => fn()) cleanups.forEach(fn => fn())
cleanupNode(container) cleanupNode(container)
container.remove() if (!container._sig_leave) container.remove()
} }
} }
} }
const when = (cond, render, { enter, leave } = {}) => { const when = (cond, SIP, NOP = null) => {
const wrap = h('div', { style: 'display:contents' }) const anchor = doc.createTextNode("")
let view = null const root = h("div", { style: "display:contents" }, [anchor])
let currentView = null
const wait = (el, cb) => { watch(
if (!el) return cb() () => !!(isFunc(cond) ? cond() : cond),
let done = false show => {
const finish = () => !done && (done = true, cb()) if (currentView) {
el.addEventListener('transitionend', finish, { once: true }) currentView.destroy()
el.addEventListener('animationend', finish, { once: true }) currentView = null
setTimeout(finish, 500)
} }
watch(cond, on => { const content = show ? SIP : NOP
if (on && !view) { if (content) {
const el = (view = render(render)).container.firstChild currentView = render(() => isFunc(content) ? content() : content)
wrap.appendChild(view.container) root.insertBefore(currentView.container, anchor)
if (enter && el) {
el.classList.add(enter); el.clientTop
el.classList.add(enter + '-active')
wait(el, () => el.classList.remove(enter, enter + '-active'))
}
} else if (!on && view) {
const el = view.container.firstChild
const destroyView = () => (view.destroy(), view = null)
if (leave && el) {
el.classList.add(leave)
wait(el, destroyView)
} else {
destroyView()
} }
} }
}) )
return onUnmount(() => view?.destroy()), wrap onUnmount(() => currentView?.destroy())
return root
} }
const fx = ({ name, duration = 200, scale, slide, rotate, blur }, child) => {
const el = typeof child === 'function' ? child() : child;
if (!(el instanceof Node)) return el;
if (name) {
el.style.animation = `${name}-in ${duration}ms`;
el._sig_leave = (done) => {
el.style.animation = `${name}-out ${duration}ms`;
el.addEventListener('animationend', done, { once: true });
};
return el;
}
const hasTransform = scale || slide || rotate || blur;
el.style.transition = hasTransform ? `all ${duration}ms` : '';
el.style.opacity = '0';
if (scale) el.style.transform = 'scale(0.95)';
if (slide) el.style.transform = 'translateY(-10px)';
if (rotate) el.style.transform = 'rotate(-2deg)';
if (blur) el.style.filter = 'blur(4px)';
requestAnimationFrame(() => {
el.style.opacity = '1';
el.style.transform = scale || slide || rotate || blur ? '' : 'none';
});
el._sig_leave = (done) => {
el.style.opacity = '0';
el.addEventListener('transitionend', done, { once: true });
};
return el;
};
const each = (src, itemFn, keyFn) => { const each = (src, itemFn, keyFn) => {
const anchor = doc.createTextNode("") const anchor = doc.createTextNode("")
const root = h("div", { style: "display:contents" }, [anchor]) const root = h("div", { style: "display:contents" }, [anchor])
@@ -503,6 +532,52 @@ router.to = p => window.location.hash = p.replace(/^#?\/?/, "#/")
router.back = () => window.history.back() router.back = () => window.history.back()
router.path = () => window.location.hash.replace(/^#/, "") || "/" router.path = () => window.location.hash.replace(/^#/, "") || "/"
const req = ({ url, method = 'GET', headers = {} }) => {
const loading = $(false);
const error = $(null);
const data = $(null);
let controller = null;
let timeoutId = null;
const run = async (body = null) => {
controller?.abort();
clearTimeout(timeoutId);
controller = new AbortController();
timeoutId = setTimeout(() => controller.abort(), 10000);
loading(true);
error(null);
try {
const isFormData = body instanceof FormData;
const res = await fetch(url, {
method,
headers: isFormData ? headers : { 'Content-Type': 'application/json', ...headers },
body: isFormData ? body : (body ? JSON.stringify(body) : undefined),
signal: controller.signal
});
const text = await res.text();
const json = text ? JSON.parse(text) : null;
if (!res.ok) throw new Error(json?.message || res.statusText);
data(json);
return json;
} catch (e) {
if (e.name !== 'AbortError') error(e.message);
throw e;
} finally {
loading(false);
clearTimeout(timeoutId);
controller = null;
timeoutId = null;
}
};
const abort = () => controller?.abort();
return { run, abort, loading, error, data };
};
const mount = (comp, target) => { const mount = (comp, target) => {
const t = typeof target === "string" ? doc.querySelector(target) : target const t = typeof target === "string" ? doc.querySelector(target) : target
if (!t) return if (!t) return
@@ -513,7 +588,7 @@ const mount = (comp, target) => {
return inst return inst
} }
const SigPro = Object.freeze({ $, $$, watch, h, when, each, router, mount, batch }) const SigPro = Object.freeze({ $, $$, watch, h, when, each, fx, router, req, mount, batch })
if (typeof window !== "undefined") { if (typeof window !== "undefined") {
Object.assign(window, SigPro) Object.assign(window, SigPro)
@@ -523,4 +598,4 @@ if (typeof window !== "undefined") {
}) })
} }
export { $, $$, watch, h, when, each, router, mount, batch } export { $, $$, watch, h, when, each, fx, router, req, mount, batch }