literaljs 7.0.1 → 8.0.0
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/LICENSE +1 -1
- package/README.md +222 -24
- package/build/index.js +1 -2
- package/build/index.m.js +1 -2
- package/build/index.modern.mjs +1 -0
- package/build/index.umd.js +1 -2
- package/package.json +24 -11
- package/build/index.js.map +0 -1
- package/build/index.m.js.map +0 -1
- package/build/index.umd.js.map +0 -1
- package/literalJS.png +0 -0
- package/test/component.test.js +0 -95
- package/test/h.test.js +0 -207
- package/test/lifecycle.test.js +0 -97
- package/test/render.test.js +0 -176
- package/yarn-error.log +0 -4258
package/LICENSE
CHANGED
package/README.md
CHANGED
|
@@ -3,31 +3,229 @@
|
|
|
3
3
|
</p>
|
|
4
4
|
|
|
5
5
|
---
|
|
6
|
+
|
|
6
7
|
# LiteralJS
|
|
7
|
-
### A small JavaScript library for building user interfaces.
|
|
8
8
|
|
|
9
|
-
###
|
|
10
|
-
|
|
9
|
+
### A small, fast JavaScript library for building reactive user interfaces.
|
|
10
|
+
|
|
11
|
+
**v8.0.0** — Zero runtime dependencies. Modern browser targets (Chrome 90+, Firefox 90+, Safari 14+).
|
|
12
|
+
|
|
13
|
+
---
|
|
14
|
+
|
|
15
|
+
## Quick Start
|
|
16
|
+
|
|
17
|
+
### npm
|
|
18
|
+
|
|
19
|
+
```bash
|
|
20
|
+
npm install literaljs
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
```js
|
|
24
|
+
import { h, component, App } from 'literaljs';
|
|
25
|
+
|
|
26
|
+
const Hello = component({
|
|
27
|
+
name: 'Hello',
|
|
28
|
+
state: { count: 0 },
|
|
29
|
+
render() {
|
|
30
|
+
return h('div', {}, [
|
|
31
|
+
h('p', {}, `Count: ${this.getState().count}`),
|
|
32
|
+
h('button', {
|
|
33
|
+
events: { click: () => this.setState({ count: this.getState().count + 1 }) }
|
|
34
|
+
}, 'Increment')
|
|
35
|
+
]);
|
|
36
|
+
}
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
new App(Hello, {}).mount('app');
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
### CDN (UMD)
|
|
43
|
+
|
|
44
|
+
```html
|
|
45
|
+
<script src="https://unpkg.com/literaljs/build/index.umd.js"></script>
|
|
46
|
+
<script>
|
|
47
|
+
const { h, component, App } = LiteralJS;
|
|
48
|
+
// ...same as above
|
|
49
|
+
</script>
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
---
|
|
53
|
+
|
|
54
|
+
## Public API
|
|
55
|
+
|
|
56
|
+
| Export | Purpose |
|
|
57
|
+
|--------|---------|
|
|
58
|
+
| `h(tag, attrs, ...children)` | Create VNode (JSX/Hyperscript/Object syntax) |
|
|
59
|
+
| `component(config)` | Define a component with state, methods, lifecycle |
|
|
60
|
+
| `new App(rootComponent, initialStore)` | Create an app instance |
|
|
61
|
+
| `app.mount(domId)` | Mount to a DOM element |
|
|
62
|
+
| `app.unmount()` | Remove from DOM and clean up |
|
|
63
|
+
| `app.setStore(updates)` | Update global store |
|
|
64
|
+
| `render(rootComponent, domId, store)` | Legacy one-liner (backward-compatible) |
|
|
65
|
+
|
|
66
|
+
---
|
|
67
|
+
|
|
68
|
+
## Architecture Highlights
|
|
69
|
+
|
|
70
|
+
### v8 Performance Features
|
|
71
|
+
|
|
72
|
+
- **Keyed reconciliation** — `key` attribute preserves DOM identity across reordering
|
|
73
|
+
- **WeakMap event delegation** — Fast path skips ancestor walk when no handlers exist
|
|
74
|
+
- **Component render memoization** — Shallow props comparison skips re-execution when props unchanged
|
|
75
|
+
- **Text node fast path** — Updates `textContent` in-place instead of `replaceChild`
|
|
76
|
+
- **Reference-equality short-circuits** — Skips diffing for unchanged VNodes, children, and attributes
|
|
77
|
+
- **Deferred patching** — DOM mutations complete before content patches for consistent state
|
|
78
|
+
|
|
79
|
+
### Module Structure
|
|
80
|
+
|
|
81
|
+
```
|
|
82
|
+
App (container)
|
|
83
|
+
├── StateManager — global store + component state
|
|
84
|
+
├── EventManager — WeakMap delegation + event cache
|
|
85
|
+
├── Renderer — DOM creation + virtual DOM diff/patch
|
|
86
|
+
├── LifecycleQueue — mounted/updated/unmounted hooks
|
|
87
|
+
├── DiffEngine — keyed + non-keyed child reconciliation
|
|
88
|
+
└── MountOrchestrator — tree generation + render cycle
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
---
|
|
92
|
+
|
|
93
|
+
## Bundle Size
|
|
94
|
+
|
|
95
|
+
| Format | Size | Gzipped | Brotli |
|
|
96
|
+
|--------|------|---------|--------|
|
|
97
|
+
| ESM (modern) | 13.86 kB | 4.25 kB | 3.91 kB |
|
|
98
|
+
| UMD | 14.06 kB | 4.34 kB | 3.96 kB |
|
|
99
|
+
| CJS | 14.19 kB | 4.35 kB | 3.93 kB |
|
|
100
|
+
|
|
101
|
+
Build target: Chrome 90+, Firefox 90+, Safari 14+ (ES2018). No `Object.assign` polyfill.
|
|
102
|
+
|
|
103
|
+
---
|
|
104
|
+
|
|
105
|
+
## Performance (JSDOM, local benchmarks)
|
|
106
|
+
|
|
107
|
+
| Benchmark | v7 Baseline | v8 Optimized | Change |
|
|
108
|
+
|-----------|-------------|--------------|--------|
|
|
109
|
+
| create 1k rows | ~45ms | ~47ms | stable |
|
|
110
|
+
| replace 1k rows | ~65ms | ~6ms | **-91%** |
|
|
111
|
+
| partial update | ~186ms | ~100ms | **-46%** |
|
|
112
|
+
| select row | ~22ms | ~0.05ms | **-99.8%** |
|
|
113
|
+
| swap rows | — | ~123ms* | *JSDOM artifact; real Chrome near-instant |
|
|
114
|
+
| remove row | ~74ms | ~5ms | **-93%** |
|
|
115
|
+
| clear rows | ~3ms | ~0.01ms | **-99.7%** |
|
|
116
|
+
|
|
117
|
+
*All benchmarks on JSDOM. Real Chrome results for `swap` are expected to be ~1ms (pointer swap).*
|
|
118
|
+
|
|
119
|
+
---
|
|
120
|
+
|
|
121
|
+
## Features
|
|
122
|
+
|
|
123
|
+
- **Small** — ~4.25 kB gzipped, zero dependencies
|
|
124
|
+
- **Fast** — Keyed diffing, WeakMap event delegation, render memoization
|
|
125
|
+
- **Simple** — Plain functions + structured components. No classes, no hooks, no magic.
|
|
126
|
+
- **Virtual DOM** — Diffed updates only on state change
|
|
127
|
+
- **Flexible Syntax** — JSX, Hyperscript (`h()`), or Object syntax
|
|
128
|
+
- **Lifecycle Methods** — `mounted`, `updated`, `unmounted`
|
|
129
|
+
- **Global + Component State** — Built-in store; no Redux needed
|
|
130
|
+
- **Event Delegation** — Most events delegated to root for performance
|
|
131
|
+
- **Isolated Instances** — Multiple `App` instances on one page, no globals
|
|
132
|
+
|
|
133
|
+
---
|
|
134
|
+
|
|
135
|
+
## Component API
|
|
136
|
+
|
|
137
|
+
```js
|
|
138
|
+
const MyComponent = component({
|
|
139
|
+
name: 'MyComponent', // Required, for debugging
|
|
140
|
+
state: { count: 0 }, // Initial component state
|
|
141
|
+
methods() { // Bound functions available as `this.methodName`
|
|
142
|
+
return {
|
|
143
|
+
increment() { this.setState({ count: this.getState().count + 1 }); }
|
|
144
|
+
};
|
|
145
|
+
},
|
|
146
|
+
mounted() { console.log('mounted'); },
|
|
147
|
+
updated() { console.log('updated'); },
|
|
148
|
+
unmounted() { console.log('unmounted'); },
|
|
149
|
+
render() {
|
|
150
|
+
const { count } = this.getState();
|
|
151
|
+
return h('div', {}, [
|
|
152
|
+
h('p', {}, `Count: ${count}`),
|
|
153
|
+
h('button', { events: { click: () => this.increment() } }, 'Add')
|
|
154
|
+
]);
|
|
155
|
+
}
|
|
156
|
+
});
|
|
157
|
+
```
|
|
158
|
+
|
|
159
|
+
### Props
|
|
160
|
+
|
|
161
|
+
```js
|
|
162
|
+
// Parent passes props
|
|
163
|
+
h(MyComponent, { key: 'unique', title: 'Hello' })
|
|
164
|
+
|
|
165
|
+
// Child accesses via this.props
|
|
166
|
+
render() {
|
|
167
|
+
return h('h1', {}, this.props.title);
|
|
168
|
+
}
|
|
169
|
+
```
|
|
170
|
+
|
|
171
|
+
### Keys
|
|
172
|
+
|
|
173
|
+
Use `key` for list items to enable DOM identity preservation:
|
|
174
|
+
|
|
175
|
+
```js
|
|
176
|
+
h('ul', {}, items.map(item =>
|
|
177
|
+
h('li', { key: item.id }, item.name)
|
|
178
|
+
));
|
|
179
|
+
```
|
|
180
|
+
|
|
181
|
+
---
|
|
182
|
+
|
|
183
|
+
## State Management
|
|
184
|
+
|
|
185
|
+
### Component State (private)
|
|
11
186
|
|
|
12
|
-
Recent 7.0.1 microbundle:
|
|
13
187
|
```js
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
188
|
+
this.getState(); // Read current state
|
|
189
|
+
this.setState({ count: 5 }); // Update and trigger re-render
|
|
190
|
+
```
|
|
191
|
+
|
|
192
|
+
### Global Store (shared)
|
|
193
|
+
|
|
194
|
+
```js
|
|
195
|
+
// In any component
|
|
196
|
+
this.setStore({ theme: 'dark' }); // Update global store
|
|
197
|
+
|
|
198
|
+
// Access in render
|
|
199
|
+
render() {
|
|
200
|
+
const theme = this.getStore().theme;
|
|
201
|
+
return h('div', { class: theme }, ...);
|
|
202
|
+
}
|
|
203
|
+
```
|
|
204
|
+
|
|
205
|
+
---
|
|
206
|
+
|
|
207
|
+
## Documentation
|
|
208
|
+
|
|
209
|
+
- **Full docs**: https://literaljs.com
|
|
210
|
+
- **Architecture decisions**: `docs/ADR/`
|
|
211
|
+
- **Phase plans**: `docs/v8-*-plan.md`
|
|
212
|
+
|
|
213
|
+
---
|
|
214
|
+
|
|
215
|
+
## js-framework-benchmark
|
|
216
|
+
|
|
217
|
+
A keyed benchmark entry is included in `js-framework-benchmark-entry/`.
|
|
218
|
+
|
|
219
|
+
```bash
|
|
220
|
+
cd js-framework-benchmark-entry
|
|
221
|
+
npm install
|
|
222
|
+
npm run build-prod
|
|
223
|
+
```
|
|
224
|
+
|
|
225
|
+
Copy to [krausest/js-framework-benchmark](https://github.com/krausest/js-framework-benchmark) `frameworks/keyed/literaljs/` and run `npm run bench`.
|
|
226
|
+
|
|
227
|
+
---
|
|
228
|
+
|
|
229
|
+
## License
|
|
230
|
+
|
|
231
|
+
MIT
|
package/build/index.js
CHANGED
|
@@ -1,2 +1 @@
|
|
|
1
|
-
var t,e,n,i={},r={},o="eid",f=[],u={},d=[],s=["abort","blur","error","focus","load","mouseenter","mouseleave","resize","scroll","unload"],a="string",c="number",h="boolean",v="undefined",p="object",y="function",l="events",m="style",b="value";function g(t){return Array.isArray(t)}function k(){return Math.random().toString(36).substring(2)+(new Date).getTime().toString(36)}function S(t,e){for(var n=0,i=e.length;n<i;n++)g(e[n])?S(t,e[n]):t.push(e[n]);return t}function N(t,e){for(var n in e)t[n]=e[n];return t}function A(t,e,n){return N(N(t,e),n)}function O(t,e){for(var n=[],i=arguments.length-2;i-- >0;)n[i]=arguments[i+2];return{e:t,a:N({},e),c:S([],n)}}function j(r,o,f){void 0===f&&(f={}),i=N({},f),e=r,t=document.getElementById(o),n=C(r()),B(t,n),_()}function x(t){void 0===t&&(t={});var e=t.name;void 0===e&&(e="");var n=t.state;void 0===n&&(n={});var o=t.mounted;void 0===o&&(o=function(){});var f=t.updated;void 0===f&&(f=function(){});var u=t.unmounted;void 0===u&&(u=function(){});var d=t.methods;void 0===d&&(d=function(){});var s=t.render;void 0===s&&(s=function(){});var a=k();return function(t){return void 0===t&&(t={}),{id:a,init:function(){return this.id=a,this.name=e+(t.key?"_"+t.key:""),this.key=this.id+(this.name||""),this.props=t,this.store=i,this.getStore=E,this.setStore=function(t,e){typeof t===y&&(t=t(i)),i=A({},i,t),z(),typeof e===y&&e.bind(this)()}.bind(this),this.getState=function(){return N({},r[this.key])}.bind(this),this.setState=function(t,e){typeof t===y&&(t=t(r[this.key])),r[this.key]=A({},r[this.key],t),z(),typeof e===y&&e.bind(this)()}.bind(this),this.createState=function(){typeof r[this.key]===v&&(r[this.key]=n)}.bind(this)(),this.deleteState=function(){delete r[this.key]}.bind(this),this.mounted=o.bind(this),this.updated=f.bind(this),this.unmounted=u.bind(this),function(t,e){for(var n in typeof e===y&&(e=e.bind(t)()),e)t[n]=e[n].bind(t)}(this,d),this.render=s.bind(this),this}}}}function E(){return N({},i)}function C(t){if(typeof t===v)return t;if(typeof t.e===y){var e=N({},t.a.props);delete t.a.props,t=t.e(e)}if(typeof t.init===y){var n=new t.init;typeof(t=n.render())===p&&(t.id=n.id,t.m=n.mounted,t.up=n.updated,t.un=n.unmounted,t.d=n.deleteState)}if(g(t.c))for(var i=0,r=t.c.length;i<r;i++)t.c[i]=C(t.c[i]);return t}function L(t){var e=typeof t;if(e===a||e===c)return document.createTextNode(t);if(e===h||e===v)return document.createTextNode("");var n=document.createElement(t.e);if(typeof t.a===p)for(var i=Object.keys(t.a),r=i.length;r--;)i[r]===l?(t.eid=k(),w(t.eid,t.a[i[r]],n)):i[r]===m?J(t.a[i[r]],n):n.setAttribute(i[r],t.a[i[r]]);if(q(t,"m"),g(t.c))for(var o=0,f=t.c.length;o<f;o++)B(n,t.c[o]);return n}function T(t){if("#document"!==t.parentNode.nodeName){var e=t.getAttribute("data-"+o);return typeof e===a?e:T(t.parentNode)}}function w(e,n,i){i.setAttribute("data-"+o,e),u[e]=n;for(var r=Object.keys(n),d=r.length;d--;)s.includes(r[d])?i.addEventListener(r[d],u[e][r[d]]):f.includes(r[d])||(f.push(r[d]),t.addEventListener(r[d],function(t){var e=T(t.target);if(typeof e===a&&u[e]){var n=u[e][t.type];typeof n===y&&n(t)}}))}function J(t,e){if(typeof t!==v)for(var n=Object.keys(t),i=n.length;i--;)e.style[n[i]]=t[n[i]]}function z(i){var r=N({},n),o=C(e());n=o,function t(e,n,i,r){var o=typeof i,f=typeof r;if(f===v)B(e,i);else if(o===v)D(r),n.remove();else if(function(t,e,n,i){return n!==i||t.e!==e.e||t.id!==e.id||(n===a||n===c)&&t!==e}(i,r,o,f))D(r),e.replaceChild(L(i),n);else if(o===p){var u;!function(t,e,n){q(e,"up");for(var i,r,o=Object.keys(e.a),f=o.length;f--;)if(r=e.a[o[f]],i=n.a[o[f]],o[f]===m){if(a=typeof(d=i),(s=typeof(u=r))===p&&a===p&&JSON.stringify(u)===JSON.stringify(d)||s===v&&a===v)continue;t.removeAttribute(o[f]),J(r,t)}else o[f]===l?(M(t,n.eid),I(n.eid),e.eid=k(),w(e.eid,r,t)):o[f]===b&&r!==i?t.value=r:typeof i!==v&&r===i||t.setAttribute(o[f],r);var u,d,s,a}(n,i,r);for(var d=r.c.length;d--&&typeof i.c[d]===v;)typeof(u=n.lastChild)===p&&(D(r.c[d]),u.remove());for(var s=0,h=i.c.length;s<h;s++)t(n,n.childNodes[s],i.c[s],r.c[s])}}(t,t.firstChild,o,r),_()}function B(t,e){t.appendChild(L(e))}function D(t){if(typeof t!==v&&(q(t,"un"),q(t,"d"),I(t.eid),g(t.c)))for(var e=t.c.length;e--;)D(t.c[e])}function I(t){typeof t===a&&delete u[t]}function M(t,e){for(var n=Object.keys(u[e]),i=n.length;i--;)s.includes(n[i])&&t.removeEventListener(n[i],u[e][n[i]])}function _(){for(var t;t=d.shift();)t()}function q(t,e){typeof t===p&&typeof t[e]===y&&d.push(t[e])}module.exports={component:x,render:j,h:O},exports.h=O,exports.render=j,exports.component=x;
|
|
2
|
-
//# sourceMappingURL=index.js.map
|
|
1
|
+
class e{constructor(e,t,n){void 0===t&&(t={}),void 0===n&&(n=[]),this.e=e,this.a=t,this.c=n}withLifecycle(e){return e.mounted&&(this.m=e.mounted),e.updated&&(this.up=e.updated),e.unmounted&&(this.un=e.unmounted),e.deleteState&&(this.d=e.deleteState),this}withIdentity(e,t){return this.definitionId=e,this.instanceId=t,this.id=e+"_"+t,this}}function t(e,n){for(let s=0,i=n.length;s<i;s++)Array.isArray(n[s])?t(e,n[s]):e.push(n[s]);return e}function n(){return Math.random().toString(36).substring(2)+(new Date).getTime().toString(36)}class s{constructor(e,t,n,s,i,o){this.definitionId=e,this.instanceId=t,this.name=n,this.key=`${e}_${t}_${n}`,this.props=s,this._stateManager=o,this._config=i,o.createState(this.key,i.state),this.mounted=i.mounted.bind(this),this.updated=i.updated.bind(this),this.unmounted=i.unmounted.bind(this),this.deleteState=()=>{this._stateManager.deleteState(this.key)},this._renderedVNode=null,this._previousProps=null;const r=i.render.bind(this);this.render=()=>{if(!this.shouldUpdate())return this._renderedVNode;const e=r();return this._renderedVNode=e,this._previousProps=this.props,e}}getStore(){return this._stateManager.getStore()}setStore(e,t){this._stateManager.setStore(e,t)}getState(){return this._stateManager.getState(this.key)}setState(e,t){this._stateManager.setState(this.key,e,t)}createState(){this._stateManager.createState(this.key,this._config.state)}deleteState(){this._stateManager.deleteState(this.key)}shouldUpdate(){return!this._previousProps||!function(e,t){if(e===t)return!0;if(!e||!t||"object"!=typeof e||"object"!=typeof t)return!1;const n=Object.keys(e),s=Object.keys(t);if(n.length!==s.length)return!1;for(let s=0;s<n.length;s++)if(e[n[s]]!==t[n[s]])return!1;return!0}(this.props,this._previousProps)}bindMethods(e){"function"==typeof e&&(e=e.bind(this)());for(let t in e)this[t]=e[t].bind(this)}}function i(e,t){for(let n in t)e[n]=t[n];return e}class o{constructor(e){void 0===e&&(e={}),this.store={...e},this.componentStates={},this.listeners=[]}subscribe(e){this.listeners.push(e)}unsubscribe(e){const t=this.listeners.indexOf(e);t>-1&&this.listeners.splice(t,1)}_notify(){for(let e=0;e<this.listeners.length;e++)this.listeners[e]()}getStore(){return{...this.store}}setStore(e,t){"function"==typeof e&&(e=e(this.store)),this.store=i(i({},this.store),e),this._notify(),"function"==typeof t&&t()}createState(e,t){e in this.componentStates||(this.componentStates[e]={...t})}getState(e){return{...this.componentStates[e]}}setState(e,t,n){"function"==typeof t&&(t=t(this.componentStates[e])),this.componentStates[e]=i(i({},this.componentStates[e]),t),this._notify(),"function"==typeof n&&n()}deleteState(e){delete this.componentStates[e]}reset(){this.store={},this.componentStates={},this.listeners=[]}}class r{constructor(e,t,n){this.eventManager=e,this.lifecycleQueue=t,this.diffEngine=n}mount(e,t){const n=this.createElement(t);n&&e.appendChild(n)}createElement(e){const t=typeof e;return"string"===t||"number"===t?document.createTextNode(e):"boolean"===t||"undefined"===t?document.createTextNode(""):"object"===t&&e.e?this._createElementNode(e):null}_createElementNode(e){const t=document.createElement(e.e);if("object"==typeof e.a){const n=Object.keys(e.a);for(let s=n.length;s--;)if("events"===n[s])e.eid=this.eventManager.bindEvents(t,e.a[n[s]]);else if("style"===n[s])this._addStyles(e.a[n[s]],t);else{if("key"===n[s])continue;t.setAttribute(n[s],e.a[n[s]])}}if(this.lifecycleQueue.mounted(e),Array.isArray(e.c))for(let n=0,s=e.c.length;n<s;n++){const s=this.createElement(e.c[n]);s&&t.appendChild(s)}return t}_addStyles(e,t){if(void 0===e)return;const n=Object.keys(e);for(let s=n.length;s--;)t.style[n[s]]=e[n[s]]}patch(e,t,n,s){const i=typeof n;if(void 0===s)this._appendChild(e,n);else if("undefined"===i)this._unmountNodeAndChildren(s),t&&t.remove();else if(this.diffEngine.areNodesEqual(n,s)){if("object"===i&&n.e){if(n===s)return;if(this._updateNodes(t,n,s),n.c!==s.c)if(this.diffEngine._hasKeys(n.c)||this.diffEngine._hasKeys(s.c))this.diffEngine.reconcileChildren(t,n.c,s.c,this);else{let e;for(let i=s.c?s.c.length:0;i--&&void 0===n.c[i];)e=t.lastChild,e&&(this._unmountNodeAndChildren(s.c[i]),e.remove());if(Array.isArray(n.c))for(let e=0,i=n.c.length;e<i;e++)this.patch(t,t.childNodes[e]||null,n.c[e],s.c?s.c[e]:void 0)}}}else if(!t||3!==t.nodeType||"string"!==i&&"number"!==i)this._unmountNodeAndChildren(s),t&&e.replaceChild(this.createElement(n),t);else{const e=String(n);t.textContent!==e&&(t.textContent=e)}}_appendChild(e,t){const n=this.createElement(t);n&&e.appendChild(n)}_updateNodes(e,t,n){if(this.lifecycleQueue.updated(t),t.a===n.a)return;if(!t.a||!n.a)return;const s=Object.keys(t.a);let i,o;for(let r=s.length;r--;)if(o=t.a[s[r]],i=n.a[s[r]],"style"===s[r]){if(this._stylesAreEqual(o,i))continue;e.removeAttribute(s[r]),this._addStyles(o,e)}else if("events"===s[r])this.eventManager.unbindEvents(e,n.eid),t.eid=this.eventManager.bindEvents(e,o);else{if("key"===s[r])continue;"value"===s[r]&&o!==i?e.value=o:void 0!==i&&o===i||e.setAttribute(s[r],o)}}_stylesAreEqual(e,t){if(e===t)return!0;const n=typeof e,s=typeof t;if("undefined"===n&&"undefined"===s)return!0;if("object"!==n||"object"!==s)return!1;const i=Object.keys(e),o=Object.keys(t);if(i.length!==o.length)return!1;for(let n=i.length;n--;)if(e[i[n]]!==t[i[n]])return!1;return!0}_unmountNodeAndChildren(e){if(null!=e&&(this.lifecycleQueue.unmounted(e),this.lifecycleQueue.deleteState(e),this.eventManager.deleteCachedEvent(e.eid),Array.isArray(e.c)))for(let t=e.c.length;t--;)this._unmountNodeAndChildren(e.c[t])}unmount(e,t){this._unmountNodeAndChildren(t),e&&(e.innerHTML="")}}class h{constructor(e,t,n){this._getCache=e,this._resolveUUID=t,this._elementToUUID=n,this._attachedTypes=new Set,this._rootNode=null}attach(e,t){this._attachedTypes.has(t)||(this._attachedTypes.add(t),this._rootNode=e,e._literaljs_hasEvents=!0,e.addEventListener(t,e=>this._handle(e)))}_handle(e){if(!this._rootNode||!this._rootNode._literaljs_hasEvents)return;let t=e.target;for(;t&&t!==this._rootNode;){let n;if(this._elementToUUID&&(n=this._elementToUUID.get(t))){const t=this._getCache(n);if(t&&"function"==typeof t[e.type])return void t[e.type](e)}const s=this._resolveUUID(t);if("string"==typeof s){const t=this._getCache(s);if(t&&"function"==typeof t[e.type])return void t[e.type](e)}t=t.parentNode}}reset(){this._attachedTypes.clear()}}const d=["abort","blur","error","focus","load","mouseenter","mouseleave","resize","scroll","unload"],c="eid";class u{constructor(){this.cache={},this.rootNode=null,this._elementToUUID=new WeakMap,this.eventBus=new h(e=>this.cache[e],e=>this.getEventUUID(e),this._elementToUUID)}setRootNode(e){this.rootNode=e}getEventUUID(e){if(!e||!e.parentNode)return;if("#document"===e.parentNode.nodeName)return;const t=e.getAttribute("data-"+c);return"string"==typeof t?t:this.getEventUUID(e.parentNode)}bindEvents(e,t){const s=n();e.setAttribute("data-"+c,s),this.cache[s]=t,this._elementToUUID.set(e,s);const i=Object.keys(t);for(let t=i.length;t--;)d.includes(i[t])?e.addEventListener(i[t],this.cache[s][i[t]]):this.eventBus.attach(this.rootNode,i[t]);return s}unbindEvents(e,t){if(!this.cache[t])return;const n=Object.keys(this.cache[t]);for(let s=n.length;s--;)d.includes(n[s])&&e.removeEventListener(n[s],this.cache[t][n[s]]);this.deleteCachedEvent(t),this._elementToUUID.delete(e),e.removeAttribute("data-"+c)}addEvents(e,t,n){n.setAttribute("data-"+c,e),this.cache[e]=t,this._elementToUUID.set(n,e);const s=Object.keys(t);for(let t=s.length;t--;)d.includes(s[t])?n.addEventListener(s[t],this.cache[e][s[t]]):this.eventBus.attach(this.rootNode,s[t])}removeIndividualEvents(e,t){if(!this.cache[t])return;const n=Object.keys(this.cache[t]);for(let s=n.length;s--;)d.includes(n[s])&&e.removeEventListener(n[s],this.cache[t][n[s]])}deleteCachedEvent(e){"string"==typeof e&&delete this.cache[e]}destroy(){this.rootNode&&(this.rootNode._literaljs_hasEvents=!1),this.cache={},this.rootNode=null,this.eventBus.reset()}}class a{constructor(){this.queue=[]}mounted(e){"object"==typeof e&&"function"==typeof e.m&&this.queue.push(e.m)}updated(e){"object"==typeof e&&"function"==typeof e.up&&this.queue.push(e.up)}unmounted(e){"object"==typeof e&&"function"==typeof e.un&&this.queue.push(e.un)}deleteState(e){"object"==typeof e&&"function"==typeof e.d&&this.queue.push(e.d)}flush(){let e;for(;e=this.queue.shift();)e()}clear(){this.queue=[]}}function l(t,n){if(null==t)return t;if("function"==typeof t.e){const e={...t.a.props};delete(t={e:t.e,a:{...t.a},c:t.c}).a.props,t=t.e(e)}if(t&&"function"==typeof t.bind){const s=t.bind(n);let i;try{i=s.render()}catch(e){console.error("Component render error:",e),i={e:"div",a:{class:"error-boundary"},c:[`${e.name}: ${e.message}`]}}if("object"==typeof i&&null!==i&&i instanceof e)i.withIdentity(t.definitionId||s.definitionId,t.instanceId||s.instanceId),i.withLifecycle({mounted:s.mounted,updated:s.updated,unmounted:s.unmounted,deleteState:s.deleteState}),t=i;else if("object"==typeof i&&null!==i){const n=new e(i.e,i.a,i.c);n.withIdentity(t.definitionId||s.definitionId,t.instanceId||s.instanceId),n.withLifecycle({mounted:s.mounted,updated:s.updated,unmounted:s.unmounted,deleteState:s.deleteState}),t=n}else t=i}if(t&&Array.isArray(t.c))for(let e=0,s=t.c.length;e<s;e++)t.c[e]=l(t.c[e],n);return t}class f{areNodesEqual(e,t){const n=typeof e;return n===typeof t&&("object"===n&&e.e===t.e&&e.id===t.id||("string"===n||"number"===n)&&e===t)}reconcileChildren(e,t,n,s){return Array.isArray(t)?this._hasKeys(t)||this._hasKeys(n)?this._reconcileKeyed(e,t,n,s):this._reconcileNonKeyed(e,t,n,s):0}_reconcileNonKeyed(e,t,n,s){const i=t.length;if(0===i){if(n)for(let e=0;e<n.length;e++)s._unmountNodeAndChildren(n[e]);return e.innerHTML="",0}const o=e.childNodes;for(let r=0;r<i;r++)s.patch(e,o[r]||null,t[r],n?n[r]:void 0);return i}_hasKeys(e){if(!Array.isArray(e))return!1;for(let t=0,n=e.length;t<n;t++)if(e[t]&&e[t].a&&null!=e[t].a.key)return!0;return!1}_reconcileKeyed(e,t,n,s){const i=n?n.length:0,o=t.length;if(0===o){for(let e=0;e<i;e++)s._unmountNodeAndChildren(n[e]);return e.innerHTML="",0}const r=Object.create(null),h=Object.create(null);for(let t=0;t<i;t++){const s=n[t].a?n[t].a.key:null,i=e.childNodes[t];null!=s&&i&&(r[s]=i,h[s]=n[t])}const d=new Array(o),c=new Array(o);for(let e=0;e<o;e++){const n=t[e].a?t[e].a.key:null;null!=n&&r[n]?(d[e]=r[n],c[e]=h[n],delete r[n]):(d[e]=null,c[e]=null)}for(const t in r){const n=r[t];n&&(s.patch(e,n,void 0,h[t]),n.remove())}let u=e.firstChild;for(let n=0;n<o;n++){const i=d[n];if(i&&i===u)u=u.nextSibling;else if(i)e.insertBefore(i,u);else{const i=s.createElement(t[n]);i&&e.insertBefore(i,u)}}for(;u;){const e=u.nextSibling;u.remove(),u=e}for(let n=0;n<o;n++)c[n]&&s.patch(e,e.childNodes[n],t[n],c[n]);return o}removeExtraChildren(e,t,n,s){const i=t.c?t.c.length:0;if(!(i<=n))for(let o=i;o-- >n;){const n=e.lastChild;n&&(s._unmountNodeAndChildren(t.c[o]),n.remove())}}}class p{constructor(e,t,n){this.generateTree=e||l,this.renderer=t,this.lifecycleQueue=n}mount(e,t,n){const s=this.generateTree(t(),n);return this.renderer.mount(e,s),this.lifecycleQueue.flush(),s}update(e,t,n,s){const i=this.generateTree(t(),n);return this.renderer.patch(e,e.firstChild,i,s),this.lifecycleQueue.flush(),i}unmount(e,t){t&&(this.renderer.unmount(e,t),this.lifecycleQueue.flush())}}class y{constructor(e,t){void 0===t&&(t="microtask"),this._renderFn=e,this._scheduler=t,this._scheduled=!1}schedule(){this._scheduled||(this._scheduled=!0,"microtask"===this._scheduler?Promise.resolve().then(()=>this._execute()):"sync"===this._scheduler&&this._execute())}_execute(){this._scheduled=!1,this._renderFn()}}class m{constructor(e,t){void 0===t&&(t={}),this.rootComponent=e,this.savedTree=e,this.currentTree=null,this.rootNode=null,this.stateManager=new o(t),this.lifecycleQueue=new a,this.eventManager=new u,this.renderer=new r(this.eventManager,this.lifecycleQueue,new f),this.mountOrchestrator=new p(l,this.renderer,this.lifecycleQueue),this.updateLoop=new y(()=>this.update(),"microtask"),this._boundSchedule=()=>this.updateLoop.schedule(),this.stateManager.subscribe(this._boundSchedule)}mount(e){if(this.rootNode=document.getElementById(e),!this.rootNode)throw new Error(`Cannot mount app: element with id "${e}" not found`);return this.eventManager.setRootNode(this.rootNode),this.currentTree=this.mountOrchestrator.mount(this.rootNode,this.savedTree,this.stateManager),this}unmount(){this.mountOrchestrator.unmount(this.rootNode,this.currentTree),this.eventManager.destroy(),this.stateManager.unsubscribe(()=>this.updateLoop.schedule()),this.rootNode&&(this.rootNode.innerHTML=""),this.currentTree=null,this.rootNode=null}scheduleUpdate(){this.updateLoop.schedule()}update(){this.rootNode&&this.savedTree&&(this.currentTree=this.mountOrchestrator.update(this.rootNode,this.savedTree,this.stateManager,this.currentTree))}getStore(){return this.stateManager.getStore()}setStore(e,t){this.stateManager.setStore(e,t)}}exports.App=m,exports.EventManager=u,exports.LifecycleQueue=a,exports.Renderer=r,exports.StateManager=o,exports.component=function(e){let t=void 0===e?{}:e,i=t.name,o=void 0===i?"":i,r=t.state,h=void 0===r?{}:r,d=t.mounted,c=void 0===d?function(){}:d,u=t.updated,a=void 0===u?function(){}:u,l=t.unmounted,f=void 0===l?function(){}:l,p=t.methods,y=void 0===p?function(){}:p,m=t.render,_=void 0===m?function(){}:m;const g=n(),v=new Map;return function(e){void 0===e&&(e={});const t=e.key||"__default__";if(!v.has(t)){const i=n(),r=o+(e.key?"_"+e.key:"");v.set(t,{definitionId:g,instanceId:i,key:`${g}_${i}_${r}`,_props:null,_cachedRender:null,bind(e){const t=new s(g,i,r,this._props,{state:h,mounted:c,updated:a,unmounted:f,methods:y,render:_},e);return t.bindMethods(y),t}})}const i=v.get(t);return i._props=e,i}},exports.createApp=function(e,t,n){const s=new m(e,n);return s.mount(t),s},exports.flatten=t,exports.generateTree=l,exports.generateUUID=n,exports.h=function(n,s){const i={};for(let e in s)i[e]=s[e];return new e(n,i,t([],[].slice.call(arguments,2)))},exports.isVNode=function(t){return t instanceof e||null!==t&&"object"==typeof t&&"e"in t},exports.render=function(e,t,n){void 0===n&&(n={});const s=new m(e,n);return s.mount(t),s};
|
package/build/index.m.js
CHANGED
|
@@ -1,2 +1 @@
|
|
|
1
|
-
var t,e,n,i={},r={},o="eid",f=[],u={},d=[],a=["abort","blur","error","focus","load","mouseenter","mouseleave","resize","scroll","unload"],s="string",c="number",h="boolean",v="undefined",p="object",y="function",l="events",m="style",b="value";function g(t){return Array.isArray(t)}function k(){return Math.random().toString(36).substring(2)+(new Date).getTime().toString(36)}function S(t,e){for(var n=0,i=e.length;n<i;n++)g(e[n])?S(t,e[n]):t.push(e[n]);return t}function N(t,e){for(var n in e)t[n]=e[n];return t}function A(t,e,n){return N(N(t,e),n)}function O(t,e){for(var n=[],i=arguments.length-2;i-- >0;)n[i]=arguments[i+2];return{e:t,a:N({},e),c:S([],n)}}function j(r,o,f){void 0===f&&(f={}),i=N({},f),e=r,t=document.getElementById(o),n=C(r()),B(t,n),_()}function E(t){void 0===t&&(t={});var e=t.name;void 0===e&&(e="");var n=t.state;void 0===n&&(n={});var o=t.mounted;void 0===o&&(o=function(){});var f=t.updated;void 0===f&&(f=function(){});var u=t.unmounted;void 0===u&&(u=function(){});var d=t.methods;void 0===d&&(d=function(){});var a=t.render;void 0===a&&(a=function(){});var s=k();return function(t){return void 0===t&&(t={}),{id:s,init:function(){return this.id=s,this.name=e+(t.key?"_"+t.key:""),this.key=this.id+(this.name||""),this.props=t,this.store=i,this.getStore=x,this.setStore=function(t,e){typeof t===y&&(t=t(i)),i=A({},i,t),z(),typeof e===y&&e.bind(this)()}.bind(this),this.getState=function(){return N({},r[this.key])}.bind(this),this.setState=function(t,e){typeof t===y&&(t=t(r[this.key])),r[this.key]=A({},r[this.key],t),z(),typeof e===y&&e.bind(this)()}.bind(this),this.createState=function(){typeof r[this.key]===v&&(r[this.key]=n)}.bind(this)(),this.deleteState=function(){delete r[this.key]}.bind(this),this.mounted=o.bind(this),this.updated=f.bind(this),this.unmounted=u.bind(this),function(t,e){for(var n in typeof e===y&&(e=e.bind(t)()),e)t[n]=e[n].bind(t)}(this,d),this.render=a.bind(this),this}}}}function x(){return N({},i)}function C(t){if(typeof t===v)return t;if(typeof t.e===y){var e=N({},t.a.props);delete t.a.props,t=t.e(e)}if(typeof t.init===y){var n=new t.init;typeof(t=n.render())===p&&(t.id=n.id,t.m=n.mounted,t.up=n.updated,t.un=n.unmounted,t.d=n.deleteState)}if(g(t.c))for(var i=0,r=t.c.length;i<r;i++)t.c[i]=C(t.c[i]);return t}function L(t){var e=typeof t;if(e===s||e===c)return document.createTextNode(t);if(e===h||e===v)return document.createTextNode("");var n=document.createElement(t.e);if(typeof t.a===p)for(var i=Object.keys(t.a),r=i.length;r--;)i[r]===l?(t.eid=k(),w(t.eid,t.a[i[r]],n)):i[r]===m?J(t.a[i[r]],n):n.setAttribute(i[r],t.a[i[r]]);if(q(t,"m"),g(t.c))for(var o=0,f=t.c.length;o<f;o++)B(n,t.c[o]);return n}function T(t){if("#document"!==t.parentNode.nodeName){var e=t.getAttribute("data-"+o);return typeof e===s?e:T(t.parentNode)}}function w(e,n,i){i.setAttribute("data-"+o,e),u[e]=n;for(var r=Object.keys(n),d=r.length;d--;)a.includes(r[d])?i.addEventListener(r[d],u[e][r[d]]):f.includes(r[d])||(f.push(r[d]),t.addEventListener(r[d],function(t){var e=T(t.target);if(typeof e===s&&u[e]){var n=u[e][t.type];typeof n===y&&n(t)}}))}function J(t,e){if(typeof t!==v)for(var n=Object.keys(t),i=n.length;i--;)e.style[n[i]]=t[n[i]]}function z(i){var r=N({},n),o=C(e());n=o,function t(e,n,i,r){var o=typeof i,f=typeof r;if(f===v)B(e,i);else if(o===v)D(r),n.remove();else if(function(t,e,n,i){return n!==i||t.e!==e.e||t.id!==e.id||(n===s||n===c)&&t!==e}(i,r,o,f))D(r),e.replaceChild(L(i),n);else if(o===p){var u;!function(t,e,n){q(e,"up");for(var i,r,o=Object.keys(e.a),f=o.length;f--;)if(r=e.a[o[f]],i=n.a[o[f]],o[f]===m){if(s=typeof(d=i),(a=typeof(u=r))===p&&s===p&&JSON.stringify(u)===JSON.stringify(d)||a===v&&s===v)continue;t.removeAttribute(o[f]),J(r,t)}else o[f]===l?(M(t,n.eid),I(n.eid),e.eid=k(),w(e.eid,r,t)):o[f]===b&&r!==i?t.value=r:typeof i!==v&&r===i||t.setAttribute(o[f],r);var u,d,a,s}(n,i,r);for(var d=r.c.length;d--&&typeof i.c[d]===v;)typeof(u=n.lastChild)===p&&(D(r.c[d]),u.remove());for(var a=0,h=i.c.length;a<h;a++)t(n,n.childNodes[a],i.c[a],r.c[a])}}(t,t.firstChild,o,r),_()}function B(t,e){t.appendChild(L(e))}function D(t){if(typeof t!==v&&(q(t,"un"),q(t,"d"),I(t.eid),g(t.c)))for(var e=t.c.length;e--;)D(t.c[e])}function I(t){typeof t===s&&delete u[t]}function M(t,e){for(var n=Object.keys(u[e]),i=n.length;i--;)a.includes(n[i])&&t.removeEventListener(n[i],u[e][n[i]])}function _(){for(var t;t=d.shift();)t()}function q(t,e){typeof t===p&&typeof t[e]===y&&d.push(t[e])}module.exports={component:E,render:j,h:O};export{O as h,j as render,E as component};
|
|
2
|
-
//# sourceMappingURL=index.m.js.map
|
|
1
|
+
class e{constructor(e,t,n){void 0===t&&(t={}),void 0===n&&(n=[]),this.e=e,this.a=t,this.c=n}withLifecycle(e){return e.mounted&&(this.m=e.mounted),e.updated&&(this.up=e.updated),e.unmounted&&(this.un=e.unmounted),e.deleteState&&(this.d=e.deleteState),this}withIdentity(e,t){return this.definitionId=e,this.instanceId=t,this.id=e+"_"+t,this}}function t(e,n){for(let s=0,i=n.length;s<i;s++)Array.isArray(n[s])?t(e,n[s]):e.push(n[s]);return e}function n(n,s){const i={};for(let e in s)i[e]=s[e];return new e(n,i,t([],[].slice.call(arguments,2)))}function s(t){return t instanceof e||null!==t&&"object"==typeof t&&"e"in t}function i(){return Math.random().toString(36).substring(2)+(new Date).getTime().toString(36)}class o{constructor(e,t,n,s,i,o){this.definitionId=e,this.instanceId=t,this.name=n,this.key=`${e}_${t}_${n}`,this.props=s,this._stateManager=o,this._config=i,o.createState(this.key,i.state),this.mounted=i.mounted.bind(this),this.updated=i.updated.bind(this),this.unmounted=i.unmounted.bind(this),this.deleteState=()=>{this._stateManager.deleteState(this.key)},this._renderedVNode=null,this._previousProps=null;const r=i.render.bind(this);this.render=()=>{if(!this.shouldUpdate())return this._renderedVNode;const e=r();return this._renderedVNode=e,this._previousProps=this.props,e}}getStore(){return this._stateManager.getStore()}setStore(e,t){this._stateManager.setStore(e,t)}getState(){return this._stateManager.getState(this.key)}setState(e,t){this._stateManager.setState(this.key,e,t)}createState(){this._stateManager.createState(this.key,this._config.state)}deleteState(){this._stateManager.deleteState(this.key)}shouldUpdate(){return!this._previousProps||!function(e,t){if(e===t)return!0;if(!e||!t||"object"!=typeof e||"object"!=typeof t)return!1;const n=Object.keys(e),s=Object.keys(t);if(n.length!==s.length)return!1;for(let s=0;s<n.length;s++)if(e[n[s]]!==t[n[s]])return!1;return!0}(this.props,this._previousProps)}bindMethods(e){"function"==typeof e&&(e=e.bind(this)());for(let t in e)this[t]=e[t].bind(this)}}function r(e){let t=void 0===e?{}:e,n=t.name,s=void 0===n?"":n,r=t.state,h=void 0===r?{}:r,d=t.mounted,c=void 0===d?function(){}:d,u=t.updated,a=void 0===u?function(){}:u,l=t.unmounted,f=void 0===l?function(){}:l,p=t.methods,y=void 0===p?function(){}:p,m=t.render,_=void 0===m?function(){}:m;const g=i(),v=new Map;return function(e){void 0===e&&(e={});const t=e.key||"__default__";if(!v.has(t)){const n=i(),r=s+(e.key?"_"+e.key:"");v.set(t,{definitionId:g,instanceId:n,key:`${g}_${n}_${r}`,_props:null,_cachedRender:null,bind(e){const t=new o(g,n,r,this._props,{state:h,mounted:c,updated:a,unmounted:f,methods:y,render:_},e);return t.bindMethods(y),t}})}const n=v.get(t);return n._props=e,n}}function h(e,t){for(let n in t)e[n]=t[n];return e}class d{constructor(e){void 0===e&&(e={}),this.store={...e},this.componentStates={},this.listeners=[]}subscribe(e){this.listeners.push(e)}unsubscribe(e){const t=this.listeners.indexOf(e);t>-1&&this.listeners.splice(t,1)}_notify(){for(let e=0;e<this.listeners.length;e++)this.listeners[e]()}getStore(){return{...this.store}}setStore(e,t){"function"==typeof e&&(e=e(this.store)),this.store=h(h({},this.store),e),this._notify(),"function"==typeof t&&t()}createState(e,t){e in this.componentStates||(this.componentStates[e]={...t})}getState(e){return{...this.componentStates[e]}}setState(e,t,n){"function"==typeof t&&(t=t(this.componentStates[e])),this.componentStates[e]=h(h({},this.componentStates[e]),t),this._notify(),"function"==typeof n&&n()}deleteState(e){delete this.componentStates[e]}reset(){this.store={},this.componentStates={},this.listeners=[]}}class c{constructor(e,t,n){this.eventManager=e,this.lifecycleQueue=t,this.diffEngine=n}mount(e,t){const n=this.createElement(t);n&&e.appendChild(n)}createElement(e){const t=typeof e;return"string"===t||"number"===t?document.createTextNode(e):"boolean"===t||"undefined"===t?document.createTextNode(""):"object"===t&&e.e?this._createElementNode(e):null}_createElementNode(e){const t=document.createElement(e.e);if("object"==typeof e.a){const n=Object.keys(e.a);for(let s=n.length;s--;)if("events"===n[s])e.eid=this.eventManager.bindEvents(t,e.a[n[s]]);else if("style"===n[s])this._addStyles(e.a[n[s]],t);else{if("key"===n[s])continue;t.setAttribute(n[s],e.a[n[s]])}}if(this.lifecycleQueue.mounted(e),Array.isArray(e.c))for(let n=0,s=e.c.length;n<s;n++){const s=this.createElement(e.c[n]);s&&t.appendChild(s)}return t}_addStyles(e,t){if(void 0===e)return;const n=Object.keys(e);for(let s=n.length;s--;)t.style[n[s]]=e[n[s]]}patch(e,t,n,s){const i=typeof n;if(void 0===s)this._appendChild(e,n);else if("undefined"===i)this._unmountNodeAndChildren(s),t&&t.remove();else if(this.diffEngine.areNodesEqual(n,s)){if("object"===i&&n.e){if(n===s)return;if(this._updateNodes(t,n,s),n.c!==s.c)if(this.diffEngine._hasKeys(n.c)||this.diffEngine._hasKeys(s.c))this.diffEngine.reconcileChildren(t,n.c,s.c,this);else{let e;for(let i=s.c?s.c.length:0;i--&&void 0===n.c[i];)e=t.lastChild,e&&(this._unmountNodeAndChildren(s.c[i]),e.remove());if(Array.isArray(n.c))for(let e=0,i=n.c.length;e<i;e++)this.patch(t,t.childNodes[e]||null,n.c[e],s.c?s.c[e]:void 0)}}}else if(!t||3!==t.nodeType||"string"!==i&&"number"!==i)this._unmountNodeAndChildren(s),t&&e.replaceChild(this.createElement(n),t);else{const e=String(n);t.textContent!==e&&(t.textContent=e)}}_appendChild(e,t){const n=this.createElement(t);n&&e.appendChild(n)}_updateNodes(e,t,n){if(this.lifecycleQueue.updated(t),t.a===n.a)return;if(!t.a||!n.a)return;const s=Object.keys(t.a);let i,o;for(let r=s.length;r--;)if(o=t.a[s[r]],i=n.a[s[r]],"style"===s[r]){if(this._stylesAreEqual(o,i))continue;e.removeAttribute(s[r]),this._addStyles(o,e)}else if("events"===s[r])this.eventManager.unbindEvents(e,n.eid),t.eid=this.eventManager.bindEvents(e,o);else{if("key"===s[r])continue;"value"===s[r]&&o!==i?e.value=o:void 0!==i&&o===i||e.setAttribute(s[r],o)}}_stylesAreEqual(e,t){if(e===t)return!0;const n=typeof e,s=typeof t;if("undefined"===n&&"undefined"===s)return!0;if("object"!==n||"object"!==s)return!1;const i=Object.keys(e),o=Object.keys(t);if(i.length!==o.length)return!1;for(let n=i.length;n--;)if(e[i[n]]!==t[i[n]])return!1;return!0}_unmountNodeAndChildren(e){if(null!=e&&(this.lifecycleQueue.unmounted(e),this.lifecycleQueue.deleteState(e),this.eventManager.deleteCachedEvent(e.eid),Array.isArray(e.c)))for(let t=e.c.length;t--;)this._unmountNodeAndChildren(e.c[t])}unmount(e,t){this._unmountNodeAndChildren(t),e&&(e.innerHTML="")}}class u{constructor(e,t,n){this._getCache=e,this._resolveUUID=t,this._elementToUUID=n,this._attachedTypes=new Set,this._rootNode=null}attach(e,t){this._attachedTypes.has(t)||(this._attachedTypes.add(t),this._rootNode=e,e._literaljs_hasEvents=!0,e.addEventListener(t,e=>this._handle(e)))}_handle(e){if(!this._rootNode||!this._rootNode._literaljs_hasEvents)return;let t=e.target;for(;t&&t!==this._rootNode;){let n;if(this._elementToUUID&&(n=this._elementToUUID.get(t))){const t=this._getCache(n);if(t&&"function"==typeof t[e.type])return void t[e.type](e)}const s=this._resolveUUID(t);if("string"==typeof s){const t=this._getCache(s);if(t&&"function"==typeof t[e.type])return void t[e.type](e)}t=t.parentNode}}reset(){this._attachedTypes.clear()}}const a=["abort","blur","error","focus","load","mouseenter","mouseleave","resize","scroll","unload"],l="eid";class f{constructor(){this.cache={},this.rootNode=null,this._elementToUUID=new WeakMap,this.eventBus=new u(e=>this.cache[e],e=>this.getEventUUID(e),this._elementToUUID)}setRootNode(e){this.rootNode=e}getEventUUID(e){if(!e||!e.parentNode)return;if("#document"===e.parentNode.nodeName)return;const t=e.getAttribute("data-"+l);return"string"==typeof t?t:this.getEventUUID(e.parentNode)}bindEvents(e,t){const n=i();e.setAttribute("data-"+l,n),this.cache[n]=t,this._elementToUUID.set(e,n);const s=Object.keys(t);for(let t=s.length;t--;)a.includes(s[t])?e.addEventListener(s[t],this.cache[n][s[t]]):this.eventBus.attach(this.rootNode,s[t]);return n}unbindEvents(e,t){if(!this.cache[t])return;const n=Object.keys(this.cache[t]);for(let s=n.length;s--;)a.includes(n[s])&&e.removeEventListener(n[s],this.cache[t][n[s]]);this.deleteCachedEvent(t),this._elementToUUID.delete(e),e.removeAttribute("data-"+l)}addEvents(e,t,n){n.setAttribute("data-"+l,e),this.cache[e]=t,this._elementToUUID.set(n,e);const s=Object.keys(t);for(let t=s.length;t--;)a.includes(s[t])?n.addEventListener(s[t],this.cache[e][s[t]]):this.eventBus.attach(this.rootNode,s[t])}removeIndividualEvents(e,t){if(!this.cache[t])return;const n=Object.keys(this.cache[t]);for(let s=n.length;s--;)a.includes(n[s])&&e.removeEventListener(n[s],this.cache[t][n[s]])}deleteCachedEvent(e){"string"==typeof e&&delete this.cache[e]}destroy(){this.rootNode&&(this.rootNode._literaljs_hasEvents=!1),this.cache={},this.rootNode=null,this.eventBus.reset()}}class p{constructor(){this.queue=[]}mounted(e){"object"==typeof e&&"function"==typeof e.m&&this.queue.push(e.m)}updated(e){"object"==typeof e&&"function"==typeof e.up&&this.queue.push(e.up)}unmounted(e){"object"==typeof e&&"function"==typeof e.un&&this.queue.push(e.un)}deleteState(e){"object"==typeof e&&"function"==typeof e.d&&this.queue.push(e.d)}flush(){let e;for(;e=this.queue.shift();)e()}clear(){this.queue=[]}}function y(t,n){if(null==t)return t;if("function"==typeof t.e){const e={...t.a.props};delete(t={e:t.e,a:{...t.a},c:t.c}).a.props,t=t.e(e)}if(t&&"function"==typeof t.bind){const s=t.bind(n);let i;try{i=s.render()}catch(e){console.error("Component render error:",e),i={e:"div",a:{class:"error-boundary"},c:[`${e.name}: ${e.message}`]}}if("object"==typeof i&&null!==i&&i instanceof e)i.withIdentity(t.definitionId||s.definitionId,t.instanceId||s.instanceId),i.withLifecycle({mounted:s.mounted,updated:s.updated,unmounted:s.unmounted,deleteState:s.deleteState}),t=i;else if("object"==typeof i&&null!==i){const n=new e(i.e,i.a,i.c);n.withIdentity(t.definitionId||s.definitionId,t.instanceId||s.instanceId),n.withLifecycle({mounted:s.mounted,updated:s.updated,unmounted:s.unmounted,deleteState:s.deleteState}),t=n}else t=i}if(t&&Array.isArray(t.c))for(let e=0,s=t.c.length;e<s;e++)t.c[e]=y(t.c[e],n);return t}class m{areNodesEqual(e,t){const n=typeof e;return n===typeof t&&("object"===n&&e.e===t.e&&e.id===t.id||("string"===n||"number"===n)&&e===t)}reconcileChildren(e,t,n,s){return Array.isArray(t)?this._hasKeys(t)||this._hasKeys(n)?this._reconcileKeyed(e,t,n,s):this._reconcileNonKeyed(e,t,n,s):0}_reconcileNonKeyed(e,t,n,s){const i=t.length;if(0===i){if(n)for(let e=0;e<n.length;e++)s._unmountNodeAndChildren(n[e]);return e.innerHTML="",0}const o=e.childNodes;for(let r=0;r<i;r++)s.patch(e,o[r]||null,t[r],n?n[r]:void 0);return i}_hasKeys(e){if(!Array.isArray(e))return!1;for(let t=0,n=e.length;t<n;t++)if(e[t]&&e[t].a&&null!=e[t].a.key)return!0;return!1}_reconcileKeyed(e,t,n,s){const i=n?n.length:0,o=t.length;if(0===o){for(let e=0;e<i;e++)s._unmountNodeAndChildren(n[e]);return e.innerHTML="",0}const r=Object.create(null),h=Object.create(null);for(let t=0;t<i;t++){const s=n[t].a?n[t].a.key:null,i=e.childNodes[t];null!=s&&i&&(r[s]=i,h[s]=n[t])}const d=new Array(o),c=new Array(o);for(let e=0;e<o;e++){const n=t[e].a?t[e].a.key:null;null!=n&&r[n]?(d[e]=r[n],c[e]=h[n],delete r[n]):(d[e]=null,c[e]=null)}for(const t in r){const n=r[t];n&&(s.patch(e,n,void 0,h[t]),n.remove())}let u=e.firstChild;for(let n=0;n<o;n++){const i=d[n];if(i&&i===u)u=u.nextSibling;else if(i)e.insertBefore(i,u);else{const i=s.createElement(t[n]);i&&e.insertBefore(i,u)}}for(;u;){const e=u.nextSibling;u.remove(),u=e}for(let n=0;n<o;n++)c[n]&&s.patch(e,e.childNodes[n],t[n],c[n]);return o}removeExtraChildren(e,t,n,s){const i=t.c?t.c.length:0;if(!(i<=n))for(let o=i;o-- >n;){const n=e.lastChild;n&&(s._unmountNodeAndChildren(t.c[o]),n.remove())}}}class _{constructor(e,t,n){this.generateTree=e||y,this.renderer=t,this.lifecycleQueue=n}mount(e,t,n){const s=this.generateTree(t(),n);return this.renderer.mount(e,s),this.lifecycleQueue.flush(),s}update(e,t,n,s){const i=this.generateTree(t(),n);return this.renderer.patch(e,e.firstChild,i,s),this.lifecycleQueue.flush(),i}unmount(e,t){t&&(this.renderer.unmount(e,t),this.lifecycleQueue.flush())}}class g{constructor(e,t){void 0===t&&(t="microtask"),this._renderFn=e,this._scheduler=t,this._scheduled=!1}schedule(){this._scheduled||(this._scheduled=!0,"microtask"===this._scheduler?Promise.resolve().then(()=>this._execute()):"sync"===this._scheduler&&this._execute())}_execute(){this._scheduled=!1,this._renderFn()}}class v{constructor(e,t){void 0===t&&(t={}),this.rootComponent=e,this.savedTree=e,this.currentTree=null,this.rootNode=null,this.stateManager=new d(t),this.lifecycleQueue=new p,this.eventManager=new f,this.renderer=new c(this.eventManager,this.lifecycleQueue,new m),this.mountOrchestrator=new _(y,this.renderer,this.lifecycleQueue),this.updateLoop=new g(()=>this.update(),"microtask"),this._boundSchedule=()=>this.updateLoop.schedule(),this.stateManager.subscribe(this._boundSchedule)}mount(e){if(this.rootNode=document.getElementById(e),!this.rootNode)throw new Error(`Cannot mount app: element with id "${e}" not found`);return this.eventManager.setRootNode(this.rootNode),this.currentTree=this.mountOrchestrator.mount(this.rootNode,this.savedTree,this.stateManager),this}unmount(){this.mountOrchestrator.unmount(this.rootNode,this.currentTree),this.eventManager.destroy(),this.stateManager.unsubscribe(()=>this.updateLoop.schedule()),this.rootNode&&(this.rootNode.innerHTML=""),this.currentTree=null,this.rootNode=null}scheduleUpdate(){this.updateLoop.schedule()}update(){this.rootNode&&this.savedTree&&(this.currentTree=this.mountOrchestrator.update(this.rootNode,this.savedTree,this.stateManager,this.currentTree))}getStore(){return this.stateManager.getStore()}setStore(e,t){this.stateManager.setStore(e,t)}}function b(e,t,n){const s=new v(e,n);return s.mount(t),s}function N(e,t,n){void 0===n&&(n={});const s=new v(e,n);return s.mount(t),s}export{v as App,f as EventManager,p as LifecycleQueue,c as Renderer,d as StateManager,r as component,b as createApp,t as flatten,y as generateTree,i as generateUUID,n as h,s as isVNode,N as render};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
class e{constructor(e,t={},n=[]){this.e=e,this.a=t,this.c=n}withLifecycle(e){return e.mounted&&(this.m=e.mounted),e.updated&&(this.up=e.updated),e.unmounted&&(this.un=e.unmounted),e.deleteState&&(this.d=e.deleteState),this}withIdentity(e,t){return this.definitionId=e,this.instanceId=t,this.id=e+"_"+t,this}}function t(e,n){for(let s=0,i=n.length;s<i;s++)Array.isArray(n[s])?t(e,n[s]):e.push(n[s]);return e}function n(n,s,...i){const r={};for(let e in s)r[e]=s[e];return new e(n,r,t([],i))}function s(t){return t instanceof e||null!==t&&"object"==typeof t&&"e"in t}function i(){return Math.random().toString(36).substring(2)+(new Date).getTime().toString(36)}class r{constructor(e,t,n,s,i,r){this.definitionId=e,this.instanceId=t,this.name=n,this.key=`${e}_${t}_${n}`,this.props=s,this._stateManager=r,this._config=i,r.createState(this.key,i.state),this.mounted=i.mounted.bind(this),this.updated=i.updated.bind(this),this.unmounted=i.unmounted.bind(this),this.deleteState=()=>{this._stateManager.deleteState(this.key)},this._renderedVNode=null,this._previousProps=null;const o=i.render.bind(this);this.render=()=>{if(!this.shouldUpdate())return this._renderedVNode;const e=o();return this._renderedVNode=e,this._previousProps=this.props,e}}getStore(){return this._stateManager.getStore()}setStore(e,t){this._stateManager.setStore(e,t)}getState(){return this._stateManager.getState(this.key)}setState(e,t){this._stateManager.setState(this.key,e,t)}createState(){this._stateManager.createState(this.key,this._config.state)}deleteState(){this._stateManager.deleteState(this.key)}shouldUpdate(){return!this._previousProps||!function(e,t){if(e===t)return!0;if(!e||!t||"object"!=typeof e||"object"!=typeof t)return!1;const n=Object.keys(e),s=Object.keys(t);if(n.length!==s.length)return!1;for(let s=0;s<n.length;s++)if(e[n[s]]!==t[n[s]])return!1;return!0}(this.props,this._previousProps)}bindMethods(e){"function"==typeof e&&(e=e.bind(this)());for(let t in e)this[t]=e[t].bind(this)}}function o({name:e="",state:t={},mounted:n=function(){},updated:s=function(){},unmounted:o=function(){},methods:h=function(){},render:c=function(){}}={}){const u=i(),d=new Map;return function(a={}){const l=a.key||"__default__";if(!d.has(l)){const f=i(),p=e+(a.key?"_"+a.key:"");d.set(l,{definitionId:u,instanceId:f,key:`${u}_${f}_${p}`,_props:null,_cachedRender:null,bind(e){const i=new r(u,f,p,this._props,{state:t,mounted:n,updated:s,unmounted:o,methods:h,render:c},e);return i.bindMethods(h),i}})}const f=d.get(l);return f._props=a,f}}function h(){return h=Object.assign?Object.assign.bind():function(e){for(var t=1;t<arguments.length;t++){var n=arguments[t];for(var s in n)({}).hasOwnProperty.call(n,s)&&(e[s]=n[s])}return e},h.apply(null,arguments)}function c(e,t){for(let n in t)e[n]=t[n];return e}class u{constructor(e={}){this.store=h({},e),this.componentStates={},this.listeners=[]}subscribe(e){this.listeners.push(e)}unsubscribe(e){const t=this.listeners.indexOf(e);t>-1&&this.listeners.splice(t,1)}_notify(){for(let e=0;e<this.listeners.length;e++)this.listeners[e]()}getStore(){return h({},this.store)}setStore(e,t){"function"==typeof e&&(e=e(this.store)),this.store=c(c({},this.store),e),this._notify(),"function"==typeof t&&t()}createState(e,t){e in this.componentStates||(this.componentStates[e]=h({},t))}getState(e){return h({},this.componentStates[e])}setState(e,t,n){"function"==typeof t&&(t=t(this.componentStates[e])),this.componentStates[e]=c(c({},this.componentStates[e]),t),this._notify(),"function"==typeof n&&n()}deleteState(e){delete this.componentStates[e]}reset(){this.store={},this.componentStates={},this.listeners=[]}}class d{constructor(e,t,n){this.eventManager=e,this.lifecycleQueue=t,this.diffEngine=n}mount(e,t){const n=this.createElement(t);n&&e.appendChild(n)}createElement(e){const t=typeof e;return"string"===t||"number"===t?document.createTextNode(e):"boolean"===t||"undefined"===t?document.createTextNode(""):"object"===t&&e.e?this._createElementNode(e):null}_createElementNode(e){const t=document.createElement(e.e);if("object"==typeof e.a){const n=Object.keys(e.a);for(let s=n.length;s--;)if("events"===n[s])e.eid=this.eventManager.bindEvents(t,e.a[n[s]]);else if("style"===n[s])this._addStyles(e.a[n[s]],t);else{if("key"===n[s])continue;t.setAttribute(n[s],e.a[n[s]])}}if(this.lifecycleQueue.mounted(e),Array.isArray(e.c))for(let n=0,s=e.c.length;n<s;n++){const s=this.createElement(e.c[n]);s&&t.appendChild(s)}return t}_addStyles(e,t){if(void 0===e)return;const n=Object.keys(e);for(let s=n.length;s--;)t.style[n[s]]=e[n[s]]}patch(e,t,n,s){const i=typeof n;if(void 0===s)this._appendChild(e,n);else if("undefined"===i)this._unmountNodeAndChildren(s),t&&t.remove();else if(this.diffEngine.areNodesEqual(n,s)){if("object"===i&&n.e){if(n===s)return;if(this._updateNodes(t,n,s),n.c!==s.c)if(this.diffEngine._hasKeys(n.c)||this.diffEngine._hasKeys(s.c))this.diffEngine.reconcileChildren(t,n.c,s.c,this);else{let e;for(let i=s.c?s.c.length:0;i--&&void 0===n.c[i];)e=t.lastChild,e&&(this._unmountNodeAndChildren(s.c[i]),e.remove());if(Array.isArray(n.c))for(let e=0,i=n.c.length;e<i;e++)this.patch(t,t.childNodes[e]||null,n.c[e],s.c?s.c[e]:void 0)}}}else if(!t||3!==t.nodeType||"string"!==i&&"number"!==i)this._unmountNodeAndChildren(s),t&&e.replaceChild(this.createElement(n),t);else{const e=String(n);t.textContent!==e&&(t.textContent=e)}}_appendChild(e,t){const n=this.createElement(t);n&&e.appendChild(n)}_updateNodes(e,t,n){if(this.lifecycleQueue.updated(t),t.a===n.a)return;if(!t.a||!n.a)return;const s=Object.keys(t.a);let i,r;for(let o=s.length;o--;)if(r=t.a[s[o]],i=n.a[s[o]],"style"===s[o]){if(this._stylesAreEqual(r,i))continue;e.removeAttribute(s[o]),this._addStyles(r,e)}else if("events"===s[o])this.eventManager.unbindEvents(e,n.eid),t.eid=this.eventManager.bindEvents(e,r);else{if("key"===s[o])continue;"value"===s[o]&&r!==i?e.value=r:void 0!==i&&r===i||e.setAttribute(s[o],r)}}_stylesAreEqual(e,t){if(e===t)return!0;const n=typeof e,s=typeof t;if("undefined"===n&&"undefined"===s)return!0;if("object"!==n||"object"!==s)return!1;const i=Object.keys(e),r=Object.keys(t);if(i.length!==r.length)return!1;for(let n=i.length;n--;)if(e[i[n]]!==t[i[n]])return!1;return!0}_unmountNodeAndChildren(e){if(null!=e&&(this.lifecycleQueue.unmounted(e),this.lifecycleQueue.deleteState(e),this.eventManager.deleteCachedEvent(e.eid),Array.isArray(e.c)))for(let t=e.c.length;t--;)this._unmountNodeAndChildren(e.c[t])}unmount(e,t){this._unmountNodeAndChildren(t),e&&(e.innerHTML="")}}class a{constructor(e,t,n){this._getCache=e,this._resolveUUID=t,this._elementToUUID=n,this._attachedTypes=new Set,this._rootNode=null}attach(e,t){this._attachedTypes.has(t)||(this._attachedTypes.add(t),this._rootNode=e,e._literaljs_hasEvents=!0,e.addEventListener(t,e=>this._handle(e)))}_handle(e){if(!this._rootNode||!this._rootNode._literaljs_hasEvents)return;let t=e.target;for(;t&&t!==this._rootNode;){let n;if(this._elementToUUID&&(n=this._elementToUUID.get(t))){const t=this._getCache(n);if(t&&"function"==typeof t[e.type])return void t[e.type](e)}const s=this._resolveUUID(t);if("string"==typeof s){const t=this._getCache(s);if(t&&"function"==typeof t[e.type])return void t[e.type](e)}t=t.parentNode}}reset(){this._attachedTypes.clear()}}const l=["abort","blur","error","focus","load","mouseenter","mouseleave","resize","scroll","unload"],f="eid";class p{constructor(){this.cache={},this.rootNode=null,this._elementToUUID=new WeakMap,this.eventBus=new a(e=>this.cache[e],e=>this.getEventUUID(e),this._elementToUUID)}setRootNode(e){this.rootNode=e}getEventUUID(e){if(!e||!e.parentNode)return;if("#document"===e.parentNode.nodeName)return;const t=e.getAttribute("data-"+f);return"string"==typeof t?t:this.getEventUUID(e.parentNode)}bindEvents(e,t){const n=i();e.setAttribute("data-"+f,n),this.cache[n]=t,this._elementToUUID.set(e,n);const s=Object.keys(t);for(let t=s.length;t--;)l.includes(s[t])?e.addEventListener(s[t],this.cache[n][s[t]]):this.eventBus.attach(this.rootNode,s[t]);return n}unbindEvents(e,t){if(!this.cache[t])return;const n=Object.keys(this.cache[t]);for(let s=n.length;s--;)l.includes(n[s])&&e.removeEventListener(n[s],this.cache[t][n[s]]);this.deleteCachedEvent(t),this._elementToUUID.delete(e),e.removeAttribute("data-"+f)}addEvents(e,t,n){n.setAttribute("data-"+f,e),this.cache[e]=t,this._elementToUUID.set(n,e);const s=Object.keys(t);for(let t=s.length;t--;)l.includes(s[t])?n.addEventListener(s[t],this.cache[e][s[t]]):this.eventBus.attach(this.rootNode,s[t])}removeIndividualEvents(e,t){if(!this.cache[t])return;const n=Object.keys(this.cache[t]);for(let s=n.length;s--;)l.includes(n[s])&&e.removeEventListener(n[s],this.cache[t][n[s]])}deleteCachedEvent(e){"string"==typeof e&&delete this.cache[e]}destroy(){this.rootNode&&(this.rootNode._literaljs_hasEvents=!1),this.cache={},this.rootNode=null,this.eventBus.reset()}}class y{constructor(){this.queue=[]}mounted(e){"object"==typeof e&&"function"==typeof e.m&&this.queue.push(e.m)}updated(e){"object"==typeof e&&"function"==typeof e.up&&this.queue.push(e.up)}unmounted(e){"object"==typeof e&&"function"==typeof e.un&&this.queue.push(e.un)}deleteState(e){"object"==typeof e&&"function"==typeof e.d&&this.queue.push(e.d)}flush(){let e;for(;e=this.queue.shift();)e()}clear(){this.queue=[]}}function m(t,n){if(null==t)return t;if("function"==typeof t.e){const e=h({},t.a.props);delete(t={e:t.e,a:h({},t.a),c:t.c}).a.props,t=t.e(e)}if(t&&"function"==typeof t.bind){const s=t.bind(n);let i;try{i=s.render()}catch(e){console.error("Component render error:",e),i={e:"div",a:{class:"error-boundary"},c:[`${e.name}: ${e.message}`]}}if("object"==typeof i&&null!==i&&i instanceof e)i.withIdentity(t.definitionId||s.definitionId,t.instanceId||s.instanceId),i.withLifecycle({mounted:s.mounted,updated:s.updated,unmounted:s.unmounted,deleteState:s.deleteState}),t=i;else if("object"==typeof i&&null!==i){const n=new e(i.e,i.a,i.c);n.withIdentity(t.definitionId||s.definitionId,t.instanceId||s.instanceId),n.withLifecycle({mounted:s.mounted,updated:s.updated,unmounted:s.unmounted,deleteState:s.deleteState}),t=n}else t=i}if(t&&Array.isArray(t.c))for(let e=0,s=t.c.length;e<s;e++)t.c[e]=m(t.c[e],n);return t}class _{areNodesEqual(e,t){const n=typeof e;return n===typeof t&&("object"===n&&e.e===t.e&&e.id===t.id||("string"===n||"number"===n)&&e===t)}reconcileChildren(e,t,n,s){return Array.isArray(t)?this._hasKeys(t)||this._hasKeys(n)?this._reconcileKeyed(e,t,n,s):this._reconcileNonKeyed(e,t,n,s):0}_reconcileNonKeyed(e,t,n,s){const i=t.length;if(0===i){if(n)for(let e=0;e<n.length;e++)s._unmountNodeAndChildren(n[e]);return e.innerHTML="",0}const r=e.childNodes;for(let o=0;o<i;o++)s.patch(e,r[o]||null,t[o],n?n[o]:void 0);return i}_hasKeys(e){if(!Array.isArray(e))return!1;for(let t=0,n=e.length;t<n;t++)if(e[t]&&e[t].a&&null!=e[t].a.key)return!0;return!1}_reconcileKeyed(e,t,n,s){const i=n?n.length:0,r=t.length;if(0===r){for(let e=0;e<i;e++)s._unmountNodeAndChildren(n[e]);return e.innerHTML="",0}const o=Object.create(null),h=Object.create(null);for(let t=0;t<i;t++){const s=n[t].a?n[t].a.key:null,i=e.childNodes[t];null!=s&&i&&(o[s]=i,h[s]=n[t])}const c=new Array(r),u=new Array(r);for(let e=0;e<r;e++){const n=t[e].a?t[e].a.key:null;null!=n&&o[n]?(c[e]=o[n],u[e]=h[n],delete o[n]):(c[e]=null,u[e]=null)}for(const t in o){const n=o[t];n&&(s.patch(e,n,void 0,h[t]),n.remove())}let d=e.firstChild;for(let n=0;n<r;n++){const i=c[n];if(i&&i===d)d=d.nextSibling;else if(i)e.insertBefore(i,d);else{const i=s.createElement(t[n]);i&&e.insertBefore(i,d)}}for(;d;){const e=d.nextSibling;d.remove(),d=e}for(let n=0;n<r;n++)u[n]&&s.patch(e,e.childNodes[n],t[n],u[n]);return r}removeExtraChildren(e,t,n,s){const i=t.c?t.c.length:0;if(!(i<=n))for(let r=i;r-- >n;){const n=e.lastChild;n&&(s._unmountNodeAndChildren(t.c[r]),n.remove())}}}class g{constructor(e,t,n){this.generateTree=e||m,this.renderer=t,this.lifecycleQueue=n}mount(e,t,n){const s=this.generateTree(t(),n);return this.renderer.mount(e,s),this.lifecycleQueue.flush(),s}update(e,t,n,s){const i=this.generateTree(t(),n);return this.renderer.patch(e,e.firstChild,i,s),this.lifecycleQueue.flush(),i}unmount(e,t){t&&(this.renderer.unmount(e,t),this.lifecycleQueue.flush())}}class v{constructor(e,t="microtask"){this._renderFn=e,this._scheduler=t,this._scheduled=!1}schedule(){this._scheduled||(this._scheduled=!0,"microtask"===this._scheduler?Promise.resolve().then(()=>this._execute()):"sync"===this._scheduler&&this._execute())}_execute(){this._scheduled=!1,this._renderFn()}}class b{constructor(e,t={}){this.rootComponent=e,this.savedTree=e,this.currentTree=null,this.rootNode=null,this.stateManager=new u(t),this.lifecycleQueue=new y,this.eventManager=new p,this.renderer=new d(this.eventManager,this.lifecycleQueue,new _),this.mountOrchestrator=new g(m,this.renderer,this.lifecycleQueue),this.updateLoop=new v(()=>this.update(),"microtask"),this._boundSchedule=()=>this.updateLoop.schedule(),this.stateManager.subscribe(this._boundSchedule)}mount(e){if(this.rootNode=document.getElementById(e),!this.rootNode)throw new Error(`Cannot mount app: element with id "${e}" not found`);return this.eventManager.setRootNode(this.rootNode),this.currentTree=this.mountOrchestrator.mount(this.rootNode,this.savedTree,this.stateManager),this}unmount(){this.mountOrchestrator.unmount(this.rootNode,this.currentTree),this.eventManager.destroy(),this.stateManager.unsubscribe(()=>this.updateLoop.schedule()),this.rootNode&&(this.rootNode.innerHTML=""),this.currentTree=null,this.rootNode=null}scheduleUpdate(){this.updateLoop.schedule()}update(){this.rootNode&&this.savedTree&&(this.currentTree=this.mountOrchestrator.update(this.rootNode,this.savedTree,this.stateManager,this.currentTree))}getStore(){return this.stateManager.getStore()}setStore(e,t){this.stateManager.setStore(e,t)}}function N(e,t,n){const s=new b(e,n);return s.mount(t),s}function S(e,t,n={}){const s=new b(e,n);return s.mount(t),s}export{b as App,p as EventManager,y as LifecycleQueue,d as Renderer,u as StateManager,o as component,N as createApp,t as flatten,m as generateTree,i as generateUUID,n as h,s as isVNode,S as render};
|
package/build/index.umd.js
CHANGED
|
@@ -1,2 +1 @@
|
|
|
1
|
-
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t(e.literaljs={})}(this,function(e){var t,n,i,o={},r={},f="eid",u=[],d={},s=[],a=["abort","blur","error","focus","load","mouseenter","mouseleave","resize","scroll","unload"],c="string",h="number",p="boolean",v="undefined",y="object",l="function",m="events",b="style",g="value";function k(e){return Array.isArray(e)}function S(){return Math.random().toString(36).substring(2)+(new Date).getTime().toString(36)}function j(e,t){for(var n=0,i=t.length;n<i;n++)k(t[n])?j(e,t[n]):e.push(t[n]);return e}function N(e,t){for(var n in t)e[n]=t[n];return e}function A(e,t,n){return N(N(e,t),n)}function O(e,t){for(var n=[],i=arguments.length-2;i-- >0;)n[i]=arguments[i+2];return{e:e,a:N({},t),c:j([],n)}}function x(e,r,f){void 0===f&&(f={}),o=N({},f),n=e,t=document.getElementById(r),i=L(e()),D(t,i),q()}function E(e){void 0===e&&(e={});var t=e.name;void 0===t&&(t="");var n=e.state;void 0===n&&(n={});var i=e.mounted;void 0===i&&(i=function(){});var f=e.updated;void 0===f&&(f=function(){});var u=e.unmounted;void 0===u&&(u=function(){});var d=e.methods;void 0===d&&(d=function(){});var s=e.render;void 0===s&&(s=function(){});var a=S();return function(e){return void 0===e&&(e={}),{id:a,init:function(){return this.id=a,this.name=t+(e.key?"_"+e.key:""),this.key=this.id+(this.name||""),this.props=e,this.store=o,this.getStore=C,this.setStore=function(e,t){typeof e===l&&(e=e(o)),o=A({},o,e),B(),typeof t===l&&t.bind(this)()}.bind(this),this.getState=function(){return N({},r[this.key])}.bind(this),this.setState=function(e,t){typeof e===l&&(e=e(r[this.key])),r[this.key]=A({},r[this.key],e),B(),typeof t===l&&t.bind(this)()}.bind(this),this.createState=function(){typeof r[this.key]===v&&(r[this.key]=n)}.bind(this)(),this.deleteState=function(){delete r[this.key]}.bind(this),this.mounted=i.bind(this),this.updated=f.bind(this),this.unmounted=u.bind(this),function(e,t){for(var n in typeof t===l&&(t=t.bind(e)()),t)e[n]=t[n].bind(e)}(this,d),this.render=s.bind(this),this}}}}function C(){return N({},o)}function L(e){if(typeof e===v)return e;if(typeof e.e===l){var t=N({},e.a.props);delete e.a.props,e=e.e(t)}if(typeof e.init===l){var n=new e.init;typeof(e=n.render())===y&&(e.id=n.id,e.m=n.mounted,e.up=n.updated,e.un=n.unmounted,e.d=n.deleteState)}if(k(e.c))for(var i=0,o=e.c.length;i<o;i++)e.c[i]=L(e.c[i]);return e}function T(e){var t=typeof e;if(t===c||t===h)return document.createTextNode(e);if(t===p||t===v)return document.createTextNode("");var n=document.createElement(e.e);if(typeof e.a===y)for(var i=Object.keys(e.a),o=i.length;o--;)i[o]===m?(e.eid=S(),J(e.eid,e.a[i[o]],n)):i[o]===b?z(e.a[i[o]],n):n.setAttribute(i[o],e.a[i[o]]);if(F(e,"m"),k(e.c))for(var r=0,f=e.c.length;r<f;r++)D(n,e.c[r]);return n}function w(e){if("#document"!==e.parentNode.nodeName){var t=e.getAttribute("data-"+f);return typeof t===c?t:w(e.parentNode)}}function J(e,n,i){i.setAttribute("data-"+f,e),d[e]=n;for(var o=Object.keys(n),r=o.length;r--;)a.includes(o[r])?i.addEventListener(o[r],d[e][o[r]]):u.includes(o[r])||(u.push(o[r]),t.addEventListener(o[r],function(e){var t=w(e.target);if(typeof t===c&&d[t]){var n=d[t][e.type];typeof n===l&&n(e)}}))}function z(e,t){if(typeof e!==v)for(var n=Object.keys(e),i=n.length;i--;)t.style[n[i]]=e[n[i]]}function B(e){var o=N({},i),r=L(n());i=r,function e(t,n,i,o){var r=typeof i,f=typeof o;if(f===v)D(t,i);else if(r===v)I(o),n.remove();else if(function(e,t,n,i){return n!==i||e.e!==t.e||e.id!==t.id||(n===c||n===h)&&e!==t}(i,o,r,f))I(o),t.replaceChild(T(i),n);else if(r===y){var u;!function(e,t,n){F(t,"up");for(var i,o,r=Object.keys(t.a),f=r.length;f--;)if(o=t.a[r[f]],i=n.a[r[f]],r[f]===b){if(a=typeof(d=i),(s=typeof(u=o))===y&&a===y&&JSON.stringify(u)===JSON.stringify(d)||s===v&&a===v)continue;e.removeAttribute(r[f]),z(o,e)}else r[f]===m?(_(e,n.eid),M(n.eid),t.eid=S(),J(t.eid,o,e)):r[f]===g&&o!==i?e.value=o:typeof i!==v&&o===i||e.setAttribute(r[f],o);var u,d,s,a}(n,i,o);for(var d=o.c.length;d--&&typeof i.c[d]===v;)typeof(u=n.lastChild)===y&&(I(o.c[d]),u.remove());for(var s=0,a=i.c.length;s<a;s++)e(n,n.childNodes[s],i.c[s],o.c[s])}}(t,t.firstChild,r,o),q()}function D(e,t){e.appendChild(T(t))}function I(e){if(typeof e!==v&&(F(e,"un"),F(e,"d"),M(e.eid),k(e.c)))for(var t=e.c.length;t--;)I(e.c[t])}function M(e){typeof e===c&&delete d[e]}function _(e,t){for(var n=Object.keys(d[t]),i=n.length;i--;)a.includes(n[i])&&e.removeEventListener(n[i],d[t][n[i]])}function q(){for(var e;e=s.shift();)e()}function F(e,t){typeof e===y&&typeof e[t]===l&&s.push(e[t])}module.exports={component:E,render:x,h:O},e.h=O,e.render=x,e.component=E});
|
|
2
|
-
//# sourceMappingURL=index.umd.js.map
|
|
1
|
+
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t((e||self).literaljs={})}(this,function(e){class t{constructor(e,t,n){void 0===t&&(t={}),void 0===n&&(n=[]),this.e=e,this.a=t,this.c=n}withLifecycle(e){return e.mounted&&(this.m=e.mounted),e.updated&&(this.up=e.updated),e.unmounted&&(this.un=e.unmounted),e.deleteState&&(this.d=e.deleteState),this}withIdentity(e,t){return this.definitionId=e,this.instanceId=t,this.id=e+"_"+t,this}}function n(e,t){for(let s=0,i=t.length;s<i;s++)Array.isArray(t[s])?n(e,t[s]):e.push(t[s]);return e}function s(){return Math.random().toString(36).substring(2)+(new Date).getTime().toString(36)}class i{constructor(e,t,n,s,i,o){this.definitionId=e,this.instanceId=t,this.name=n,this.key=`${e}_${t}_${n}`,this.props=s,this._stateManager=o,this._config=i,o.createState(this.key,i.state),this.mounted=i.mounted.bind(this),this.updated=i.updated.bind(this),this.unmounted=i.unmounted.bind(this),this.deleteState=()=>{this._stateManager.deleteState(this.key)},this._renderedVNode=null,this._previousProps=null;const r=i.render.bind(this);this.render=()=>{if(!this.shouldUpdate())return this._renderedVNode;const e=r();return this._renderedVNode=e,this._previousProps=this.props,e}}getStore(){return this._stateManager.getStore()}setStore(e,t){this._stateManager.setStore(e,t)}getState(){return this._stateManager.getState(this.key)}setState(e,t){this._stateManager.setState(this.key,e,t)}createState(){this._stateManager.createState(this.key,this._config.state)}deleteState(){this._stateManager.deleteState(this.key)}shouldUpdate(){return!this._previousProps||!function(e,t){if(e===t)return!0;if(!e||!t||"object"!=typeof e||"object"!=typeof t)return!1;const n=Object.keys(e),s=Object.keys(t);if(n.length!==s.length)return!1;for(let s=0;s<n.length;s++)if(e[n[s]]!==t[n[s]])return!1;return!0}(this.props,this._previousProps)}bindMethods(e){"function"==typeof e&&(e=e.bind(this)());for(let t in e)this[t]=e[t].bind(this)}}function o(e,t){for(let n in t)e[n]=t[n];return e}class r{constructor(e){void 0===e&&(e={}),this.store={...e},this.componentStates={},this.listeners=[]}subscribe(e){this.listeners.push(e)}unsubscribe(e){const t=this.listeners.indexOf(e);t>-1&&this.listeners.splice(t,1)}_notify(){for(let e=0;e<this.listeners.length;e++)this.listeners[e]()}getStore(){return{...this.store}}setStore(e,t){"function"==typeof e&&(e=e(this.store)),this.store=o(o({},this.store),e),this._notify(),"function"==typeof t&&t()}createState(e,t){e in this.componentStates||(this.componentStates[e]={...t})}getState(e){return{...this.componentStates[e]}}setState(e,t,n){"function"==typeof t&&(t=t(this.componentStates[e])),this.componentStates[e]=o(o({},this.componentStates[e]),t),this._notify(),"function"==typeof n&&n()}deleteState(e){delete this.componentStates[e]}reset(){this.store={},this.componentStates={},this.listeners=[]}}class h{constructor(e,t,n){this.eventManager=e,this.lifecycleQueue=t,this.diffEngine=n}mount(e,t){const n=this.createElement(t);n&&e.appendChild(n)}createElement(e){const t=typeof e;return"string"===t||"number"===t?document.createTextNode(e):"boolean"===t||"undefined"===t?document.createTextNode(""):"object"===t&&e.e?this._createElementNode(e):null}_createElementNode(e){const t=document.createElement(e.e);if("object"==typeof e.a){const n=Object.keys(e.a);for(let s=n.length;s--;)if("events"===n[s])e.eid=this.eventManager.bindEvents(t,e.a[n[s]]);else if("style"===n[s])this._addStyles(e.a[n[s]],t);else{if("key"===n[s])continue;t.setAttribute(n[s],e.a[n[s]])}}if(this.lifecycleQueue.mounted(e),Array.isArray(e.c))for(let n=0,s=e.c.length;n<s;n++){const s=this.createElement(e.c[n]);s&&t.appendChild(s)}return t}_addStyles(e,t){if(void 0===e)return;const n=Object.keys(e);for(let s=n.length;s--;)t.style[n[s]]=e[n[s]]}patch(e,t,n,s){const i=typeof n;if(void 0===s)this._appendChild(e,n);else if("undefined"===i)this._unmountNodeAndChildren(s),t&&t.remove();else if(this.diffEngine.areNodesEqual(n,s)){if("object"===i&&n.e){if(n===s)return;if(this._updateNodes(t,n,s),n.c!==s.c)if(this.diffEngine._hasKeys(n.c)||this.diffEngine._hasKeys(s.c))this.diffEngine.reconcileChildren(t,n.c,s.c,this);else{let e;for(let i=s.c?s.c.length:0;i--&&void 0===n.c[i];)e=t.lastChild,e&&(this._unmountNodeAndChildren(s.c[i]),e.remove());if(Array.isArray(n.c))for(let e=0,i=n.c.length;e<i;e++)this.patch(t,t.childNodes[e]||null,n.c[e],s.c?s.c[e]:void 0)}}}else if(!t||3!==t.nodeType||"string"!==i&&"number"!==i)this._unmountNodeAndChildren(s),t&&e.replaceChild(this.createElement(n),t);else{const e=String(n);t.textContent!==e&&(t.textContent=e)}}_appendChild(e,t){const n=this.createElement(t);n&&e.appendChild(n)}_updateNodes(e,t,n){if(this.lifecycleQueue.updated(t),t.a===n.a)return;if(!t.a||!n.a)return;const s=Object.keys(t.a);let i,o;for(let r=s.length;r--;)if(o=t.a[s[r]],i=n.a[s[r]],"style"===s[r]){if(this._stylesAreEqual(o,i))continue;e.removeAttribute(s[r]),this._addStyles(o,e)}else if("events"===s[r])this.eventManager.unbindEvents(e,n.eid),t.eid=this.eventManager.bindEvents(e,o);else{if("key"===s[r])continue;"value"===s[r]&&o!==i?e.value=o:void 0!==i&&o===i||e.setAttribute(s[r],o)}}_stylesAreEqual(e,t){if(e===t)return!0;const n=typeof e,s=typeof t;if("undefined"===n&&"undefined"===s)return!0;if("object"!==n||"object"!==s)return!1;const i=Object.keys(e),o=Object.keys(t);if(i.length!==o.length)return!1;for(let n=i.length;n--;)if(e[i[n]]!==t[i[n]])return!1;return!0}_unmountNodeAndChildren(e){if(null!=e&&(this.lifecycleQueue.unmounted(e),this.lifecycleQueue.deleteState(e),this.eventManager.deleteCachedEvent(e.eid),Array.isArray(e.c)))for(let t=e.c.length;t--;)this._unmountNodeAndChildren(e.c[t])}unmount(e,t){this._unmountNodeAndChildren(t),e&&(e.innerHTML="")}}class d{constructor(e,t,n){this._getCache=e,this._resolveUUID=t,this._elementToUUID=n,this._attachedTypes=new Set,this._rootNode=null}attach(e,t){this._attachedTypes.has(t)||(this._attachedTypes.add(t),this._rootNode=e,e._literaljs_hasEvents=!0,e.addEventListener(t,e=>this._handle(e)))}_handle(e){if(!this._rootNode||!this._rootNode._literaljs_hasEvents)return;let t=e.target;for(;t&&t!==this._rootNode;){let n;if(this._elementToUUID&&(n=this._elementToUUID.get(t))){const t=this._getCache(n);if(t&&"function"==typeof t[e.type])return void t[e.type](e)}const s=this._resolveUUID(t);if("string"==typeof s){const t=this._getCache(s);if(t&&"function"==typeof t[e.type])return void t[e.type](e)}t=t.parentNode}}reset(){this._attachedTypes.clear()}}const c=["abort","blur","error","focus","load","mouseenter","mouseleave","resize","scroll","unload"],u="eid";class a{constructor(){this.cache={},this.rootNode=null,this._elementToUUID=new WeakMap,this.eventBus=new d(e=>this.cache[e],e=>this.getEventUUID(e),this._elementToUUID)}setRootNode(e){this.rootNode=e}getEventUUID(e){if(!e||!e.parentNode)return;if("#document"===e.parentNode.nodeName)return;const t=e.getAttribute("data-"+u);return"string"==typeof t?t:this.getEventUUID(e.parentNode)}bindEvents(e,t){const n=s();e.setAttribute("data-"+u,n),this.cache[n]=t,this._elementToUUID.set(e,n);const i=Object.keys(t);for(let t=i.length;t--;)c.includes(i[t])?e.addEventListener(i[t],this.cache[n][i[t]]):this.eventBus.attach(this.rootNode,i[t]);return n}unbindEvents(e,t){if(!this.cache[t])return;const n=Object.keys(this.cache[t]);for(let s=n.length;s--;)c.includes(n[s])&&e.removeEventListener(n[s],this.cache[t][n[s]]);this.deleteCachedEvent(t),this._elementToUUID.delete(e),e.removeAttribute("data-"+u)}addEvents(e,t,n){n.setAttribute("data-"+u,e),this.cache[e]=t,this._elementToUUID.set(n,e);const s=Object.keys(t);for(let t=s.length;t--;)c.includes(s[t])?n.addEventListener(s[t],this.cache[e][s[t]]):this.eventBus.attach(this.rootNode,s[t])}removeIndividualEvents(e,t){if(!this.cache[t])return;const n=Object.keys(this.cache[t]);for(let s=n.length;s--;)c.includes(n[s])&&e.removeEventListener(n[s],this.cache[t][n[s]])}deleteCachedEvent(e){"string"==typeof e&&delete this.cache[e]}destroy(){this.rootNode&&(this.rootNode._literaljs_hasEvents=!1),this.cache={},this.rootNode=null,this.eventBus.reset()}}class l{constructor(){this.queue=[]}mounted(e){"object"==typeof e&&"function"==typeof e.m&&this.queue.push(e.m)}updated(e){"object"==typeof e&&"function"==typeof e.up&&this.queue.push(e.up)}unmounted(e){"object"==typeof e&&"function"==typeof e.un&&this.queue.push(e.un)}deleteState(e){"object"==typeof e&&"function"==typeof e.d&&this.queue.push(e.d)}flush(){let e;for(;e=this.queue.shift();)e()}clear(){this.queue=[]}}function f(e,n){if(null==e)return e;if("function"==typeof e.e){const t={...e.a.props};delete(e={e:e.e,a:{...e.a},c:e.c}).a.props,e=e.e(t)}if(e&&"function"==typeof e.bind){const s=e.bind(n);let i;try{i=s.render()}catch(e){console.error("Component render error:",e),i={e:"div",a:{class:"error-boundary"},c:[`${e.name}: ${e.message}`]}}if("object"==typeof i&&null!==i&&i instanceof t)i.withIdentity(e.definitionId||s.definitionId,e.instanceId||s.instanceId),i.withLifecycle({mounted:s.mounted,updated:s.updated,unmounted:s.unmounted,deleteState:s.deleteState}),e=i;else if("object"==typeof i&&null!==i){const n=new t(i.e,i.a,i.c);n.withIdentity(e.definitionId||s.definitionId,e.instanceId||s.instanceId),n.withLifecycle({mounted:s.mounted,updated:s.updated,unmounted:s.unmounted,deleteState:s.deleteState}),e=n}else e=i}if(e&&Array.isArray(e.c))for(let t=0,s=e.c.length;t<s;t++)e.c[t]=f(e.c[t],n);return e}class p{areNodesEqual(e,t){const n=typeof e;return n===typeof t&&("object"===n&&e.e===t.e&&e.id===t.id||("string"===n||"number"===n)&&e===t)}reconcileChildren(e,t,n,s){return Array.isArray(t)?this._hasKeys(t)||this._hasKeys(n)?this._reconcileKeyed(e,t,n,s):this._reconcileNonKeyed(e,t,n,s):0}_reconcileNonKeyed(e,t,n,s){const i=t.length;if(0===i){if(n)for(let e=0;e<n.length;e++)s._unmountNodeAndChildren(n[e]);return e.innerHTML="",0}const o=e.childNodes;for(let r=0;r<i;r++)s.patch(e,o[r]||null,t[r],n?n[r]:void 0);return i}_hasKeys(e){if(!Array.isArray(e))return!1;for(let t=0,n=e.length;t<n;t++)if(e[t]&&e[t].a&&null!=e[t].a.key)return!0;return!1}_reconcileKeyed(e,t,n,s){const i=n?n.length:0,o=t.length;if(0===o){for(let e=0;e<i;e++)s._unmountNodeAndChildren(n[e]);return e.innerHTML="",0}const r=Object.create(null),h=Object.create(null);for(let t=0;t<i;t++){const s=n[t].a?n[t].a.key:null,i=e.childNodes[t];null!=s&&i&&(r[s]=i,h[s]=n[t])}const d=new Array(o),c=new Array(o);for(let e=0;e<o;e++){const n=t[e].a?t[e].a.key:null;null!=n&&r[n]?(d[e]=r[n],c[e]=h[n],delete r[n]):(d[e]=null,c[e]=null)}for(const t in r){const n=r[t];n&&(s.patch(e,n,void 0,h[t]),n.remove())}let u=e.firstChild;for(let n=0;n<o;n++){const i=d[n];if(i&&i===u)u=u.nextSibling;else if(i)e.insertBefore(i,u);else{const i=s.createElement(t[n]);i&&e.insertBefore(i,u)}}for(;u;){const e=u.nextSibling;u.remove(),u=e}for(let n=0;n<o;n++)c[n]&&s.patch(e,e.childNodes[n],t[n],c[n]);return o}removeExtraChildren(e,t,n,s){const i=t.c?t.c.length:0;if(!(i<=n))for(let o=i;o-- >n;){const n=e.lastChild;n&&(s._unmountNodeAndChildren(t.c[o]),n.remove())}}}class y{constructor(e,t,n){this.generateTree=e||f,this.renderer=t,this.lifecycleQueue=n}mount(e,t,n){const s=this.generateTree(t(),n);return this.renderer.mount(e,s),this.lifecycleQueue.flush(),s}update(e,t,n,s){const i=this.generateTree(t(),n);return this.renderer.patch(e,e.firstChild,i,s),this.lifecycleQueue.flush(),i}unmount(e,t){t&&(this.renderer.unmount(e,t),this.lifecycleQueue.flush())}}class m{constructor(e,t){void 0===t&&(t="microtask"),this._renderFn=e,this._scheduler=t,this._scheduled=!1}schedule(){this._scheduled||(this._scheduled=!0,"microtask"===this._scheduler?Promise.resolve().then(()=>this._execute()):"sync"===this._scheduler&&this._execute())}_execute(){this._scheduled=!1,this._renderFn()}}class _{constructor(e,t){void 0===t&&(t={}),this.rootComponent=e,this.savedTree=e,this.currentTree=null,this.rootNode=null,this.stateManager=new r(t),this.lifecycleQueue=new l,this.eventManager=new a,this.renderer=new h(this.eventManager,this.lifecycleQueue,new p),this.mountOrchestrator=new y(f,this.renderer,this.lifecycleQueue),this.updateLoop=new m(()=>this.update(),"microtask"),this._boundSchedule=()=>this.updateLoop.schedule(),this.stateManager.subscribe(this._boundSchedule)}mount(e){if(this.rootNode=document.getElementById(e),!this.rootNode)throw new Error(`Cannot mount app: element with id "${e}" not found`);return this.eventManager.setRootNode(this.rootNode),this.currentTree=this.mountOrchestrator.mount(this.rootNode,this.savedTree,this.stateManager),this}unmount(){this.mountOrchestrator.unmount(this.rootNode,this.currentTree),this.eventManager.destroy(),this.stateManager.unsubscribe(()=>this.updateLoop.schedule()),this.rootNode&&(this.rootNode.innerHTML=""),this.currentTree=null,this.rootNode=null}scheduleUpdate(){this.updateLoop.schedule()}update(){this.rootNode&&this.savedTree&&(this.currentTree=this.mountOrchestrator.update(this.rootNode,this.savedTree,this.stateManager,this.currentTree))}getStore(){return this.stateManager.getStore()}setStore(e,t){this.stateManager.setStore(e,t)}}e.App=_,e.EventManager=a,e.LifecycleQueue=l,e.Renderer=h,e.StateManager=r,e.component=function(e){let t=void 0===e?{}:e,n=t.name,o=void 0===n?"":n,r=t.state,h=void 0===r?{}:r,d=t.mounted,c=void 0===d?function(){}:d,u=t.updated,a=void 0===u?function(){}:u,l=t.unmounted,f=void 0===l?function(){}:l,p=t.methods,y=void 0===p?function(){}:p,m=t.render,_=void 0===m?function(){}:m;const g=s(),v=new Map;return function(e){void 0===e&&(e={});const t=e.key||"__default__";if(!v.has(t)){const n=s(),r=o+(e.key?"_"+e.key:"");v.set(t,{definitionId:g,instanceId:n,key:`${g}_${n}_${r}`,_props:null,_cachedRender:null,bind(e){const t=new i(g,n,r,this._props,{state:h,mounted:c,updated:a,unmounted:f,methods:y,render:_},e);return t.bindMethods(y),t}})}const n=v.get(t);return n._props=e,n}},e.createApp=function(e,t,n){const s=new _(e,n);return s.mount(t),s},e.flatten=n,e.generateTree=f,e.generateUUID=s,e.h=function(e,s){const i={};for(let e in s)i[e]=s[e];return new t(e,i,n([],[].slice.call(arguments,2)))},e.isVNode=function(e){return e instanceof t||null!==e&&"object"==typeof e&&"e"in e},e.render=function(e,t,n){void 0===n&&(n={});const s=new _(e,n);return s.mount(t),s}});
|
package/package.json
CHANGED
|
@@ -1,26 +1,39 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "literaljs",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "8.0.0",
|
|
4
4
|
"description": "A small JavaScript library for building reactive user interfaces.",
|
|
5
5
|
"main": "build/index.js",
|
|
6
6
|
"module": "build/index.m.js",
|
|
7
7
|
"source": "src/index.js",
|
|
8
8
|
"scripts": {
|
|
9
|
-
"
|
|
10
|
-
"
|
|
9
|
+
"build": "microbundle --no-sourcemap",
|
|
10
|
+
"test": "jest --coverage",
|
|
11
|
+
"test:watch": "jest --watch",
|
|
12
|
+
"rebuild": "rm -rf build/ && npm run build",
|
|
11
13
|
"prepare": "npm run build",
|
|
12
|
-
"
|
|
13
|
-
"test": "jest --coverage"
|
|
14
|
+
"benchmark": "node benchmark/suite.js"
|
|
14
15
|
},
|
|
15
16
|
"author": "Michael Farrell",
|
|
16
17
|
"license": "MIT",
|
|
18
|
+
"files": [
|
|
19
|
+
"build/"
|
|
20
|
+
],
|
|
21
|
+
"jest": {
|
|
22
|
+
"verbose": true,
|
|
23
|
+
"testEnvironment": "jsdom"
|
|
24
|
+
},
|
|
17
25
|
"babel": {
|
|
18
|
-
"presets":
|
|
26
|
+
"presets": [
|
|
27
|
+
["@babel/preset-env", { "targets": { "chrome": "90", "firefox": "90", "safari": "14" } }]
|
|
28
|
+
]
|
|
19
29
|
},
|
|
20
|
-
"dependencies": {},
|
|
21
30
|
"devDependencies": {
|
|
22
|
-
"babel
|
|
23
|
-
"
|
|
24
|
-
"
|
|
31
|
+
"@babel/core": "^7.26.0",
|
|
32
|
+
"@babel/preset-env": "^7.26.0",
|
|
33
|
+
"babel-jest": "^30.4.1",
|
|
34
|
+
"jest": "^30.4.2",
|
|
35
|
+
"jest-environment-jsdom": "^30.4.1",
|
|
36
|
+
"jsdom": "^29.1.1",
|
|
37
|
+
"microbundle": "^0.15.1"
|
|
25
38
|
}
|
|
26
|
-
}
|
|
39
|
+
}
|
package/build/index.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sources":["../src/index.js"],"sourcesContent":["let ROOT_NODE, SAVED_TREE, CURRENT_TREE;\nlet GLOBAL_STORE = {};\nlet COMPONENT_STATE = {};\nconst EVENTS_KEY = 'eid';\nconst DELEGATED_EVENTS_ADDED = [];\nconst EVENTS_CACHE = {};\nconst LIFECYCLE_QUEUE = [];\nconst NON_DELEGATED_EVENTS = [\n\t'abort',\n\t'blur',\n\t'error',\n\t'focus',\n\t'load',\n\t'mouseenter',\n\t'mouseleave',\n\t'resize',\n\t'scroll',\n\t'unload'\n];\nconst STRING_TYPE = 'string';\nconst NUMBER_TYPE = 'number';\nconst BOOLEAN_TYPE = 'boolean';\nconst UNDEFINED_TYPE = 'undefined';\nconst OBJECT_TYPE = 'object';\nconst FUNCTION_TYPE = 'function';\nconst EVENTS_TYPE = 'events';\nconst STYLE_TYPE = 'style';\nconst VALUE_TYPE = 'value';\n\nfunction isArray(val) {\n\treturn Array.isArray(val);\n}\n\nfunction generateUUID() {\n\treturn (\n\t\tMath.random()\n\t\t\t.toString(36)\n\t\t\t.substring(2) + new Date().getTime().toString(36)\n\t);\n}\n\nfunction flatten(out, a) {\n\tfor (let i = 0, len = a.length; i < len; i++) {\n\t\tif (isArray(a[i])) flatten(out, a[i]);\n\t\telse out.push(a[i]);\n\t}\n\treturn out;\n}\n\nfunction assign(out, a) {\n\tfor (let i in a) out[i] = a[i];\n\treturn out;\n}\n\nfunction merge(out, a, b) {\n\treturn assign(assign(out, a), b);\n}\n\nexport function h(el, attr, ...children) {\n\treturn {\n\t\te: el,\n\t\ta: assign({}, attr),\n\t\tc: flatten([], children)\n\t};\n}\n\nexport function render(components, documentNodeID, state = {}) {\n\t// Initialize application state\n\tGLOBAL_STORE = assign({}, state);\n\t// Cache initial tree for re-instanting with new states\n\t// Cache the initial tree for diffing later\n\tSAVED_TREE = components;\n\t// Grab root node to mount application on\n\tROOT_NODE = document.getElementById(documentNodeID);\n\t// Create first component tree\n\tCURRENT_TREE = generateTreeVN(components());\n\t// Append the new tree to our application for the first paint\n\tappendChild(ROOT_NODE, CURRENT_TREE);\n\t// Initial Lifecycle Trigger\n\tshiftLifcycleQueue();\n}\n\nexport function component({\n\tname = '',\n\tstate = {},\n\tmounted = function() {},\n\tupdated = function() {},\n\tunmounted = function() {},\n\tmethods = function() {},\n\trender = function() {}\n} = {}) {\n\tconst componentUUID = generateUUID();\n\treturn function(props = {}) {\n\t\treturn {\n\t\t\tid: componentUUID,\n\t\t\tinit: function() {\n\t\t\t\tthis.id = componentUUID;\n\t\t\t\tthis.name = name + (props.key ? '_' + props.key : '');\n\t\t\t\tthis.key = this.id + (this.name || '');\n\t\t\t\tthis.props = props;\n\t\t\t\tthis.store = GLOBAL_STORE;\n\t\t\t\tthis.getStore = getStore;\n\t\t\t\tthis.setStore = setStore.bind(this);\n\t\t\t\tthis.getState = getState.bind(this);\n\t\t\t\tthis.setState = setState.bind(this);\n\t\t\t\tthis.createState = function() {\n\t\t\t\t\tif (typeof COMPONENT_STATE[this.key] === UNDEFINED_TYPE) {\n\t\t\t\t\t\tCOMPONENT_STATE[this.key] = state;\n\t\t\t\t\t}\n\t\t\t\t}.bind(this)();\n\t\t\t\tthis.deleteState = function() {\n\t\t\t\t\tdelete COMPONENT_STATE[this.key];\n\t\t\t\t}.bind(this);\n\t\t\t\tthis.mounted = mounted.bind(this);\n\t\t\t\tthis.updated = updated.bind(this);\n\t\t\t\tthis.unmounted = unmounted.bind(this);\n\t\t\t\tbindMethods(this, methods);\n\t\t\t\tthis.render = render.bind(this);\n\t\t\t\treturn this;\n\t\t\t}\n\t\t};\n\t};\n}\n\nfunction getStore() {\n\treturn assign({}, GLOBAL_STORE);\n}\n\nfunction setStore(newStore, callback) {\n\tif (typeof newStore === FUNCTION_TYPE) {\n\t\tnewStore = newStore(GLOBAL_STORE);\n\t}\n\tGLOBAL_STORE = merge({}, GLOBAL_STORE, newStore);\n\tinitiateDiff();\n\tif (typeof callback === FUNCTION_TYPE) callback.bind(this)();\n}\n\nfunction getState() {\n\treturn assign({}, COMPONENT_STATE[this.key]);\n}\n\nfunction setState(newState, callback) {\n\tif (typeof newState === FUNCTION_TYPE) {\n\t\tnewState = newState(COMPONENT_STATE[this.key]);\n\t}\n\tCOMPONENT_STATE[this.key] = merge({}, COMPONENT_STATE[this.key], newState);\n\tinitiateDiff();\n\tif (typeof callback === FUNCTION_TYPE) callback.bind(this)();\n}\n\nfunction bindMethods(component, methods) {\n\tif (typeof methods === FUNCTION_TYPE) {\n\t\tmethods = methods.bind(component)();\n\t}\n\n\tfor (let func in methods) {\n\t\tcomponent[func] = methods[func].bind(component);\n\t}\n}\n\nfunction generateTreeVN(newC) {\n\tif (typeof newC === UNDEFINED_TYPE) {\n\t\treturn newC;\n\t} else if (typeof newC.e === FUNCTION_TYPE) {\n\t\t// For Components in the application to inject props.\n\t\tconst props = assign({}, newC.a.props);\n\t\tdelete newC.a.props;\n\t\tnewC = newC.e(props);\n\t}\n\n\tif (typeof newC.init === FUNCTION_TYPE) {\n\t\t// For the Root Component And Componets\n\t\t// Initialize The Component\n\t\tconst cObj = new newC.init();\n\t\t// Render Component Markup\n\t\tnewC = cObj.render();\n\n\t\tif (typeof newC === OBJECT_TYPE) {\n\t\t\t// Attach Id and LC Methods To Render Object\n\t\t\tnewC.id = cObj.id;\n\t\t\tnewC.m = cObj.mounted;\n\t\t\tnewC.up = cObj.updated;\n\t\t\tnewC.un = cObj.unmounted;\n\t\t\tnewC.d = cObj.deleteState;\n\t\t}\n\t}\n\n\tif (isArray(newC.c)) {\n\t\tfor (let i = 0, len = newC.c.length; i < len; i++) {\n\t\t\tnewC.c[i] = generateTreeVN(newC.c[i]);\n\t\t}\n\t}\n\n\treturn newC;\n}\n\nfunction createDomNode(data) {\n\tconst type = typeof data;\n\tif (type === STRING_TYPE || type === NUMBER_TYPE) {\n\t\treturn document.createTextNode(data);\n\t} else if (type === BOOLEAN_TYPE || type === UNDEFINED_TYPE) {\n\t\treturn document.createTextNode('');\n\t} else {\n\t\tconst newE = document.createElement(data.e);\n\n\t\tif (typeof data.a === OBJECT_TYPE) {\n\t\t\tconst keys = Object.keys(data.a);\n\t\t\tfor (let i = keys.length; i--; ) {\n\t\t\t\tif (keys[i] === EVENTS_TYPE) {\n\t\t\t\t\tdata.eid = generateUUID();\n\t\t\t\t\taddEvents(data.eid, data.a[keys[i]], newE);\n\t\t\t\t} else if (keys[i] === STYLE_TYPE) {\n\t\t\t\t\taddStyles(data.a[keys[i]], newE);\n\t\t\t\t} else {\n\t\t\t\t\tnewE.setAttribute(keys[i], data.a[keys[i]]);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\tpushLifecycle(data, 'm');\n\n\t\tif (isArray(data.c)) {\n\t\t\tfor (let i = 0, len = data.c.length; i < len; i++) {\n\t\t\t\tappendChild(newE, data.c[i]);\n\t\t\t}\n\t\t}\n\n\t\treturn newE;\n\t}\n}\n\nfunction getEventUUID(element) {\n\tif (element.parentNode.nodeName === '#document') return;\n\tconst eid = element.getAttribute('data-' + EVENTS_KEY);\n\tif (typeof eid === STRING_TYPE) return eid;\n\treturn getEventUUID(element.parentNode);\n}\n\nfunction addEvents(eid, eventObj, targetNode) {\n\t// Generate UUID here for event functions\n\t// Send event functions into a cache for lookup by UUID\n\t// UUID is pulled during Event Delegation and the appropriate\n\t// event is picked by type.\n\ttargetNode.setAttribute('data-' + EVENTS_KEY, eid);\n\tEVENTS_CACHE[eid] = eventObj;\n\n\t// Prevent certain events from being delegated due to them not bubbling.\n\tconst keys = Object.keys(eventObj);\n\tfor (let i = keys.length; i--; ) {\n\t\tif (NON_DELEGATED_EVENTS.includes(keys[i])) {\n\t\t\t// Attach event directly if non-bubbling / non-delegated\n\t\t\ttargetNode.addEventListener(keys[i], EVENTS_CACHE[eid][keys[i]]);\n\t\t} else if (!DELEGATED_EVENTS_ADDED.includes(keys[i])) {\n\t\t\t// Added listener type to keep track of what kinds of events\n\t\t\t// are added to the root node.\n\t\t\tDELEGATED_EVENTS_ADDED.push(keys[i]);\n\t\t\tROOT_NODE.addEventListener(keys[i], function(e) {\n\t\t\t\tconst uuid = getEventUUID(e.target);\n\t\t\t\tif (typeof uuid === STRING_TYPE && EVENTS_CACHE[uuid]) {\n\t\t\t\t\tconst event = EVENTS_CACHE[uuid][e.type];\n\t\t\t\t\tif (typeof event === FUNCTION_TYPE) event(e);\n\t\t\t\t}\n\t\t\t});\n\t\t}\n\t}\n}\n\nfunction addStyles(styles, e) {\n\tif (typeof styles === UNDEFINED_TYPE) return;\n\tconst keys = Object.keys(styles);\n\tfor (let i = keys.length; i--; ) {\n\t\te.style[keys[i]] = styles[keys[i]];\n\t}\n}\n\nfunction initiateDiff(uuid) {\n\t// Generate new and old trees\n\tconst oldTree = assign({}, CURRENT_TREE);\n\tconst newTree = generateTreeVN(SAVED_TREE());\n\t// Cache new tree for future diffs\n\tCURRENT_TREE = newTree;\n\t// Initiate diff of trees...\n\tdiffAndPatch(ROOT_NODE, ROOT_NODE.firstChild, newTree, oldTree);\n\t// Trigger Lifecycle Methods...\n\tshiftLifcycleQueue();\n}\n\nfunction diffAndPatch(parentNode, targetNode, newVN, oldVN) {\n\tconst nType = typeof newVN;\n\tconst oType = typeof oldVN;\n\n\tif (oType === UNDEFINED_TYPE) {\n\t\tappendChild(parentNode, newVN);\n\t} else if (nType === UNDEFINED_TYPE) {\n\t\tunmountNodeAndChildren(oldVN);\n\t\ttargetNode.remove();\n\t} else if (nodesNotEqual(newVN, oldVN, nType, oType)) {\n\t\tunmountNodeAndChildren(oldVN);\n\t\tparentNode.replaceChild(createDomNode(newVN), targetNode);\n\t} else if (nType === OBJECT_TYPE) {\n\t\tupdateNodes(targetNode, newVN, oldVN);\n\n\t\tlet lastChild;\n\t\tfor (let i = oldVN.c.length; i--; ) {\n\t\t\tif (typeof newVN.c[i] !== UNDEFINED_TYPE) break;\n\t\t\tlastChild = targetNode.lastChild;\n\t\t\tif (typeof lastChild === OBJECT_TYPE) {\n\t\t\t\tunmountNodeAndChildren(oldVN.c[i]);\n\t\t\t\tlastChild.remove();\n\t\t\t}\n\t\t}\n\n\t\t// Recurse through children and diff.\n\t\tfor (let i = 0, len = newVN.c.length; i < len; i++) {\n\t\t\tdiffAndPatch(\n\t\t\t\ttargetNode,\n\t\t\t\ttargetNode.childNodes[i],\n\t\t\t\tnewVN.c[i],\n\t\t\t\toldVN.c[i]\n\t\t\t);\n\t\t}\n\t}\n}\n\nfunction appendChild(parentNode, newNode) {\n\tparentNode.appendChild(createDomNode(newNode));\n}\n\nfunction nodesNotEqual(newVN, oldVN, nType, oType) {\n\treturn (\n\t\tnType !== oType ||\n\t\tnewVN.e !== oldVN.e ||\n\t\tnewVN.id !== oldVN.id ||\n\t\t((nType === STRING_TYPE || nType === NUMBER_TYPE) && newVN !== oldVN)\n\t);\n}\n\nfunction updateNodes(targetNode, newVN, oldVN) {\n\tpushLifecycle(newVN, 'up');\n\n\tconst keys = Object.keys(newVN.a);\n\tlet oldVal, newVal;\n\tfor (let i = keys.length; i--; ) {\n\t\tnewVal = newVN.a[keys[i]];\n\t\toldVal = oldVN.a[keys[i]];\n\t\tif (keys[i] === STYLE_TYPE) {\n\t\t\tif (stylesAreEqual(newVal, oldVal)) continue;\n\t\t\ttargetNode.removeAttribute(keys[i]);\n\t\t\taddStyles(newVal, targetNode);\n\t\t} else if (keys[i] === EVENTS_TYPE) {\n\t\t\tremoveIndividuallyBoundEvents(targetNode, oldVN.eid);\n\t\t\tdeleteCachedEvent(oldVN.eid);\n\t\t\tnewVN.eid = generateUUID();\n\t\t\taddEvents(newVN.eid, newVal, targetNode);\n\t\t} else if (keys[i] === VALUE_TYPE && newVal !== oldVal) {\n\t\t\ttargetNode.value = newVal;\n\t\t} else if (typeof oldVal === UNDEFINED_TYPE || newVal !== oldVal) {\n\t\t\ttargetNode.setAttribute(keys[i], newVal);\n\t\t}\n\t}\n}\n\nfunction unmountNodeAndChildren(node) {\n\t// Recursivly unmounts all children.\n\tif (typeof node === UNDEFINED_TYPE) return;\n\tpushLifecycle(node, 'un');\n\tpushLifecycle(node, 'd');\n\tdeleteCachedEvent(node.eid);\n\tif (isArray(node.c)) {\n\t\tfor (let i = node.c.length; i--; ) {\n\t\t\tunmountNodeAndChildren(node.c[i]);\n\t\t}\n\t}\n}\n\nfunction deleteCachedEvent(eid) {\n\tif (typeof eid === STRING_TYPE) delete EVENTS_CACHE[eid];\n}\n\nfunction removeIndividuallyBoundEvents(targetNode, eid) {\n\tconst keys = Object.keys(EVENTS_CACHE[eid]);\n\tfor (let i = keys.length; i--; ) {\n\t\tif (NON_DELEGATED_EVENTS.includes(keys[i])) {\n\t\t\ttargetNode.removeEventListener(keys[i], EVENTS_CACHE[eid][keys[i]]);\n\t\t}\n\t}\n}\n\nfunction stylesAreEqual(newStyle, oldStyle) {\n\tconst newType = typeof newStyle;\n\tconst oldType = typeof oldStyle;\n\n\treturn (\n\t\t(newType === OBJECT_TYPE &&\n\t\t\toldType === OBJECT_TYPE &&\n\t\t\tJSON.stringify(newStyle) === JSON.stringify(oldStyle)) ||\n\t\t(newType === UNDEFINED_TYPE && oldType === UNDEFINED_TYPE)\n\t);\n}\n\nfunction shiftLifcycleQueue() {\n\tlet lc;\n\twhile ((lc = LIFECYCLE_QUEUE.shift())) lc();\n}\n\nfunction pushLifecycle(node, lcName) {\n\tif (typeof node === OBJECT_TYPE && typeof node[lcName] === FUNCTION_TYPE) {\n\t\tLIFECYCLE_QUEUE.push(node[lcName]);\n\t}\n}\n\nmodule.exports = {\n\tcomponent,\n\trender,\n\th\n};\n"],"names":["let","ROOT_NODE","SAVED_TREE","CURRENT_TREE","GLOBAL_STORE","COMPONENT_STATE","EVENTS_KEY","DELEGATED_EVENTS_ADDED","EVENTS_CACHE","LIFECYCLE_QUEUE","NON_DELEGATED_EVENTS","STRING_TYPE","NUMBER_TYPE","BOOLEAN_TYPE","UNDEFINED_TYPE","OBJECT_TYPE","FUNCTION_TYPE","EVENTS_TYPE","STYLE_TYPE","VALUE_TYPE","isArray","val","Array","generateUUID","Math","random","toString","substring","Date","getTime","flatten","out","a","i","len","length","push","assign","merge","b","h","el","attr","e","c","children","render","components","documentNodeID","state","document","getElementById","generateTreeVN","appendChild","shiftLifcycleQueue","component","ref","componentUUID","props","id","init","name","key","this","store","getStore","setStore","newStore","callback","initiateDiff","bind","getState","setState","newState","createState","deleteState","mounted","updated","unmounted","methods","func","bindMethods","newC","cObj","m","up","un","d","createDomNode","data","type","createTextNode","newE","createElement","keys","Object","eid","addEvents","addStyles","setAttribute","pushLifecycle","getEventUUID","element","parentNode","nodeName","getAttribute","eventObj","targetNode","includes","addEventListener","uuid","target","event","styles","style","oldTree","newTree","diffAndPatch","newVN","oldVN","nType","oType","unmountNodeAndChildren","remove","nodesNotEqual","replaceChild","lastChild","oldVal","newVal","oldType","oldStyle","newType","newStyle","JSON","stringify","removeAttribute","removeIndividuallyBoundEvents","deleteCachedEvent","value","updateNodes","childNodes","firstChild","newNode","node","removeEventListener","lc","shift","lcName","module","exports"],"mappings":"AAAAA,IAAIC,EAAWC,EAAYC,EACvBC,EAAe,GACfC,EAAkB,GAChBC,EAAa,MACbC,EAAyB,GACzBC,EAAe,GACfC,EAAkB,GAClBC,EAAuB,CAC5B,QACA,OACA,QACA,QACA,OACA,aACA,aACA,SACA,SACA,UAEKC,EAAc,SACdC,EAAc,SACdC,EAAe,UACfC,EAAiB,YACjBC,EAAc,SACdC,EAAgB,WAChBC,EAAc,SACdC,EAAa,QACbC,EAAa,QAEnB,SAASC,EAAQC,UACTC,MAAMF,QAAQC,GAGtB,SAASE,WAEPC,KAAKC,SACHC,SAAS,IACTC,UAAU,IAAK,IAAIC,MAAOC,UAAUH,SAAS,IAIjD,SAASI,EAAQC,EAAKC,OAChBhC,IAAIiC,EAAI,EAAGC,EAAMF,EAAEG,OAAQF,EAAIC,EAAKD,IACpCb,EAAQY,EAAEC,IAAKH,EAAQC,EAAKC,EAAEC,IAC7BF,EAAIK,KAAKJ,EAAEC,WAEVF,EAGR,SAASM,EAAON,EAAKC,OACfhC,IAAIiC,KAAKD,EAAGD,EAAIE,GAAKD,EAAEC,UACrBF,EAGR,SAASO,EAAMP,EAAKC,EAAGO,UACfF,EAAOA,EAAON,EAAKC,GAAIO,GAGxB,SAASC,EAAEC,EAAIC,uEACd,CACNC,EAAGF,EACHT,EAAGK,EAAO,GAAIK,GACdE,EAAGd,EA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","sources":["../src/index.js"],"sourcesContent":["let ROOT_NODE, SAVED_TREE, CURRENT_TREE;\nlet GLOBAL_STORE = {};\nlet COMPONENT_STATE = {};\nconst EVENTS_KEY = 'eid';\nconst DELEGATED_EVENTS_ADDED = [];\nconst EVENTS_CACHE = {};\nconst LIFECYCLE_QUEUE = [];\nconst NON_DELEGATED_EVENTS = [\n\t'abort',\n\t'blur',\n\t'error',\n\t'focus',\n\t'load',\n\t'mouseenter',\n\t'mouseleave',\n\t'resize',\n\t'scroll',\n\t'unload'\n];\nconst STRING_TYPE = 'string';\nconst NUMBER_TYPE = 'number';\nconst BOOLEAN_TYPE = 'boolean';\nconst UNDEFINED_TYPE = 'undefined';\nconst OBJECT_TYPE = 'object';\nconst FUNCTION_TYPE = 'function';\nconst EVENTS_TYPE = 'events';\nconst STYLE_TYPE = 'style';\nconst VALUE_TYPE = 'value';\n\nfunction isArray(val) {\n\treturn Array.isArray(val);\n}\n\nfunction generateUUID() {\n\treturn (\n\t\tMath.random()\n\t\t\t.toString(36)\n\t\t\t.substring(2) + new Date().getTime().toString(36)\n\t);\n}\n\nfunction flatten(out, a) {\n\tfor (let i = 0, len = a.length; i < len; i++) {\n\t\tif (isArray(a[i])) flatten(out, a[i]);\n\t\telse out.push(a[i]);\n\t}\n\treturn out;\n}\n\nfunction assign(out, a) {\n\tfor (let i in a) out[i] = a[i];\n\treturn out;\n}\n\nfunction merge(out, a, b) {\n\treturn assign(assign(out, a), b);\n}\n\nexport function h(el, attr, ...children) {\n\treturn {\n\t\te: el,\n\t\ta: assign({}, attr),\n\t\tc: flatten([], children)\n\t};\n}\n\nexport function render(components, documentNodeID, state = {}) {\n\t// Initialize application state\n\tGLOBAL_STORE = assign({}, state);\n\t// Cache initial tree for re-instanting with new states\n\t// Cache the initial tree for diffing later\n\tSAVED_TREE = components;\n\t// Grab root node to mount application on\n\tROOT_NODE = document.getElementById(documentNodeID);\n\t// Create first component tree\n\tCURRENT_TREE = generateTreeVN(components());\n\t// Append the new tree to our application for the first paint\n\tappendChild(ROOT_NODE, CURRENT_TREE);\n\t// Initial Lifecycle Trigger\n\tshiftLifcycleQueue();\n}\n\nexport function component({\n\tname = '',\n\tstate = {},\n\tmounted = function() {},\n\tupdated = function() {},\n\tunmounted = function() {},\n\tmethods = function() {},\n\trender = function() {}\n} = {}) {\n\tconst componentUUID = generateUUID();\n\treturn function(props = {}) {\n\t\treturn {\n\t\t\tid: componentUUID,\n\t\t\tinit: function() {\n\t\t\t\tthis.id = componentUUID;\n\t\t\t\tthis.name = name + (props.key ? '_' + props.key : '');\n\t\t\t\tthis.key = this.id + (this.name || '');\n\t\t\t\tthis.props = props;\n\t\t\t\tthis.store = GLOBAL_STORE;\n\t\t\t\tthis.getStore = getStore;\n\t\t\t\tthis.setStore = setStore.bind(this);\n\t\t\t\tthis.getState = getState.bind(this);\n\t\t\t\tthis.setState = setState.bind(this);\n\t\t\t\tthis.createState = function() {\n\t\t\t\t\tif (typeof COMPONENT_STATE[this.key] === UNDEFINED_TYPE) {\n\t\t\t\t\t\tCOMPONENT_STATE[this.key] = state;\n\t\t\t\t\t}\n\t\t\t\t}.bind(this)();\n\t\t\t\tthis.deleteState = function() {\n\t\t\t\t\tdelete COMPONENT_STATE[this.key];\n\t\t\t\t}.bind(this);\n\t\t\t\tthis.mounted = mounted.bind(this);\n\t\t\t\tthis.updated = updated.bind(this);\n\t\t\t\tthis.unmounted = unmounted.bind(this);\n\t\t\t\tbindMethods(this, methods);\n\t\t\t\tthis.render = render.bind(this);\n\t\t\t\treturn this;\n\t\t\t}\n\t\t};\n\t};\n}\n\nfunction getStore() {\n\treturn assign({}, GLOBAL_STORE);\n}\n\nfunction setStore(newStore, callback) {\n\tif (typeof newStore === FUNCTION_TYPE) {\n\t\tnewStore = newStore(GLOBAL_STORE);\n\t}\n\tGLOBAL_STORE = merge({}, GLOBAL_STORE, newStore);\n\tinitiateDiff();\n\tif (typeof callback === FUNCTION_TYPE) callback.bind(this)();\n}\n\nfunction getState() {\n\treturn assign({}, COMPONENT_STATE[this.key]);\n}\n\nfunction setState(newState, callback) {\n\tif (typeof newState === FUNCTION_TYPE) {\n\t\tnewState = newState(COMPONENT_STATE[this.key]);\n\t}\n\tCOMPONENT_STATE[this.key] = merge({}, COMPONENT_STATE[this.key], newState);\n\tinitiateDiff();\n\tif (typeof callback === FUNCTION_TYPE) callback.bind(this)();\n}\n\nfunction bindMethods(component, methods) {\n\tif (typeof methods === FUNCTION_TYPE) {\n\t\tmethods = methods.bind(component)();\n\t}\n\n\tfor (let func in methods) {\n\t\tcomponent[func] = methods[func].bind(component);\n\t}\n}\n\nfunction generateTreeVN(newC) {\n\tif (typeof newC === UNDEFINED_TYPE) {\n\t\treturn newC;\n\t} else if (typeof newC.e === FUNCTION_TYPE) {\n\t\t// For Components in the application to inject props.\n\t\tconst props = assign({}, newC.a.props);\n\t\tdelete newC.a.props;\n\t\tnewC = newC.e(props);\n\t}\n\n\tif (typeof newC.init === FUNCTION_TYPE) {\n\t\t// For the Root Component And Componets\n\t\t// Initialize The Component\n\t\tconst cObj = new newC.init();\n\t\t// Render Component Markup\n\t\tnewC = cObj.render();\n\n\t\tif (typeof newC === OBJECT_TYPE) {\n\t\t\t// Attach Id and LC Methods To Render Object\n\t\t\tnewC.id = cObj.id;\n\t\t\tnewC.m = cObj.mounted;\n\t\t\tnewC.up = cObj.updated;\n\t\t\tnewC.un = cObj.unmounted;\n\t\t\tnewC.d = cObj.deleteState;\n\t\t}\n\t}\n\n\tif (isArray(newC.c)) {\n\t\tfor (let i = 0, len = newC.c.length; i < len; i++) {\n\t\t\tnewC.c[i] = generateTreeVN(newC.c[i]);\n\t\t}\n\t}\n\n\treturn newC;\n}\n\nfunction createDomNode(data) {\n\tconst type = typeof data;\n\tif (type === STRING_TYPE || type === NUMBER_TYPE) {\n\t\treturn document.createTextNode(data);\n\t} else if (type === BOOLEAN_TYPE || type === UNDEFINED_TYPE) {\n\t\treturn document.createTextNode('');\n\t} else {\n\t\tconst newE = document.createElement(data.e);\n\n\t\tif (typeof data.a === OBJECT_TYPE) {\n\t\t\tconst keys = Object.keys(data.a);\n\t\t\tfor (let i = keys.length; i--; ) {\n\t\t\t\tif (keys[i] === EVENTS_TYPE) {\n\t\t\t\t\tdata.eid = generateUUID();\n\t\t\t\t\taddEvents(data.eid, data.a[keys[i]], newE);\n\t\t\t\t} else if (keys[i] === STYLE_TYPE) {\n\t\t\t\t\taddStyles(data.a[keys[i]], newE);\n\t\t\t\t} else {\n\t\t\t\t\tnewE.setAttribute(keys[i], data.a[keys[i]]);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\tpushLifecycle(data, 'm');\n\n\t\tif (isArray(data.c)) {\n\t\t\tfor (let i = 0, len = data.c.length; i < len; i++) {\n\t\t\t\tappendChild(newE, data.c[i]);\n\t\t\t}\n\t\t}\n\n\t\treturn newE;\n\t}\n}\n\nfunction getEventUUID(element) {\n\tif (element.parentNode.nodeName === '#document') return;\n\tconst eid = element.getAttribute('data-' + EVENTS_KEY);\n\tif (typeof eid === STRING_TYPE) return eid;\n\treturn getEventUUID(element.parentNode);\n}\n\nfunction addEvents(eid, eventObj, targetNode) {\n\t// Generate UUID here for event functions\n\t// Send event functions into a cache for lookup by UUID\n\t// UUID is pulled during Event Delegation and the appropriate\n\t// event is picked by type.\n\ttargetNode.setAttribute('data-' + EVENTS_KEY, eid);\n\tEVENTS_CACHE[eid] = eventObj;\n\n\t// Prevent certain events from being delegated due to them not bubbling.\n\tconst keys = Object.keys(eventObj);\n\tfor (let i = keys.length; i--; ) {\n\t\tif (NON_DELEGATED_EVENTS.includes(keys[i])) {\n\t\t\t// Attach event directly if non-bubbling / non-delegated\n\t\t\ttargetNode.addEventListener(keys[i], EVENTS_CACHE[eid][keys[i]]);\n\t\t} else if (!DELEGATED_EVENTS_ADDED.includes(keys[i])) {\n\t\t\t// Added listener type to keep track of what kinds of events\n\t\t\t// are added to the root node.\n\t\t\tDELEGATED_EVENTS_ADDED.push(keys[i]);\n\t\t\tROOT_NODE.addEventListener(keys[i], function(e) {\n\t\t\t\tconst uuid = getEventUUID(e.target);\n\t\t\t\tif (typeof uuid === STRING_TYPE && EVENTS_CACHE[uuid]) {\n\t\t\t\t\tconst event = EVENTS_CACHE[uuid][e.type];\n\t\t\t\t\tif (typeof event === FUNCTION_TYPE) event(e);\n\t\t\t\t}\n\t\t\t});\n\t\t}\n\t}\n}\n\nfunction addStyles(styles, e) {\n\tif (typeof styles === UNDEFINED_TYPE) return;\n\tconst keys = Object.keys(styles);\n\tfor (let i = keys.length; i--; ) {\n\t\te.style[keys[i]] = styles[keys[i]];\n\t}\n}\n\nfunction initiateDiff(uuid) {\n\t// Generate new and old trees\n\tconst oldTree = assign({}, CURRENT_TREE);\n\tconst newTree = generateTreeVN(SAVED_TREE());\n\t// Cache new tree for future diffs\n\tCURRENT_TREE = newTree;\n\t// Initiate diff of trees...\n\tdiffAndPatch(ROOT_NODE, ROOT_NODE.firstChild, newTree, oldTree);\n\t// Trigger Lifecycle Methods...\n\tshiftLifcycleQueue();\n}\n\nfunction diffAndPatch(parentNode, targetNode, newVN, oldVN) {\n\tconst nType = typeof newVN;\n\tconst oType = typeof oldVN;\n\n\tif (oType === UNDEFINED_TYPE) {\n\t\tappendChild(parentNode, newVN);\n\t} else if (nType === UNDEFINED_TYPE) {\n\t\tunmountNodeAndChildren(oldVN);\n\t\ttargetNode.remove();\n\t} else if (nodesNotEqual(newVN, oldVN, nType, oType)) {\n\t\tunmountNodeAndChildren(oldVN);\n\t\tparentNode.replaceChild(createDomNode(newVN), targetNode);\n\t} else if (nType === OBJECT_TYPE) {\n\t\tupdateNodes(targetNode, newVN, oldVN);\n\n\t\tlet lastChild;\n\t\tfor (let i = oldVN.c.length; i--; ) {\n\t\t\tif (typeof newVN.c[i] !== UNDEFINED_TYPE) break;\n\t\t\tlastChild = targetNode.lastChild;\n\t\t\tif (typeof lastChild === OBJECT_TYPE) {\n\t\t\t\tunmountNodeAndChildren(oldVN.c[i]);\n\t\t\t\tlastChild.remove();\n\t\t\t}\n\t\t}\n\n\t\t// Recurse through children and diff.\n\t\tfor (let i = 0, len = newVN.c.length; i < len; i++) {\n\t\t\tdiffAndPatch(\n\t\t\t\ttargetNode,\n\t\t\t\ttargetNode.childNodes[i],\n\t\t\t\tnewVN.c[i],\n\t\t\t\toldVN.c[i]\n\t\t\t);\n\t\t}\n\t}\n}\n\nfunction appendChild(parentNode, newNode) {\n\tparentNode.appendChild(createDomNode(newNode));\n}\n\nfunction nodesNotEqual(newVN, oldVN, nType, oType) {\n\treturn (\n\t\tnType !== oType ||\n\t\tnewVN.e !== oldVN.e ||\n\t\tnewVN.id !== oldVN.id ||\n\t\t((nType === STRING_TYPE || nType === NUMBER_TYPE) && newVN !== oldVN)\n\t);\n}\n\nfunction updateNodes(targetNode, newVN, oldVN) {\n\tpushLifecycle(newVN, 'up');\n\n\tconst keys = Object.keys(newVN.a);\n\tlet oldVal, newVal;\n\tfor (let i = keys.length; i--; ) {\n\t\tnewVal = newVN.a[keys[i]];\n\t\toldVal = oldVN.a[keys[i]];\n\t\tif (keys[i] === STYLE_TYPE) {\n\t\t\tif (stylesAreEqual(newVal, oldVal)) continue;\n\t\t\ttargetNode.removeAttribute(keys[i]);\n\t\t\taddStyles(newVal, targetNode);\n\t\t} else if (keys[i] === EVENTS_TYPE) {\n\t\t\tremoveIndividuallyBoundEvents(targetNode, oldVN.eid);\n\t\t\tdeleteCachedEvent(oldVN.eid);\n\t\t\tnewVN.eid = generateUUID();\n\t\t\taddEvents(newVN.eid, newVal, targetNode);\n\t\t} else if (keys[i] === VALUE_TYPE && newVal !== oldVal) {\n\t\t\ttargetNode.value = newVal;\n\t\t} else if (typeof oldVal === UNDEFINED_TYPE || newVal !== oldVal) {\n\t\t\ttargetNode.setAttribute(keys[i], newVal);\n\t\t}\n\t}\n}\n\nfunction unmountNodeAndChildren(node) {\n\t// Recursivly unmounts all children.\n\tif (typeof node === UNDEFINED_TYPE) return;\n\tpushLifecycle(node, 'un');\n\tpushLifecycle(node, 'd');\n\tdeleteCachedEvent(node.eid);\n\tif (isArray(node.c)) {\n\t\tfor (let i = node.c.length; i--; ) {\n\t\t\tunmountNodeAndChildren(node.c[i]);\n\t\t}\n\t}\n}\n\nfunction deleteCachedEvent(eid) {\n\tif (typeof eid === STRING_TYPE) delete EVENTS_CACHE[eid];\n}\n\nfunction removeIndividuallyBoundEvents(targetNode, eid) {\n\tconst keys = Object.keys(EVENTS_CACHE[eid]);\n\tfor (let i = keys.length; i--; ) {\n\t\tif (NON_DELEGATED_EVENTS.includes(keys[i])) {\n\t\t\ttargetNode.removeEventListener(keys[i], EVENTS_CACHE[eid][keys[i]]);\n\t\t}\n\t}\n}\n\nfunction stylesAreEqual(newStyle, oldStyle) {\n\tconst newType = typeof newStyle;\n\tconst oldType = typeof oldStyle;\n\n\treturn (\n\t\t(newType === OBJECT_TYPE &&\n\t\t\toldType === OBJECT_TYPE &&\n\t\t\tJSON.stringify(newStyle) === JSON.stringify(oldStyle)) ||\n\t\t(newType === UNDEFINED_TYPE && oldType === UNDEFINED_TYPE)\n\t);\n}\n\nfunction shiftLifcycleQueue() {\n\tlet lc;\n\twhile ((lc = LIFECYCLE_QUEUE.shift())) lc();\n}\n\nfunction pushLifecycle(node, lcName) {\n\tif (typeof node === OBJECT_TYPE && typeof node[lcName] === FUNCTION_TYPE) {\n\t\tLIFECYCLE_QUEUE.push(node[lcName]);\n\t}\n}\n\nmodule.exports = {\n\tcomponent,\n\trender,\n\th\n};\n"],"names":["let","ROOT_NODE","SAVED_TREE","CURRENT_TREE","GLOBAL_STORE","COMPONENT_STATE","EVENTS_KEY","DELEGATED_EVENTS_ADDED","EVENTS_CACHE","LIFECYCLE_QUEUE","NON_DELEGATED_EVENTS","STRING_TYPE","NUMBER_TYPE","BOOLEAN_TYPE","UNDEFINED_TYPE","OBJECT_TYPE","FUNCTION_TYPE","EVENTS_TYPE","STYLE_TYPE","VALUE_TYPE","isArray","val","Array","generateUUID","Math","random","toString","substring","Date","getTime","flatten","out","a","i","len","length","push","assign","merge","b","h","el","attr","e","c","children","render","components","documentNodeID","state","document","getElementById","generateTreeVN","appendChild","shiftLifcycleQueue","component","ref","componentUUID","props","id","init","name","key","this","store","getStore","setStore","newStore","callback","initiateDiff","bind","getState","setState","newState","createState","deleteState","mounted","updated","unmounted","methods","func","bindMethods","newC","cObj","m","up","un","d","createDomNode","data","type","createTextNode","newE","createElement","keys","Object","eid","addEvents","addStyles","setAttribute","pushLifecycle","getEventUUID","element","parentNode","nodeName","getAttribute","eventObj","targetNode","includes","addEventListener","uuid","target","event","styles","style","oldTree","newTree","diffAndPatch","newVN","oldVN","nType","oType","unmountNodeAndChildren","remove","nodesNotEqual","replaceChild","lastChild","oldVal","newVal","oldType","oldStyle","newType","newStyle","JSON","stringify","removeAttribute","removeIndividuallyBoundEvents","deleteCachedEvent","value","updateNodes","childNodes","firstChild","newNode","node","removeEventListener","lc","shift","lcName","module","exports"],"mappings":"AAAAA,IAAIC,EAAWC,EAAYC,EACvBC,EAAe,GACfC,EAAkB,GAChBC,EAAa,MACbC,EAAyB,GACzBC,EAAe,GACfC,EAAkB,GAClBC,EAAuB,CAC5B,QACA,OACA,QACA,QACA,OACA,aACA,aACA,SACA,SACA,UAEKC,EAAc,SACdC,EAAc,SACdC,EAAe,UACfC,EAAiB,YACjBC,EAAc,SACdC,EAAgB,WAChBC,EAAc,SACdC,EAAa,QACbC,EAAa,QAEnB,SAASC,EAAQC,UACTC,MAAMF,QAAQC,GAGtB,SAASE,WAEPC,KAAKC,SACHC,SAAS,IACTC,UAAU,IAAK,IAAIC,MAAOC,UAAUH,SAAS,IAIjD,SAASI,EAAQC,EAAKC,OAChBhC,IAAIiC,EAAI,EAAGC,EAAMF,EAAEG,OAAQF,EAAIC,EAAKD,IACpCb,EAAQY,EAAEC,IAAKH,EAAQC,EAAKC,EAAEC,IAC7BF,EAAIK,KAAKJ,EAAEC,WAEVF,EAGR,SAASM,EAAON,EAAKC,OACfhC,IAAIiC,KAAKD,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