ajo 0.1.22 → 0.1.24
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/context.cjs +1 -0
- package/dist/context.js +11 -0
- package/dist/html.cjs +1 -1
- package/dist/html.js +83 -64
- package/dist/index.cjs +1 -1
- package/dist/index.js +79 -129
- package/dist/stream.cjs +1 -0
- package/dist/stream.js +24 -0
- package/package.json +15 -5
- package/readme.md +0 -438
- package/types.ts +90 -73
package/dist/context.cjs
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const o=Symbol.for("ajo.context"),r=(t,e=Symbol())=>function(...l){const n=this??c;return n?l.length?n[o][e]=l[0]:e in n[o]?n[o][e]:t:t};let c=null;const u=(...t)=>t.length?c=t[0]:c;exports.Context=o;exports.context=r;exports.current=u;
|
package/dist/context.js
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
const e = Symbol.for("ajo.context"), r = (t, o = Symbol()) => function(...l) {
|
|
2
|
+
const n = this ?? c;
|
|
3
|
+
return n ? l.length ? n[e][o] = l[0] : o in n[e] ? n[e][o] : t : t;
|
|
4
|
+
};
|
|
5
|
+
let c = null;
|
|
6
|
+
const s = (...t) => t.length ? c = t[0] : c;
|
|
7
|
+
export {
|
|
8
|
+
e as Context,
|
|
9
|
+
r as context,
|
|
10
|
+
s as current
|
|
11
|
+
};
|
package/dist/html.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const f=require("./context.cjs"),$=new Set(["area","base","br","col","command","embed","hr","img","input","keygen","link","meta","param","source","track","wbr"]),g=new Set(["key","skip","memo","ref"]),S=new Set(["nodeName","children",...g]),b=Symbol.for("ajo.args"),d=e=>e.replace(/[&<>"']/g,i=>`&#${i.charCodeAt(0)};`),o=()=>{},A=e=>[...w(e)].join(""),w=function*(e,{alloc:i=()=>"",placeholder:r=o,push:t=o}={}){yield*y(a(e,{alloc:i,placeholder:r,push:t}))},a=(e,i)=>{const r=typeof e;if(!(e==null||r==="boolean")){if(r==="string"||r==="number")return String(e);if(Array.isArray(e)){const t=[];for(e of e.flat(1/0)){const n=a(e,i);n!=null&&t.push(n)}return t.length===1?t[0]:t}if("nodeName"in e){if(typeof e.nodeName=="function")return v(e,i);const t={nodeName:e.nodeName};for(const n in e)!S.has(n)&&!n.startsWith("set:")&&(t[n]=e[n]);return"children"in e&&(t.children=a(e.children,i)),t}return String(e)}},v=({nodeName:e,fallback:i=e.fallback,...r},t)=>{if(e.src){const c=t.alloc();return t.push({id:c,src:e.src,h:r,done:!0}),t.placeholder(c,i)}const n=e.constructor.name;if(n==="GeneratorFunction")return x(e,r,t);if(n==="AsyncGeneratorFunction")return j(e,i,r,t);if(r=e(r),r instanceof Promise){if(t.push===o)return i;const c=t.alloc();return r.then(s=>t.push({id:c,h:a(s,{...t,alloc:(u=c)=>t.alloc(u)}),done:!0})),t.placeholder(c,i)}return a(r,t)},x=(e,i,r)=>{var p,m;const t={...e.attrs},n={...e.args};for(const l in i)l.startsWith("attr:")?t[l.slice(5)]=i[l]:g.has(l)||l.startsWith("set:")?t[l]=i[l]:n[l]=i[l];const c={[f.Context]:Object.assign(Object.create(null),(p=f.current())==null?void 0:p[f.Context]),[b]:n,render:o,next:o,return:o,throw:l=>{throw l}},s=e.call(c,n),u=f.current();f.current(c);try{return{...t,nodeName:e.is??"div",children:a(s.next().value,r)}}finally{(m=s.return)==null||m.call(s),f.current(u)}},j=(e,i,r,t)=>{if(t.push===o)return i;const n=t.alloc();return Promise.resolve().then(async()=>{var s;const c=e(r);t={...t,alloc:(u=n)=>t.alloc(u)};try{for(r=await c.next();!r.done;)t.push({id:n,h:a(r.value,t),done:!1}),r=await c.next();t.push({id:n,h:a(r.value,t),done:!0})}catch(u){t.push({id:n,h:a(u,t),done:!0})}finally{(s=c.return)==null||s.call(c)}}),t.placeholder(n,i)},y=function*(e){if(typeof e=="string")yield d(e);else if(Array.isArray(e))for(e of e)yield*y(e);else if("nodeName"in e){const{nodeName:i,children:r}=e;let t="";for(const n in e)S.has(n)||n.startsWith("set:")||e[n]==null||e[n]===!1||(e[n]===!0?t+=` ${n}`:t+=` ${n}="${d(String(e[n]))}"`);$.has(i)?yield`<${i}${t}>`:(yield`<${i}${t}>`,r!=null&&(yield*y(r)),yield`</${i}>`)}else yield d(String(e))};exports.html=w;exports.render=A;
|
package/dist/html.js
CHANGED
|
@@ -1,72 +1,91 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
1
|
+
import { Context as g, current as f } from "./context.js";
|
|
2
|
+
const $ = /* @__PURE__ */ new Set(["area", "base", "br", "col", "command", "embed", "hr", "img", "input", "keygen", "link", "meta", "param", "source", "track", "wbr"]), w = /* @__PURE__ */ new Set(["key", "skip", "memo", "ref"]), S = /* @__PURE__ */ new Set(["nodeName", "children", ...w]), A = Symbol.for("ajo.args"), d = (e) => e.replace(/[&<>"']/g, (i) => `&#${i.charCodeAt(0)};`), o = () => {
|
|
3
|
+
}, W = (e) => [...b(e)].join(""), b = function* (e, { alloc: i = () => "", placeholder: r = o, push: n = o } = {}) {
|
|
4
|
+
yield* y(a(e, { alloc: i, placeholder: r, push: n }));
|
|
5
|
+
}, a = (e, i) => {
|
|
6
|
+
const r = typeof e;
|
|
7
|
+
if (!(e == null || r === "boolean")) {
|
|
8
|
+
if (r === "string" || r === "number") return String(e);
|
|
9
|
+
if (Array.isArray(e)) {
|
|
10
|
+
const n = [];
|
|
11
|
+
for (e of e.flat(1 / 0)) {
|
|
12
|
+
const t = a(e, i);
|
|
13
|
+
t != null && n.push(t);
|
|
14
|
+
}
|
|
15
|
+
return n.length === 1 ? n[0] : n;
|
|
16
|
+
}
|
|
17
|
+
if ("nodeName" in e) {
|
|
18
|
+
if (typeof e.nodeName == "function") return v(e, i);
|
|
19
|
+
const n = { nodeName: e.nodeName };
|
|
20
|
+
for (const t in e) !S.has(t) && !t.startsWith("set:") && (n[t] = e[t]);
|
|
21
|
+
return "children" in e && (n.children = a(e.children, i)), n;
|
|
9
22
|
}
|
|
10
|
-
|
|
11
|
-
for (e of Array.isArray(e) ? e.flat(1 / 0) : [e]) {
|
|
12
|
-
const o = typeof e;
|
|
13
|
-
if (!(e == null || o === "boolean"))
|
|
14
|
-
if (o === "object" && "nodeName" in e)
|
|
15
|
-
if (t.h && (yield t.h, t.h = ""), typeof e.nodeName == "function")
|
|
16
|
-
if (e.nodeName.constructor.name === "GeneratorFunction") {
|
|
17
|
-
const r = Object.assign({}, e.nodeName.attrs), l = Object.assign({}, e.nodeName.args);
|
|
18
|
-
for (const a in e) {
|
|
19
|
-
const d = e[a];
|
|
20
|
-
a.startsWith("attr:") ? r[a.slice(5)] = d : u.has(a) || a.startsWith("set:") ? r[a] = d : l[a] = d;
|
|
21
|
-
}
|
|
22
|
-
r.nodeName = e.nodeName.is ?? "div", r.children = j(e.nodeName, l), yield r;
|
|
23
|
-
} else yield* N(e.nodeName(e), t, !1);
|
|
24
|
-
else yield e;
|
|
25
|
-
else t.h += e;
|
|
23
|
+
return String(e);
|
|
26
24
|
}
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
25
|
+
}, v = ({ nodeName: e, fallback: i = e.fallback, ...r }, n) => {
|
|
26
|
+
if (e.src) {
|
|
27
|
+
const c = n.alloc();
|
|
28
|
+
return n.push({ id: c, src: e.src, h: r, done: !0 }), n.placeholder(c, i);
|
|
29
|
+
}
|
|
30
|
+
const t = e.constructor.name;
|
|
31
|
+
if (t === "GeneratorFunction") return x(e, r, n);
|
|
32
|
+
if (t === "AsyncGeneratorFunction") return j(e, i, r, n);
|
|
33
|
+
if (r = e(r), r instanceof Promise) {
|
|
34
|
+
if (n.push === o) return i;
|
|
35
|
+
const c = n.alloc();
|
|
36
|
+
return r.then((s) => n.push({ id: c, h: a(s, { ...n, alloc: (u = c) => n.alloc(u) }), done: !0 })), n.placeholder(c, i);
|
|
37
|
+
}
|
|
38
|
+
return a(r, n);
|
|
39
|
+
}, x = (e, i, r) => {
|
|
40
|
+
var p, m;
|
|
41
|
+
const n = { ...e.attrs }, t = { ...e.args };
|
|
42
|
+
for (const l in i)
|
|
43
|
+
l.startsWith("attr:") ? n[l.slice(5)] = i[l] : w.has(l) || l.startsWith("set:") ? n[l] = i[l] : t[l] = i[l];
|
|
44
|
+
const c = {
|
|
45
|
+
[g]: Object.assign(/* @__PURE__ */ Object.create(null), (p = f()) == null ? void 0 : p[g]),
|
|
46
|
+
[A]: t,
|
|
47
|
+
render: o,
|
|
48
|
+
next: o,
|
|
49
|
+
return: o,
|
|
50
|
+
throw: (l) => {
|
|
51
|
+
throw l;
|
|
52
|
+
}
|
|
53
|
+
}, s = e.call(c, t), u = f();
|
|
54
|
+
f(c);
|
|
33
55
|
try {
|
|
34
|
-
|
|
35
|
-
*[Symbol.iterator]() {
|
|
36
|
-
for (; ; ) yield t;
|
|
37
|
-
},
|
|
38
|
-
[$]: t,
|
|
39
|
-
[c]: Object.create((i == null ? void 0 : i[c]) ?? null),
|
|
40
|
-
render: s,
|
|
41
|
-
queueMicrotask: s,
|
|
42
|
-
requestAnimationFrame: s,
|
|
43
|
-
effect: s,
|
|
44
|
-
cleanup: s,
|
|
45
|
-
next() {
|
|
46
|
-
if (i === n) return;
|
|
47
|
-
const l = i;
|
|
48
|
-
i = n, o = m(r.next().value), i = l;
|
|
49
|
-
},
|
|
50
|
-
throw(l) {
|
|
51
|
-
o = m(r.throw(l).value);
|
|
52
|
-
},
|
|
53
|
-
return() {
|
|
54
|
-
r.return();
|
|
55
|
-
}
|
|
56
|
-
}, t);
|
|
57
|
-
n.next();
|
|
58
|
-
} catch (r) {
|
|
59
|
-
n.throw(r);
|
|
56
|
+
return { ...n, nodeName: e.is ?? "div", children: a(s.next().value, r) };
|
|
60
57
|
} finally {
|
|
61
|
-
|
|
58
|
+
(m = s.return) == null || m.call(s), f(u);
|
|
62
59
|
}
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
const
|
|
66
|
-
return
|
|
60
|
+
}, j = (e, i, r, n) => {
|
|
61
|
+
if (n.push === o) return i;
|
|
62
|
+
const t = n.alloc();
|
|
63
|
+
return Promise.resolve().then(async () => {
|
|
64
|
+
var s;
|
|
65
|
+
const c = e(r);
|
|
66
|
+
n = { ...n, alloc: (u = t) => n.alloc(u) };
|
|
67
|
+
try {
|
|
68
|
+
for (r = await c.next(); !r.done; )
|
|
69
|
+
n.push({ id: t, h: a(r.value, n), done: !1 }), r = await c.next();
|
|
70
|
+
n.push({ id: t, h: a(r.value, n), done: !0 });
|
|
71
|
+
} catch (u) {
|
|
72
|
+
n.push({ id: t, h: a(u, n), done: !0 });
|
|
73
|
+
} finally {
|
|
74
|
+
(s = c.return) == null || s.call(c);
|
|
75
|
+
}
|
|
76
|
+
}), n.placeholder(t, i);
|
|
77
|
+
}, y = function* (e) {
|
|
78
|
+
if (typeof e == "string") yield d(e);
|
|
79
|
+
else if (Array.isArray(e)) for (e of e) yield* y(e);
|
|
80
|
+
else if ("nodeName" in e) {
|
|
81
|
+
const { nodeName: i, children: r } = e;
|
|
82
|
+
let n = "";
|
|
83
|
+
for (const t in e)
|
|
84
|
+
S.has(t) || t.startsWith("set:") || e[t] == null || e[t] === !1 || (e[t] === !0 ? n += ` ${t}` : n += ` ${t}="${d(String(e[t]))}"`);
|
|
85
|
+
$.has(i) ? yield `<${i}${n}>` : (yield `<${i}${n}>`, r != null && (yield* y(r)), yield `</${i}>`);
|
|
86
|
+
} else yield d(String(e));
|
|
67
87
|
};
|
|
68
88
|
export {
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
m as render
|
|
89
|
+
b as html,
|
|
90
|
+
W as render
|
|
72
91
|
};
|
package/dist/index.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const b=Symbol.for("ajo.key"),
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const c=require("./context.cjs"),f=Symbol.for("ajo.keyed"),b=Symbol.for("ajo.key"),j=Symbol.for("ajo.memo"),m=Symbol.for("ajo.ref"),v=Symbol.for("ajo.cache"),u=Symbol.for("ajo.generator"),o=Symbol.for("ajo.iterator"),l=Symbol.for("ajo.args"),p=t=>t.children,A=(t,r,...e)=>((r??(r={})).nodeName=t,!("children"in r)&&e.length&&(r.children=e.length==1?e[0]:e),r),y=(t,r)=>{let e=r.firstChild;for(t of x(t)){const n=O(t,r,e);e==null?g(r,n):n==e?e=e.nextSibling:g(r,n,n==e.nextSibling?e=C(r,e):e)}for(;e;){const n=e.nextSibling;r.removeChild(e),e=n}},C=(t,r)=>{const e=r.nextSibling;return g(t,r),e.nextSibling},x=function*(t){const r=typeof t;if(!(t==null||r=="boolean"))if(r==="string"||r==="number")yield String(t);else if(Array.isArray(t))for(t of t)yield*x(t);else"nodeName"in t?typeof t.nodeName=="function"?yield*E(t):yield t:yield String(t)},E=function*({nodeName:t,...r}){t.constructor.name=="GeneratorFunction"?yield N(t,r):yield*x(t(r))},N=function(t,r){const e={...t.attrs},n={...t.args};for(const i in r)i.startsWith("attr:")?e[i.slice(5)]=r[i]:i=="key"||i=="memo"||i.startsWith("set:")?e[i]=r[i]:n[i]=r[i];return{...e,nodeName:t.is??"div",skip:!0,ref:B.bind(null,t,n)}},O=(t,r,e)=>typeof t=="string"?M(t,e):T(t,r,e),M=(t,r)=>{for(;r&&r.nodeType!=3;)r=r.nextSibling;return r?r.data!=t&&(r.data=t):r=document.createTextNode(t),r},T=({nodeName:t,children:r,key:e,skip:n,memo:i,ref:w,...h},a,s)=>{if(e!=null)s=(a[f]??(a[f]=new Map)).get(e)??s;else for(;s&&s.localName!=t;)s=s.nextSibling;return s??(s=document.createElementNS(h.xmlns??a.namespaceURI,t)),e!=null&&s[b]!=e&&(s[b]!=null&&S(s),a[f].set(s[b]=e,s)),(i==null||F(s[j],s[j]=i))&&(k(s[v]??W(s),s[v]=h,s),n||y(r,s),typeof w=="function"&&(s[m]=w)(s)),s},k=(t,r,e)=>{for(const n in{...t,...r})t[n]!==r[n]&&(n.startsWith("set:")?e[n.slice(4)]=r[n]:r[n]==null||r[n]===!1?e.removeAttribute(n):e.setAttribute(n,r[n]===!0?"":r[n]))},F=(t,r)=>Array.isArray(t)&&Array.isArray(r)?t.some((e,n)=>e!==r[n]):t!==r,W=t=>Array.from(t.attributes).reduce((r,e)=>(r[e.name]=e.value,r),{}),g=(t,r,e)=>{if(!r.contains(document.activeElement))return t.insertBefore(r,e);const n=r.nextSibling;for(;e&&e!=r;){const i=e.nextSibling;t.insertBefore(e,n),e=i}},S=t=>{var r,e;for(const n of t.children)S(n);(r=t[f])==null||r.clear(),(e=t[m])==null||e.call(t,null)},B=(t,{skip:r,ref:e,...n},i)=>{i&&(i[u]??(i[u]=(R(i),t)),i[m]=q.bind(null,e,i),Object.assign(i[l]??(i[l]={}),n),r||i.next())},R=t=>{var r;Object.assign(t,G),t[c.Context]=Object.create(((r=c.current())==null?void 0:r[c.Context])??null)},q=(t,r,e)=>{typeof t=="function"&&t(e),e||r.return()},G={render(){var t;(t=c.current())!=null&&t.contains(this)||this.next()},next(){var r;const t=c.current();c.current(this);try{const{value:e,done:n}=(this[o]??(this[o]=this[u].call(this,this[l]))).next();y(e,this),(r=this[m])==null||r.call(this,this),n&&this.return()}catch(e){this.throw(e)}finally{c.current(t)}},throw(t){var r;for(let e=this;e;e=e.parentNode)if(typeof((r=e[o])==null?void 0:r.throw)=="function")try{return y(e[o].throw(t).value,e)}catch(n){t=new Error(n instanceof Error?n.message:n,{cause:t})}throw t},return(){var t;try{(t=this[o])==null||t.return()}catch(r){this.throw(r)}finally{this[o]=null}}};new MutationObserver(t=>t.forEach(r=>r.removedNodes.forEach(e=>e.isConnected||e.nodeType==1&&S(e)))).observe(document,{childList:!0,subtree:!0});exports.Fragment=p;exports.h=A;exports.render=y;
|
package/dist/index.js
CHANGED
|
@@ -1,151 +1,101 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
} else if (t instanceof Node)
|
|
8
|
-
o = t;
|
|
9
|
-
else {
|
|
10
|
-
for (; o && !(o.localName == t.nodeName && (o[b] ?? (o[b] = t.key)) == t.key); ) o = o.nextSibling;
|
|
11
|
-
if (o ?? (o = Object.assign(document.createElementNS(t.xmlns ?? e.namespaceURI, t.nodeName), { [b]: t.key })), t.memo == null || W(o[k], o[k] = t.memo)) {
|
|
12
|
-
let s, m = {}, w = o[A] ?? Array.from(o.attributes).reduce((r, N) => (r[N.name] = N.value, r), {});
|
|
13
|
-
for (const r of Object.keys(Object.assign({}, w, t)))
|
|
14
|
-
T.has(r) || w[r] === (s = m[r] = t[r]) || (r.startsWith("set:") ? o[r.slice(4)] = s : s == null || s === !1 ? o.removeAttribute(r) : o.setAttribute(r, s === !0 ? "" : s));
|
|
15
|
-
o[A] = m, t.skip || x(t.children, o), typeof t.ref == "function" && (o[S] = t.ref)(o);
|
|
16
|
-
}
|
|
17
|
-
}
|
|
18
|
-
o === n ? n = n.nextSibling : B(e, o, n);
|
|
19
|
-
}
|
|
20
|
-
for (; n && n != i; ) {
|
|
21
|
-
const o = n.nextSibling;
|
|
22
|
-
n.nodeType == 1 && C(n), e.removeChild(n), n = o;
|
|
1
|
+
import { Context as j, current as f } from "./context.js";
|
|
2
|
+
const a = Symbol.for("ajo.keyed"), y = Symbol.for("ajo.key"), p = Symbol.for("ajo.memo"), m = Symbol.for("ajo.ref"), v = Symbol.for("ajo.cache"), u = Symbol.for("ajo.generator"), o = Symbol.for("ajo.iterator"), l = Symbol.for("ajo.args"), U = (t) => t.children, q = (t, r, ...n) => ((r ?? (r = {})).nodeName = t, !("children" in r) && n.length && (r.children = n.length == 1 ? n[0] : n), r), b = (t, r) => {
|
|
3
|
+
let n = r.firstChild;
|
|
4
|
+
for (t of x(t)) {
|
|
5
|
+
const e = C(t, r, n);
|
|
6
|
+
n == null ? g(r, e) : e == n ? n = n.nextSibling : g(r, e, e == n.nextSibling ? n = A(r, n) : n);
|
|
23
7
|
}
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
if (typeof t == "object" && "nodeName" in t)
|
|
28
|
-
if (e.h && (yield e.h, e.h = ""), typeof t.nodeName == "function")
|
|
29
|
-
if (t.nodeName.constructor.name == "GeneratorFunction") {
|
|
30
|
-
const i = Object.assign({}, t.nodeName.attrs), o = Object.assign({}, t.nodeName.args);
|
|
31
|
-
for (const s in t) {
|
|
32
|
-
const m = t[s];
|
|
33
|
-
s.startsWith("attr:") ? i[s.slice(5)] = m : s == "key" || s == "memo" || s.startsWith("set:") ? i[s] = m : o[s] = m;
|
|
34
|
-
}
|
|
35
|
-
i.nodeName = t.nodeName.is ?? "div", i.skip = !0, i.ref = R.bind(null, t.nodeName, o), yield i;
|
|
36
|
-
} else yield* O(t.nodeName(t), e, !1);
|
|
37
|
-
else yield t;
|
|
38
|
-
else e.h += t;
|
|
39
|
-
n && e.h && (yield e.h);
|
|
40
|
-
}, B = (t, e, n) => {
|
|
41
|
-
if (e.contains(document.activeElement)) {
|
|
42
|
-
const i = e.nextSibling;
|
|
43
|
-
for (; n && n != e; ) {
|
|
44
|
-
const o = n.nextSibling;
|
|
45
|
-
t.insertBefore(n, i), n = o;
|
|
46
|
-
}
|
|
47
|
-
} else t.insertBefore(e, n);
|
|
48
|
-
}, C = (t) => {
|
|
49
|
-
var e;
|
|
50
|
-
for (const n of t.children) C(n);
|
|
51
|
-
(e = t[S]) == null || e.call(t, null);
|
|
52
|
-
}, g = Symbol.for("ajo.generator"), f = Symbol.for("ajo.iterator"), l = Symbol.for("ajo.args"), d = Symbol.for("ajo.context"), y = Symbol.for("ajo.effects"), c = Symbol.for("ajo.disposers"), p = Symbol.for("ajo.cleanups"), R = (t, e, n) => {
|
|
53
|
-
if (!n) return;
|
|
54
|
-
n[g] ?? (n[g] = (Object.assign(n, U), n[d] = Object.create((a == null ? void 0 : a[d]) ?? null), t));
|
|
55
|
-
const { skip: i, ref: o, ...s } = e;
|
|
56
|
-
n[S] = I.bind(null, o, n), Object.assign(n[l] ?? (n[l] = {}), s), i || n.next();
|
|
57
|
-
}, I = (t, e, n) => {
|
|
58
|
-
typeof t == "function" && t(n), n || e.return();
|
|
59
|
-
}, z = (t) => t.isConnected && t.render(), u = (t, e = y) => {
|
|
60
|
-
var n;
|
|
61
|
-
if (e == y) for (const i of t.children) u(i, e);
|
|
62
|
-
if ((n = t[e]) != null && n.size) for (const i of t[e]) {
|
|
63
|
-
t[e].delete(i);
|
|
64
|
-
try {
|
|
65
|
-
const o = i();
|
|
66
|
-
e == y && typeof o == "function" && (t[c] ?? (t[c] = /* @__PURE__ */ new Set())).add(o);
|
|
67
|
-
} catch (o) {
|
|
68
|
-
t.throw(o);
|
|
69
|
-
}
|
|
8
|
+
for (; n; ) {
|
|
9
|
+
const e = n.nextSibling;
|
|
10
|
+
r.removeChild(n), n = e;
|
|
70
11
|
}
|
|
71
|
-
},
|
|
72
|
-
|
|
73
|
-
t
|
|
74
|
-
},
|
|
75
|
-
const
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
12
|
+
}, A = (t, r) => {
|
|
13
|
+
const n = r.nextSibling;
|
|
14
|
+
return g(t, r), n.nextSibling;
|
|
15
|
+
}, x = function* (t) {
|
|
16
|
+
const r = typeof t;
|
|
17
|
+
if (!(t == null || r == "boolean"))
|
|
18
|
+
if (r === "string" || r === "number") yield String(t);
|
|
19
|
+
else if (Array.isArray(t)) for (t of t) yield* x(t);
|
|
20
|
+
else "nodeName" in t ? typeof t.nodeName == "function" ? yield* E(t) : yield t : yield String(t);
|
|
21
|
+
}, E = function* ({ nodeName: t, ...r }) {
|
|
22
|
+
t.constructor.name == "GeneratorFunction" ? yield N(t, r) : yield* x(t(r));
|
|
23
|
+
}, N = function(t, r) {
|
|
24
|
+
const n = { ...t.attrs }, e = { ...t.args };
|
|
25
|
+
for (const i in r)
|
|
26
|
+
i.startsWith("attr:") ? n[i.slice(5)] = r[i] : i == "key" || i == "memo" || i.startsWith("set:") ? n[i] = r[i] : e[i] = r[i];
|
|
27
|
+
return { ...n, nodeName: t.is ?? "div", skip: !0, ref: B.bind(null, t, e) };
|
|
28
|
+
}, C = (t, r, n) => typeof t == "string" ? O(t, n) : k(t, r, n), O = (t, r) => {
|
|
29
|
+
for (; r && r.nodeType != 3; ) r = r.nextSibling;
|
|
30
|
+
return r ? r.data != t && (r.data = t) : r = document.createTextNode(t), r;
|
|
31
|
+
}, k = ({ nodeName: t, children: r, key: n, skip: e, memo: i, ref: w, ...h }, c, s) => {
|
|
32
|
+
if (n != null) s = (c[a] ?? (c[a] = /* @__PURE__ */ new Map())).get(n) ?? s;
|
|
33
|
+
else for (; s && s.localName != t; ) s = s.nextSibling;
|
|
34
|
+
return s ?? (s = document.createElementNS(h.xmlns ?? c.namespaceURI, t)), n != null && s[y] != n && (s[y] != null && S(s), c[a].set(s[y] = n, s)), (i == null || T(s[p], s[p] = i)) && (M(s[v] ?? W(s), s[v] = h, s), e || b(r, s), typeof w == "function" && (s[m] = w)(s)), s;
|
|
35
|
+
}, M = (t, r, n) => {
|
|
36
|
+
for (const e in { ...t, ...r })
|
|
37
|
+
t[e] !== r[e] && (e.startsWith("set:") ? n[e.slice(4)] = r[e] : r[e] == null || r[e] === !1 ? n.removeAttribute(e) : n.setAttribute(e, r[e] === !0 ? "" : r[e]));
|
|
38
|
+
}, T = (t, r) => Array.isArray(t) && Array.isArray(r) ? t.some((n, e) => n !== r[e]) : t !== r, W = (t) => Array.from(t.attributes).reduce((r, n) => (r[n.name] = n.value, r), {}), g = (t, r, n) => {
|
|
39
|
+
if (!r.contains(document.activeElement)) return t.insertBefore(r, n);
|
|
40
|
+
const e = r.nextSibling;
|
|
41
|
+
for (; n && n != r; ) {
|
|
42
|
+
const i = n.nextSibling;
|
|
43
|
+
t.insertBefore(n, e), n = i;
|
|
83
44
|
}
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
45
|
+
}, S = (t) => {
|
|
46
|
+
var r, n;
|
|
47
|
+
for (const e of t.children) S(e);
|
|
48
|
+
(r = t[a]) == null || r.clear(), (n = t[m]) == null || n.call(t, null);
|
|
49
|
+
}, B = (t, { skip: r, ref: n, ...e }, i) => {
|
|
50
|
+
i && (i[u] ?? (i[u] = (F(i), t)), i[m] = R.bind(null, n, i), Object.assign(i[l] ?? (i[l] = {}), e), r || i.next());
|
|
51
|
+
}, F = (t) => {
|
|
52
|
+
var r;
|
|
53
|
+
Object.assign(t, G), t[j] = Object.create(((r = f()) == null ? void 0 : r[j]) ?? null);
|
|
54
|
+
}, R = (t, r, n) => {
|
|
55
|
+
typeof t == "function" && t(n), n || r.return();
|
|
56
|
+
}, G = {
|
|
94
57
|
render() {
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
queueMicrotask() {
|
|
98
|
-
h(E, this);
|
|
99
|
-
},
|
|
100
|
-
requestAnimationFrame() {
|
|
101
|
-
h(M, this);
|
|
102
|
-
},
|
|
103
|
-
effect(t) {
|
|
104
|
-
return v(this, y, t);
|
|
105
|
-
},
|
|
106
|
-
cleanup(t) {
|
|
107
|
-
return v(this, p, t);
|
|
58
|
+
var t;
|
|
59
|
+
(t = f()) != null && t.contains(this) || this.next();
|
|
108
60
|
},
|
|
109
61
|
next() {
|
|
110
|
-
var
|
|
111
|
-
const t =
|
|
112
|
-
|
|
62
|
+
var r;
|
|
63
|
+
const t = f();
|
|
64
|
+
f(this);
|
|
113
65
|
try {
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
this.throw(o);
|
|
66
|
+
const { value: n, done: e } = (this[o] ?? (this[o] = this[u].call(this, this[l]))).next();
|
|
67
|
+
b(n, this), (r = this[m]) == null || r.call(this, this), e && this.return();
|
|
68
|
+
} catch (n) {
|
|
69
|
+
this.throw(n);
|
|
119
70
|
} finally {
|
|
120
|
-
|
|
71
|
+
f(t);
|
|
121
72
|
}
|
|
122
73
|
},
|
|
123
74
|
throw(t) {
|
|
124
|
-
var
|
|
125
|
-
for (let n = this; n; n = n.parentNode) if (typeof ((
|
|
126
|
-
return
|
|
127
|
-
} catch (
|
|
128
|
-
t = new Error(
|
|
75
|
+
var r;
|
|
76
|
+
for (let n = this; n; n = n.parentNode) if (typeof ((r = n[o]) == null ? void 0 : r.throw) == "function") try {
|
|
77
|
+
return b(n[o].throw(t).value, n);
|
|
78
|
+
} catch (e) {
|
|
79
|
+
t = new Error(e instanceof Error ? e.message : e, { cause: t });
|
|
129
80
|
}
|
|
130
81
|
throw t;
|
|
131
82
|
},
|
|
132
83
|
return() {
|
|
133
|
-
var t
|
|
84
|
+
var t;
|
|
134
85
|
try {
|
|
135
|
-
(t = this[
|
|
136
|
-
} catch (
|
|
137
|
-
this.throw(
|
|
86
|
+
(t = this[o]) == null || t.return();
|
|
87
|
+
} catch (r) {
|
|
88
|
+
this.throw(r);
|
|
138
89
|
} finally {
|
|
139
|
-
this[
|
|
90
|
+
this[o] = null;
|
|
140
91
|
}
|
|
141
92
|
}
|
|
142
|
-
}, J = (t, e = Symbol()) => function(...n) {
|
|
143
|
-
const i = this ?? a;
|
|
144
|
-
return i ? n.length == 0 ? e in i[d] ? i[d][e] : t : i[d][e] = n[0] : t;
|
|
145
93
|
};
|
|
94
|
+
new MutationObserver(
|
|
95
|
+
(t) => t.forEach((r) => r.removedNodes.forEach((n) => n.isConnected || n.nodeType == 1 && S(n)))
|
|
96
|
+
).observe(document, { childList: !0, subtree: !0 });
|
|
146
97
|
export {
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
x as render
|
|
98
|
+
U as Fragment,
|
|
99
|
+
q as h,
|
|
100
|
+
b as render
|
|
151
101
|
};
|
package/dist/stream.cjs
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const a=require("./index.cjs"),m=require("./html.cjs"),y=async function*(o,n=""){const s=new Map([[n,0]]),t=new Set,i=[],r=(e=n)=>(s.set(e,(s.get(e)??0)+1),e?`${e}:${s.get(e)-1}`:String(s.get(e)-1)),h=(e,c)=>({nodeName:"div","data-ssr":e,children:c}),f=e=>{const c=Promise.resolve(`<script>window.$stream?.push(${JSON.stringify(e)})<\/script>`);t.add(c),c.then(u=>i.push(u)).finally(()=>t.delete(c))};for(const e of m.html(o,{alloc:r,placeholder:h,push:f}))yield e;for(;t.size||i.length;){for(;i.length;)yield i.shift();t.size&&await Promise.race(t)}},d=new Set;async function l({id:o,src:n,h:s}){const t=document.querySelector(`[data-ssr="${o}"]`);if(!t)return d.add({id:o,src:n,h:s});n?a.render(a.h((await import(n)).default,s),t):a.render(s,t);const i=o+":";for(const r of d)r.id.startsWith(i)&&(d.delete(r),l(r))}exports.hydrate=l;exports.stream=y;
|
package/dist/stream.js
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { render as l, h as m } from "./index.js";
|
|
2
|
+
import { html as u } from "./html.js";
|
|
3
|
+
const y = async function* (i, n = "") {
|
|
4
|
+
const s = /* @__PURE__ */ new Map([[n, 0]]), e = /* @__PURE__ */ new Set(), o = [], c = (t = n) => (s.set(t, (s.get(t) ?? 0) + 1), t ? `${t}:${s.get(t) - 1}` : String(s.get(t) - 1)), d = (t, r) => ({ nodeName: "div", "data-ssr": t, children: r }), f = (t) => {
|
|
5
|
+
const r = Promise.resolve(`<script>window.$stream?.push(${JSON.stringify(t)})<\/script>`);
|
|
6
|
+
e.add(r), r.then((h) => o.push(h)).finally(() => e.delete(r));
|
|
7
|
+
};
|
|
8
|
+
for (const t of u(i, { alloc: c, placeholder: d, push: f })) yield t;
|
|
9
|
+
for (; e.size || o.length; ) {
|
|
10
|
+
for (; o.length; ) yield o.shift();
|
|
11
|
+
e.size && await Promise.race(e);
|
|
12
|
+
}
|
|
13
|
+
}, a = /* @__PURE__ */ new Set();
|
|
14
|
+
async function p({ id: i, src: n, h: s }) {
|
|
15
|
+
const e = document.querySelector(`[data-ssr="${i}"]`);
|
|
16
|
+
if (!e) return a.add({ id: i, src: n, h: s });
|
|
17
|
+
n ? l(m((await import(n)).default, s), e) : l(s, e);
|
|
18
|
+
const o = i + ":";
|
|
19
|
+
for (const c of a) c.id.startsWith(o) && (a.delete(c), p(c));
|
|
20
|
+
}
|
|
21
|
+
export {
|
|
22
|
+
p as hydrate,
|
|
23
|
+
y as stream
|
|
24
|
+
};
|
package/package.json
CHANGED
|
@@ -1,21 +1,31 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "ajo",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.24",
|
|
4
4
|
"description": "ajo is a JavaScript view library for building user interfaces",
|
|
5
5
|
"type": "module",
|
|
6
|
+
"types": "./types.ts",
|
|
6
7
|
"module": "./dist/index.js",
|
|
7
8
|
"main": "./dist/index.cjs",
|
|
8
|
-
"types": "./types.ts",
|
|
9
9
|
"exports": {
|
|
10
10
|
".": {
|
|
11
11
|
"types": "./types.ts",
|
|
12
12
|
"import": "./dist/index.js",
|
|
13
13
|
"require": "./dist/index.cjs"
|
|
14
14
|
},
|
|
15
|
+
"./context": {
|
|
16
|
+
"types": "./types.ts",
|
|
17
|
+
"import": "./dist/context.js",
|
|
18
|
+
"require": "./dist/context.cjs"
|
|
19
|
+
},
|
|
15
20
|
"./html": {
|
|
16
21
|
"types": "./types.ts",
|
|
17
22
|
"import": "./dist/html.js",
|
|
18
23
|
"require": "./dist/html.cjs"
|
|
24
|
+
},
|
|
25
|
+
"./stream": {
|
|
26
|
+
"types": "./types.ts",
|
|
27
|
+
"import": "./dist/stream.js",
|
|
28
|
+
"require": "./dist/stream.cjs"
|
|
19
29
|
}
|
|
20
30
|
},
|
|
21
31
|
"files": [
|
|
@@ -23,11 +33,11 @@
|
|
|
23
33
|
"types.ts"
|
|
24
34
|
],
|
|
25
35
|
"devDependencies": {
|
|
26
|
-
"@types/node": "
|
|
27
|
-
"happy-dom": "
|
|
36
|
+
"@types/node": "24.0.0",
|
|
37
|
+
"happy-dom": "18.0.1",
|
|
28
38
|
"vite": "6.3.5",
|
|
29
39
|
"vite-tsconfig-paths": "5.1.4",
|
|
30
|
-
"vitest": "3.2.
|
|
40
|
+
"vitest": "3.2.3"
|
|
31
41
|
},
|
|
32
42
|
"keywords": [
|
|
33
43
|
"ui",
|
package/readme.md
CHANGED
|
@@ -23,444 +23,6 @@ Ajo is a lightweight and efficient JavaScript library for building dynamic user
|
|
|
23
23
|
- Lightweight design
|
|
24
24
|
- Server-Side Rendering (SSR) support
|
|
25
25
|
|
|
26
|
-
## Quick Start
|
|
27
|
-
|
|
28
|
-
### Installation
|
|
29
|
-
|
|
30
|
-
```bash
|
|
31
|
-
npm install ajo
|
|
32
|
-
```
|
|
33
|
-
|
|
34
|
-
### Basic Usage
|
|
35
|
-
|
|
36
|
-
```jsx
|
|
37
|
-
/** @jsx h */
|
|
38
|
-
import { h, render } from 'ajo'
|
|
39
|
-
|
|
40
|
-
const Greeting = ({ name }) => <h1>Hello, {name}!</h1>
|
|
41
|
-
|
|
42
|
-
function* Counter() {
|
|
43
|
-
|
|
44
|
-
let count = 0
|
|
45
|
-
|
|
46
|
-
const increment = () => {
|
|
47
|
-
count++
|
|
48
|
-
this.render()
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
while (true) yield (
|
|
52
|
-
<>
|
|
53
|
-
<p>Count: {count}</p>
|
|
54
|
-
<button set:onclick={increment}>Increment</button>
|
|
55
|
-
</>
|
|
56
|
-
)
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
function* App() {
|
|
60
|
-
while (true) yield (
|
|
61
|
-
<>
|
|
62
|
-
<Greeting name="Ajo Developer" />
|
|
63
|
-
<Counter />
|
|
64
|
-
</>
|
|
65
|
-
)
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
render(<App />, document.body)
|
|
69
|
-
```
|
|
70
|
-
|
|
71
|
-
## Core Concepts
|
|
72
|
-
|
|
73
|
-
### HTML Attributes vs DOM Properties
|
|
74
|
-
|
|
75
|
-
Ajo distinguishes between HTML attributes and DOM properties. Use regular attributes for HTML attributes, and the `set:` prefix to set DOM properties directly:
|
|
76
|
-
|
|
77
|
-
```jsx
|
|
78
|
-
<input
|
|
79
|
-
type="text"
|
|
80
|
-
id="username"
|
|
81
|
-
class="form-input"
|
|
82
|
-
placeholder="Enter username"
|
|
83
|
-
set:value={inputValue}
|
|
84
|
-
set:onclick={handleClick}
|
|
85
|
-
set:disabled={isDisabled}
|
|
86
|
-
/>
|
|
87
|
-
```
|
|
88
|
-
|
|
89
|
-
In this example:
|
|
90
|
-
- `type`, `id`, `class`, and `placeholder` are regular HTML attributes.
|
|
91
|
-
- `set:value`, `set:onclick`, and `set:disabled` are DOM properties set directly on the element.
|
|
92
|
-
|
|
93
|
-
### Special Attributes
|
|
94
|
-
|
|
95
|
-
Ajo uses special attributes for optimization and control:
|
|
96
|
-
- `key`: For efficient list rendering
|
|
97
|
-
- `skip`: To prevent rendering of child elements
|
|
98
|
-
- `memo`: For memoization of components or elements
|
|
99
|
-
- `ref`: To get references to DOM nodes or component instances
|
|
100
|
-
|
|
101
|
-
```jsx
|
|
102
|
-
<TodoItem
|
|
103
|
-
key={todo.id}
|
|
104
|
-
memo={[todo.completed]}
|
|
105
|
-
ref={el => el ? todosRefs.add(el) : todosRefs.delete(todo.id)}
|
|
106
|
-
>
|
|
107
|
-
<TodoTitle>{todo.title}</TodoTitle>
|
|
108
|
-
<div set:innerHTML={marked(todo.content)} skip></div>
|
|
109
|
-
</TodoItem>
|
|
110
|
-
```
|
|
111
|
-
|
|
112
|
-
### Stateless and Stateful Components
|
|
113
|
-
|
|
114
|
-
Stateless components are simple functions:
|
|
115
|
-
|
|
116
|
-
```jsx
|
|
117
|
-
const Greeting = ({ name }) => <h1>Hello, {name}!</h1>
|
|
118
|
-
```
|
|
119
|
-
|
|
120
|
-
Stateful components use generator functions:
|
|
121
|
-
|
|
122
|
-
```jsx
|
|
123
|
-
function* Counter() {
|
|
124
|
-
|
|
125
|
-
let count = 0
|
|
126
|
-
|
|
127
|
-
while (true) yield (
|
|
128
|
-
<button set:onclick={() => { count++; this.render(); }}>
|
|
129
|
-
{count}
|
|
130
|
-
</button>
|
|
131
|
-
)
|
|
132
|
-
}
|
|
133
|
-
```
|
|
134
|
-
|
|
135
|
-
State handling in Ajo is straightforward:
|
|
136
|
-
- State is managed using regular variables within the generator function.
|
|
137
|
-
- The `this.render()` method triggers a re-render when state changes.
|
|
138
|
-
- The `this.cleanup()` method is used to clean up resources when the component unmounts.
|
|
139
|
-
- Each iteration of the generator function represents a new render cycle.
|
|
140
|
-
|
|
141
|
-
```jsx
|
|
142
|
-
function* Timer() {
|
|
143
|
-
|
|
144
|
-
let seconds = 0
|
|
145
|
-
|
|
146
|
-
const intervalId = setInterval(() => {
|
|
147
|
-
seconds++
|
|
148
|
-
this.render() // Trigger a re-render
|
|
149
|
-
}, 1000)
|
|
150
|
-
|
|
151
|
-
this.cleanup(() => clearInterval(intervalId)) // Cleanup
|
|
152
|
-
|
|
153
|
-
while (true) yield <div>Seconds: {seconds}</div>
|
|
154
|
-
}
|
|
155
|
-
```
|
|
156
|
-
|
|
157
|
-
### Component Lifecycle
|
|
158
|
-
|
|
159
|
-
Stateful components have a simple lifecycle pattern:
|
|
160
|
-
- After mount: Component's element is mounted and the generator function is called. You can initialize resources here.
|
|
161
|
-
- Before render: Before component's children render
|
|
162
|
-
- After render: After component's children render
|
|
163
|
-
- Before re-render: Before component's children re-render
|
|
164
|
-
- Before unmount: Component's element is about to be unmounted. You can clean up resources here.
|
|
165
|
-
|
|
166
|
-
```jsx
|
|
167
|
-
function* LifecycleDemo() {
|
|
168
|
-
|
|
169
|
-
console.log('After mount')
|
|
170
|
-
|
|
171
|
-
this.cleanup(() => console.log('Before unmount'))
|
|
172
|
-
|
|
173
|
-
while (true) {
|
|
174
|
-
|
|
175
|
-
console.log('Before render')
|
|
176
|
-
|
|
177
|
-
this.effect(() => console.log('After render'))
|
|
178
|
-
|
|
179
|
-
yield <div>Hello, Ajo!</div>
|
|
180
|
-
|
|
181
|
-
console.log('Before re-render')
|
|
182
|
-
}
|
|
183
|
-
}
|
|
184
|
-
```
|
|
185
|
-
|
|
186
|
-
### attr: Attributes
|
|
187
|
-
|
|
188
|
-
Use the `attr:` prefix to add HTML attributes to a component's root element:
|
|
189
|
-
|
|
190
|
-
```jsx
|
|
191
|
-
function* CustomButton(props) {
|
|
192
|
-
while (true) yield <>{props.children}</>
|
|
193
|
-
}
|
|
194
|
-
CustomButton.is = 'button'
|
|
195
|
-
|
|
196
|
-
// Usage
|
|
197
|
-
<CustomButton attr:class="primary" attr:id="submit-btn">
|
|
198
|
-
Click me
|
|
199
|
-
</CustomButton>
|
|
200
|
-
```
|
|
201
|
-
|
|
202
|
-
### Component.attrs, Component.args and Component.is
|
|
203
|
-
|
|
204
|
-
Use `Component.attrs` to set default attributes for a component:
|
|
205
|
-
|
|
206
|
-
```jsx
|
|
207
|
-
function* CustomButton(args) {
|
|
208
|
-
while (true) yield <>{props.children}</>
|
|
209
|
-
}
|
|
210
|
-
CustomButton.attrs = { class: 'btn btn-primary' }
|
|
211
|
-
```
|
|
212
|
-
|
|
213
|
-
Use `Component.args` to set default arguments for a component:
|
|
214
|
-
|
|
215
|
-
```jsx
|
|
216
|
-
function* Greeting(args) {
|
|
217
|
-
while (true) yield <>Hello, {props.name}!</>
|
|
218
|
-
}
|
|
219
|
-
Greeting.args = { name: 'Guest' }
|
|
220
|
-
```
|
|
221
|
-
|
|
222
|
-
Use `Component.is` to specify the HTML element for a component:
|
|
223
|
-
|
|
224
|
-
```jsx
|
|
225
|
-
function* CustomInput(args) {
|
|
226
|
-
while (true) yield <>{props.children}</>
|
|
227
|
-
}
|
|
228
|
-
CustomInput.is = 'input'
|
|
229
|
-
```
|
|
230
|
-
|
|
231
|
-
## API
|
|
232
|
-
|
|
233
|
-
### `ajo` module
|
|
234
|
-
|
|
235
|
-
#### `h(type, props?, ...children)`
|
|
236
|
-
|
|
237
|
-
Creates virtual DOM elements.
|
|
238
|
-
|
|
239
|
-
```javascript
|
|
240
|
-
const element = h('div', { class: 'container' }, 'Hello, Ajo!')
|
|
241
|
-
```
|
|
242
|
-
|
|
243
|
-
#### `render(vnode, container)`
|
|
244
|
-
|
|
245
|
-
Renders a virtual DOM tree into a DOM element.
|
|
246
|
-
|
|
247
|
-
```javascript
|
|
248
|
-
render(h(App), document.body)
|
|
249
|
-
```
|
|
250
|
-
|
|
251
|
-
#### `Fragment({ children })`
|
|
252
|
-
|
|
253
|
-
A component for grouping elements without adding extra nodes to the DOM.
|
|
254
|
-
|
|
255
|
-
```jsx
|
|
256
|
-
const List = () => (
|
|
257
|
-
<Fragment>
|
|
258
|
-
<li>Item 1</li>
|
|
259
|
-
<li>Item 2</li>
|
|
260
|
-
</Fragment>
|
|
261
|
-
)
|
|
262
|
-
```
|
|
263
|
-
|
|
264
|
-
#### `context(defaultValue)`
|
|
265
|
-
|
|
266
|
-
Creates a context with an optional default value.
|
|
267
|
-
|
|
268
|
-
```javascript
|
|
269
|
-
const ThemeContext = context('light')
|
|
270
|
-
|
|
271
|
-
// In a stateless component:
|
|
272
|
-
const StatelessComponent = () => {
|
|
273
|
-
const theme = ThemeContext()
|
|
274
|
-
return <div>Current theme: {theme}</div>
|
|
275
|
-
}
|
|
276
|
-
|
|
277
|
-
// In a stateful component:
|
|
278
|
-
function* StatefulComponent() {
|
|
279
|
-
while (true) {
|
|
280
|
-
const theme = ThemeContext()
|
|
281
|
-
yield <div>Current theme: {theme}</div>
|
|
282
|
-
}
|
|
283
|
-
}
|
|
284
|
-
|
|
285
|
-
// Setting context value:
|
|
286
|
-
function* App() {
|
|
287
|
-
|
|
288
|
-
ThemeContext('dark')
|
|
289
|
-
|
|
290
|
-
while (true) {
|
|
291
|
-
yield (
|
|
292
|
-
<>
|
|
293
|
-
<StatelessComponent />
|
|
294
|
-
<StatefulComponent />
|
|
295
|
-
</>
|
|
296
|
-
)
|
|
297
|
-
}
|
|
298
|
-
}
|
|
299
|
-
```
|
|
300
|
-
|
|
301
|
-
#### Stateful Components and Component Methods
|
|
302
|
-
|
|
303
|
-
Ajo's stateful components are implemented as generator functions and have access to several special methods:
|
|
304
|
-
|
|
305
|
-
```javascript
|
|
306
|
-
function* StatefulComponent(props) {
|
|
307
|
-
// Component logic here
|
|
308
|
-
while (true) {
|
|
309
|
-
yield (/* JSX */)
|
|
310
|
-
}
|
|
311
|
-
}
|
|
312
|
-
```
|
|
313
|
-
|
|
314
|
-
Component methods:
|
|
315
|
-
|
|
316
|
-
- `this.render()`: Triggers a re-render of the component. It's the primary method for updating the component's UI after state changes.
|
|
317
|
-
|
|
318
|
-
```javascript
|
|
319
|
-
function* Counter() {
|
|
320
|
-
|
|
321
|
-
let count = 0
|
|
322
|
-
|
|
323
|
-
const increment = () => {
|
|
324
|
-
count++
|
|
325
|
-
this.render() // Re-render to reflect the new count
|
|
326
|
-
}
|
|
327
|
-
|
|
328
|
-
while (true) {
|
|
329
|
-
yield <button set:onclick={increment}>{count}</button>
|
|
330
|
-
}
|
|
331
|
-
}
|
|
332
|
-
```
|
|
333
|
-
|
|
334
|
-
- `this.queueMicrotasks()`: Schedules an asynchronous re-render of the component. This is useful for batching multiple updates together for better performance.
|
|
335
|
-
|
|
336
|
-
```javascript
|
|
337
|
-
function* AsyncCounter() {
|
|
338
|
-
|
|
339
|
-
let count = 0
|
|
340
|
-
|
|
341
|
-
const increment = () => {
|
|
342
|
-
count++
|
|
343
|
-
this.queueMicrotask() // Schedule an asynchronous re-render
|
|
344
|
-
}
|
|
345
|
-
|
|
346
|
-
while (true) {
|
|
347
|
-
yield <button set:onclick={increment}>{count}</button>
|
|
348
|
-
}
|
|
349
|
-
}
|
|
350
|
-
```
|
|
351
|
-
|
|
352
|
-
- `this.requestAnimationFrame()`: Similar to `this.queueMicrotask()`, but uses `requestAnimationFrame` for smooth animations.
|
|
353
|
-
|
|
354
|
-
```javascript
|
|
355
|
-
function* AnimatedCounter() {
|
|
356
|
-
|
|
357
|
-
let count = 0
|
|
358
|
-
|
|
359
|
-
const increment = () => {
|
|
360
|
-
count++
|
|
361
|
-
this.requestAnimationFrame() // Schedule a re-render on the next animation frame
|
|
362
|
-
}
|
|
363
|
-
|
|
364
|
-
while (true) {
|
|
365
|
-
yield <button set:onclick={increment}>{count}</button>
|
|
366
|
-
}
|
|
367
|
-
}
|
|
368
|
-
```
|
|
369
|
-
|
|
370
|
-
- `this.cleanup(fn)`: Registers a callback to be executed when the component unmounts. This is useful for cleaning up resources or subscriptions.
|
|
371
|
-
|
|
372
|
-
```javascript
|
|
373
|
-
function* TimerComponent() {
|
|
374
|
-
|
|
375
|
-
let seconds = 0
|
|
376
|
-
|
|
377
|
-
const timer = setInterval(() => {
|
|
378
|
-
seconds++
|
|
379
|
-
this.render()
|
|
380
|
-
}, 1000)
|
|
381
|
-
|
|
382
|
-
this.cleanup(() => clearInterval(timer)) // Clean up the timer when unmounting
|
|
383
|
-
|
|
384
|
-
while (true) {
|
|
385
|
-
yield <div>Seconds: {seconds}</div>
|
|
386
|
-
}
|
|
387
|
-
}
|
|
388
|
-
```
|
|
389
|
-
|
|
390
|
-
- `this.effect(fn)`: Registers a callback to be run after the next render.
|
|
391
|
-
|
|
392
|
-
```javascript
|
|
393
|
-
function* DataFetchingComponent() {
|
|
394
|
-
|
|
395
|
-
let data = null
|
|
396
|
-
|
|
397
|
-
this.effect(async () => {
|
|
398
|
-
const response = await fetch('https://api.example.com/data')
|
|
399
|
-
data = await response.json()
|
|
400
|
-
this.render()
|
|
401
|
-
})
|
|
402
|
-
|
|
403
|
-
while (true) {
|
|
404
|
-
yield data ? <div>{JSON.stringify(data)}</div> : <div>Loading...</div>
|
|
405
|
-
}
|
|
406
|
-
}
|
|
407
|
-
```
|
|
408
|
-
|
|
409
|
-
If you want to execute an effect after each render, place it inside the main loop:
|
|
410
|
-
|
|
411
|
-
```javascript
|
|
412
|
-
function* EffectfulComponent() {
|
|
413
|
-
|
|
414
|
-
let count = 0
|
|
415
|
-
|
|
416
|
-
while (true) {
|
|
417
|
-
|
|
418
|
-
this.effect(() => console.log(`Count updated to ${count}`))
|
|
419
|
-
|
|
420
|
-
yield <button set:onclick={() => { count++; this.render() }}>{count}</button>
|
|
421
|
-
|
|
422
|
-
console.log(`Optionally you can clean up effect for count ${count} here`)
|
|
423
|
-
}
|
|
424
|
-
}
|
|
425
|
-
```
|
|
426
|
-
|
|
427
|
-
- `this.next()`: Advances the generator to the next yield point. It's automatically called by `this.render()` and is rarely used directly.
|
|
428
|
-
|
|
429
|
-
- `this.throw(error)`: Throws an error in the generator. Useful for error propagation and creating error boundaries.
|
|
430
|
-
|
|
431
|
-
- `this.return()`: Completes the generator execution. It's automatically called when a component is unmounted, but can be used manually to reset a component's state.
|
|
432
|
-
|
|
433
|
-
These methods provide powerful control over the component's lifecycle, state management, and side effects, allowing for efficient and flexible UI updates. Note that `this.throw()` and `this.return()` are often called automatically by Ajo in response to errors or component unmounting, respectively, but can also be used manually when needed.
|
|
434
|
-
|
|
435
|
-
### `ajo/html` module
|
|
436
|
-
|
|
437
|
-
For server-side rendering:
|
|
438
|
-
|
|
439
|
-
#### `render(vnode)`
|
|
440
|
-
|
|
441
|
-
Renders a virtual DOM tree to an HTML string.
|
|
442
|
-
|
|
443
|
-
```javascript
|
|
444
|
-
import { render } from 'ajo/html'
|
|
445
|
-
import { App } from './components'
|
|
446
|
-
|
|
447
|
-
const html = render(<App />)
|
|
448
|
-
```
|
|
449
|
-
|
|
450
|
-
#### `html(vnode)`
|
|
451
|
-
|
|
452
|
-
Generates an iterator of HTML strings for streaming.
|
|
453
|
-
|
|
454
|
-
```javascript
|
|
455
|
-
import { html } from 'ajo/html'
|
|
456
|
-
import { App } from './components'
|
|
457
|
-
|
|
458
|
-
for (const chunk of html(<App />)) {
|
|
459
|
-
// Send chunk to the client
|
|
460
|
-
stream.write(chunk)
|
|
461
|
-
}
|
|
462
|
-
```
|
|
463
|
-
|
|
464
26
|
## License
|
|
465
27
|
|
|
466
28
|
ISC © [Cristian Falcone](cristianfalcone.com)
|
package/types.ts
CHANGED
|
@@ -1,82 +1,99 @@
|
|
|
1
1
|
declare module 'ajo' {
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
function context<T>(fallback?: T): (value?: T) => T
|
|
3
|
+
type Tag = keyof (HTMLElementTagNameMap & SVGElementTagNameMap)
|
|
4
|
+
|
|
5
|
+
type Type = Tag | Stateless | Stateful
|
|
6
|
+
|
|
7
|
+
type Component<TProps extends Props = {}> = Stateless<TProps> | Stateful<TProps>
|
|
8
|
+
|
|
9
|
+
type Props = Record<string, unknown>
|
|
10
|
+
|
|
11
|
+
type VNode<TTag extends Type, TProps extends Props> = TProps & {
|
|
12
|
+
nodeName: TTag,
|
|
13
|
+
children?: Children,
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
type Children = unknown
|
|
17
|
+
|
|
18
|
+
type ElementType<TTag> = TTag extends keyof HTMLElementTagNameMap
|
|
19
|
+
? HTMLElementTagNameMap[TTag]
|
|
20
|
+
: TTag extends keyof SVGElementTagNameMap
|
|
21
|
+
? SVGElementTagNameMap[TTag]
|
|
22
|
+
: never
|
|
23
|
+
|
|
24
|
+
type SpecialProps<TElement> = {
|
|
25
|
+
key: unknown,
|
|
26
|
+
skip: boolean,
|
|
27
|
+
memo: unknown,
|
|
28
|
+
ref: (el: TElement | null) => void,
|
|
29
|
+
} & ElementChildrenAttribute
|
|
30
|
+
|
|
31
|
+
type PropSetter<TTag> = {
|
|
32
|
+
[K in keyof ElementType<TTag> as `set:${Exclude<K, symbol>}`]: ElementType<TTag>[K]
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
type AttrSetter = {
|
|
36
|
+
[key: `attr:${string}`]: unknown
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
type Stateless<TArguments extends Props = {}> = (args: TArguments) => Children
|
|
40
|
+
|
|
41
|
+
type Stateful<TArguments extends Props = {}, TTag extends Tag = 'div'> = {
|
|
42
|
+
(this: StatefulElement<TTag>, args: StatefulProps<TArguments, TTag>): Iterator<Children>
|
|
43
|
+
} & (TTag extends 'div' ? { is?: TTag } : { is: TTag }) & { attrs?: Partial<PropSetter<TTag>> & Props, args?: Partial<TArguments> }
|
|
44
|
+
|
|
45
|
+
type StatefulProps<TArguments, TTag> =
|
|
46
|
+
Partial<SpecialProps<StatefulElement<TTag>> & PropSetter<TTag>> &
|
|
47
|
+
AttrSetter &
|
|
48
|
+
TArguments
|
|
49
|
+
|
|
50
|
+
type StatefulElement<TTag> = ElementType<TTag> & {
|
|
51
|
+
render: () => void,
|
|
52
|
+
next: () => void,
|
|
53
|
+
throw: (value?: unknown) => void,
|
|
54
|
+
return: () => void,
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
type IntrinsicElements = {
|
|
58
|
+
[TTag in Tag]: Partial<PropSetter<TTag> & SpecialProps<ElementType<TTag>>> & Props
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
type ElementChildrenAttribute = { children: Children }
|
|
62
|
+
|
|
63
|
+
function Fragment({ children }: ElementChildrenAttribute): typeof children
|
|
64
|
+
function h(tag: Type, props?: Props | null, ...children: Children[]): VNode<Type, Props>
|
|
65
|
+
function render(h: Children, el: Element, child?: Node, ref?: Node): void
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
declare module 'ajo/context' {
|
|
69
|
+
function context<T>(fallback?: T): (value?: T) => T
|
|
71
70
|
}
|
|
72
71
|
|
|
73
72
|
declare module 'ajo/html' {
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
73
|
+
|
|
74
|
+
type Patch = {
|
|
75
|
+
id: string,
|
|
76
|
+
h?: import('ajo').Children,
|
|
77
|
+
src?: string,
|
|
78
|
+
done?: boolean,
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
type Hooks = {
|
|
82
|
+
alloc?: (parentId: string) => string,
|
|
83
|
+
placeholder?: (id: string, children: import('ajo').Children) => unknown,
|
|
84
|
+
push?: (patch: Patch) => void,
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
function render(h: import('ajo').Children): string
|
|
88
|
+
function html(h: import('ajo').Children, hooks?: Hooks): IterableIterator<string>
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
declare module 'ajo/stream' {
|
|
92
|
+
function stream(h: import('ajo').Children): AsyncIterableIterator<string>
|
|
93
|
+
function hydrate(patch: import('ajo/html').Patch): Promise<void>
|
|
77
94
|
}
|
|
78
95
|
|
|
79
96
|
declare namespace JSX {
|
|
80
|
-
|
|
81
|
-
|
|
97
|
+
type ElementChildrenAttribute = import('ajo').ElementChildrenAttribute
|
|
98
|
+
type IntrinsicElements = import('ajo').IntrinsicElements
|
|
82
99
|
}
|