ajo 0.0.26 → 0.1.2

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/ajo.js ADDED
@@ -0,0 +1,122 @@
1
+ var C = Object.defineProperty;
2
+ var T = (t, e, i) => e in t ? C(t, e, { enumerable: !0, configurable: !0, writable: !0, value: i }) : t[e] = i;
3
+ var f = (t, e, i) => (T(t, typeof e != "symbol" ? e + "" : e, i), i);
4
+ const d = Symbol(), w = Symbol(), m = Symbol(), { isArray: g, prototype: { slice: b } } = Array, { assign: v } = Object, z = ({ children: t }) => t, q = function(t, e) {
5
+ const i = { ...e, nodeName: t }, { length: r } = arguments;
6
+ return "children" in i || r < 3 || (i.children = r === 3 ? arguments[2] : b.call(arguments, 2)), i;
7
+ }, E = (t, e, i) => {
8
+ let r = e.firstChild;
9
+ for (t of p(t)) {
10
+ let n = r;
11
+ if (typeof t == "string") {
12
+ for (; n && n.nodeType != 3; )
13
+ n = n.nextSibling;
14
+ n ? n.data != t && (n.data = t) : n = document.createTextNode(t);
15
+ } else {
16
+ const { xmlns: s = i, nodeName: a, is: c, key: l, skip: o, memo: u, ref: x, children: N, ...k } = t;
17
+ for (; n && !(n.localName === a && (n[d] ?? (n[d] = l)) == l); )
18
+ n = n.nextSibling;
19
+ n ?? (n = M(s, a, c, l)), (u == null || A(n[w], n[w] = u)) && (F(k, n), o || E(N, n, s), typeof x == "function" && x(n));
20
+ }
21
+ n === r ? r = r.nextSibling : H(e, n, r);
22
+ }
23
+ for (; r; ) {
24
+ const n = r.nextSibling;
25
+ e.removeChild(r), r = n;
26
+ }
27
+ }, A = (t, e) => g(t) && g(e) ? t.some((i, r) => i !== e[r]) : t !== e, M = (t, e, i, r) => {
28
+ const n = t ? document.createElementNS(t, e, { is: i }) : document.createElement(e, { is: i });
29
+ return n[d] = r, n;
30
+ }, F = (t, e) => {
31
+ const i = e[m] ?? (e[m] = b.call(e.attributes).reduce((r, { name: n, value: s }) => (r[n] = s, r), {}));
32
+ for (const r in { ...i, ...e[m] = t }) {
33
+ const n = t[r];
34
+ n !== i[r] && (r.startsWith("set:") ? e[r.slice(4)] = n : n == null || n === !1 ? e.removeAttribute(r) : e.setAttribute(r, n === !0 ? "" : n));
35
+ }
36
+ }, H = (t, e, i) => {
37
+ if (e.contains(document.activeElement)) {
38
+ const r = e.nextSibling;
39
+ for (; i && i != e; ) {
40
+ const n = i.nextSibling;
41
+ t.insertBefore(i, r), i = n;
42
+ }
43
+ } else
44
+ t.insertBefore(e, i);
45
+ }, p = function* (t, e = { value: "" }, i = !0) {
46
+ for (t of g(t) ? t : [t]) {
47
+ if (t == null || typeof t == "boolean")
48
+ continue;
49
+ const { nodeName: r, ...n } = t, s = typeof r;
50
+ s === "string" ? (e.value && (yield e.value, e.value = ""), yield t) : s === "function" ? yield* p(r(n), e, !1) : g(t) ? yield* p(t, e, !1) : e.value += t;
51
+ }
52
+ i && e.value && (yield e.value);
53
+ };
54
+ let B = 0, h = null, y, S;
55
+ const G = (t, { as: e } = {}) => {
56
+ var n;
57
+ if (((n = t == null ? void 0 : t.constructor) == null ? void 0 : n.name) !== "GeneratorFunction")
58
+ throw new TypeError("fn is not a generator function");
59
+ const i = `host-${B++}`;
60
+ class r extends W(e) {
61
+ constructor() {
62
+ super(...arguments);
63
+ f(this, "generator");
64
+ f(this, "args");
65
+ f(this, "ref");
66
+ }
67
+ disconnectedCallback() {
68
+ this.return();
69
+ }
70
+ refresh() {
71
+ L(this);
72
+ }
73
+ next({ method: c = "next", argument: l } = {}) {
74
+ try {
75
+ const { done: o, value: u } = (this.generator ?? (this.generator = t.call(this, this.args)))[c](l);
76
+ if (o)
77
+ return c !== "return" && this.return(u);
78
+ E(u, this), typeof this.ref == "function" && this.ref(this);
79
+ } catch (o) {
80
+ this.throw(o);
81
+ }
82
+ }
83
+ throw(c) {
84
+ var l;
85
+ for (let o = this.parentNode; o; o = o.parentNode)
86
+ if (typeof ((l = o.generator) == null ? void 0 : l.throw) == "function")
87
+ return o.next({ method: "throw", argument: c });
88
+ throw c;
89
+ }
90
+ return(c) {
91
+ try {
92
+ this.next({ method: "return", argument: c });
93
+ } finally {
94
+ this.generator = null;
95
+ }
96
+ }
97
+ *[Symbol.iterator]() {
98
+ for (; this.isConnected; )
99
+ yield this.args;
100
+ }
101
+ }
102
+ return customElements.define(i, r, { extends: e }), ({ attrs: s = {}, args: a = {}, ref: c, ...l }) => {
103
+ for (const o in l)
104
+ o.startsWith("arg:") ? a[o.slice(4)] = l[o] : (o === "children" ? a : s)[o] = l[o];
105
+ return q(e || i, v(s, { is: e && i, skip: !0, ref: (o) => v(o, { args: a, ref: c }).refresh() }));
106
+ };
107
+ }, L = (t) => {
108
+ (y ?? (y = /* @__PURE__ */ new Set())).add(t), h ?? (h = requestAnimationFrame(() => {
109
+ for (const e of y)
110
+ e.isConnected && e.next();
111
+ y.clear(), h = null;
112
+ }));
113
+ }, W = (t) => {
114
+ let e = (S ?? (S = /* @__PURE__ */ new Map())).get(t);
115
+ return e || (e = document.createElement(t).constructor, e === HTMLUnknownElement && (e = HTMLElement), S.set(t, e)), e;
116
+ };
117
+ export {
118
+ z as Fragment,
119
+ G as component,
120
+ q as h,
121
+ E as render
122
+ };
@@ -0,0 +1 @@
1
+ (function(s,l){typeof exports=="object"&&typeof module<"u"?l(exports):typeof define=="function"&&define.amd?define(["exports"],l):(s=typeof globalThis<"u"?globalThis:s||self,l(s.Ajo={}))})(this,function(s){"use strict";var W=Object.defineProperty;var z=(s,l,f)=>l in s?W(s,l,{enumerable:!0,configurable:!0,writable:!0,value:f}):s[l]=f;var p=(s,l,f)=>(z(s,typeof l!="symbol"?l+"":l,f),f);const l=Symbol(),f=Symbol(),y=Symbol(),{isArray:g,prototype:{slice:w}}=Array,{assign:S}=Object,k=({children:t})=>t,b=function(t,e){const o={...e,nodeName:t},{length:i}=arguments;return"children"in o||i<3||(o.children=i===3?arguments[2]:w.call(arguments,2)),o},h=(t,e,o)=>{let i=e.firstChild;for(t of x(t)){let n=i;if(typeof t=="string"){for(;n&&n.nodeType!=3;)n=n.nextSibling;n?n.data!=t&&(n.data=t):n=document.createTextNode(t)}else{const{xmlns:u=o,nodeName:d,is:a,key:c,skip:r,memo:m,ref:N,children:L,...O}=t;for(;n&&!(n.localName===d&&(n[l]??(n[l]=c))==c);)n=n.nextSibling;n??(n=C(u,d,a,c)),(m==null||A(n[f],n[f]=m))&&(M(O,n),r||h(L,n,u),typeof N=="function"&&N(n))}n===i?i=i.nextSibling:j(e,n,i)}for(;i;){const n=i.nextSibling;e.removeChild(i),i=n}},A=(t,e)=>g(t)&&g(e)?t.some((o,i)=>o!==e[i]):t!==e,C=(t,e,o,i)=>{const n=t?document.createElementNS(t,e,{is:o}):document.createElement(e,{is:o});return n[l]=i,n},M=(t,e)=>{const o=e[y]??(e[y]=w.call(e.attributes).reduce((i,{name:n,value:u})=>(i[n]=u,i),{}));for(const i in{...o,...e[y]=t}){const n=t[i];n!==o[i]&&(i.startsWith("set:")?e[i.slice(4)]=n:n==null||n===!1?e.removeAttribute(i):e.setAttribute(i,n===!0?"":n))}},j=(t,e,o)=>{if(e.contains(document.activeElement)){const i=e.nextSibling;for(;o&&o!=e;){const n=o.nextSibling;t.insertBefore(o,i),o=n}}else t.insertBefore(e,o)},x=function*(t,e={value:""},o=!0){for(t of g(t)?t:[t]){if(t==null||typeof t=="boolean")continue;const{nodeName:i,...n}=t,u=typeof i;u==="string"?(e.value&&(yield e.value,e.value=""),yield t):u==="function"?yield*x(i(n),e,!1):g(t)?yield*x(t,e,!1):e.value+=t}o&&e.value&&(yield e.value)};let q=0,E=null,v,T;const F=(t,{as:e}={})=>{var n;if(((n=t==null?void 0:t.constructor)==null?void 0:n.name)!=="GeneratorFunction")throw new TypeError("fn is not a generator function");const o=`host-${q++}`;class i extends B(e){constructor(){super(...arguments);p(this,"generator");p(this,"args");p(this,"ref")}disconnectedCallback(){this.return()}refresh(){H(this)}next({method:a="next",argument:c}={}){try{const{done:r,value:m}=(this.generator??(this.generator=t.call(this,this.args)))[a](c);if(r)return a!=="return"&&this.return(m);h(m,this),typeof this.ref=="function"&&this.ref(this)}catch(r){this.throw(r)}}throw(a){var c;for(let r=this.parentNode;r;r=r.parentNode)if(typeof((c=r.generator)==null?void 0:c.throw)=="function")return r.next({method:"throw",argument:a});throw a}return(a){try{this.next({method:"return",argument:a})}finally{this.generator=null}}*[Symbol.iterator](){for(;this.isConnected;)yield this.args}}return customElements.define(o,i,{extends:e}),({attrs:u={},args:d={},ref:a,...c})=>{for(const r in c)r.startsWith("arg:")?d[r.slice(4)]=c[r]:(r==="children"?d:u)[r]=c[r];return b(e||o,S(u,{is:e&&o,skip:!0,ref:r=>S(r,{args:d,ref:a}).refresh()}))}},H=t=>{(v??(v=new Set)).add(t),E??(E=requestAnimationFrame(()=>{for(const e of v)e.isConnected&&e.next();v.clear(),E=null}))},B=t=>{let e=(T??(T=new Map)).get(t);return e||(e=document.createElement(t).constructor,e===HTMLUnknownElement&&(e=HTMLElement),T.set(t,e)),e};s.Fragment=k,s.component=F,s.h=b,s.render=h,Object.defineProperty(s,Symbol.toStringTag,{value:"Module"})});
package/package.json CHANGED
@@ -1,22 +1,24 @@
1
1
  {
2
2
  "name": "ajo",
3
- "version": "0.0.26",
4
- "description": "ajo is a JavaScript view library for building user interfaces",
3
+ "version": "0.1.2",
4
+ "description": "Ajo is a JavaScript view library for building user interfaces",
5
5
  "type": "module",
6
- "module": "index.js",
7
- "main": "index.cjs",
8
- "unpkg": "index.min.js",
6
+ "module": "./dist/ajo.js",
7
+ "main": "./dist/ajo.umd.cjs",
9
8
  "exports": {
10
9
  ".": {
11
- "import": "./index.js",
12
- "require": "./index.cjs",
13
- "browser": "./index.min.js"
10
+ "import": "./dist/ajo.js",
11
+ "require": "./dist/ajo.umd.cjs"
14
12
  }
15
13
  },
16
14
  "files": [
17
- "index.js",
18
- "index.min.js"
15
+ "dist"
19
16
  ],
17
+ "devDependencies": {
18
+ "jsdom": "^22.1.0",
19
+ "vite": "^4.5.0",
20
+ "vitest": "^0.34.6"
21
+ },
20
22
  "keywords": [
21
23
  "ui",
22
24
  "frontend",
@@ -24,21 +26,15 @@
24
26
  "dom",
25
27
  "jsx"
26
28
  ],
27
- "scripts": {
28
- "build": "npm run build:require && npm run build:browser",
29
- "build:require": "esbuild --format=cjs --out-extension:.js=.cjs --outdir=. index.js",
30
- "build:browser": "esbuild --format=iife --out-extension:.js=.min.js --outdir=. --global-name=ajo --minify index.js",
31
- "release": "npm test && git commit -am \"$npm_package_version\" && git tag $npm_package_version && git push && git push --tags && npm publish",
32
- "test": "npm run build && uvu"
33
- },
34
29
  "repository": "cristianfalcone/ajo",
35
30
  "author": "Cristian Falcone",
36
31
  "license": "ISC",
37
32
  "bugs": "https://github.com/cristianfalcone/ajo/issues",
38
33
  "homepage": "https://github.com/cristianfalcone/ajo#readme",
39
- "devDependencies": {
40
- "backdom": "^0.0.8",
41
- "esbuild": "^0.15.10",
42
- "uvu": "^0.5.6"
34
+ "scripts": {
35
+ "test": "vitest --run",
36
+ "build": "vite build",
37
+ "bump": "pnpm version patch && git push && git push --tags",
38
+ "release": "pnpm test && pnpm build && pnpm bump && pnpm publish"
43
39
  }
44
- }
40
+ }
package/readme.md CHANGED
@@ -1,5 +1,5 @@
1
- # ajo
2
- ajo is a JavaScript view library for building user interfaces
1
+ # Ajo
2
+ Ajo is a JavaScript view library for building user interfaces
3
3
 
