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.
@@ -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;
@@ -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{isArray:m,from:$}=Array,p="",{create:v,assign:g,entries:x,prototype:{hasOwnProperty:w},hasOwn:k=(e,t)=>w.call(e,t)}=Object,c=e=>$(d(e)).join(""),d=function*(e){for(e of u(e))if(typeof e=="string")yield f(e);else{const{nodeName:t,skip:o,children:r=""}=e;let n="";for(const[l,i]of x(e))N.has(l)||l.startsWith("set:")||i==null||i===!1||(n+=i===!0?` ${l}`:` ${l}="${f(String(i))}"`);S.has(t)?yield`<${t}${n}>`:o?yield`<${t}${n}></${t}>`:(yield`<${t}${n}>`,typeof r=="string"&&r.startsWith(p)?yield r.slice(1):yield*d(r),yield`</${t}>`)}},u=function*(e,t={value:""},o=!0){for(e of m(e)?e:[e])if(!(e==null||typeof e=="boolean"))if(k(e,"nodeName")){const{value:r}=t,{nodeName:n}=e;if(r&&(yield r,t.value=""),typeof n=="function")if(n.constructor.name==="GeneratorFunction"){const l={},i=g({},n.attrs);for(const a in e){const y=e[a];a.startsWith("attr:")?i[a.slice(5)]=y:l[a]=y}i.nodeName=n.is??"div",i.children=O(n,l),yield i}else yield*u(n(e),t,!1);else yield e}else m(e)?yield*u(e,t,!1):t.value+=e;o&&t.value&&(yield t.value)},S=new Set("area,base,br,col,command,embed,hr,img,input,keygen,link,meta,param,source,track,wbr".split(",")),N=new Set("nodeName,key,skip,memo,ref,children".split(",")),f=e=>e.replace(/[&<>"']/g,t=>`&#${t.charCodeAt(0)};`);let s=null;const O=(e,t)=>{let o,r;try{const n=e.call(o={$args:t,$context:v((s==null?void 0:s.$context)??null),render(){s!==o&&o.next()},next(){const l=s;s=o,r=c(n.next().value),s=l},throw(l){r=c(n.throw(l).value)},return(){n.return()}},t);o.next()}catch(n){o.throw(n)}finally{o.return()}return p+r},j=(e,t=Symbol())=>function(){const o=this??s;return o?arguments.length===0?t in o.$context?o.$context[t]:e:o.$context[t]=arguments[0]:e};exports.context=j;exports.html=d;exports.render=c;
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
- const { isArray: d, from: $ } = Array, m = "", { create: v, assign: x, entries: g, prototype: { hasOwnProperty: w }, hasOwn: k = (e, t) => w.call(e, t) } = Object, y = (e) => $(p(e)).join(""), p = function* (e) {
2
- for (e of c(e))
3
- if (typeof e == "string") yield f(e);
4
- else {
5
- const { nodeName: t, skip: o, children: l = "" } = e;
6
- let n = "";
7
- for (const [r, i] of g(e))
8
- S.has(r) || r.startsWith("set:") || i == null || i === !1 || (n += i === !0 ? ` ${r}` : ` ${r}="${f(String(i))}"`);
9
- N.has(t) ? yield `<${t}${n}>` : o ? yield `<${t}${n}></${t}>` : (yield `<${t}${n}>`, typeof l == "string" && l.startsWith(m) ? yield l.slice(1) : yield* p(l), yield `</${t}>`);
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
- }, c = function* (e, t = { value: "" }, o = !0) {
12
- for (e of d(e) ? e : [e])
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
- const n = e.call(o = {
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
- o.return();
57
+ (S = a.return) == null || S.call(a), u(d);
55
58
  }
56
- return m + l;
57
- }, O = (e, t = Symbol()) => function() {
58
- const o = this ?? s;
59
- return o ? arguments.length === 0 ? t in o.$context ? o.$context[t] : e : o.$context[t] = arguments[0] : e;
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
- O as context,
63
- p as html,
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=({children:t})=>t,N=function(t,n){return(n??(n=$(null))).nodeName=t,"children"in n||arguments.length<3||(n.children=arguments.length===3?arguments[2]:k.call(arguments,2)),n},d=(t,n)=>{let e=n.firstChild;for(t of h(t)){let o=e;if(typeof t=="string"){for(;o&&o.nodeType!=3;)o=o.nextSibling;o?o.data!=t&&(o.data=t):o=document.createTextNode(t)}else{const{nodeName:i,key:a,skip:c,memo:s,ref:f,children:w}=t;for(;o&&!(o.localName===i&&(o.$key??(o.$key=a))==a);)o=o.nextSibling;if(o??(o=m(document.createElementNS(t.xmlns??i==="svg"?O:n.namespaceURI,i),{$key:a})),s==null||F(o.$memo,o.$memo=s)){const{$props:y}=o,x={};for(const l of p(m({},y,t))){if(T.has(l))continue;const u=x[l]=t[l];u!==(y==null?void 0:y[l])&&(l.startsWith("set:")?o[l.slice(4)]=u:u==null||u===!1?o.removeAttribute(l):o.setAttribute(l,u===!0?"":u))}o.$props=x,c||d(w,o),typeof f=="function"&&(o.$ref=f)(o)}}o===e?e=e.nextSibling:C(n,o,e)}for(;e;){const o=e.nextSibling;e.nodeType===1&&v(e),n.removeChild(e),e=o}},{isArray:g,prototype:{slice:k}}=Array,{create:$,assign:m,keys:p,prototype:{hasOwnProperty:b},hasOwn:A=(t,n)=>b.call(t,n)}=Object,O="http://www.w3.org/2000/svg",T=new Set("nodeName,key,skip,memo,ref,children".split(",")),F=(t,n)=>g(t)&&g(n)?t.some((e,o)=>e!==n[o]):t!==n,h=function*(t,n={value:""},e=!0){for(t of g(t)?t:[t])if(!(t==null||typeof t=="boolean"))if(A(t,"nodeName")){const{value:o}=n,{nodeName:i}=t;if(o&&(yield o,n.value=""),typeof i=="function")if(i.constructor.name==="GeneratorFunction"){const a={},c=m({},i.attrs);for(const s of p(t)){const f=t[s];s.startsWith("attr:")?c[s.slice(5)]=f:s==="key"||s==="memo"?c[s]=f:a[s]=f}c.nodeName=i.is??"div",c.skip=!0,c.ref=j.bind(null,i,a),yield c}else yield*h(i(t),n,!1);else yield t}else g(t)?yield*h(t,n,!1):n.value+=t;e&&n.value&&(yield n.value)},j=(t,n,e)=>{if(!e)return;e.$generator??(e.$generator=(m(e,E),e.$context??(e.$context=$((r==null?void 0:r.$context)??null)),t));const{skip:o,ref:i,...a}=n;e.$ref=B.bind(null,i,e),m(e.$args??(e.$args={}),a),o||e.next()},B=(t,n,e)=>{typeof t=="function"&&t(e),e||n.return()},C=(t,n,e)=>{if(n.contains(document.activeElement)){const o=n.nextSibling;for(;e&&e!=n;){const i=e.nextSibling;t.insertBefore(e,o),e=i}}else t.insertBefore(n,e)},v=t=>{for(const e of t.children)v(e);const{$ref:n}=t;typeof n=="function"&&n(null)};let r=null;const E={render(){r!=null&&r.contains(this)||this.next()},next(){const t=r;r=this;try{d((this.$iterator??(this.$iterator=this.$generator.call(this,this.$args))).next().value,this),typeof this.$ref=="function"&&this.$ref(this)}catch(n){this.throw(n)}finally{r=t}},throw(t){var n;for(let e=this;e;e=e.parentNode)if(typeof((n=e.$iterator)==null?void 0:n.throw)=="function")try{return d(e.$iterator.throw(t).value,e)}catch{}throw t},return(){var t;try{(t=this.$iterator)==null||t.return()}catch(n){this.throw(n)}finally{this.$iterator=null}}},P=(t,n=Symbol())=>function(){const e=this??r;return e?arguments.length===0?n in e.$context?e.$context[n]:t:e.$context[n]=arguments[0]:t};exports.Fragment=S;exports.context=P;exports.h=N;exports.render=d;
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
- const F = ({ children: t }) => t, W = function(t, n) {
2
- return (n ?? (n = h(null))).nodeName = t, "children" in n || arguments.length < 3 || (n.children = arguments.length === 3 ? arguments[2] : N.call(arguments, 2)), n;
3
- }, g = (t, n) => {
4
- let e = n.firstChild;
5
- for (t of x(t)) {
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 (; e; ) {
26
- const i = e.nextSibling;
27
- e.nodeType === 1 && v(e), n.removeChild(e), e = i;
7
+ for (; n && n != s; ) {
8
+ const e = n.nextSibling;
9
+ n.nodeType == 1 && w(n), r.removeChild(n), n = e;
28
10
  }
29
- }, { isArray: d, prototype: { slice: N } } = Array, { create: h, assign: m, keys: p, prototype: { hasOwnProperty: S }, hasOwn: k = (t, n) => S.call(t, n) } = Object, b = "http://www.w3.org/2000/svg", A = new Set("nodeName,key,skip,memo,ref,children".split(",")), O = (t, n) => d(t) && d(n) ? t.some((e, i) => e !== n[i]) : t !== n, x = function* (t, n = { value: "" }, e = !0) {
30
- for (t of d(t) ? t : [t])
31
- if (!(t == null || typeof t == "boolean"))
32
- if (k(t, "nodeName")) {
33
- const { value: i } = n, { nodeName: o } = t;
34
- if (i && (yield i, n.value = ""), typeof o == "function")
35
- if (o.constructor.name === "GeneratorFunction") {
36
- const a = {}, c = m({}, o.attrs);
37
- for (const s of p(t)) {
38
- const f = t[s];
39
- s.startsWith("attr:") ? c[s.slice(5)] = f : s === "key" || s === "memo" ? c[s] = f : a[s] = f;
40
- }
41
- c.nodeName = o.is ?? "div", c.skip = !0, c.ref = T.bind(null, o, a), yield c;
42
- } else yield* x(o(t), n, !1);
43
- else yield t;
44
- } else d(t) ? yield* x(t, n, !1) : n.value += t;
45
- e && n.value && (yield n.value);
46
- }, T = (t, n, e) => {
47
- if (!e) return;
48
- e.$generator ?? (e.$generator = (m(e, E), e.$context ?? (e.$context = h((r == null ? void 0 : r.$context) ?? null)), t));
49
- const { skip: i, ref: o, ...a } = n;
50
- e.$ref = B.bind(null, o, e), m(e.$args ?? (e.$args = {}), a), i || e.next();
51
- }, B = (t, n, e) => {
52
- typeof t == "function" && t(e), e || n.return();
53
- }, C = (t, n, e) => {
54
- if (n.contains(document.activeElement)) {
55
- const i = n.nextSibling;
56
- for (; e && e != n; ) {
57
- const o = e.nextSibling;
58
- t.insertBefore(e, i), e = o;
59
- }
60
- } else t.insertBefore(n, e);
61
- }, v = (t) => {
62
- for (const e of t.children) v(e);
63
- const { $ref: n } = t;
64
- typeof n == "function" && n(null);
65
- };
66
- let r = null;
67
- const E = {
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
- r != null && r.contains(this) || this.next();
53
+ var t;
54
+ (t = a()) != null && t.contains(this) || this.next();
70
55
  },
71
56
  next() {
72
- const t = r;
73
- r = this;
57
+ var r;
58
+ const t = a();
59
+ a(this);
74
60
  try {
75
- g((this.$iterator ?? (this.$iterator = this.$generator.call(this, this.$args))).next().value, this), typeof this.$ref == "function" && this.$ref(this);
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
- r = t;
66
+ a(t);
80
67
  }
81
68
  },
82
69
  throw(t) {
83
- var n;
84
- for (let e = this; e; e = e.parentNode) if (typeof ((n = e.$iterator) == null ? void 0 : n.throw) == "function") try {
85
- return g(e.$iterator.throw(t).value, e);
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.$iterator) == null || t.return();
94
- } catch (n) {
95
- this.throw(n);
81
+ (t = this[o]) == null || t.return();
82
+ } catch (r) {
83
+ this.throw(r);
96
84
  } finally {
97
- this.$iterator = null;
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
- F as Fragment,
106
- j as context,
107
- W as h,
108
- g as render
90
+ I as Fragment,
91
+ M as h,
92
+ y as render
109
93
  };
@@ -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
@@ -1,6 +1,6 @@
1
1
  ISC License
2
2
 
3
- Copyright (c) 2024, Cristian Falcone
3
+ Copyright (c) 2022-2025, Cristian Falcone
4
4
 
5
5
  Permission to use, copy, modify, and/or distribute this software for any
6
6
  purpose with or without fee is hereby granted, provided that the above
package/package.json CHANGED
@@ -1,32 +1,46 @@
1
1
  {
2
2
  "name": "ajo",
3
- "version": "0.1.21",
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
- "types": "./types.ts"
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": "^22.2.0",
27
- "jsdom": "24.1.1",
28
- "vite": "5.4.0",
29
- "vitest": "2.0.5"
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
- "scripts": {
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 | Function | Component
5
+ type Type = Tag | Stateless | Stateful
6
6
 
7
- type Attrs = Record<string, unknown>
7
+ type Component<TProps extends Props = {}> = Stateless<TProps> | Stateful<TProps>
8
8
 
9
- type VNode<TTag extends Type> = { nodeName: TTag } & Attrs
9
+ type Props = Record<string, unknown>
10
10
 
11
- type Children = any
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 = Tag> = TTag extends keyof HTMLElementTagNameMap
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 AjoAttrs<TElement> = {
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 = Tag> = {
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 Function<TArguments extends Attrs = Attrs> = (args: TArguments) => Children
39
+ type Stateless<TArguments extends Props = {}> = (args: TArguments) => Children
35
40
 
36
- type Component<TArguments extends Attrs = Attrs, TTag extends Tag = 'div'> = {
37
- (this: ComponentElement<TArguments, TTag>, args: ComponentProps<TArguments, TTag>): Iterator<Children, unknown, unknown>
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 ComponentProps<TArguments extends Attrs = Attrs, TTag extends Tag = 'div'> =
41
- Partial<PropSetter<TTag> & AjoAttrs<ComponentElement<TArguments, TTag>>> &
45
+ type StatefulProps<TArguments, TTag> =
46
+ Partial<SpecialProps<StatefulElement<TTag>> & PropSetter<TTag>> &
42
47
  AttrSetter &
43
48
  TArguments
44
49
 
45
- type ComponentElement<TArguments extends Attrs = Attrs, TTag extends Tag = Tag> = ElementType<TTag> & {
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> & AjoAttrs<ElementType<TTag>>> & Attrs
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<TTag extends Tag>(tag: TTag, props?: Attrs | null, ...children: Array<unknown>): VNode<TTag>
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
- }