ajo 0.1.21 → 0.1.23
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 -58
- package/dist/index.cjs +1 -1
- package/dist/index.js +69 -85
- package/dist/stream.cjs +1 -0
- package/dist/stream.js +22 -0
- package/license +1 -1
- package/package.json +28 -20
- package/readme.md +1 -369
- package/types.ts +45 -32
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 u=require("./context.cjs"),A=new Set(["area","base","br","col","command","embed","hr","img","input","keygen","link","meta","param","source","track","wbr"]),w=new Set(["key","skip","memo","ref"]),$=new Set(["nodeName","children",...w]),j=Symbol.for("ajo.args"),l=e=>e.replace(/[&<>"']/g,r=>`&#${r.charCodeAt(0)};`),m=(e,r)=>({nodeName:"div","data-ssr":e,children:r}),y=()=>{},x=e=>[...v(e)].join(""),v=function*(e,r=()=>"",i=y){yield*g(f(e,r,i))},f=(e,r,i)=>{if(!(e==null||typeof e=="boolean")){if(typeof e=="string"||typeof e=="number")return String(e);if(Array.isArray(e)){const n=[];for(e of e.flat(1/0)){const t=f(e,r,i);t!=null&&n.push(t)}return n.length===1?n[0]:n}if(typeof e=="object"&&"nodeName"in e){if(typeof e.nodeName=="function")return k(e,r,i);const n={nodeName:e.nodeName};for(const t in e)!$.has(t)&&!t.startsWith("set:")&&(n[t]=e[t]);return"children"in e&&(n.children=f(e.children,r,i)),n}return String(e)}},k=({nodeName:e,fallback:r=e.fallback,...i},n,t)=>{if(e.src){const o=n();return t({id:o,src:e.src,h:i,done:!0}),m(o,r)}const s=e.constructor.name;if(s==="GeneratorFunction")return p(e,i,n,t);if(s==="AsyncGeneratorFunction")return G(e,r,i,n,t);if(i=e(i),i instanceof Promise){if(t===y)return r;const o=n();return i.then(a=>t({id:o,h:f(a,(d=o)=>n(d),t),done:!0})),m(o,r)}return f(i,n,t)},p=(e,r,i,n)=>{var S,b;const t={...e.attrs},s={...e.args};for(const c in r)c.startsWith("attr:")?t[c.slice(5)]=r[c]:w.has(c)||c.startsWith("set:")?t[c]=r[c]:s[c]=r[c];const o={[u.Context]:Object.assign(Object.create(null),(S=u.current())==null?void 0:S[u.Context]),[j]:s,render:y,next:y,return:y,throw:c=>{throw c}},a=e.call(o,s),d=u.current();u.current(o);try{return{...t,nodeName:e.is??"div",children:f(a.next().value,i,n)}}finally{(b=a.return)==null||b.call(a),u.current(d)}},G=(e,r,i,n,t)=>{if(t===y)return r;const s=n();return Promise.resolve().then(async()=>{var a;n=(d=s)=>n(d);const o=e(i);try{for(i=await o.next();!i.done;)t({id:s,h:f(i.value,n,t),done:!1}),i=await o.next();t({id:s,h:f(i.value,n,t),done:!0})}catch(d){t({id:s,h:f(d,n,t),done:!0})}finally{(a=o.return)==null||a.call(o)}}),m(s,r)},g=function*(e){if(typeof e=="string")yield l(e);else if(Array.isArray(e))for(e of e)yield*g(e);else if(typeof e=="object"&&"nodeName"in e){const{nodeName:r,children:i}=e;let n="";for(const t in e)$.has(t)||t.startsWith("set:")||e[t]==null||e[t]===!1||(e[t]===!0?n+=` ${t}`:n+=` ${t}="${l(String(e[t]))}"`);A.has(r)?yield`<${r}${n}>`:(yield`<${r}${n}>`,i!=null&&(yield*g(i)),yield`</${r}>`)}else yield l(String(e))};exports.html=v;exports.render=x;
|
package/dist/html.js
CHANGED
|
@@ -1,65 +1,90 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
1
|
+
import { Context as $, current as u } from "./context.js";
|
|
2
|
+
const v = /* @__PURE__ */ new Set(["area", "base", "br", "col", "command", "embed", "hr", "img", "input", "keygen", "link", "meta", "param", "source", "track", "wbr"]), b = /* @__PURE__ */ new Set(["key", "skip", "memo", "ref"]), A = /* @__PURE__ */ new Set(["nodeName", "children", ...b]), j = Symbol.for("ajo.args"), l = (e) => e.replace(/[&<>"']/g, (r) => `&#${r.charCodeAt(0)};`), m = (e, r) => ({ nodeName: "div", "data-ssr": e, children: r }), y = () => {
|
|
3
|
+
}, N = (e) => [...k(e)].join(""), k = function* (e, r = () => "", i = y) {
|
|
4
|
+
yield* g(f(e, r, i));
|
|
5
|
+
}, f = (e, r, i) => {
|
|
6
|
+
if (!(e == null || typeof e == "boolean")) {
|
|
7
|
+
if (typeof e == "string" || typeof e == "number") return String(e);
|
|
8
|
+
if (Array.isArray(e)) {
|
|
9
|
+
const n = [];
|
|
10
|
+
for (e of e.flat(1 / 0)) {
|
|
11
|
+
const t = f(e, r, i);
|
|
12
|
+
t != null && n.push(t);
|
|
13
|
+
}
|
|
14
|
+
return n.length === 1 ? n[0] : n;
|
|
15
|
+
}
|
|
16
|
+
if (typeof e == "object" && "nodeName" in e) {
|
|
17
|
+
if (typeof e.nodeName == "function") return p(e, r, i);
|
|
18
|
+
const n = { nodeName: e.nodeName };
|
|
19
|
+
for (const t in e) !A.has(t) && !t.startsWith("set:") && (n[t] = e[t]);
|
|
20
|
+
return "children" in e && (n.children = f(e.children, r, i)), n;
|
|
21
|
+
}
|
|
22
|
+
return String(e);
|
|
23
|
+
}
|
|
24
|
+
}, p = ({ nodeName: e, fallback: r = e.fallback, ...i }, n, t) => {
|
|
25
|
+
if (e.src) {
|
|
26
|
+
const o = n();
|
|
27
|
+
return t({ id: o, src: e.src, h: i, done: !0 }), m(o, r);
|
|
28
|
+
}
|
|
29
|
+
const s = e.constructor.name;
|
|
30
|
+
if (s === "GeneratorFunction") return x(e, i, n, t);
|
|
31
|
+
if (s === "AsyncGeneratorFunction") return G(e, r, i, n, t);
|
|
32
|
+
if (i = e(i), i instanceof Promise) {
|
|
33
|
+
if (t === y) return r;
|
|
34
|
+
const o = n();
|
|
35
|
+
return i.then((a) => t({ id: o, h: f(a, (d = o) => n(d), t), done: !0 })), m(o, r);
|
|
36
|
+
}
|
|
37
|
+
return f(i, n, t);
|
|
38
|
+
}, x = (e, r, i, n) => {
|
|
39
|
+
var w, S;
|
|
40
|
+
const t = { ...e.attrs }, s = { ...e.args };
|
|
41
|
+
for (const c in r)
|
|
42
|
+
c.startsWith("attr:") ? t[c.slice(5)] = r[c] : b.has(c) || c.startsWith("set:") ? t[c] = r[c] : s[c] = r[c];
|
|
43
|
+
const o = {
|
|
44
|
+
[$]: Object.assign(/* @__PURE__ */ Object.create(null), (w = u()) == null ? void 0 : w[$]),
|
|
45
|
+
[j]: s,
|
|
46
|
+
render: y,
|
|
47
|
+
next: y,
|
|
48
|
+
return: y,
|
|
49
|
+
throw: (c) => {
|
|
50
|
+
throw c;
|
|
10
51
|
}
|
|
11
|
-
},
|
|
12
|
-
|
|
13
|
-
if (!(e == null || typeof e == "boolean"))
|
|
14
|
-
if (k(e, "nodeName")) {
|
|
15
|
-
const { value: l } = t, { nodeName: n } = e;
|
|
16
|
-
if (l && (yield l, t.value = ""), typeof n == "function")
|
|
17
|
-
if (n.constructor.name === "GeneratorFunction") {
|
|
18
|
-
const r = {}, i = x({}, n.attrs);
|
|
19
|
-
for (const a in e) {
|
|
20
|
-
const u = e[a];
|
|
21
|
-
a.startsWith("attr:") ? i[a.slice(5)] = u : r[a] = u;
|
|
22
|
-
}
|
|
23
|
-
i.nodeName = n.is ?? "div", i.children = A(n, r), yield i;
|
|
24
|
-
} else yield* c(n(e), t, !1);
|
|
25
|
-
else yield e;
|
|
26
|
-
} else d(e) ? yield* c(e, t, !1) : t.value += e;
|
|
27
|
-
o && t.value && (yield t.value);
|
|
28
|
-
}, N = new Set("area,base,br,col,command,embed,hr,img,input,keygen,link,meta,param,source,track,wbr".split(",")), S = new Set("nodeName,key,skip,memo,ref,children".split(",")), f = (e) => e.replace(/[&<>"']/g, (t) => `&#${t.charCodeAt(0)};`);
|
|
29
|
-
let s = null;
|
|
30
|
-
const A = (e, t) => {
|
|
31
|
-
let o, l;
|
|
52
|
+
}, a = e.call(o, s), d = u();
|
|
53
|
+
u(o);
|
|
32
54
|
try {
|
|
33
|
-
|
|
34
|
-
$args: t,
|
|
35
|
-
$context: v((s == null ? void 0 : s.$context) ?? null),
|
|
36
|
-
render() {
|
|
37
|
-
s !== o && o.next();
|
|
38
|
-
},
|
|
39
|
-
next() {
|
|
40
|
-
const r = s;
|
|
41
|
-
s = o, l = y(n.next().value), s = r;
|
|
42
|
-
},
|
|
43
|
-
throw(r) {
|
|
44
|
-
l = y(n.throw(r).value);
|
|
45
|
-
},
|
|
46
|
-
return() {
|
|
47
|
-
n.return();
|
|
48
|
-
}
|
|
49
|
-
}, t);
|
|
50
|
-
o.next();
|
|
51
|
-
} catch (n) {
|
|
52
|
-
o.throw(n);
|
|
55
|
+
return { ...t, nodeName: e.is ?? "div", children: f(a.next().value, i, n) };
|
|
53
56
|
} finally {
|
|
54
|
-
|
|
57
|
+
(S = a.return) == null || S.call(a), u(d);
|
|
55
58
|
}
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
const
|
|
59
|
-
return
|
|
59
|
+
}, G = (e, r, i, n, t) => {
|
|
60
|
+
if (t === y) return r;
|
|
61
|
+
const s = n();
|
|
62
|
+
return Promise.resolve().then(async () => {
|
|
63
|
+
var a;
|
|
64
|
+
n = (d = s) => n(d);
|
|
65
|
+
const o = e(i);
|
|
66
|
+
try {
|
|
67
|
+
for (i = await o.next(); !i.done; )
|
|
68
|
+
t({ id: s, h: f(i.value, n, t), done: !1 }), i = await o.next();
|
|
69
|
+
t({ id: s, h: f(i.value, n, t), done: !0 });
|
|
70
|
+
} catch (d) {
|
|
71
|
+
t({ id: s, h: f(d, n, t), done: !0 });
|
|
72
|
+
} finally {
|
|
73
|
+
(a = o.return) == null || a.call(o);
|
|
74
|
+
}
|
|
75
|
+
}), m(s, r);
|
|
76
|
+
}, g = function* (e) {
|
|
77
|
+
if (typeof e == "string") yield l(e);
|
|
78
|
+
else if (Array.isArray(e)) for (e of e) yield* g(e);
|
|
79
|
+
else if (typeof e == "object" && "nodeName" in e) {
|
|
80
|
+
const { nodeName: r, children: i } = e;
|
|
81
|
+
let n = "";
|
|
82
|
+
for (const t in e)
|
|
83
|
+
A.has(t) || t.startsWith("set:") || e[t] == null || e[t] === !1 || (e[t] === !0 ? n += ` ${t}` : n += ` ${t}="${l(String(e[t]))}"`);
|
|
84
|
+
v.has(r) ? yield `<${r}${n}>` : (yield `<${r}${n}>`, i != null && (yield* g(i)), yield `</${r}>`);
|
|
85
|
+
} else yield l(String(e));
|
|
60
86
|
};
|
|
61
87
|
export {
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
y as render
|
|
88
|
+
k as html,
|
|
89
|
+
N as render
|
|
65
90
|
};
|
package/dist/index.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const S=(
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const c=require("./context.cjs"),a=Symbol.for("ajo.key"),b=Symbol.for("ajo.memo"),y=Symbol.for("ajo.ref"),S=Symbol.for("ajo.cache"),f=Symbol.for("ajo.generator"),o=Symbol.for("ajo.iterator"),u=Symbol.for("ajo.args"),p=t=>t.children,A=(t,r,...n)=>((r??(r={})).nodeName=t,!("children"in r)&&n.length&&(r.children=n.length==1?n[0]:n),r),l=(t,r,n=r.firstChild,s)=>{for(t of m(t)){const e=C(t,r,n);e===n?n=n.nextSibling:F(r,e,n)}for(;n&&n!=s;){const e=n.nextSibling;n.nodeType==1&&j(n),r.removeChild(n),n=e}},m=function*(t,r={h:""},n=!0){if(!(t==null||typeof t=="boolean")){if(Array.isArray(t))for(t of t.flat(1/0))yield*m(t,r,!1);else typeof t=="object"&&"nodeName"in t?(r.h&&(yield r.h,r.h=""),typeof t.nodeName=="function"?yield*v(t,r):yield t):r.h+=t;n&&r.h&&(yield r.h)}},v=function*({nodeName:t,...r},n){t.constructor.name=="GeneratorFunction"?yield h(t,r):yield*m(t(r),n,!1)},h=function(t,r){const n={...t.attrs},s={...t.args};for(const e in r)e.startsWith("attr:")?n[e.slice(5)]=r[e]:e=="key"||e=="memo"||e.startsWith("set:")?n[e]=r[e]:s[e]=r[e];return{...n,nodeName:t.is??"div",skip:!0,ref:W.bind(null,t,s)}},C=(t,r,n)=>typeof t=="string"?N(t,n):O(t,r,n),N=(t,r)=>{for(;r&&r.nodeType!=3;)r=r.nextSibling;return r?r.data!=t&&(r.data=t):r=document.createTextNode(t),r},O=({nodeName:t,children:r,key:n,skip:s,memo:e,ref:g,...x},w,i)=>{for(;i&&!(i.localName==t&&(i[a]??(i[a]=n))==n);)i=i.nextSibling;return i??(i=Object.assign(document.createElementNS(x.xmlns??w.namespaceURI,t),{[a]:n})),(e==null||E(i[b],i[b]=e))&&(k(i[S]??T(i),i[S]=x,i),s||l(r,i),typeof g=="function"&&(i[y]=g)(i)),i},k=(t,r,n)=>{for(const s in{...t,...r})t[s]!==r[s]&&(s.startsWith("set:")?n[s.slice(4)]=r[s]:r[s]==null||r[s]===!1?n.removeAttribute(s):n.setAttribute(s,r[s]===!0?"":r[s]))},E=(t,r)=>Array.isArray(t)&&Array.isArray(r)?t.some((n,s)=>n!==r[s]):t!==r,T=t=>Array.from(t.attributes).reduce((r,n)=>(r[n.name]=n.value,r),{}),F=(t,r,n)=>{if(!r.contains(document.activeElement))return t.insertBefore(r,n);const s=r.nextSibling;for(;n&&n!=r;){const e=n.nextSibling;t.insertBefore(n,s),n=e}},j=t=>{var r;for(const n of t.children)j(n);(r=t[y])==null||r.call(t,null)},W=(t,{skip:r,ref:n,...s},e)=>{e&&(e[f]??(e[f]=(B(e),t)),e[y]=M.bind(null,n,e),Object.assign(e[u]??(e[u]={}),s),r||e.next())},B=t=>{var r;Object.assign(t,R),t[c.Context]=Object.create(((r=c.current())==null?void 0:r[c.Context])??null)},M=(t,r,n)=>{typeof t=="function"&&t(n),n||r.return()},R={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:n,done:s}=(this[o]??(this[o]=this[f].call(this,this[u]))).next();l(n,this),(r=this[y])==null||r.call(this,this),s&&this.return()}catch(n){this.throw(n)}finally{c.current(t)}},throw(t){var r;for(let n=this;n;n=n.parentNode)if(typeof((r=n[o])==null?void 0:r.throw)=="function")try{return l(n[o].throw(t).value,n)}catch(s){t=new Error(s instanceof Error?s.message:s,{cause:t})}throw t},return(){var t;try{(t=this[o])==null||t.return()}catch(r){this.throw(r)}finally{this[o]=null}}};exports.Fragment=p;exports.h=A;exports.render=l;
|
package/dist/index.js
CHANGED
|
@@ -1,109 +1,93 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
let i = e;
|
|
7
|
-
if (typeof t == "string") {
|
|
8
|
-
for (; i && i.nodeType != 3; ) i = i.nextSibling;
|
|
9
|
-
i ? i.data != t && (i.data = t) : i = document.createTextNode(t);
|
|
10
|
-
} else {
|
|
11
|
-
const { nodeName: o, key: a, skip: c, memo: s, ref: f, children: w } = t;
|
|
12
|
-
for (; i && !(i.localName === o && (i.$key ?? (i.$key = a)) == a); ) i = i.nextSibling;
|
|
13
|
-
if (i ?? (i = m(document.createElementNS(t.xmlns ?? o === "svg" ? b : n.namespaceURI, o), { $key: a })), s == null || O(i.$memo, i.$memo = s)) {
|
|
14
|
-
const { $props: y } = i, $ = {};
|
|
15
|
-
for (const l of p(m({}, y, t))) {
|
|
16
|
-
if (A.has(l)) continue;
|
|
17
|
-
const u = $[l] = t[l];
|
|
18
|
-
u !== (y == null ? void 0 : y[l]) && (l.startsWith("set:") ? i[l.slice(4)] = u : u == null || u === !1 ? i.removeAttribute(l) : i.setAttribute(l, u === !0 ? "" : u));
|
|
19
|
-
}
|
|
20
|
-
i.$props = $, c || g(w, i), typeof f == "function" && (i.$ref = f)(i);
|
|
21
|
-
}
|
|
22
|
-
}
|
|
23
|
-
i === e ? e = e.nextSibling : C(n, i, e);
|
|
1
|
+
import { Context as b, current as a } from "./context.js";
|
|
2
|
+
const c = Symbol.for("ajo.key"), S = Symbol.for("ajo.memo"), l = Symbol.for("ajo.ref"), j = Symbol.for("ajo.cache"), f = Symbol.for("ajo.generator"), o = Symbol.for("ajo.iterator"), u = Symbol.for("ajo.args"), I = (t) => t.children, M = (t, r, ...n) => ((r ?? (r = {})).nodeName = t, !("children" in r) && n.length && (r.children = n.length == 1 ? n[0] : n), r), y = (t, r, n = r.firstChild, s) => {
|
|
3
|
+
for (t of m(t)) {
|
|
4
|
+
const e = h(t, r, n);
|
|
5
|
+
e === n ? n = n.nextSibling : T(r, e, n);
|
|
24
6
|
}
|
|
25
|
-
for (;
|
|
26
|
-
const
|
|
27
|
-
|
|
7
|
+
for (; n && n != s; ) {
|
|
8
|
+
const e = n.nextSibling;
|
|
9
|
+
n.nodeType == 1 && w(n), r.removeChild(n), n = e;
|
|
28
10
|
}
|
|
29
|
-
},
|
|
30
|
-
|
|
31
|
-
if (
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
const
|
|
50
|
-
|
|
51
|
-
},
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
const
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
11
|
+
}, m = function* (t, r = { h: "" }, n = !0) {
|
|
12
|
+
if (!(t == null || typeof t == "boolean")) {
|
|
13
|
+
if (Array.isArray(t)) for (t of t.flat(1 / 0)) yield* m(t, r, !1);
|
|
14
|
+
else typeof t == "object" && "nodeName" in t ? (r.h && (yield r.h, r.h = ""), typeof t.nodeName == "function" ? yield* A(t, r) : yield t) : r.h += t;
|
|
15
|
+
n && r.h && (yield r.h);
|
|
16
|
+
}
|
|
17
|
+
}, A = function* ({ nodeName: t, ...r }, n) {
|
|
18
|
+
t.constructor.name == "GeneratorFunction" ? yield v(t, r) : yield* m(t(r), n, !1);
|
|
19
|
+
}, v = function(t, r) {
|
|
20
|
+
const n = { ...t.attrs }, s = { ...t.args };
|
|
21
|
+
for (const e in r)
|
|
22
|
+
e.startsWith("attr:") ? n[e.slice(5)] = r[e] : e == "key" || e == "memo" || e.startsWith("set:") ? n[e] = r[e] : s[e] = r[e];
|
|
23
|
+
return { ...n, nodeName: t.is ?? "div", skip: !0, ref: W.bind(null, t, s) };
|
|
24
|
+
}, h = (t, r, n) => typeof t == "string" ? N(t, n) : k(t, r, n), N = (t, r) => {
|
|
25
|
+
for (; r && r.nodeType != 3; ) r = r.nextSibling;
|
|
26
|
+
return r ? r.data != t && (r.data = t) : r = document.createTextNode(t), r;
|
|
27
|
+
}, k = ({ nodeName: t, children: r, key: n, skip: s, memo: e, ref: x, ...g }, p, i) => {
|
|
28
|
+
for (; i && !(i.localName == t && (i[c] ?? (i[c] = n)) == n); ) i = i.nextSibling;
|
|
29
|
+
return i ?? (i = Object.assign(document.createElementNS(g.xmlns ?? p.namespaceURI, t), { [c]: n })), (e == null || E(i[S], i[S] = e)) && (C(i[j] ?? O(i), i[j] = g, i), s || y(r, i), typeof x == "function" && (i[l] = x)(i)), i;
|
|
30
|
+
}, C = (t, r, n) => {
|
|
31
|
+
for (const s in { ...t, ...r })
|
|
32
|
+
t[s] !== r[s] && (s.startsWith("set:") ? n[s.slice(4)] = r[s] : r[s] == null || r[s] === !1 ? n.removeAttribute(s) : n.setAttribute(s, r[s] === !0 ? "" : r[s]));
|
|
33
|
+
}, E = (t, r) => Array.isArray(t) && Array.isArray(r) ? t.some((n, s) => n !== r[s]) : t !== r, O = (t) => Array.from(t.attributes).reduce((r, n) => (r[n.name] = n.value, r), {}), T = (t, r, n) => {
|
|
34
|
+
if (!r.contains(document.activeElement)) return t.insertBefore(r, n);
|
|
35
|
+
const s = r.nextSibling;
|
|
36
|
+
for (; n && n != r; ) {
|
|
37
|
+
const e = n.nextSibling;
|
|
38
|
+
t.insertBefore(n, s), n = e;
|
|
39
|
+
}
|
|
40
|
+
}, w = (t) => {
|
|
41
|
+
var r;
|
|
42
|
+
for (const n of t.children) w(n);
|
|
43
|
+
(r = t[l]) == null || r.call(t, null);
|
|
44
|
+
}, W = (t, { skip: r, ref: n, ...s }, e) => {
|
|
45
|
+
e && (e[f] ?? (e[f] = (B(e), t)), e[l] = F.bind(null, n, e), Object.assign(e[u] ?? (e[u] = {}), s), r || e.next());
|
|
46
|
+
}, B = (t) => {
|
|
47
|
+
var r;
|
|
48
|
+
Object.assign(t, R), t[b] = Object.create(((r = a()) == null ? void 0 : r[b]) ?? null);
|
|
49
|
+
}, F = (t, r, n) => {
|
|
50
|
+
typeof t == "function" && t(n), n || r.return();
|
|
51
|
+
}, R = {
|
|
68
52
|
render() {
|
|
69
|
-
|
|
53
|
+
var t;
|
|
54
|
+
(t = a()) != null && t.contains(this) || this.next();
|
|
70
55
|
},
|
|
71
56
|
next() {
|
|
72
|
-
|
|
73
|
-
|
|
57
|
+
var r;
|
|
58
|
+
const t = a();
|
|
59
|
+
a(this);
|
|
74
60
|
try {
|
|
75
|
-
|
|
61
|
+
const { value: n, done: s } = (this[o] ?? (this[o] = this[f].call(this, this[u]))).next();
|
|
62
|
+
y(n, this), (r = this[l]) == null || r.call(this, this), s && this.return();
|
|
76
63
|
} catch (n) {
|
|
77
64
|
this.throw(n);
|
|
78
65
|
} finally {
|
|
79
|
-
|
|
66
|
+
a(t);
|
|
80
67
|
}
|
|
81
68
|
},
|
|
82
69
|
throw(t) {
|
|
83
|
-
var
|
|
84
|
-
for (let
|
|
85
|
-
return
|
|
86
|
-
} catch {
|
|
70
|
+
var r;
|
|
71
|
+
for (let n = this; n; n = n.parentNode) if (typeof ((r = n[o]) == null ? void 0 : r.throw) == "function") try {
|
|
72
|
+
return y(n[o].throw(t).value, n);
|
|
73
|
+
} catch (s) {
|
|
74
|
+
t = new Error(s instanceof Error ? s.message : s, { cause: t });
|
|
87
75
|
}
|
|
88
76
|
throw t;
|
|
89
77
|
},
|
|
90
78
|
return() {
|
|
91
79
|
var t;
|
|
92
80
|
try {
|
|
93
|
-
(t = this
|
|
94
|
-
} catch (
|
|
95
|
-
this.throw(
|
|
81
|
+
(t = this[o]) == null || t.return();
|
|
82
|
+
} catch (r) {
|
|
83
|
+
this.throw(r);
|
|
96
84
|
} finally {
|
|
97
|
-
this
|
|
85
|
+
this[o] = null;
|
|
98
86
|
}
|
|
99
87
|
}
|
|
100
|
-
}, j = (t, n = Symbol()) => function() {
|
|
101
|
-
const e = this ?? r;
|
|
102
|
-
return e ? arguments.length === 0 ? n in e.$context ? e.$context[n] : t : e.$context[n] = arguments[0] : t;
|
|
103
88
|
};
|
|
104
89
|
export {
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
g as render
|
|
90
|
+
I as Fragment,
|
|
91
|
+
M as h,
|
|
92
|
+
y as render
|
|
109
93
|
};
|
package/dist/stream.cjs
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const o=require("./index.cjs"),d=require("./html.cjs"),h=async function*(n){const t=new Set,e=[],s=new Map,l=(i="")=>{const r=s.get(i)??(s.set(i,0),0);return s.set(i,r+1),i?`${i}:${r}`:String(r)},c=i=>{const r=Promise.resolve(`<script>window.$stream?.push(${JSON.stringify(i)})<\/script>`);t.add(r),r.then(a=>e.push(a)).finally(()=>t.delete(r))};for(n of d.html(n,l,c))yield n;for(;t.size;)for(await Promise.race(t);e.length;)yield e.shift();for(;e.length;)yield e.shift()},u=async({id:n,src:t,h:e})=>{const s=document.querySelector(`[data-ssr="${n}"]`);s&&(t?o.render(o.h((await import(t)).default,e),s):o.render(e,s))};exports.hydrate=u;exports.stream=h;
|
package/dist/stream.js
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { render as n, h as d } from "./index.js";
|
|
2
|
+
import { html as h } from "./html.js";
|
|
3
|
+
const u = async function* (r) {
|
|
4
|
+
const e = /* @__PURE__ */ new Set(), t = [], s = /* @__PURE__ */ new Map(), l = (i = "") => {
|
|
5
|
+
const o = s.get(i) ?? (s.set(i, 0), 0);
|
|
6
|
+
return s.set(i, o + 1), i ? `${i}:${o}` : String(o);
|
|
7
|
+
}, c = (i) => {
|
|
8
|
+
const o = Promise.resolve(`<script>window.$stream?.push(${JSON.stringify(i)})<\/script>`);
|
|
9
|
+
e.add(o), o.then((a) => t.push(a)).finally(() => e.delete(o));
|
|
10
|
+
};
|
|
11
|
+
for (r of h(r, l, c)) yield r;
|
|
12
|
+
for (; e.size; )
|
|
13
|
+
for (await Promise.race(e); t.length; ) yield t.shift();
|
|
14
|
+
for (; t.length; ) yield t.shift();
|
|
15
|
+
}, w = async ({ id: r, src: e, h: t }) => {
|
|
16
|
+
const s = document.querySelector(`[data-ssr="${r}"]`);
|
|
17
|
+
s && (e ? n(d((await import(e)).default, t), s) : n(t, s));
|
|
18
|
+
};
|
|
19
|
+
export {
|
|
20
|
+
w as hydrate,
|
|
21
|
+
u as stream
|
|
22
|
+
};
|
package/license
CHANGED
package/package.json
CHANGED
|
@@ -1,32 +1,46 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "ajo",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.23",
|
|
4
4
|
"description": "ajo is a JavaScript view library for building user interfaces",
|
|
5
5
|
"type": "module",
|
|
6
|
-
"module": "./dist/index.js",
|
|
7
|
-
"main": "./dist/index.cjs",
|
|
8
|
-
"types": "./types.ts",
|
|
9
6
|
"exports": {
|
|
10
7
|
".": {
|
|
8
|
+
"types": "./types.ts",
|
|
11
9
|
"import": "./dist/index.js",
|
|
12
|
-
"require": "./dist/index.cjs"
|
|
13
|
-
"types": "./types.ts"
|
|
10
|
+
"require": "./dist/index.cjs"
|
|
14
11
|
},
|
|
15
12
|
"./html": {
|
|
13
|
+
"types": "./types.ts",
|
|
16
14
|
"import": "./dist/html.js",
|
|
17
|
-
"require": "./dist/html.cjs"
|
|
18
|
-
|
|
15
|
+
"require": "./dist/html.cjs"
|
|
16
|
+
},
|
|
17
|
+
"./context": {
|
|
18
|
+
"types": "./types.ts",
|
|
19
|
+
"import": "./dist/context.js",
|
|
20
|
+
"require": "./dist/context.cjs"
|
|
21
|
+
},
|
|
22
|
+
"./stream": {
|
|
23
|
+
"types": "./types.ts",
|
|
24
|
+
"import": "./dist/stream.js",
|
|
25
|
+
"require": "./dist/stream.cjs"
|
|
19
26
|
}
|
|
20
27
|
},
|
|
21
28
|
"files": [
|
|
22
29
|
"dist",
|
|
23
30
|
"types.ts"
|
|
24
31
|
],
|
|
32
|
+
"scripts": {
|
|
33
|
+
"test": "vitest --run",
|
|
34
|
+
"build": "vite build",
|
|
35
|
+
"bump": "pnpm version patch && git push && git push --tags",
|
|
36
|
+
"release": "pnpm test && pnpm build && pnpm bump && pnpm publish"
|
|
37
|
+
},
|
|
25
38
|
"devDependencies": {
|
|
26
|
-
"@types/node": "
|
|
27
|
-
"
|
|
28
|
-
"vite": "
|
|
29
|
-
"
|
|
39
|
+
"@types/node": "24.0.0",
|
|
40
|
+
"happy-dom": "18.0.1",
|
|
41
|
+
"vite": "6.3.5",
|
|
42
|
+
"vite-tsconfig-paths": "5.1.4",
|
|
43
|
+
"vitest": "3.2.3"
|
|
30
44
|
},
|
|
31
45
|
"keywords": [
|
|
32
46
|
"ui",
|
|
@@ -39,11 +53,5 @@
|
|
|
39
53
|
"author": "Cristian Falcone",
|
|
40
54
|
"license": "ISC",
|
|
41
55
|
"bugs": "https://github.com/cristianfalcone/ajo/issues",
|
|
42
|
-
"homepage": "https://github.com/cristianfalcone/ajo#readme"
|
|
43
|
-
|
|
44
|
-
"test": "vitest --run",
|
|
45
|
-
"build": "vite build",
|
|
46
|
-
"bump": "pnpm version patch && git push && git push --tags",
|
|
47
|
-
"release": "pnpm test && pnpm build && pnpm bump && pnpm publish"
|
|
48
|
-
}
|
|
49
|
-
}
|
|
56
|
+
"homepage": "https://github.com/cristianfalcone/ajo#readme"
|
|
57
|
+
}
|
package/readme.md
CHANGED
|
@@ -23,374 +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
|
-
let count = 0
|
|
44
|
-
|
|
45
|
-
const increment = () => {
|
|
46
|
-
count++
|
|
47
|
-
this.render()
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
while (true) {
|
|
51
|
-
yield (
|
|
52
|
-
<>
|
|
53
|
-
<p>Count: {count}</p>
|
|
54
|
-
<button set:onclick={increment}>Increment</button>
|
|
55
|
-
</>
|
|
56
|
-
)
|
|
57
|
-
}
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
function* App() {
|
|
61
|
-
while (true) {
|
|
62
|
-
yield (
|
|
63
|
-
<>
|
|
64
|
-
<Greeting name="Ajo Developer" />
|
|
65
|
-
<Counter />
|
|
66
|
-
</>
|
|
67
|
-
)
|
|
68
|
-
}
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
render(<App />, document.body)
|
|
72
|
-
```
|
|
73
|
-
|
|
74
|
-
## Core Concepts
|
|
75
|
-
|
|
76
|
-
### HTML Attributes vs DOM Properties
|
|
77
|
-
|
|
78
|
-
Ajo distinguishes between HTML attributes and DOM properties. Use regular attributes for HTML attributes, and the `set:` prefix to set DOM properties directly:
|
|
79
|
-
|
|
80
|
-
```jsx
|
|
81
|
-
<input
|
|
82
|
-
type="text"
|
|
83
|
-
id="username"
|
|
84
|
-
class="form-input"
|
|
85
|
-
placeholder="Enter username"
|
|
86
|
-
set:value={inputValue}
|
|
87
|
-
set:onclick={handleClick}
|
|
88
|
-
set:disabled={isDisabled}
|
|
89
|
-
/>
|
|
90
|
-
```
|
|
91
|
-
|
|
92
|
-
In this example:
|
|
93
|
-
- `type`, `id`, `class`, and `placeholder` are regular HTML attributes.
|
|
94
|
-
- `set:value`, `set:onclick`, and `set:disabled` are DOM properties set directly on the element.
|
|
95
|
-
|
|
96
|
-
### Special Attributes
|
|
97
|
-
|
|
98
|
-
Ajo uses special attributes for optimization and control:
|
|
99
|
-
- `key`: For efficient list rendering
|
|
100
|
-
- `skip`: To prevent rendering of child elements
|
|
101
|
-
- `memo`: For memoization of components or elements
|
|
102
|
-
- `ref`: To get references to DOM nodes or component instances
|
|
103
|
-
|
|
104
|
-
```jsx
|
|
105
|
-
<TodoItem
|
|
106
|
-
key={todo.id}
|
|
107
|
-
memo={[todo.completed]}
|
|
108
|
-
ref={el => el ? todosRefs.add(el) : todosRefs.delete(todo.id)}
|
|
109
|
-
>
|
|
110
|
-
<TodoTitle>{todo.title}</TodoTitle>
|
|
111
|
-
<div set:innerHTML={marked(todo.content)} skip></div>
|
|
112
|
-
</TodoItem>
|
|
113
|
-
```
|
|
114
|
-
|
|
115
|
-
### Stateless and Stateful Components
|
|
116
|
-
|
|
117
|
-
Stateless components are simple functions:
|
|
118
|
-
|
|
119
|
-
```jsx
|
|
120
|
-
const Greeting = ({ name }) => <h1>Hello, {name}!</h1>
|
|
121
|
-
```
|
|
122
|
-
|
|
123
|
-
Stateful components use generator functions:
|
|
124
|
-
|
|
125
|
-
```jsx
|
|
126
|
-
function* Counter() {
|
|
127
|
-
let count = 0
|
|
128
|
-
while (true) {
|
|
129
|
-
yield (
|
|
130
|
-
<button set:onclick={() => { count++; this.render(); }}>
|
|
131
|
-
{count}
|
|
132
|
-
</button>
|
|
133
|
-
)
|
|
134
|
-
}
|
|
135
|
-
}
|
|
136
|
-
```
|
|
137
|
-
|
|
138
|
-
State handling in Ajo is straightforward:
|
|
139
|
-
- State is managed using regular variables within the generator function.
|
|
140
|
-
- The `this.render()` method triggers a re-render when state changes.
|
|
141
|
-
- Each iteration of the generator function represents a new render cycle.
|
|
142
|
-
|
|
143
|
-
```jsx
|
|
144
|
-
function* Timer() {
|
|
145
|
-
let seconds = 0
|
|
146
|
-
const intervalId = setInterval(() => {
|
|
147
|
-
seconds++
|
|
148
|
-
this.render() // Trigger a re-render
|
|
149
|
-
}, 1000)
|
|
150
|
-
|
|
151
|
-
try {
|
|
152
|
-
while (true) {
|
|
153
|
-
yield <div>Seconds: {seconds}</div>
|
|
154
|
-
}
|
|
155
|
-
} finally {
|
|
156
|
-
clearInterval(intervalId) // Cleanup
|
|
157
|
-
}
|
|
158
|
-
}
|
|
159
|
-
```
|
|
160
|
-
|
|
161
|
-
### Component Lifecycle
|
|
162
|
-
|
|
163
|
-
Stateful components have a simple lifecycle:
|
|
164
|
-
- Initialization: When the generator is first called
|
|
165
|
-
- Rendering: Each time the generator yields
|
|
166
|
-
- Cleanup: When the generator's `finally` block is executed
|
|
167
|
-
|
|
168
|
-
```jsx
|
|
169
|
-
function* LifecycleDemo() {
|
|
170
|
-
console.log('Initialized')
|
|
171
|
-
try {
|
|
172
|
-
while (true) {
|
|
173
|
-
console.log('Rendering')
|
|
174
|
-
yield <div>Hello, Ajo!</div>
|
|
175
|
-
}
|
|
176
|
-
} finally {
|
|
177
|
-
console.log('Cleanup')
|
|
178
|
-
}
|
|
179
|
-
}
|
|
180
|
-
```
|
|
181
|
-
|
|
182
|
-
### attr: Attributes
|
|
183
|
-
|
|
184
|
-
Use the `attr:` prefix to add HTML attributes to a component's root element:
|
|
185
|
-
|
|
186
|
-
```jsx
|
|
187
|
-
function* CustomButton(props) {
|
|
188
|
-
while (true) yield <>{props.children}</>
|
|
189
|
-
}
|
|
190
|
-
CustomButton.is = 'button'
|
|
191
|
-
|
|
192
|
-
// Usage
|
|
193
|
-
<CustomButton attr:class="primary" attr:id="submit-btn">
|
|
194
|
-
Click me
|
|
195
|
-
</CustomButton>
|
|
196
|
-
```
|
|
197
|
-
|
|
198
|
-
### Component.attrs and Component.is
|
|
199
|
-
|
|
200
|
-
Use `Component.attrs` to set default attributes for a component:
|
|
201
|
-
|
|
202
|
-
```jsx
|
|
203
|
-
function* CustomButton(props) {
|
|
204
|
-
while (true) yield <>{props.children}</>
|
|
205
|
-
}
|
|
206
|
-
CustomButton.attrs = { class: 'btn btn-primary' }
|
|
207
|
-
```
|
|
208
|
-
|
|
209
|
-
Use `Component.is` to specify the HTML element for a component:
|
|
210
|
-
|
|
211
|
-
```jsx
|
|
212
|
-
function* CustomInput(props) {
|
|
213
|
-
while (true) yield <>{props.children}</>
|
|
214
|
-
}
|
|
215
|
-
CustomInput.is = 'input'
|
|
216
|
-
```
|
|
217
|
-
|
|
218
|
-
## API
|
|
219
|
-
|
|
220
|
-
### `ajo` module
|
|
221
|
-
|
|
222
|
-
#### `h(type, props?, ...children)`
|
|
223
|
-
|
|
224
|
-
Creates virtual DOM elements.
|
|
225
|
-
|
|
226
|
-
```javascript
|
|
227
|
-
const element = h('div', { class: 'container' }, 'Hello, Ajo!')
|
|
228
|
-
```
|
|
229
|
-
|
|
230
|
-
#### `render(vnode, container)`
|
|
231
|
-
|
|
232
|
-
Renders a virtual DOM tree into a DOM element.
|
|
233
|
-
|
|
234
|
-
```javascript
|
|
235
|
-
render(h(App), document.body)
|
|
236
|
-
```
|
|
237
|
-
|
|
238
|
-
#### `Fragment({ children })`
|
|
239
|
-
|
|
240
|
-
A component for grouping elements without adding extra nodes to the DOM.
|
|
241
|
-
|
|
242
|
-
```jsx
|
|
243
|
-
const List = () => (
|
|
244
|
-
<Fragment>
|
|
245
|
-
<li>Item 1</li>
|
|
246
|
-
<li>Item 2</li>
|
|
247
|
-
</Fragment>
|
|
248
|
-
)
|
|
249
|
-
```
|
|
250
|
-
|
|
251
|
-
#### `context(defaultValue)`
|
|
252
|
-
|
|
253
|
-
Creates a context with an optional default value.
|
|
254
|
-
|
|
255
|
-
```javascript
|
|
256
|
-
const ThemeContext = context('light')
|
|
257
|
-
|
|
258
|
-
// In a stateless component:
|
|
259
|
-
const StatelessComponent = () => {
|
|
260
|
-
const theme = ThemeContext()
|
|
261
|
-
return <div>Current theme: {theme}</div>
|
|
262
|
-
}
|
|
263
|
-
|
|
264
|
-
// In a stateful component:
|
|
265
|
-
function* StatefulComponent() {
|
|
266
|
-
while (true) {
|
|
267
|
-
const theme = ThemeContext()
|
|
268
|
-
yield <div>Current theme: {theme}</div>
|
|
269
|
-
}
|
|
270
|
-
}
|
|
271
|
-
|
|
272
|
-
// Setting context value:
|
|
273
|
-
function* App() {
|
|
274
|
-
ThemeContext('dark')
|
|
275
|
-
while (true) {
|
|
276
|
-
yield (
|
|
277
|
-
<>
|
|
278
|
-
<FunctionalComponent />
|
|
279
|
-
<StatefulComponent />
|
|
280
|
-
</>
|
|
281
|
-
)
|
|
282
|
-
}
|
|
283
|
-
}
|
|
284
|
-
```
|
|
285
|
-
|
|
286
|
-
#### Stateful Components and Component Methods
|
|
287
|
-
|
|
288
|
-
Ajo's stateful components are implemented as generator functions and have access to several special methods:
|
|
289
|
-
|
|
290
|
-
```javascript
|
|
291
|
-
function* StatefulComponent(props) {
|
|
292
|
-
// Component logic here
|
|
293
|
-
while (true) {
|
|
294
|
-
yield (/* JSX */)
|
|
295
|
-
}
|
|
296
|
-
}
|
|
297
|
-
```
|
|
298
|
-
|
|
299
|
-
Component methods:
|
|
300
|
-
|
|
301
|
-
- `this.render()`: Triggers a re-render of the component. It's the primary method for updating the component's UI after state changes.
|
|
302
|
-
|
|
303
|
-
```javascript
|
|
304
|
-
function* Counter() {
|
|
305
|
-
let count = 0
|
|
306
|
-
const increment = () => {
|
|
307
|
-
count++
|
|
308
|
-
this.render() // Re-render to reflect the new count
|
|
309
|
-
}
|
|
310
|
-
while (true) {
|
|
311
|
-
yield <button set:onclick={increment}>{count}</button>
|
|
312
|
-
}
|
|
313
|
-
}
|
|
314
|
-
```
|
|
315
|
-
|
|
316
|
-
- `this.next()`: Advances the generator to the next yield point. It's automatically called by `this.render()` and is rarely used directly.
|
|
317
|
-
|
|
318
|
-
- `this.throw(error)`: Throws an error in the generator. Useful for error propagation and creating error boundaries. Ajo automatically calls this method when an error occurs during rendering.
|
|
319
|
-
|
|
320
|
-
```javascript
|
|
321
|
-
function* ErrorBoundary(props) {
|
|
322
|
-
try {
|
|
323
|
-
while (true) {
|
|
324
|
-
yield <div>{props.children}</div>
|
|
325
|
-
}
|
|
326
|
-
} catch (error) {
|
|
327
|
-
yield <div>An error occurred: {error.message}</div>
|
|
328
|
-
}
|
|
329
|
-
}
|
|
330
|
-
```
|
|
331
|
-
|
|
332
|
-
- `this.return()`: Completes the generator execution. It's automatically called by Ajo when a component is unmounted, but can be used manually to reset a component's state.
|
|
333
|
-
|
|
334
|
-
```javascript
|
|
335
|
-
function* ResetableComponent() {
|
|
336
|
-
let count = 0
|
|
337
|
-
const reset = () => {
|
|
338
|
-
this.return() // Reset the generator
|
|
339
|
-
this.render() // Re-render from the beginning
|
|
340
|
-
}
|
|
341
|
-
while (true) {
|
|
342
|
-
yield (
|
|
343
|
-
<div>
|
|
344
|
-
<p>Count: {count}</p>
|
|
345
|
-
<button set:onclick={() => { count++; this.render(); }}>Increment</button>
|
|
346
|
-
<button set:onclick={reset}>Reset</button>
|
|
347
|
-
</div>
|
|
348
|
-
)
|
|
349
|
-
}
|
|
350
|
-
}
|
|
351
|
-
```
|
|
352
|
-
|
|
353
|
-
- `this.$args`: Provides access to the current args of the component.
|
|
354
|
-
|
|
355
|
-
```javascript
|
|
356
|
-
function* DynamicGreeting() {
|
|
357
|
-
while (true) {
|
|
358
|
-
yield <h1>Hello, {this.$args.name}!</h1>
|
|
359
|
-
}
|
|
360
|
-
}
|
|
361
|
-
```
|
|
362
|
-
|
|
363
|
-
These methods provide powerful control over the component's lifecycle and state management, 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.
|
|
364
|
-
|
|
365
|
-
### `ajo/html` module
|
|
366
|
-
|
|
367
|
-
For server-side rendering:
|
|
368
|
-
|
|
369
|
-
#### `render(vnode)`
|
|
370
|
-
|
|
371
|
-
Renders a virtual DOM tree to an HTML string.
|
|
372
|
-
|
|
373
|
-
```javascript
|
|
374
|
-
import { render } from 'ajo/html'
|
|
375
|
-
import { App } from './components'
|
|
376
|
-
|
|
377
|
-
const html = render(<App />)
|
|
378
|
-
```
|
|
379
|
-
|
|
380
|
-
#### `html(vnode)`
|
|
381
|
-
|
|
382
|
-
Generates an iterator of HTML strings for streaming.
|
|
383
|
-
|
|
384
|
-
```javascript
|
|
385
|
-
import { html } from 'ajo/html'
|
|
386
|
-
import { App } from './components'
|
|
387
|
-
|
|
388
|
-
for (const chunk of html(<App />)) {
|
|
389
|
-
// Send chunk to the client
|
|
390
|
-
stream.write(chunk)
|
|
391
|
-
}
|
|
392
|
-
```
|
|
393
|
-
|
|
394
26
|
## License
|
|
395
27
|
|
|
396
|
-
ISC © [Cristian Falcone](cristianfalcone.com)
|
|
28
|
+
ISC © [Cristian Falcone](cristianfalcone.com)
|
package/types.ts
CHANGED
|
@@ -2,28 +2,33 @@ declare module 'ajo' {
|
|
|
2
2
|
|
|
3
3
|
type Tag = keyof (HTMLElementTagNameMap & SVGElementTagNameMap)
|
|
4
4
|
|
|
5
|
-
type Type = Tag |
|
|
5
|
+
type Type = Tag | Stateless | Stateful
|
|
6
6
|
|
|
7
|
-
type
|
|
7
|
+
type Component<TProps extends Props = {}> = Stateless<TProps> | Stateful<TProps>
|
|
8
8
|
|
|
9
|
-
type
|
|
9
|
+
type Props = Record<string, unknown>
|
|
10
10
|
|
|
11
|
-
type
|
|
11
|
+
type VNode<TTag extends Type, TProps extends Props> = TProps & {
|
|
12
|
+
nodeName: TTag,
|
|
13
|
+
children?: Children,
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
type Children = unknown
|
|
12
17
|
|
|
13
|
-
type ElementType<TTag
|
|
18
|
+
type ElementType<TTag> = TTag extends keyof HTMLElementTagNameMap
|
|
14
19
|
? HTMLElementTagNameMap[TTag]
|
|
15
20
|
: TTag extends keyof SVGElementTagNameMap
|
|
16
21
|
? SVGElementTagNameMap[TTag]
|
|
17
22
|
: never
|
|
18
23
|
|
|
19
|
-
type
|
|
20
|
-
key: unknown
|
|
21
|
-
skip: boolean
|
|
22
|
-
memo: unknown
|
|
23
|
-
ref: (el: TElement | null) => void
|
|
24
|
+
type SpecialProps<TElement> = {
|
|
25
|
+
key: unknown,
|
|
26
|
+
skip: boolean,
|
|
27
|
+
memo: unknown,
|
|
28
|
+
ref: (el: TElement | null) => void,
|
|
24
29
|
} & ElementChildrenAttribute
|
|
25
30
|
|
|
26
|
-
type PropSetter<TTag
|
|
31
|
+
type PropSetter<TTag> = {
|
|
27
32
|
[K in keyof ElementType<TTag> as `set:${Exclude<K, symbol>}`]: ElementType<TTag>[K]
|
|
28
33
|
}
|
|
29
34
|
|
|
@@ -31,35 +36,49 @@ declare module 'ajo' {
|
|
|
31
36
|
[key: `attr:${string}`]: unknown
|
|
32
37
|
}
|
|
33
38
|
|
|
34
|
-
type
|
|
39
|
+
type Stateless<TArguments extends Props = {}> = (args: TArguments) => Children
|
|
35
40
|
|
|
36
|
-
type
|
|
37
|
-
(this:
|
|
38
|
-
} & (TTag extends 'div' ? { is?: TTag } : { is: TTag })
|
|
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> }
|
|
39
44
|
|
|
40
|
-
type
|
|
41
|
-
Partial<
|
|
45
|
+
type StatefulProps<TArguments, TTag> =
|
|
46
|
+
Partial<SpecialProps<StatefulElement<TTag>> & PropSetter<TTag>> &
|
|
42
47
|
AttrSetter &
|
|
43
48
|
TArguments
|
|
44
49
|
|
|
45
|
-
type
|
|
46
|
-
$args: TArguments,
|
|
47
|
-
$context: { [key: symbol]: unknown },
|
|
50
|
+
type StatefulElement<TTag> = ElementType<TTag> & {
|
|
48
51
|
render: () => void,
|
|
49
|
-
next: () => void
|
|
50
|
-
throw: (value?: unknown) => void
|
|
51
|
-
return: () => void
|
|
52
|
+
next: () => void,
|
|
53
|
+
throw: (value?: unknown) => void,
|
|
54
|
+
return: () => void,
|
|
52
55
|
}
|
|
53
56
|
|
|
54
57
|
type IntrinsicElements = {
|
|
55
|
-
[TTag in Tag]: Partial<PropSetter<TTag> &
|
|
58
|
+
[TTag in Tag]: Partial<PropSetter<TTag> & SpecialProps<ElementType<TTag>>> & Props
|
|
56
59
|
}
|
|
57
60
|
|
|
58
61
|
type ElementChildrenAttribute = { children: Children }
|
|
59
62
|
|
|
60
63
|
function Fragment({ children }: ElementChildrenAttribute): typeof children
|
|
61
|
-
function h
|
|
62
|
-
function render(h: Children, el: Element): void
|
|
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/html' {
|
|
69
|
+
|
|
70
|
+
type Patch = { id: string, h?: import('ajo').Children, src?: string, done?: boolean }
|
|
71
|
+
|
|
72
|
+
function render(h: import('ajo').Children): string
|
|
73
|
+
function html(h: import('ajo').Children, alloc?: (parentId: string) => string, push?: (patch: Patch) => void): IterableIterator<string>
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
declare module 'ajo/stream' {
|
|
77
|
+
function stream(h: import('ajo').Children): AsyncIterableIterator<string>
|
|
78
|
+
function hydrate(patch: import('ajo/html').Patch): Promise<void>
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
declare module 'ajo/context' {
|
|
63
82
|
function context<T>(fallback?: T): (value?: T) => T
|
|
64
83
|
}
|
|
65
84
|
|
|
@@ -67,9 +86,3 @@ declare namespace JSX {
|
|
|
67
86
|
type ElementChildrenAttribute = import('ajo').ElementChildrenAttribute
|
|
68
87
|
type IntrinsicElements = import('ajo').IntrinsicElements
|
|
69
88
|
}
|
|
70
|
-
|
|
71
|
-
declare namespace React {
|
|
72
|
-
const createElement: typeof import('ajo').h
|
|
73
|
-
const Fragment: typeof import('ajo').Fragment
|
|
74
|
-
type ReactNode = import('ajo').Children
|
|
75
|
-
}
|