4
4
  ## install
5
5
 
@@ -7,7 +7,7 @@ ajo is a JavaScript view library for building user interfaces
7
7
  npm install ajo
8
8
  ```
9
9
 
10
- ## render a JSX element to a DOM element
10
+ ## render JSX into DOM
11
11
 
12
12
  ```jsx
13
13
  /** @jsx h */
@@ -18,7 +18,7 @@ document.body.innerHTML = '<div>Hello World</div>'
18
18
  render(<div>Goodbye World</div>, document.body)
19
19
  ```
20
20
 
21
- ## function element (stateless)
21
+ ## stateless component
22
22
 
23
23
  ```jsx
24
24
  /** @jsx h */
@@ -29,25 +29,30 @@ const Greet = ({ name }) => <div>Hello {name}</div>
29
29
  render(<Greet name="World" />, document.body)
30
30
  ```
31
31
 
32
- ## component element (stateful)
32
+ ## stateful component
33
33
 
34
34
  ```jsx
35
35
  /** @jsx h */
36
- import { h, component, useState, render } from 'ajo'
36
+ import { h, render, component } from 'ajo'
37
37
 
38
- const Counter = component(() => {
38
+ const Counter = component(function* ({ init = 0 }) {
39
39
 
40
- const [count, setCount] = useState(0)
40
+ let count = init
41
41
 
42
- return (
43
- <button set:onclick={() => setCount(count + 1)}>
44
- Current: {count}
45
- </button>
42
+ const handleClick = () => {
43
+ count++
44
+ this.refresh()
45
+ }
46
+
47
+ while (true) yield (
48
+ <button set:onclick={handleClick}>
49
+ Current: {count}
50
+ </button>
46
51
  )
47
52
  })
48
53
 
49
- render(<Counter />, document.body)
54
+ render(<Counter init={5} />, document.body)
50
55
  ```
51
56
 
52
57
  ## acknowledgments
53
- ajo takes heavy inspiration from [Incremental DOM](https://github.com/google/incremental-dom) and [React](https://github.com/facebook/react)
58
+ Ajo takes heavy inspiration from [Incremental DOM](https://github.com/google/incremental-dom) and [Crank.js](https://github.com/bikeshaving/crank)
package/index.cjs DELETED
@@ -1,284 +0,0 @@
1
- var __defProp = Object.defineProperty;
2
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
3
- var __getOwnPropNames = Object.getOwnPropertyNames;
4
- var __hasOwnProp = Object.prototype.hasOwnProperty;
5
- var __export = (target, all) => {
6
- for (var name in all)
7
- __defProp(target, name, { get: all[name], enumerable: true });
8
- };
9
- var __copyProps = (to, from2, except, desc) => {
10
- if (from2 && typeof from2 === "object" || typeof from2 === "function") {
11
- for (let key of __getOwnPropNames(from2))
12
- if (!__hasOwnProp.call(to, key) && key !== except)
13
- __defProp(to, key, { get: () => from2[key], enumerable: !(desc = __getOwnPropDesc(from2, key)) || desc.enumerable });
14
- }
15
- return to;
16
- };
17
- var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
18
- var ajo_exports = {};
19
- __export(ajo_exports, {
20
- Fragment: () => Fragment,
21
- component: () => component,
22
- h: () => h,
23
- render: () => render,
24
- useCallback: () => useCallback,
25
- useCatch: () => useCatch,
26
- useEffect: () => useEffect,
27
- useHost: () => useHost,
28
- useInsert: () => useInsert,
29
- useLayout: () => useLayout,
30
- useMemo: () => useMemo,
31
- useReducer: () => useReducer,
32
- useRef: () => useRef,
33
- useState: () => useState
34
- });
35
- module.exports = __toCommonJS(ajo_exports);
36
- const Fragment = ({ children }) => children, h = (nodeName, props, ...children) => {
37
- const { length } = children;
38
- children = length == 0 ? null : length == 1 ? children[0] : children;
39
- return { children, ...props, nodeName };
40
- }, render = (h2, host, ns) => {
41
- let child = host.firstChild;
42
- for (h2 of normalize(h2)) {
43
- let node = child;
44
- if (h2 instanceof Node)
45
- node = h2;
46
- else if (typeof h2 == "string") {
47
- while (node && node.nodeType != 3)
48
- node = node.nextSibling;
49
- node ? node.data != h2 && (node.data = h2) : node = document.createTextNode(h2);
50
- } else {
51
- const { xmlns = ns, nodeName, key, ref, memo, children, [FN]: fn, ...props } = h2;
52
- while (node && !(node.localName == nodeName && is(node.$key ??= key, key)))
53
- node = node.nextSibling;
54
- node ??= create(xmlns, nodeName, key);
55
- if (isObject(ref)) {
56
- ref.current = node;
57
- node.$ref = ref;
58
- }
59
- if (memo == null || some(node.$deps, node.$deps = memo)) {
60
- update(props, node);
61
- isFunction(fn) ? fn(node) : render(children, node, xmlns);
62
- }
63
- }
64
- node == child ? child = child.nextSibling : before(host, node, child);
65
- }
66
- while (child) {
67
- const next = child.nextSibling;
68
- dispose(child);
69
- host.removeChild(child);
70
- child = next;
71
- }
72
- }, component = (fn) => ({ nodeName, as, props, key, ref, memo, ...args }) => h(as ?? fn?.as ?? "c-host", {
73
- ...fn?.props,
74
- ...props,
75
- key,
76
- ref,
77
- memo,
78
- [FN]: (host) => {
79
- host.$fn = isFunction(fn) ? fn : noop;
80
- host.$args = { ...fn?.args, ...args };
81
- schedule(host);
82
- }
83
- }), useReducer = (fn, init) => {
84
- const host = useHost(), hooks = useHooks(), [i, stack] = hooks;
85
- if (i == stack.length)
86
- stack[i] = [
87
- isFunction(init) ? init() : init,
88
- (value) => {
89
- const prev = stack[i][0], next = isFunction(value) ? value(prev) : value;
90
- if (is(prev, stack[i][0] = isFunction(fn) ? fn(prev, next) : next))
91
- return;
92
- runMutation(host);
93
- }
94
- ];
95
- return stack[hooks[0]++];
96
- }, useMemo = (fn, deps) => {
97
- const hooks = useHooks(), [i, stack] = hooks;
98
- if (i == stack.length || deps == null || some(deps, stack[i][1]))
99
- stack[i] = [fn(), deps];
100
- return stack[hooks[0]++][0];
101
- }, useCatch = (fn) => {
102
- const host = useHost(), [value, setValue] = useReducer(), hooks = useHooks(), [i, stack] = hooks;
103
- stack[hooks[0]++] = fn;
104
- host.$catch ??= (value2) => {
105
- isFunction(stack[i]) && stack[i](value2);
106
- setValue(value2);
107
- };
108
- return [value, () => setValue()];
109
- }, useHost = () => current, useState = (init) => useReducer(null, init), useRef = (current2) => useMemo(() => ({ current: current2 }), []), useCallback = (fn, deps) => useMemo(() => fn, deps), useInsert = (fn, deps) => useFx(fn, deps, "$insert"), useLayout = (fn, deps) => useFx(fn, deps, "$layout"), useEffect = (fn, deps) => useFx(fn, deps, "$effect");
110
- const { isArray, from } = Array, { is } = Object, noop = () => {
111
- }, FN = Symbol(), isObject = (v) => v && typeof v == "object", isFunction = (v) => typeof v == "function", some = (a, b) => isArray(a) && isArray(b) ? a.some((v, i) => !is(v, b[i])) : !is(a, b), reduce = (v) => from(v).reduce(assign, {}), assign = (v, { name, value }) => (v[name] = value, v), microtask = globalThis.queueMicrotask ?? ((fn) => fn()), task = globalThis.requestAnimationFrame ?? microtask, create = (ns, name, key) => {
112
- const node = ns ? document.createElementNS(ns, name) : document.createElement(name);
113
- return node.$key = key, node;
114
- }, normalize = function* (h2, buffer = { data: "" }, root = true) {
115
- let data;
116
- for (h2 of isArray(h2) ? h2 : [h2]) {
117
- if (h2 == null || typeof h2 == "boolean")
118
- continue;
119
- if (typeof h2.nodeName == "string")
120
- (data = buffer.data) && (buffer.data = "", yield data), yield h2;
121
- else if (isFunction(h2.nodeName))
122
- yield* normalize(h2.nodeName(h2), buffer, false);
123
- else
124
- isArray(h2) ? yield* normalize(h2, buffer, false) : buffer.data += h2;
125
- }
126
- root && (data = buffer.data) && (yield data);
127
- }, update = (props, host) => {
128
- const prev = host.$props ??= host.hasAttributes() ? reduce(host.attributes) : {};
129
- for (const name in { ...prev, ...host.$props = props }) {
130
- let value = props[name];
131
- if (value !== prev[name])
132
- if (name.startsWith("set:"))
133
- host[name.slice(4)] = value;
134
- else if (value == null || value === false)
135
- host.removeAttribute(name);
136
- else
137
- host.setAttribute(name, value === true ? "" : value);
138
- }
139
- }, before = (host, node, child) => {
140
- if (node.contains?.(document.activeElement)) {
141
- const ref = node.nextSibling;
142
- while (child && child != node) {
143
- const next = child.nextSibling;
144
- host.insertBefore(child, ref);
145
- child = next;
146
- }
147
- } else
148
- host.insertBefore(node, child);
149
- }, useHooks = () => current.$hooks ??= [0, []], useFx = (fn, deps, key) => {
150
- const host = useHost(), hooks = useHooks(), [i, stack] = hooks, init = i == stack.length;
151
- if (init)
152
- (host[key] ??= /* @__PURE__ */ new Set()).add(stack[i] = [null, fn, deps]);
153
- else if (deps == null || some(deps, stack[i][2])) {
154
- stack[i][1] = fn;
155
- stack[i][2] = deps;
156
- }
157
- hooks[0]++;
158
- }, schedule = (host) => {
159
- if (host.$idle)
160
- return;
161
- if (globalThis.navigator?.scheduling?.isInputPending()) {
162
- idleQueue.add(host);
163
- host.$idle = true;
164
- idleId ??= requestIdleCallback(runIdle);
165
- return;
166
- }
167
- run(host);
168
- }, run = (host) => {
169
- if (host.$idle) {
170
- idleQueue.delete(host);
171
- host.$idle = false;
172
- }
173
- current = host;
174
- if (current.$hooks)
175
- current.$hooks[0] = 0;
176
- try {
177
- host.$h = host.$fn(host.$args);
178
- } catch (value) {
179
- propagate(value, host.parentNode);
180
- } finally {
181
- current = null;
182
- layoutQueue.add(host);
183
- host.$layoutQueued = true;
184
- layoutId ??= task(runLayout);
185
- }
186
- }, runIdle = () => {
187
- idleId = null;
188
- for (const host of from(idleQueue)) {
189
- idleQueue.delete(host);
190
- host.$idle = false;
191
- schedule(host);
192
- }
193
- }, runMutation = (host) => {
194
- if (host.$runQueued)
195
- return;
196
- host.$runQueued = true;
197
- microtask(() => {
198
- host.$runQueued = false;
199
- run(host);
200
- });
201
- }, runLayout = () => {
202
- layoutId = null;
203
- for (const host of layoutQueue) {
204
- layoutQueue.delete(host);
205
- host.$layoutQueued = false;
206
- runFx(host, "$insert");
207
- try {
208
- render(host.$h, host);
209
- host.$h = null;
210
- } catch (value) {
211
- propagate(value, host);
212
- } finally {
213
- runFx(host, "$layout");
214
- effectQueue.add(host);
215
- host.$effectQueued = true;
216
- effectId ??= task(runEffect);
217
- }
218
- }
219
- }, runEffect = () => {
220
- effectId = null;
221
- for (const host of effectQueue) {
222
- effectQueue.delete(host);
223
- host.$effectQueued = false;
224
- runFx(host, "$effect");
225
- }
226
- }, runFx = (host, key) => {
227
- if (host[key])
228
- for (const fx of host[key]) {
229
- const [cleanup, setup] = fx;
230
- if (isFunction(setup)) {
231
- try {
232
- if (isFunction(cleanup))
233
- cleanup();
234
- fx[0] = setup();
235
- } catch (value) {
236
- fx[0] = null;
237
- propagate(value, host.parentNode);
238
- } finally {
239
- fx[1] = null;
240
- }
241
- }
242
- }
243
- }, dispose = (host) => {
244
- if (host.nodeType != 1)
245
- return;
246
- for (const child of host.children)
247
- dispose(child);
248
- if (host.$ref)
249
- host.$ref.current = null;
250
- if (!host.$fn)
251
- return;
252
- if (host.$idle) {
253
- idleQueue.delete(host);
254
- host.$idle = false;
255
- }
256
- if (host.$layoutQueued) {
257
- layoutQueue.delete(host);
258
- host.$layoutQueued = false;
259
- }
260
- if (host.$effectQueued) {
261
- effectQueue.delete(host);
262
- host.$effectQueued = false;
263
- }
264
- for (const key of ["$insert", "$layout", "$effect"])
265
- if (host[key]) {
266
- for (const fx of host[key]) {
267
- host[key].delete(fx);
268
- try {
269
- const [cleanup] = fx;
270
- isFunction(cleanup) && cleanup();
271
- } catch (value) {
272
- propagate(value, host.parentNode);
273
- } finally {
274
- fx[0] = fx[1] = null;
275
- }
276
- }
277
- }
278
- }, propagate = (value, host) => {
279
- for (let fn; host; host = host.parentNode)
280
- if (isFunction(fn = host.$catch))
281
- return void fn(value);
282
- throw value;
283
- };
284
- let current = null, layoutQueue = /* @__PURE__ */ new Set(), effectQueue = /* @__PURE__ */ new Set(), idleQueue = /* @__PURE__ */ new Set(), layoutId = null, effectId = null, idleId = null;
package/index.js DELETED
@@ -1,369 +0,0 @@
1
- export const
2
-
3
- Fragment = ({ children }) => children,
4
-
5
- h = (nodeName, props, ...children) => {
6
- const { length } = children
7
- children = length == 0 ? null : length == 1 ? children[0] : children
8
- return { children, ...props, nodeName }
9
- },
10
-
11
- render = (h, host, ns) => {
12
-
13
- let child = host.firstChild
14
-
15
- for (h of normalize(h)) {
16
-
17
- let node = child
18
-
19
- if (h instanceof Node) node = h
20
-
21
- else if (typeof h == 'string') {
22
-
23
- while (node && node.nodeType != 3) node = node.nextSibling
24
- node ? node.data != h && (node.data = h) : node = document.createTextNode(h)
25
-
26
- } else {
27
-
28
- const { xmlns = ns, nodeName, key, ref, memo, children, [FN]: fn, ...props } = h
29
-
30
- while (node && !(node.localName == nodeName && is(node.$key ??= key, key))) node = node.nextSibling
31
- node ??= create(xmlns, nodeName, key)
32
-
33
- if (isObject(ref)) {
34
- ref.current = node
35
- node.$ref = ref
36
- }
37
-
38
- if (memo == null || some(node.$deps, node.$deps = memo)) {
39
- update(props, node)
40
- isFunction(fn) ? fn(node) : render(children, node, xmlns)
41
- }
42
- }
43
-
44
- node == child ? child = child.nextSibling : before(host, node, child)
45
- }
46
-
47
- while (child) {
48
- const next = child.nextSibling
49
- dispose(child)
50
- host.removeChild(child)
51
- child = next
52
- }
53
- },
54
-
55
- component = fn => ({ nodeName, as, props, key, ref, memo, ...args }) =>
56
-
57
- h(as ?? fn?.as ?? 'c-host', {
58
-
59
- ...fn?.props, ...props, key, ref, memo, [FN]: host => {
60
-
61
- host.$fn = isFunction(fn) ? fn : noop
62
- host.$args = { ...fn?.args, ...args }
63
-
64
- schedule(host)
65
- }
66
- }),
67
-
68
- useReducer = (fn, init) => {
69
-
70
- const host = useHost(), hooks = useHooks(), [i, stack] = hooks
71
-
72
- if (i == stack.length) stack[i] = [
73
-
74
- isFunction(init) ? init() : init,
75
-
76
- value => {
77
-
78
- const prev = stack[i][0], next = isFunction(value) ? value(prev) : value
79
-
80
- if (is(prev, stack[i][0] = isFunction(fn) ? fn(prev, next) : next)) return
81
-
82
- runMutation(host)
83
- }
84
- ]
85
-
86
- return stack[hooks[0]++]
87
- },
88
-
89
- useMemo = (fn, deps) => {
90
-
91
- const hooks = useHooks(), [i, stack] = hooks
92
-
93
- if (i == stack.length || deps == null || some(deps, stack[i][1])) stack[i] = [fn(), deps]
94
-
95
- return stack[hooks[0]++][0]
96
- },
97
-
98
- useCatch = fn => {
99
-
100
- const host = useHost(), [value, setValue] = useReducer(), hooks = useHooks(), [i, stack] = hooks
101
-
102
- stack[hooks[0]++] = fn
103
-
104
- host.$catch ??= value => {
105
- isFunction(stack[i]) && stack[i](value)
106
- setValue(value)
107
- }
108
-
109
- return [value, () => setValue()]
110
- },
111
-
112
- useHost = () => current,
113
- useState = init => useReducer(null, init),
114
-
115
- useRef = current => useMemo(() => ({ current }), []),
116
- useCallback = (fn, deps) => useMemo(() => fn, deps),
117
-
118
- useInsert = (fn, deps) => useFx(fn, deps, '$insert'),
119
- useLayout = (fn, deps) => useFx(fn, deps, '$layout'),
120
- useEffect = (fn, deps) => useFx(fn, deps, '$effect')
121
-
122
- const
123
-
124
- { isArray, from } = Array, { is } = Object, noop = () => { }, FN = Symbol(),
125
-
126
- isObject = v => v && typeof v == 'object', isFunction = v => typeof v == 'function',
127
-
128
- some = (a, b) => (isArray(a) && isArray(b)) ? a.some((v, i) => !is(v, b[i])) : !is(a, b),
129
-
130
- reduce = v => from(v).reduce(assign, {}), assign = (v, { name, value }) => ((v[name] = value), v),
131
-
132
- microtask = globalThis.queueMicrotask ?? (fn => fn()), task = globalThis.requestAnimationFrame ?? microtask,
133
-
134
- create = (ns, name, key) => {
135
- const node = ns ? document.createElementNS(ns, name) : document.createElement(name)
136
- return node.$key = key, node
137
- },
138
-
139
- normalize = function* (h, buffer = { data: '' }, root = true) {
140
-
141
- let data
142
-
143
- for (h of isArray(h) ? h : [h]) {
144
- if (h == null || typeof h == 'boolean') continue
145
- if (typeof h.nodeName == 'string') ((data = buffer.data) && (buffer.data = '', yield data)), yield h
146
- else if (isFunction(h.nodeName)) yield* normalize(h.nodeName(h), buffer, false)
147
- else isArray(h) ? yield* normalize(h, buffer, false) : buffer.data += h
148
- }
149
-
150
- root && (data = buffer.data) && (yield data)
151
- },
152
-
153
- update = (props, host) => {
154
-
155
- const prev = host.$props ??= host.hasAttributes() ? reduce(host.attributes) : {}
156
-
157
- for (const name in { ...prev, ...(host.$props = props) }) {
158
-
159
- let value = props[name]
160
-
161
- if (value !== prev[name])
162
- if (name.startsWith('set:')) host[name.slice(4)] = value
163
- else if (value == null || value === false) host.removeAttribute(name)
164
- else host.setAttribute(name, value === true ? '' : value)
165
- }
166
- },
167
-
168
- before = (host, node, child) => {
169
-
170
- if (node.contains?.(document.activeElement)) {
171
-
172
- const ref = node.nextSibling
173
-
174
- while (child && child != node) {
175
- const next = child.nextSibling
176
- host.insertBefore(child, ref)
177
- child = next
178
- }
179
-
180
- } else host.insertBefore(node, child)
181
- },
182
-
183
- useHooks = () => current.$hooks ??= [0, []],
184
-
185
- useFx = (fn, deps, key) => {
186
-
187
- const host = useHost(), hooks = useHooks(), [i, stack] = hooks, init = i == stack.length
188
-
189
- if (init) (host[key] ??= new Set).add(stack[i] = [null, fn, deps])
190
-
191
- else if (deps == null || some(deps, stack[i][2])) {
192
- stack[i][1] = fn
193
- stack[i][2] = deps
194
- }
195
-
196
- hooks[0]++
197
- },
198
-
199
- schedule = host => {
200
-
201
- if (host.$idle) return
202
-
203
- if (globalThis.navigator?.scheduling?.isInputPending()) {
204
- idleQueue.add(host)
205
- host.$idle = true
206
- idleId ??= requestIdleCallback(runIdle)
207
- return
208
- }
209
-
210
- run(host)
211
- },
212
-
213
- run = host => {
214
-
215
- if (host.$idle) {
216
- idleQueue.delete(host)
217
- host.$idle = false
218
- }
219
-
220
- current = host
221
-
222
- if (current.$hooks) current.$hooks[0] = 0
223
-
224
- try {
225
- host.$h = host.$fn(host.$args)
226
- } catch (value) {
227
- propagate(value, host.parentNode)
228
- } finally {
229
- current = null
230
- layoutQueue.add(host)
231
- host.$layoutQueued = true
232
- layoutId ??= task(runLayout)
233
- }
234
- },
235
-
236
- runIdle = () => {
237
-
238
- idleId = null
239
-
240
- for (const host of from(idleQueue)) {
241
- idleQueue.delete(host)
242
- host.$idle = false
243
- schedule(host)
244
- }
245
- },
246
-
247
- runMutation = host => {
248
-
249
- if (host.$runQueued) return
250
-
251
- host.$runQueued = true
252
- microtask(() => {
253
- host.$runQueued = false
254
- run(host)
255
- })
256
- },
257
-
258
- runLayout = () => {
259
-
260
- layoutId = null
261
-
262
- for (const host of layoutQueue) {
263
-
264
- layoutQueue.delete(host)
265
- host.$layoutQueued = false
266
-
267
- runFx(host, '$insert')
268
-
269
- try {
270
- render(host.$h, host)
271
- host.$h = null
272
- } catch (value) {
273
- propagate(value, host)
274
- } finally {
275
- runFx(host, '$layout')
276
- effectQueue.add(host)
277
- host.$effectQueued = true
278
- effectId ??= task(runEffect)
279
- }
280
- }
281
- },
282
-
283
- runEffect = () => {
284
-
285
- effectId = null
286
-
287
- for (const host of effectQueue) {
288
- effectQueue.delete(host)
289
- host.$effectQueued = false
290
- runFx(host, '$effect')
291
- }
292
- },
293
-
294
- runFx = (host, key) => {
295
-
296
- if (host[key]) for (const fx of host[key]) {
297
-
298
- const [cleanup, setup] = fx
299
-
300
- if (isFunction(setup)) {
301
- try {
302
- if (isFunction(cleanup)) cleanup()
303
- fx[0] = setup()
304
- } catch (value) {
305
- fx[0] = null
306
- propagate(value, host.parentNode)
307
- } finally {
308
- fx[1] = null
309
- }
310
- }
311
- }
312
- },
313
-
314
- dispose = host => {
315
-
316
- if (host.nodeType != 1) return
317
-
318
- for (const child of host.children) dispose(child)
319
-
320
- if (host.$ref) host.$ref.current = null
321
-
322
- if (!host.$fn) return
323
-
324
- if (host.$idle) {
325
- idleQueue.delete(host)
326
- host.$idle = false
327
- }
328
-
329
- if (host.$layoutQueued) {
330
- layoutQueue.delete(host)
331
- host.$layoutQueued = false
332
- }
333
-
334
- if (host.$effectQueued) {
335
- effectQueue.delete(host)
336
- host.$effectQueued = false
337
- }
338
-
339
- for (const key of ['$insert', '$layout', '$effect']) if (host[key]) {
340
-
341
- for (const fx of host[key]) {
342
-
343
- host[key].delete(fx)
344
-
345
- try {
346
- const [cleanup] = fx
347
- isFunction(cleanup) && cleanup()
348
- } catch (value) {
349
- propagate(value, host.parentNode)
350
- } finally {
351
- fx[0] = fx[1] = null
352
- }
353
- }
354
- }
355
- },
356
-
357
- propagate = (value, host) => {
358
- for (let fn; host; host = host.parentNode) if (isFunction(fn = host.$catch)) return void fn(value)
359
- throw value
360
- }
361
-
362
- let
363
- current = null,
364
- layoutQueue = new Set,
365
- effectQueue = new Set,
366
- idleQueue = new Set,
367
- layoutId = null,
368
- effectId = null,
369
- idleId = null
package/index.min.js DELETED
@@ -1 +0,0 @@
1
- var ajo=(()=>{var x=Object.defineProperty;var V=Object.getOwnPropertyDescriptor;var W=Object.getOwnPropertyNames;var D=Object.prototype.hasOwnProperty;var G=(e,t)=>{for(var u in t)x(e,u,{get:t[u],enumerable:!0})},J=(e,t,u,l)=>{if(t&&typeof t=="object"||typeof t=="function")for(let n of W(t))!D.call(e,n)&&n!==u&&x(e,n,{get:()=>t[n],enumerable:!(l=V(t,n))||l.enumerable});return e};var K=e=>J(x({},"__esModule",{value:!0}),e);var ye={};G(ye,{Fragment:()=>U,component:()=>X,h:()=>T,render:()=>Q,useCallback:()=>h,useCatch:()=>Y,useEffect:()=>ne,useHost:()=>p,useInsert:()=>ee,useLayout:()=>te,useMemo:()=>w,useReducer:()=>S,useRef:()=>_,useState:()=>Z});const U=({children:e})=>e,T=(e,t,...u)=>{const{length:l}=u;return u=l==0?null:l==1?u[0]:u,{children:u,...t,nodeName:e}},Q=(e,t,u)=>{let l=t.firstChild;for(e of b(e)){let n=l;if(e instanceof Node)n=e;else if(typeof e=="string"){for(;n&&n.nodeType!=3;)n=n.nextSibling;n?n.data!=e&&(n.data=e):n=document.createTextNode(e)}else{const{xmlns:r=u,nodeName:a,key:i,ref:o,memo:A,children:z,[E]:F,...P}=e;for(;n&&!(n.localName==a&&$(n.$key??=i,i));)n=n.nextSibling;n??=ie(r,a,i),ue(o)&&(o.current=n,n.$ref=o),(A==null||v(n.$deps,n.$deps=A))&&(fe(P,n),f(F)?F(n):Q(z,n,r))}n==l?l=l.nextSibling:oe(t,n,l)}for(;l;){const n=l.nextSibling;H(l),t.removeChild(l),l=n}},X=e=>({nodeName:t,as:u,props:l,key:n,ref:r,memo:a,...i})=>T(u??e?.as??"c-host",{...e?.props,...l,key:n,ref:r,memo:a,[E]:o=>{o.$fn=f(e)?e:le,o.$args={...e?.args,...i},M(o)}}),S=(e,t)=>{const u=p(),l=g(),[n,r]=l;return n==r.length&&(r[n]=[f(t)?t():t,a=>{const i=r[n][0],o=f(a)?a(i):a;$(i,r[n][0]=f(e)?e(i,o):o)||de(u)}]),r[l[0]++]},w=(e,t)=>{const u=g(),[l,n]=u;return(l==n.length||t==null||v(t,n[l][1]))&&(n[l]=[e(),t]),n[u[0]++][0]},Y=e=>{const t=p(),[u,l]=S(),n=g(),[r,a]=n;return a[n[0]++]=e,t.$catch??=i=>{f(a[r])&&a[r](i),l(i)},[u,()=>l()]},p=()=>c,Z=e=>S(null,e),_=e=>w(()=>({current:e}),[]),h=(e,t)=>w(()=>e,t),ee=(e,t)=>I(e,t,"$insert"),te=(e,t)=>I(e,t,"$layout"),ne=(e,t)=>I(e,t,"$effect"),{isArray:s,from:C}=Array,{is:$}=Object,le=()=>{},E=Symbol(),ue=e=>e&&typeof e=="object",f=e=>typeof e=="function",v=(e,t)=>s(e)&&s(t)?e.some((u,l)=>!$(u,t[l])):!$(e,t),re=e=>C(e).reduce(ae,{}),ae=(e,{name:t,value:u})=>(e[t]=u,e),j=globalThis.queueMicrotask??(e=>e()),q=globalThis.requestAnimationFrame??j,ie=(e,t,u)=>{const l=e?document.createElementNS(e,t):document.createElement(t);return l.$key=u,l},b=function*(e,t={data:""},u=!0){let l;for(e of s(e)?e:[e])e==null||typeof e=="boolean"||(typeof e.nodeName=="string"?((l=t.data)&&(t.data="",yield l),yield e):f(e.nodeName)?yield*b(e.nodeName(e),t,!1):s(e)?yield*b(e,t,!1):t.data+=e);u&&(l=t.data)&&(yield l)},fe=(e,t)=>{const u=t.$props??=t.hasAttributes()?re(t.attributes):{};for(const l in{...u,...t.$props=e}){let n=e[l];n!==u[l]&&(l.startsWith("set:")?t[l.slice(4)]=n:n==null||n===!1?t.removeAttribute(l):t.setAttribute(l,n===!0?"":n))}},oe=(e,t,u)=>{if(t.contains?.(document.activeElement)){const l=t.nextSibling;for(;u&&u!=t;){const n=u.nextSibling;e.insertBefore(u,l),u=n}}else e.insertBefore(t,u)},g=()=>c.$hooks??=[0,[]],I=(e,t,u)=>{const l=p(),n=g(),[r,a]=n;r==a.length?(l[u]??=new Set).add(a[r]=[null,e,t]):(t==null||v(t,a[r][2]))&&(a[r][1]=e,a[r][2]=t),n[0]++},M=e=>{if(!e.$idle){if(globalThis.navigator?.scheduling?.isInputPending()){d.add(e),e.$idle=!0,R??=requestIdleCallback(ce);return}B(e)}},B=e=>{e.$idle&&(d.delete(e),e.$idle=!1),c=e,c.$hooks&&(c.$hooks[0]=0);try{e.$h=e.$fn(e.$args)}catch(t){k(t,e.parentNode)}finally{c=null,y.add(e),e.$layoutQueued=!0,L??=q(se)}},ce=()=>{R=null;for(const e of C(d))d.delete(e),e.$idle=!1,M(e)},de=e=>{e.$runQueued||(e.$runQueued=!0,j(()=>{e.$runQueued=!1,B(e)}))},se=()=>{L=null;for(const e of y){y.delete(e),e.$layoutQueued=!1,N(e,"$insert");try{Q(e.$h,e),e.$h=null}catch(t){k(t,e)}finally{N(e,"$layout"),m.add(e),e.$effectQueued=!0,O??=q($e)}}},$e=()=>{O=null;for(const e of m)m.delete(e),e.$effectQueued=!1,N(e,"$effect")},N=(e,t)=>{if(e[t])for(const u of e[t]){const[l,n]=u;if(f(n))try{f(l)&&l(),u[0]=n()}catch(r){u[0]=null,k(r,e.parentNode)}finally{u[1]=null}}},H=e=>{if(e.nodeType==1){for(const t of e.children)H(t);if(e.$ref&&(e.$ref.current=null),!!e.$fn){e.$idle&&(d.delete(e),e.$idle=!1),e.$layoutQueued&&(y.delete(e),e.$layoutQueued=!1),e.$effectQueued&&(m.delete(e),e.$effectQueued=!1);for(const t of["$insert","$layout","$effect"])if(e[t])for(const u of e[t]){e[t].delete(u);try{const[l]=u;f(l)&&l()}catch(l){k(l,e.parentNode)}finally{u[0]=u[1]=null}}}}},k=(e,t)=>{for(let u;t;t=t.parentNode)if(f(u=t.$catch))return void u(e);throw e};let c=null,y=new Set,m=new Set,d=new Set,L=null,O=null,R=null;return K(ye);})();