literaljs 8.1.6 → 8.1.7
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/README.md +9 -0
- package/build/index.js +1 -1
- package/build/index.m.js +1 -1
- package/build/index.modern.mjs +1 -1
- package/build/index.umd.js +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -385,6 +385,15 @@ Pass styles as objects (not strings). String styles cause a `TypeError` because
|
|
|
385
385
|
|
|
386
386
|
## Changelog
|
|
387
387
|
|
|
388
|
+
### [8.1.7] - 2026-06-09
|
|
389
|
+
|
|
390
|
+
**Fixed**: State updates now correctly trigger DOM updates when component instances are returned directly from `render()`.
|
|
391
|
+
|
|
392
|
+
- Fixed memoization in `tree.js` to preserve component instances instead of flattening them into VNodes
|
|
393
|
+
- Added `cloneVNode()` helper to properly clone VNode trees while preserving component instances
|
|
394
|
+
- Component instances cached during memoization are now processed during tree recursion
|
|
395
|
+
- All 475 tests pass, including new regression tests for nested App and router-mounted App patterns
|
|
396
|
+
|
|
388
397
|
### [8.0.4] - 2026-05-19
|
|
389
398
|
|
|
390
399
|
**Fixed**: Sibling components without explicit `key` props now have isolated state.
|
package/build/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
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,t){const n=[{arr:t,i:0}];for(;n.length;){const t=n[n.length-1],s=t.arr,r=s.length;if(t.i>=r)n.pop();else for(let i=t.i;i<r;i++){const o=s[i];if(Array.isArray(o)){t.i=i+1,n.push({arr:o,i:0});break}null!=o&&"boolean"!=typeof o&&e.push(o),i===r-1&&n.pop()}}return e}let n=0;function s(){return"_id_"+ ++n}function r(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}class i{constructor(e,t,n,s,r,i){this.definitionId=e,this.instanceId=t,this.name=n,this.key=`${e}_${t}_${n}`,this.props=s,this._stateManager=i,this._config=r,i.createState(this.key,r.state),this.mounted=r.mounted.bind(this),this.updated=r.updated.bind(this),this.unmounted=r.unmounted.bind(this),this.deleteState=()=>{this._stateManager.deleteState(this.key)},this._renderedVNode=null,this._previousProps=null;const o=r.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.props===this._previousProps||this._previousProps&&r(this.props,this._previousProps))}bindMethods(e){"function"==typeof e&&(e=e.bind(this)());for(let t in e)this[t]=e[t].bind(this)}}let o=0;function c(){o++}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}}getStoreRef(){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]}}getStateRef(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=[]}}const a={class:"className",id:"id",value:"value",checked:"checked",disabled:"disabled",selected:"selected",readOnly:"readOnly",href:"href",src:"src",type:"type",name:"name",title:"title",placeholder:"placeholder"};class u{constructor(e,t,n){this.eventManager=e,this.lifecycleQueue=t,this.diffEngine=n}_s(e){if(!e||"object"!=typeof e||!e.e)return 1;if("string"!=typeof e.e||e.m||e.up||e.un||e.d)return 0;if(e.a&&e.a.events)return 0;if(e.c)for(let t=0;t<e.c.length;t++)if(!this._s(e.c[t]))return 0;return 1}_e(e){return String(e).replace(/[&<>"]/g,e=>"&"===e?"&":"<"===e?"<":">"===e?">":""")}_r(e){if(null==e||"boolean"==typeof e)return"";const t=typeof e;if("string"===t||"number"===t)return this._e(e);if("object"!==t||!e.e||"string"!=typeof e.e)return null;let n="<"+e.e,s=e.a;if(s)for(let e in s){if("key"===e||"events"===e)continue;let t=s[e];if("style"===e){let e="";for(let n in t)e+=n.replace(/[A-Z]/g,e=>"-"+e.toLowerCase())+":"+t[n]+";";n+=' style="'+this._e(e)+'"'}else"checked"===e||"disabled"===e?t&&(n+=" "+e):n+=" "+e+'="'+this._e(t)+'"'}if(~",br,col,hr,img,input,link,meta,area,base,param,source,track,wbr,".indexOf(","+e.e+","))return n+" />";if(n+=">",e.c)for(let t=0;t<e.c.length;t++){let s=this._r(e.c[t]);null!=s&&(n+=s)}return n+"</"+e.e+">"}mount(e,t){if(this._s(t)){const n=this._r(t);if(null!=n)return void(e.innerHTML=n)}const n=this.createElement(t);n&&e.appendChild(n)}createElement(e){const t=typeof e;return"string"===t||"number"===t?document.createTextNode(e):null==e||"boolean"===t?document.createTextNode(""):"object"===t&&e.e?this._createElementNode(e):null}_createElementNode(e){const t=document.createElement(e.e),n=e.a;if("object"==typeof n)for(let s in n){const r=n[s];if("events"===s)e.eid=this.eventManager.bindEvents(t,r);else if("style"===s)this._addStyles(r,t);else{if("key"===s)continue;{const e=a[s];e?t[e]=r:t.setAttribute(s,r)}}}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;let n="";for(let t in e)n+=t.replace(/[A-Z]/g,e=>"-"+e.toLowerCase())+": "+e[t]+";";n&&(t.style.cssText=n)}patch(e,t,n,s){const r=typeof n;if(void 0===s)this._appendChild(e,n);else if("undefined"===r)this._unmountNodeAndChildren(s,t);else if(this.diffEngine.areNodesEqual(n,s)){if("object"===r&&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{const e=s.c?s.c.length:0,r=Array.isArray(n.c)?n.c.length:0;if(0===r){if(s.c)for(let t=0;t<e;t++)this._unmountNodeAndChildren(s.c[t]);t.innerHTML=""}else if(e>r){const i=Array.from(t.childNodes);for(let t=e-1;t>=r;t--){const e=i[t];e&&this._unmountNodeAndChildren(s.c[t],e)}for(let e=0;e<r;e++)this.patch(t,i[e]||null,n.c[e],s.c?s.c[e]:void 0)}else if(Array.isArray(n.c)){const e=Array.from(t.childNodes);for(let r=0,i=n.c.length;r<i;r++)this.patch(t,e[r]||null,n.c[r],s.c?s.c[r]:void 0)}}}}else if(!t||3!==t.nodeType||"string"!==r&&"number"!==r)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){this.lifecycleQueue.updated(t);const s=t.a,r=n.a;if(s===r)return;if(!s||!r)return;let i,o;for(let c in s)if(o=s[c],i=r[c],"style"===c){if(this._stylesAreEqual(o,i))continue;e.removeAttribute(c),this._addStyles(o,e)}else if("events"===c)this.eventManager.unbindEvents(e,n.eid),t.eid=this.eventManager.bindEvents(e,o);else{if("key"===c)continue;{const t=a[c];t?void 0!==i&&o===i||(e[t]=o):void 0!==i&&o===i||e.setAttribute(c,o)}}}_stylesAreEqual(e,t){if(e===t)return!0;if("object"!=typeof e||"object"!=typeof t)return!1;for(let n in e)if(e[n]!==t[n])return!1;for(let n in t)if(!(n in e))return!1;return!0}_unmountNodeAndChildren(e,t){if(null!=e){if(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]);t&&t.remove()}}unmount(e,t){this._unmountNodeAndChildren(t),e&&(e.innerHTML="")}}class l{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)}if(t.hasAttribute&&!t.hasAttribute("data-eid")){t=t.parentNode;continue}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 f={abort:1,blur:1,error:1,focus:1,load:1,mouseenter:1,mouseleave:1,resize:1,scroll:1,unload:1},p="eid";class y{constructor(){this.cache={},this.rootNode=null,this._elementToUUID=new WeakMap,this.eventBus=new l(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-"+p);return"string"==typeof t?t:this.getEventUUID(e.parentNode)}bindEvents(e,t){const n=s();e.setAttribute("data-"+p,n),this.cache[n]=t,this._elementToUUID.set(e,n);const r=Object.keys(t),i=this.cache[n],o=this.rootNode;for(let t=r.length;t--;){const n=r[t];f[n]?e.addEventListener(n,i[n]):this.eventBus.attach(o,n)}return n}unbindEvents(e,t){if(!this.cache[t])return;const n=Object.keys(this.cache[t]);for(let s=n.length;s--;)f[n[s]]&&e.removeEventListener(n[s],this.cache[t][n[s]]);this.deleteCachedEvent(t),this._elementToUUID.delete(e),e.removeAttribute("data-"+p)}addEvents(e,t,n){n.setAttribute("data-"+p,e),this.cache[e]=t,this._elementToUUID.set(n,e);const s=Object.keys(t);for(let t=s.length;t--;)f[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--;)f[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 _{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(){for(let e=0;e<this.queue.length;e++)this.queue[e]();this.queue.length=0}clear(){this.queue.length=0}}function m(t,n){if(null==t)return t;if("function"==typeof t.e){let e;e=t.a&&t.a.props?{...t.a.props}:t.a?{...t.a}:{},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,i=n.getState(s.key);if(s._cachedProps&&r(s._props,s._cachedProps)&&r(i,s._cachedState)&&r(n.store,s._cachedStore))t={...s._cachedRender},Array.isArray(t.c)&&(t.c=t.c.slice());else{const r=s.bind(n);let i;try{i=r.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(s.definitionId||r.definitionId,s.instanceId||r.instanceId),i.withLifecycle({mounted:r.mounted,updated:r.updated,unmounted:r.unmounted,deleteState:r.deleteState}),t=i;else if(i&&"function"==typeof i.bind)t=m(i,n);else if("object"==typeof i&&null!==i){const n=new e(i.e,i.a,i.c);n.withIdentity(s.definitionId||r.definitionId,s.instanceId||r.instanceId),n.withLifecycle({mounted:r.mounted,updated:r.updated,unmounted:r.unmounted,deleteState:r.deleteState}),t=n}else t=i;s._cachedProps=s._props,s._cachedState=n.getState(s.key),s._cachedStore=n.store,s._cachedRender={...t},Array.isArray(t.c)&&(s._cachedRender.c=t.c.slice())}}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 g{areNodesEqual(e,t){const n=typeof e;return n===typeof t&&("object"===n&&e&&t&&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 r=t.length;if(0===r){if(n)for(let e=0;e<n.length;e++)s._unmountNodeAndChildren(n[e]);return e.innerHTML="",0}const i=Array.from(e.childNodes);for(let o=0;o<r;o++)s.patch(e,i[o]||null,t[o],n?n[o]:void 0);return r}_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 r=n?n.length:0,i=t.length;if(0===i){for(let e=0;e<r;e++)s._unmountNodeAndChildren(n[e]);return e.innerHTML="",0}const o=Object.create(null),c=Object.create(null),h=Array.from(e.childNodes),d=new Set;for(let e=0;e<r;e++){const t=n[e].a?n[e].a.key:null,s=h[e];null!=t&&s&&(o[t]=s,c[t]=n[e])}const a=new Array(i),u=new Array(i);for(let e=0;e<i;e++){const n=t[e].a?t[e].a.key:null;null!=n&&o[n]?(a[e]=o[n],u[e]=c[n],d.add(o[n]),delete o[n]):(a[e]=null,u[e]=null)}let l=0;for(const t in o){const n=o[t];n&&(s.patch(e,n,void 0,c[t]),n.remove(),l++)}for(let t=0;t<h.length;t++){const r=h[t];r&&!d.has(r)&&(s.patch(e,r,void 0,n[t]),r.remove(),l++)}const f=new Map;for(let e=0;e<h.length;e++)f.set(h[e],e);const p=new Array(i);for(let e=0;e<i;e++)if(a[e]){const t=f.get(a[e]);p[e]=void 0!==t?t:-1}else p[e]=-1;const y=[],_=[];for(let e=0;e<i;e++)-1!==p[e]&&(y.push(p[e]),_.push(e));const m=this._findLIS(y),g=Object.create(null);for(let e=0;e<m.length;e++)g[_[m[e]]]=!0;let v=!1,b=null,S=null;for(let n=i-1;n>=0;n--)if(g[n])b=a[n],S=null;else if(a[n])e.insertBefore(a[n],b||S),S=a[n],v=!0;else{const r=s.createElement(t[n]);r&&(e.insertBefore(r,b||S),S=r,v=!0)}const N=v||l>0?Array.from(e.childNodes):h;for(let n=0;n<i;n++)u[n]&&s.patch(e,N[n],t[n],u[n]);return i}_findLIS(e){const t=e.length;if(0===t)return[];const n=[],s=new Array(t).fill(-1);for(let r=0;r<t;r++){let t=0,i=n.length;for(;t<i;){const s=t+i>>1;e[n[s]]<e[r]?t=s+1:i=s}t>0&&(s[r]=n[t-1]),t===n.length?n.push(r):n[t]=r}const r=[];let i=n[n.length-1];for(;-1!==i;)r.unshift(i),i=s[i];return r}removeExtraChildren(e,t,n,s){const r=t.c?t.c.length:0;if(!(r<=n))for(let i=r;i-- >n;){const n=e.lastChild;n&&s._unmountNodeAndChildren(t.c[i],n)}}}class v{constructor(e,t,n){this.generateTree=e||m,this.renderer=t,this.lifecycleQueue=n}mount(e,t,n){c();const s=this.generateTree(t(),n);return this.renderer.mount(e,s),this.lifecycleQueue.flush(),s}update(e,t,n,s){c();const r=this.generateTree(t(),n);return this.renderer.patch(e,e.firstChild,r,s),this.lifecycleQueue.flush(),r}unmount(e,t){t&&(this.renderer.unmount(e,t),this.lifecycleQueue.flush())}}class b{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 S{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 _,this.eventManager=new y,this.renderer=new u(this.eventManager,this.lifecycleQueue,new g),this.mountOrchestrator=new v(m,this.renderer,this.lifecycleQueue),this.updateLoop=new b(()=>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._boundSchedule),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=S,exports.EventManager=y,exports.LifecycleQueue=_,exports.Renderer=u,exports.StateManager=d,exports.advanceRenderCycle=c,exports.component=function(e){let t=void 0===e?{}:e,n=t.name,r=void 0===n?"":n,c=t.state,h=void 0===c?{}:c,d=t.mounted,a=void 0===d?function(){}:d,u=t.updated,l=void 0===u?function(){}:u,f=t.unmounted,p=void 0===f?function(){}:f,y=t.methods,_=void 0===y?function(){}:y,m=t.render,g=void 0===m?function(){}:m;const v=s(),b=new Map;let S=0,N=-1;return function(e){void 0===e&&(e={}),N!==o&&(S=0,N=o);const t=null!=e.key?String(e.key):`__auto_${S++}__`;if(!b.has(t)){const n=s(),o=r+(null!=e.key?"_"+String(e.key):"");b.set(t,{definitionId:v,instanceId:n,key:`${v}_${n}_${o}`,_props:null,_cachedRender:null,bind(e){const t=new i(v,n,o,this._props,{state:h,mounted:a,updated:l,unmounted:p,methods:_,render:g},e);return t.bindMethods(_),t}})}const n=b.get(t);return n._props=e,n}},exports.createApp=function(e,t,n){const s=new S(e,n);return s.mount(t),s},exports.flatten=t,exports.generateTree=m,exports.generateUUID=s,exports.h=function(n,s){const r={};for(let e in s)r[e]=s[e];return new e(n,r,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 S(e,n);return s.mount(t),s};
|
|
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,t){const n=[{arr:t,i:0}];for(;n.length;){const t=n[n.length-1],s=t.arr,r=s.length;if(t.i>=r)n.pop();else for(let i=t.i;i<r;i++){const o=s[i];if(Array.isArray(o)){t.i=i+1,n.push({arr:o,i:0});break}null!=o&&"boolean"!=typeof o&&e.push(o),i===r-1&&n.pop()}}return e}let n=0;function s(){return"_id_"+ ++n}function r(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}class i{constructor(e,t,n,s,r,i){this.definitionId=e,this.instanceId=t,this.name=n,this.key=`${e}_${t}_${n}`,this.props=s,this._stateManager=i,this._config=r,i.createState(this.key,r.state),this.mounted=r.mounted.bind(this),this.updated=r.updated.bind(this),this.unmounted=r.unmounted.bind(this),this.deleteState=()=>{this._stateManager.deleteState(this.key)},this._renderedVNode=null,this._previousProps=null;const o=r.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.props===this._previousProps||this._previousProps&&r(this.props,this._previousProps))}bindMethods(e){"function"==typeof e&&(e=e.bind(this)());for(let t in e)this[t]=e[t].bind(this)}}let o=0;function c(){o++}function h(e,t){for(let n in t)e[n]=t[n];return e}class u{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}}getStoreRef(){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]}}getStateRef(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=[]}}const d={class:"className",id:"id",value:"value",checked:"checked",disabled:"disabled",selected:"selected",readOnly:"readOnly",href:"href",src:"src",type:"type",name:"name",title:"title",placeholder:"placeholder"};class a{constructor(e,t,n){this.eventManager=e,this.lifecycleQueue=t,this.diffEngine=n}_s(e){if(!e||"object"!=typeof e||!e.e)return 1;if("string"!=typeof e.e||e.m||e.up||e.un||e.d)return 0;if(e.a&&e.a.events)return 0;if(e.c)for(let t=0;t<e.c.length;t++)if(!this._s(e.c[t]))return 0;return 1}_e(e){return String(e).replace(/[&<>"]/g,e=>"&"===e?"&":"<"===e?"<":">"===e?">":""")}_r(e){if(null==e||"boolean"==typeof e)return"";const t=typeof e;if("string"===t||"number"===t)return this._e(e);if("object"!==t||!e.e||"string"!=typeof e.e)return null;let n="<"+e.e,s=e.a;if(s)for(let e in s){if("key"===e||"events"===e)continue;let t=s[e];if("style"===e){let e="";for(let n in t)e+=n.replace(/[A-Z]/g,e=>"-"+e.toLowerCase())+":"+t[n]+";";n+=' style="'+this._e(e)+'"'}else"checked"===e||"disabled"===e?t&&(n+=" "+e):n+=" "+e+'="'+this._e(t)+'"'}if(~",br,col,hr,img,input,link,meta,area,base,param,source,track,wbr,".indexOf(","+e.e+","))return n+" />";if(n+=">",e.c)for(let t=0;t<e.c.length;t++){let s=this._r(e.c[t]);null!=s&&(n+=s)}return n+"</"+e.e+">"}mount(e,t){if(this._s(t)){const n=this._r(t);if(null!=n)return void(e.innerHTML=n)}const n=this.createElement(t);n&&e.appendChild(n)}createElement(e){const t=typeof e;return"string"===t||"number"===t?document.createTextNode(e):null==e||"boolean"===t?document.createTextNode(""):"object"===t&&e.e?this._createElementNode(e):null}_createElementNode(e){const t=document.createElement(e.e),n=e.a;if("object"==typeof n)for(let s in n){const r=n[s];if("events"===s)e.eid=this.eventManager.bindEvents(t,r);else if("style"===s)this._addStyles(r,t);else{if("key"===s)continue;{const e=d[s];e?t[e]=r:t.setAttribute(s,r)}}}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;let n="";for(let t in e)n+=t.replace(/[A-Z]/g,e=>"-"+e.toLowerCase())+": "+e[t]+";";n&&(t.style.cssText=n)}patch(e,t,n,s){const r=typeof n;if(void 0===s)this._appendChild(e,n);else if("undefined"===r)this._unmountNodeAndChildren(s,t);else if(this.diffEngine.areNodesEqual(n,s)){if("object"===r&&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{const e=s.c?s.c.length:0,r=Array.isArray(n.c)?n.c.length:0;if(0===r){if(s.c)for(let t=0;t<e;t++)this._unmountNodeAndChildren(s.c[t]);t.innerHTML=""}else if(e>r){const i=Array.from(t.childNodes);for(let t=e-1;t>=r;t--){const e=i[t];e&&this._unmountNodeAndChildren(s.c[t],e)}for(let e=0;e<r;e++)this.patch(t,i[e]||null,n.c[e],s.c?s.c[e]:void 0)}else if(Array.isArray(n.c)){const e=Array.from(t.childNodes);for(let r=0,i=n.c.length;r<i;r++)this.patch(t,e[r]||null,n.c[r],s.c?s.c[r]:void 0)}}}}else if(!t||3!==t.nodeType||"string"!==r&&"number"!==r)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){this.lifecycleQueue.updated(t);const s=t.a,r=n.a;if(s===r)return;if(!s||!r)return;let i,o;for(let c in s)if(o=s[c],i=r[c],"style"===c){if(this._stylesAreEqual(o,i))continue;e.removeAttribute(c),this._addStyles(o,e)}else if("events"===c)this.eventManager.unbindEvents(e,n.eid),t.eid=this.eventManager.bindEvents(e,o);else{if("key"===c)continue;{const t=d[c];t?void 0!==i&&o===i||(e[t]=o):void 0!==i&&o===i||e.setAttribute(c,o)}}}_stylesAreEqual(e,t){if(e===t)return!0;if("object"!=typeof e||"object"!=typeof t)return!1;for(let n in e)if(e[n]!==t[n])return!1;for(let n in t)if(!(n in e))return!1;return!0}_unmountNodeAndChildren(e,t){if(null!=e){if(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]);t&&t.remove()}}unmount(e,t){this._unmountNodeAndChildren(t),e&&(e.innerHTML="")}}class l{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)}if(t.hasAttribute&&!t.hasAttribute("data-eid")){t=t.parentNode;continue}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 f={abort:1,blur:1,error:1,focus:1,load:1,mouseenter:1,mouseleave:1,resize:1,scroll:1,unload:1},p="eid";class y{constructor(){this.cache={},this.rootNode=null,this._elementToUUID=new WeakMap,this.eventBus=new l(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-"+p);return"string"==typeof t?t:this.getEventUUID(e.parentNode)}bindEvents(e,t){const n=s();e.setAttribute("data-"+p,n),this.cache[n]=t,this._elementToUUID.set(e,n);const r=Object.keys(t),i=this.cache[n],o=this.rootNode;for(let t=r.length;t--;){const n=r[t];f[n]?e.addEventListener(n,i[n]):this.eventBus.attach(o,n)}return n}unbindEvents(e,t){if(!this.cache[t])return;const n=Object.keys(this.cache[t]);for(let s=n.length;s--;)f[n[s]]&&e.removeEventListener(n[s],this.cache[t][n[s]]);this.deleteCachedEvent(t),this._elementToUUID.delete(e),e.removeAttribute("data-"+p)}addEvents(e,t,n){n.setAttribute("data-"+p,e),this.cache[e]=t,this._elementToUUID.set(n,e);const s=Object.keys(t);for(let t=s.length;t--;)f[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--;)f[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 _{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(){for(let e=0;e<this.queue.length;e++)this.queue[e]();this.queue.length=0}clear(){this.queue.length=0}}function m(e){if(!e||"object"!=typeof e||"function"==typeof e.bind)return e;const t={...e};return Array.isArray(e.c)&&(t.c=e.c.map(e=>m(e))),t}function g(t,n){if(null==t)return t;if("function"==typeof t.e){let e;e=t.a&&t.a.props?{...t.a.props}:t.a?{...t.a}:{},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,i=n.getState(s.key);if(s._cachedProps&&r(s._props,s._cachedProps)&&r(i,s._cachedState)&&r(n.store,s._cachedStore))(t=s._cachedRender)&&"function"==typeof t.bind||(t=m(t));else{const r=s.bind(n);let i;try{i=r.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(s.definitionId||r.definitionId,s.instanceId||r.instanceId),i.withLifecycle({mounted:r.mounted,updated:r.updated,unmounted:r.unmounted,deleteState:r.deleteState}),t=i;else if(i&&"function"==typeof i.bind)t=i;else if("object"==typeof i&&null!==i){const n=new e(i.e,i.a,i.c);n.withIdentity(s.definitionId||r.definitionId,s.instanceId||r.instanceId),n.withLifecycle({mounted:r.mounted,updated:r.updated,unmounted:r.unmounted,deleteState:r.deleteState}),t=n}else t=i;s._cachedProps=s._props,s._cachedState=n.getState(s.key),s._cachedStore=n.store,s._cachedRender=t&&"function"==typeof t.bind?t:m(t)}}if(t&&"function"==typeof t.bind&&(t=g(t,n)),t&&Array.isArray(t.c))for(let e=0,s=t.c.length;e<s;e++)t.c[e]=g(t.c[e],n);return t}class v{areNodesEqual(e,t){const n=typeof e;return n===typeof t&&("object"===n&&e&&t&&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 r=t.length;if(0===r){if(n)for(let e=0;e<n.length;e++)s._unmountNodeAndChildren(n[e]);return e.innerHTML="",0}const i=Array.from(e.childNodes);for(let o=0;o<r;o++)s.patch(e,i[o]||null,t[o],n?n[o]:void 0);return r}_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 r=n?n.length:0,i=t.length;if(0===i){for(let e=0;e<r;e++)s._unmountNodeAndChildren(n[e]);return e.innerHTML="",0}const o=Object.create(null),c=Object.create(null),h=Array.from(e.childNodes),u=new Set;for(let e=0;e<r;e++){const t=n[e].a?n[e].a.key:null,s=h[e];null!=t&&s&&(o[t]=s,c[t]=n[e])}const d=new Array(i),a=new Array(i);for(let e=0;e<i;e++){const n=t[e].a?t[e].a.key:null;null!=n&&o[n]?(d[e]=o[n],a[e]=c[n],u.add(o[n]),delete o[n]):(d[e]=null,a[e]=null)}let l=0;for(const t in o){const n=o[t];n&&(s.patch(e,n,void 0,c[t]),n.remove(),l++)}for(let t=0;t<h.length;t++){const r=h[t];r&&!u.has(r)&&(s.patch(e,r,void 0,n[t]),r.remove(),l++)}const f=new Map;for(let e=0;e<h.length;e++)f.set(h[e],e);const p=new Array(i);for(let e=0;e<i;e++)if(d[e]){const t=f.get(d[e]);p[e]=void 0!==t?t:-1}else p[e]=-1;const y=[],_=[];for(let e=0;e<i;e++)-1!==p[e]&&(y.push(p[e]),_.push(e));const m=this._findLIS(y),g=Object.create(null);for(let e=0;e<m.length;e++)g[_[m[e]]]=!0;let v=!1,b=null,S=null;for(let n=i-1;n>=0;n--)if(g[n])b=d[n],S=null;else if(d[n])e.insertBefore(d[n],b||S),S=d[n],v=!0;else{const r=s.createElement(t[n]);r&&(e.insertBefore(r,b||S),S=r,v=!0)}const N=v||l>0?Array.from(e.childNodes):h;for(let n=0;n<i;n++)a[n]&&s.patch(e,N[n],t[n],a[n]);return i}_findLIS(e){const t=e.length;if(0===t)return[];const n=[],s=new Array(t).fill(-1);for(let r=0;r<t;r++){let t=0,i=n.length;for(;t<i;){const s=t+i>>1;e[n[s]]<e[r]?t=s+1:i=s}t>0&&(s[r]=n[t-1]),t===n.length?n.push(r):n[t]=r}const r=[];let i=n[n.length-1];for(;-1!==i;)r.unshift(i),i=s[i];return r}removeExtraChildren(e,t,n,s){const r=t.c?t.c.length:0;if(!(r<=n))for(let i=r;i-- >n;){const n=e.lastChild;n&&s._unmountNodeAndChildren(t.c[i],n)}}}class b{constructor(e,t,n){this.generateTree=e||g,this.renderer=t,this.lifecycleQueue=n}mount(e,t,n){c();const s=this.generateTree(t(),n);return this.renderer.mount(e,s),this.lifecycleQueue.flush(),s}update(e,t,n,s){c();const r=this.generateTree(t(),n);return this.renderer.patch(e,e.firstChild,r,s),this.lifecycleQueue.flush(),r}unmount(e,t){t&&(this.renderer.unmount(e,t),this.lifecycleQueue.flush())}}class S{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 N{constructor(e,t){void 0===t&&(t={}),this.rootComponent=e,this.savedTree=e,this.currentTree=null,this.rootNode=null,this.stateManager=new u(t),this.lifecycleQueue=new _,this.eventManager=new y,this.renderer=new a(this.eventManager,this.lifecycleQueue,new v),this.mountOrchestrator=new b(g,this.renderer,this.lifecycleQueue),this.updateLoop=new S(()=>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._boundSchedule),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=N,exports.EventManager=y,exports.LifecycleQueue=_,exports.Renderer=a,exports.StateManager=u,exports.advanceRenderCycle=c,exports.component=function(e){let t=void 0===e?{}:e,n=t.name,r=void 0===n?"":n,c=t.state,h=void 0===c?{}:c,u=t.mounted,d=void 0===u?function(){}:u,a=t.updated,l=void 0===a?function(){}:a,f=t.unmounted,p=void 0===f?function(){}:f,y=t.methods,_=void 0===y?function(){}:y,m=t.render,g=void 0===m?function(){}:m;const v=s(),b=new Map;let S=0,N=-1;return function(e){void 0===e&&(e={}),N!==o&&(S=0,N=o);const t=null!=e.key?String(e.key):`__auto_${S++}__`;if(!b.has(t)){const n=s(),o=r+(null!=e.key?"_"+String(e.key):"");b.set(t,{definitionId:v,instanceId:n,key:`${v}_${n}_${o}`,_props:null,_cachedRender:null,bind(e){const t=new i(v,n,o,this._props,{state:h,mounted:d,updated:l,unmounted:p,methods:_,render:g},e);return t.bindMethods(_),t}})}const n=b.get(t);return n._props=e,n}},exports.createApp=function(e,t,n){const s=new N(e,n);return s.mount(t),s},exports.flatten=t,exports.generateTree=g,exports.generateUUID=s,exports.h=function(n,s){const r={};for(let e in s)r[e]=s[e];return new e(n,r,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 N(e,n);return s.mount(t),s};
|
package/build/index.m.js
CHANGED
|
@@ -1 +1 @@
|
|
|
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,t){const n=[{arr:t,i:0}];for(;n.length;){const t=n[n.length-1],s=t.arr,i=s.length;if(t.i>=i)n.pop();else for(let r=t.i;r<i;r++){const o=s[r];if(Array.isArray(o)){t.i=r+1,n.push({arr:o,i:0});break}null!=o&&"boolean"!=typeof o&&e.push(o),r===i-1&&n.pop()}}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}let i=0;function r(){return"_id_"+ ++i}function o(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}class c{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.props===this._previousProps||this._previousProps&&o(this.props,this._previousProps))}bindMethods(e){"function"==typeof e&&(e=e.bind(this)());for(let t in e)this[t]=e[t].bind(this)}}let h=0;function u(){h++}function d(e){let t=void 0===e?{}:e,n=t.name,s=void 0===n?"":n,i=t.state,o=void 0===i?{}:i,u=t.mounted,d=void 0===u?function(){}:u,l=t.updated,a=void 0===l?function(){}:l,f=t.unmounted,p=void 0===f?function(){}:f,y=t.methods,_=void 0===y?function(){}:y,m=t.render,g=void 0===m?function(){}:m;const v=r(),b=new Map;let S=0,N=-1;return function(e){void 0===e&&(e={}),N!==h&&(S=0,N=h);const t=null!=e.key?String(e.key):`__auto_${S++}__`;if(!b.has(t)){const n=r(),i=s+(null!=e.key?"_"+String(e.key):"");b.set(t,{definitionId:v,instanceId:n,key:`${v}_${n}_${i}`,_props:null,_cachedRender:null,bind(e){const t=new c(v,n,i,this._props,{state:o,mounted:d,updated:a,unmounted:p,methods:_,render:g},e);return t.bindMethods(_),t}})}const n=b.get(t);return n._props=e,n}}function l(e,t){for(let n in t)e[n]=t[n];return e}class a{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}}getStoreRef(){return this.store}setStore(e,t){"function"==typeof e&&(e=e(this.store)),this.store=l(l({},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]}}getStateRef(e){return this.componentStates[e]}setState(e,t,n){"function"==typeof t&&(t=t(this.componentStates[e])),this.componentStates[e]=l(l({},this.componentStates[e]),t),this._notify(),"function"==typeof n&&n()}deleteState(e){delete this.componentStates[e]}reset(){this.store={},this.componentStates={},this.listeners=[]}}const f={class:"className",id:"id",value:"value",checked:"checked",disabled:"disabled",selected:"selected",readOnly:"readOnly",href:"href",src:"src",type:"type",name:"name",title:"title",placeholder:"placeholder"};class p{constructor(e,t,n){this.eventManager=e,this.lifecycleQueue=t,this.diffEngine=n}_s(e){if(!e||"object"!=typeof e||!e.e)return 1;if("string"!=typeof e.e||e.m||e.up||e.un||e.d)return 0;if(e.a&&e.a.events)return 0;if(e.c)for(let t=0;t<e.c.length;t++)if(!this._s(e.c[t]))return 0;return 1}_e(e){return String(e).replace(/[&<>"]/g,e=>"&"===e?"&":"<"===e?"<":">"===e?">":""")}_r(e){if(null==e||"boolean"==typeof e)return"";const t=typeof e;if("string"===t||"number"===t)return this._e(e);if("object"!==t||!e.e||"string"!=typeof e.e)return null;let n="<"+e.e,s=e.a;if(s)for(let e in s){if("key"===e||"events"===e)continue;let t=s[e];if("style"===e){let e="";for(let n in t)e+=n.replace(/[A-Z]/g,e=>"-"+e.toLowerCase())+":"+t[n]+";";n+=' style="'+this._e(e)+'"'}else"checked"===e||"disabled"===e?t&&(n+=" "+e):n+=" "+e+'="'+this._e(t)+'"'}if(~",br,col,hr,img,input,link,meta,area,base,param,source,track,wbr,".indexOf(","+e.e+","))return n+" />";if(n+=">",e.c)for(let t=0;t<e.c.length;t++){let s=this._r(e.c[t]);null!=s&&(n+=s)}return n+"</"+e.e+">"}mount(e,t){if(this._s(t)){const n=this._r(t);if(null!=n)return void(e.innerHTML=n)}const n=this.createElement(t);n&&e.appendChild(n)}createElement(e){const t=typeof e;return"string"===t||"number"===t?document.createTextNode(e):null==e||"boolean"===t?document.createTextNode(""):"object"===t&&e.e?this._createElementNode(e):null}_createElementNode(e){const t=document.createElement(e.e),n=e.a;if("object"==typeof n)for(let s in n){const i=n[s];if("events"===s)e.eid=this.eventManager.bindEvents(t,i);else if("style"===s)this._addStyles(i,t);else{if("key"===s)continue;{const e=f[s];e?t[e]=i:t.setAttribute(s,i)}}}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;let n="";for(let t in e)n+=t.replace(/[A-Z]/g,e=>"-"+e.toLowerCase())+": "+e[t]+";";n&&(t.style.cssText=n)}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);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{const e=s.c?s.c.length:0,i=Array.isArray(n.c)?n.c.length:0;if(0===i){if(s.c)for(let t=0;t<e;t++)this._unmountNodeAndChildren(s.c[t]);t.innerHTML=""}else if(e>i){const r=Array.from(t.childNodes);for(let t=e-1;t>=i;t--){const e=r[t];e&&this._unmountNodeAndChildren(s.c[t],e)}for(let e=0;e<i;e++)this.patch(t,r[e]||null,n.c[e],s.c?s.c[e]:void 0)}else if(Array.isArray(n.c)){const e=Array.from(t.childNodes);for(let i=0,r=n.c.length;i<r;i++)this.patch(t,e[i]||null,n.c[i],s.c?s.c[i]: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){this.lifecycleQueue.updated(t);const s=t.a,i=n.a;if(s===i)return;if(!s||!i)return;let r,o;for(let c in s)if(o=s[c],r=i[c],"style"===c){if(this._stylesAreEqual(o,r))continue;e.removeAttribute(c),this._addStyles(o,e)}else if("events"===c)this.eventManager.unbindEvents(e,n.eid),t.eid=this.eventManager.bindEvents(e,o);else{if("key"===c)continue;{const t=f[c];t?void 0!==r&&o===r||(e[t]=o):void 0!==r&&o===r||e.setAttribute(c,o)}}}_stylesAreEqual(e,t){if(e===t)return!0;if("object"!=typeof e||"object"!=typeof t)return!1;for(let n in e)if(e[n]!==t[n])return!1;for(let n in t)if(!(n in e))return!1;return!0}_unmountNodeAndChildren(e,t){if(null!=e){if(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]);t&&t.remove()}}unmount(e,t){this._unmountNodeAndChildren(t),e&&(e.innerHTML="")}}class y{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)}if(t.hasAttribute&&!t.hasAttribute("data-eid")){t=t.parentNode;continue}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 _={abort:1,blur:1,error:1,focus:1,load:1,mouseenter:1,mouseleave:1,resize:1,scroll:1,unload:1},m="eid";class g{constructor(){this.cache={},this.rootNode=null,this._elementToUUID=new WeakMap,this.eventBus=new y(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-"+m);return"string"==typeof t?t:this.getEventUUID(e.parentNode)}bindEvents(e,t){const n=r();e.setAttribute("data-"+m,n),this.cache[n]=t,this._elementToUUID.set(e,n);const s=Object.keys(t),i=this.cache[n],o=this.rootNode;for(let t=s.length;t--;){const n=s[t];_[n]?e.addEventListener(n,i[n]):this.eventBus.attach(o,n)}return n}unbindEvents(e,t){if(!this.cache[t])return;const n=Object.keys(this.cache[t]);for(let s=n.length;s--;)_[n[s]]&&e.removeEventListener(n[s],this.cache[t][n[s]]);this.deleteCachedEvent(t),this._elementToUUID.delete(e),e.removeAttribute("data-"+m)}addEvents(e,t,n){n.setAttribute("data-"+m,e),this.cache[e]=t,this._elementToUUID.set(n,e);const s=Object.keys(t);for(let t=s.length;t--;)_[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--;)_[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 v{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(){for(let e=0;e<this.queue.length;e++)this.queue[e]();this.queue.length=0}clear(){this.queue.length=0}}function b(t,n){if(null==t)return t;if("function"==typeof t.e){let e;e=t.a&&t.a.props?{...t.a.props}:t.a?{...t.a}:{},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,i=n.getState(s.key);if(s._cachedProps&&o(s._props,s._cachedProps)&&o(i,s._cachedState)&&o(n.store,s._cachedStore))t={...s._cachedRender},Array.isArray(t.c)&&(t.c=t.c.slice());else{const i=s.bind(n);let r;try{r=i.render()}catch(e){console.error("Component render error:",e),r={e:"div",a:{class:"error-boundary"},c:[`${e.name}: ${e.message}`]}}if("object"==typeof r&&null!==r&&r instanceof e)r.withIdentity(s.definitionId||i.definitionId,s.instanceId||i.instanceId),r.withLifecycle({mounted:i.mounted,updated:i.updated,unmounted:i.unmounted,deleteState:i.deleteState}),t=r;else if(r&&"function"==typeof r.bind)t=b(r,n);else if("object"==typeof r&&null!==r){const n=new e(r.e,r.a,r.c);n.withIdentity(s.definitionId||i.definitionId,s.instanceId||i.instanceId),n.withLifecycle({mounted:i.mounted,updated:i.updated,unmounted:i.unmounted,deleteState:i.deleteState}),t=n}else t=r;s._cachedProps=s._props,s._cachedState=n.getState(s.key),s._cachedStore=n.store,s._cachedRender={...t},Array.isArray(t.c)&&(s._cachedRender.c=t.c.slice())}}if(t&&Array.isArray(t.c))for(let e=0,s=t.c.length;e<s;e++)t.c[e]=b(t.c[e],n);return t}class S{areNodesEqual(e,t){const n=typeof e;return n===typeof t&&("object"===n&&e&&t&&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=Array.from(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),c=Object.create(null),h=Array.from(e.childNodes),u=new Set;for(let e=0;e<i;e++){const t=n[e].a?n[e].a.key:null,s=h[e];null!=t&&s&&(o[t]=s,c[t]=n[e])}const d=new Array(r),l=new Array(r);for(let e=0;e<r;e++){const n=t[e].a?t[e].a.key:null;null!=n&&o[n]?(d[e]=o[n],l[e]=c[n],u.add(o[n]),delete o[n]):(d[e]=null,l[e]=null)}let a=0;for(const t in o){const n=o[t];n&&(s.patch(e,n,void 0,c[t]),n.remove(),a++)}for(let t=0;t<h.length;t++){const i=h[t];i&&!u.has(i)&&(s.patch(e,i,void 0,n[t]),i.remove(),a++)}const f=new Map;for(let e=0;e<h.length;e++)f.set(h[e],e);const p=new Array(r);for(let e=0;e<r;e++)if(d[e]){const t=f.get(d[e]);p[e]=void 0!==t?t:-1}else p[e]=-1;const y=[],_=[];for(let e=0;e<r;e++)-1!==p[e]&&(y.push(p[e]),_.push(e));const m=this._findLIS(y),g=Object.create(null);for(let e=0;e<m.length;e++)g[_[m[e]]]=!0;let v=!1,b=null,S=null;for(let n=r-1;n>=0;n--)if(g[n])b=d[n],S=null;else if(d[n])e.insertBefore(d[n],b||S),S=d[n],v=!0;else{const i=s.createElement(t[n]);i&&(e.insertBefore(i,b||S),S=i,v=!0)}const N=v||a>0?Array.from(e.childNodes):h;for(let n=0;n<r;n++)l[n]&&s.patch(e,N[n],t[n],l[n]);return r}_findLIS(e){const t=e.length;if(0===t)return[];const n=[],s=new Array(t).fill(-1);for(let i=0;i<t;i++){let t=0,r=n.length;for(;t<r;){const s=t+r>>1;e[n[s]]<e[i]?t=s+1:r=s}t>0&&(s[i]=n[t-1]),t===n.length?n.push(i):n[t]=i}const i=[];let r=n[n.length-1];for(;-1!==r;)i.unshift(r),r=s[r];return i}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)}}}class N{constructor(e,t,n){this.generateTree=e||b,this.renderer=t,this.lifecycleQueue=n}mount(e,t,n){u();const s=this.generateTree(t(),n);return this.renderer.mount(e,s),this.lifecycleQueue.flush(),s}update(e,t,n,s){u();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 A{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 E{constructor(e,t){void 0===t&&(t={}),this.rootComponent=e,this.savedTree=e,this.currentTree=null,this.rootNode=null,this.stateManager=new a(t),this.lifecycleQueue=new v,this.eventManager=new g,this.renderer=new p(this.eventManager,this.lifecycleQueue,new S),this.mountOrchestrator=new N(b,this.renderer,this.lifecycleQueue),this.updateLoop=new A(()=>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._boundSchedule),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 k(e,t,n){const s=new E(e,n);return s.mount(t),s}function w(e,t,n){void 0===n&&(n={});const s=new E(e,n);return s.mount(t),s}export{E as App,g as EventManager,v as LifecycleQueue,p as Renderer,a as StateManager,u as advanceRenderCycle,d as component,k as createApp,t as flatten,b as generateTree,r as generateUUID,n as h,s as isVNode,w as render};
|
|
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,t){const n=[{arr:t,i:0}];for(;n.length;){const t=n[n.length-1],s=t.arr,i=s.length;if(t.i>=i)n.pop();else for(let r=t.i;r<i;r++){const o=s[r];if(Array.isArray(o)){t.i=r+1,n.push({arr:o,i:0});break}null!=o&&"boolean"!=typeof o&&e.push(o),r===i-1&&n.pop()}}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}let i=0;function r(){return"_id_"+ ++i}function o(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}class c{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.props===this._previousProps||this._previousProps&&o(this.props,this._previousProps))}bindMethods(e){"function"==typeof e&&(e=e.bind(this)());for(let t in e)this[t]=e[t].bind(this)}}let h=0;function u(){h++}function d(e){let t=void 0===e?{}:e,n=t.name,s=void 0===n?"":n,i=t.state,o=void 0===i?{}:i,u=t.mounted,d=void 0===u?function(){}:u,l=t.updated,a=void 0===l?function(){}:l,f=t.unmounted,p=void 0===f?function(){}:f,y=t.methods,_=void 0===y?function(){}:y,m=t.render,g=void 0===m?function(){}:m;const v=r(),b=new Map;let S=0,N=-1;return function(e){void 0===e&&(e={}),N!==h&&(S=0,N=h);const t=null!=e.key?String(e.key):`__auto_${S++}__`;if(!b.has(t)){const n=r(),i=s+(null!=e.key?"_"+String(e.key):"");b.set(t,{definitionId:v,instanceId:n,key:`${v}_${n}_${i}`,_props:null,_cachedRender:null,bind(e){const t=new c(v,n,i,this._props,{state:o,mounted:d,updated:a,unmounted:p,methods:_,render:g},e);return t.bindMethods(_),t}})}const n=b.get(t);return n._props=e,n}}function l(e,t){for(let n in t)e[n]=t[n];return e}class a{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}}getStoreRef(){return this.store}setStore(e,t){"function"==typeof e&&(e=e(this.store)),this.store=l(l({},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]}}getStateRef(e){return this.componentStates[e]}setState(e,t,n){"function"==typeof t&&(t=t(this.componentStates[e])),this.componentStates[e]=l(l({},this.componentStates[e]),t),this._notify(),"function"==typeof n&&n()}deleteState(e){delete this.componentStates[e]}reset(){this.store={},this.componentStates={},this.listeners=[]}}const f={class:"className",id:"id",value:"value",checked:"checked",disabled:"disabled",selected:"selected",readOnly:"readOnly",href:"href",src:"src",type:"type",name:"name",title:"title",placeholder:"placeholder"};class p{constructor(e,t,n){this.eventManager=e,this.lifecycleQueue=t,this.diffEngine=n}_s(e){if(!e||"object"!=typeof e||!e.e)return 1;if("string"!=typeof e.e||e.m||e.up||e.un||e.d)return 0;if(e.a&&e.a.events)return 0;if(e.c)for(let t=0;t<e.c.length;t++)if(!this._s(e.c[t]))return 0;return 1}_e(e){return String(e).replace(/[&<>"]/g,e=>"&"===e?"&":"<"===e?"<":">"===e?">":""")}_r(e){if(null==e||"boolean"==typeof e)return"";const t=typeof e;if("string"===t||"number"===t)return this._e(e);if("object"!==t||!e.e||"string"!=typeof e.e)return null;let n="<"+e.e,s=e.a;if(s)for(let e in s){if("key"===e||"events"===e)continue;let t=s[e];if("style"===e){let e="";for(let n in t)e+=n.replace(/[A-Z]/g,e=>"-"+e.toLowerCase())+":"+t[n]+";";n+=' style="'+this._e(e)+'"'}else"checked"===e||"disabled"===e?t&&(n+=" "+e):n+=" "+e+'="'+this._e(t)+'"'}if(~",br,col,hr,img,input,link,meta,area,base,param,source,track,wbr,".indexOf(","+e.e+","))return n+" />";if(n+=">",e.c)for(let t=0;t<e.c.length;t++){let s=this._r(e.c[t]);null!=s&&(n+=s)}return n+"</"+e.e+">"}mount(e,t){if(this._s(t)){const n=this._r(t);if(null!=n)return void(e.innerHTML=n)}const n=this.createElement(t);n&&e.appendChild(n)}createElement(e){const t=typeof e;return"string"===t||"number"===t?document.createTextNode(e):null==e||"boolean"===t?document.createTextNode(""):"object"===t&&e.e?this._createElementNode(e):null}_createElementNode(e){const t=document.createElement(e.e),n=e.a;if("object"==typeof n)for(let s in n){const i=n[s];if("events"===s)e.eid=this.eventManager.bindEvents(t,i);else if("style"===s)this._addStyles(i,t);else{if("key"===s)continue;{const e=f[s];e?t[e]=i:t.setAttribute(s,i)}}}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;let n="";for(let t in e)n+=t.replace(/[A-Z]/g,e=>"-"+e.toLowerCase())+": "+e[t]+";";n&&(t.style.cssText=n)}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);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{const e=s.c?s.c.length:0,i=Array.isArray(n.c)?n.c.length:0;if(0===i){if(s.c)for(let t=0;t<e;t++)this._unmountNodeAndChildren(s.c[t]);t.innerHTML=""}else if(e>i){const r=Array.from(t.childNodes);for(let t=e-1;t>=i;t--){const e=r[t];e&&this._unmountNodeAndChildren(s.c[t],e)}for(let e=0;e<i;e++)this.patch(t,r[e]||null,n.c[e],s.c?s.c[e]:void 0)}else if(Array.isArray(n.c)){const e=Array.from(t.childNodes);for(let i=0,r=n.c.length;i<r;i++)this.patch(t,e[i]||null,n.c[i],s.c?s.c[i]: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){this.lifecycleQueue.updated(t);const s=t.a,i=n.a;if(s===i)return;if(!s||!i)return;let r,o;for(let c in s)if(o=s[c],r=i[c],"style"===c){if(this._stylesAreEqual(o,r))continue;e.removeAttribute(c),this._addStyles(o,e)}else if("events"===c)this.eventManager.unbindEvents(e,n.eid),t.eid=this.eventManager.bindEvents(e,o);else{if("key"===c)continue;{const t=f[c];t?void 0!==r&&o===r||(e[t]=o):void 0!==r&&o===r||e.setAttribute(c,o)}}}_stylesAreEqual(e,t){if(e===t)return!0;if("object"!=typeof e||"object"!=typeof t)return!1;for(let n in e)if(e[n]!==t[n])return!1;for(let n in t)if(!(n in e))return!1;return!0}_unmountNodeAndChildren(e,t){if(null!=e){if(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]);t&&t.remove()}}unmount(e,t){this._unmountNodeAndChildren(t),e&&(e.innerHTML="")}}class y{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)}if(t.hasAttribute&&!t.hasAttribute("data-eid")){t=t.parentNode;continue}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 _={abort:1,blur:1,error:1,focus:1,load:1,mouseenter:1,mouseleave:1,resize:1,scroll:1,unload:1},m="eid";class g{constructor(){this.cache={},this.rootNode=null,this._elementToUUID=new WeakMap,this.eventBus=new y(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-"+m);return"string"==typeof t?t:this.getEventUUID(e.parentNode)}bindEvents(e,t){const n=r();e.setAttribute("data-"+m,n),this.cache[n]=t,this._elementToUUID.set(e,n);const s=Object.keys(t),i=this.cache[n],o=this.rootNode;for(let t=s.length;t--;){const n=s[t];_[n]?e.addEventListener(n,i[n]):this.eventBus.attach(o,n)}return n}unbindEvents(e,t){if(!this.cache[t])return;const n=Object.keys(this.cache[t]);for(let s=n.length;s--;)_[n[s]]&&e.removeEventListener(n[s],this.cache[t][n[s]]);this.deleteCachedEvent(t),this._elementToUUID.delete(e),e.removeAttribute("data-"+m)}addEvents(e,t,n){n.setAttribute("data-"+m,e),this.cache[e]=t,this._elementToUUID.set(n,e);const s=Object.keys(t);for(let t=s.length;t--;)_[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--;)_[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 v{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(){for(let e=0;e<this.queue.length;e++)this.queue[e]();this.queue.length=0}clear(){this.queue.length=0}}function b(e){if(!e||"object"!=typeof e||"function"==typeof e.bind)return e;const t={...e};return Array.isArray(e.c)&&(t.c=e.c.map(e=>b(e))),t}function S(t,n){if(null==t)return t;if("function"==typeof t.e){let e;e=t.a&&t.a.props?{...t.a.props}:t.a?{...t.a}:{},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,i=n.getState(s.key);if(s._cachedProps&&o(s._props,s._cachedProps)&&o(i,s._cachedState)&&o(n.store,s._cachedStore))(t=s._cachedRender)&&"function"==typeof t.bind||(t=b(t));else{const i=s.bind(n);let r;try{r=i.render()}catch(e){console.error("Component render error:",e),r={e:"div",a:{class:"error-boundary"},c:[`${e.name}: ${e.message}`]}}if("object"==typeof r&&null!==r&&r instanceof e)r.withIdentity(s.definitionId||i.definitionId,s.instanceId||i.instanceId),r.withLifecycle({mounted:i.mounted,updated:i.updated,unmounted:i.unmounted,deleteState:i.deleteState}),t=r;else if(r&&"function"==typeof r.bind)t=r;else if("object"==typeof r&&null!==r){const n=new e(r.e,r.a,r.c);n.withIdentity(s.definitionId||i.definitionId,s.instanceId||i.instanceId),n.withLifecycle({mounted:i.mounted,updated:i.updated,unmounted:i.unmounted,deleteState:i.deleteState}),t=n}else t=r;s._cachedProps=s._props,s._cachedState=n.getState(s.key),s._cachedStore=n.store,s._cachedRender=t&&"function"==typeof t.bind?t:b(t)}}if(t&&"function"==typeof t.bind&&(t=S(t,n)),t&&Array.isArray(t.c))for(let e=0,s=t.c.length;e<s;e++)t.c[e]=S(t.c[e],n);return t}class N{areNodesEqual(e,t){const n=typeof e;return n===typeof t&&("object"===n&&e&&t&&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=Array.from(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),c=Object.create(null),h=Array.from(e.childNodes),u=new Set;for(let e=0;e<i;e++){const t=n[e].a?n[e].a.key:null,s=h[e];null!=t&&s&&(o[t]=s,c[t]=n[e])}const d=new Array(r),l=new Array(r);for(let e=0;e<r;e++){const n=t[e].a?t[e].a.key:null;null!=n&&o[n]?(d[e]=o[n],l[e]=c[n],u.add(o[n]),delete o[n]):(d[e]=null,l[e]=null)}let a=0;for(const t in o){const n=o[t];n&&(s.patch(e,n,void 0,c[t]),n.remove(),a++)}for(let t=0;t<h.length;t++){const i=h[t];i&&!u.has(i)&&(s.patch(e,i,void 0,n[t]),i.remove(),a++)}const f=new Map;for(let e=0;e<h.length;e++)f.set(h[e],e);const p=new Array(r);for(let e=0;e<r;e++)if(d[e]){const t=f.get(d[e]);p[e]=void 0!==t?t:-1}else p[e]=-1;const y=[],_=[];for(let e=0;e<r;e++)-1!==p[e]&&(y.push(p[e]),_.push(e));const m=this._findLIS(y),g=Object.create(null);for(let e=0;e<m.length;e++)g[_[m[e]]]=!0;let v=!1,b=null,S=null;for(let n=r-1;n>=0;n--)if(g[n])b=d[n],S=null;else if(d[n])e.insertBefore(d[n],b||S),S=d[n],v=!0;else{const i=s.createElement(t[n]);i&&(e.insertBefore(i,b||S),S=i,v=!0)}const N=v||a>0?Array.from(e.childNodes):h;for(let n=0;n<r;n++)l[n]&&s.patch(e,N[n],t[n],l[n]);return r}_findLIS(e){const t=e.length;if(0===t)return[];const n=[],s=new Array(t).fill(-1);for(let i=0;i<t;i++){let t=0,r=n.length;for(;t<r;){const s=t+r>>1;e[n[s]]<e[i]?t=s+1:r=s}t>0&&(s[i]=n[t-1]),t===n.length?n.push(i):n[t]=i}const i=[];let r=n[n.length-1];for(;-1!==r;)i.unshift(r),r=s[r];return i}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)}}}class A{constructor(e,t,n){this.generateTree=e||S,this.renderer=t,this.lifecycleQueue=n}mount(e,t,n){u();const s=this.generateTree(t(),n);return this.renderer.mount(e,s),this.lifecycleQueue.flush(),s}update(e,t,n,s){u();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 E{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 k{constructor(e,t){void 0===t&&(t={}),this.rootComponent=e,this.savedTree=e,this.currentTree=null,this.rootNode=null,this.stateManager=new a(t),this.lifecycleQueue=new v,this.eventManager=new g,this.renderer=new p(this.eventManager,this.lifecycleQueue,new N),this.mountOrchestrator=new A(S,this.renderer,this.lifecycleQueue),this.updateLoop=new E(()=>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._boundSchedule),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 w(e,t,n){const s=new k(e,n);return s.mount(t),s}function C(e,t,n){void 0===n&&(n={});const s=new k(e,n);return s.mount(t),s}export{k as App,g as EventManager,v as LifecycleQueue,p as Renderer,a as StateManager,u as advanceRenderCycle,d as component,w as createApp,t as flatten,S as generateTree,r as generateUUID,n as h,s as isVNode,C as render};
|
package/build/index.modern.mjs
CHANGED
|
@@ -1 +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,t){const n=[{arr:t,i:0}];for(;n.length;){const t=n[n.length-1],s=t.arr,r=s.length;if(t.i>=r)n.pop();else for(let i=t.i;i<r;i++){const o=s[i];if(Array.isArray(o)){t.i=i+1,n.push({arr:o,i:0});break}null!=o&&"boolean"!=typeof o&&e.push(o),i===r-1&&n.pop()}}return e}function n(n,s,...r){const i={};for(let e in s)i[e]=s[e];return new e(n,i,t([],r))}function s(t){return t instanceof e||null!==t&&"object"==typeof t&&"e"in t}let r=0;function i(){return"_id_"+ ++r}function o(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}class c{constructor(e,t,n,s,r,i){this.definitionId=e,this.instanceId=t,this.name=n,this.key=`${e}_${t}_${n}`,this.props=s,this._stateManager=i,this._config=r,i.createState(this.key,r.state),this.mounted=r.mounted.bind(this),this.updated=r.updated.bind(this),this.unmounted=r.unmounted.bind(this),this.deleteState=()=>{this._stateManager.deleteState(this.key)},this._renderedVNode=null,this._previousProps=null;const o=r.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.props===this._previousProps||this._previousProps&&o(this.props,this._previousProps))}bindMethods(e){"function"==typeof e&&(e=e.bind(this)());for(let t in e)this[t]=e[t].bind(this)}}let h=0;function u(){h++}function l({name:e="",state:t={},mounted:n=function(){},updated:s=function(){},unmounted:r=function(){},methods:o=function(){},render:u=function(){}}={}){const l=i(),a=new Map;let d=0,f=-1;return function(p={}){f!==h&&(d=0,f=h);const y=null!=p.key?String(p.key):`__auto_${d++}__`;if(!a.has(y)){const h=i(),d=e+(null!=p.key?"_"+String(p.key):"");a.set(y,{definitionId:l,instanceId:h,key:`${l}_${h}_${d}`,_props:null,_cachedRender:null,bind(e){const i=new c(l,h,d,this._props,{state:t,mounted:n,updated:s,unmounted:r,methods:o,render:u},e);return i.bindMethods(o),i}})}const _=a.get(y);return _._props=p,_}}function a(){return a=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},a.apply(null,arguments)}function d(e,t){for(let n in t)e[n]=t[n];return e}class f{constructor(e={}){this.store=a({},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 a({},this.store)}getStoreRef(){return this.store}setStore(e,t){"function"==typeof e&&(e=e(this.store)),this.store=d(d({},this.store),e),this._notify(),"function"==typeof t&&t()}createState(e,t){e in this.componentStates||(this.componentStates[e]=a({},t))}getState(e){return a({},this.componentStates[e])}getStateRef(e){return this.componentStates[e]}setState(e,t,n){"function"==typeof t&&(t=t(this.componentStates[e])),this.componentStates[e]=d(d({},this.componentStates[e]),t),this._notify(),"function"==typeof n&&n()}deleteState(e){delete this.componentStates[e]}reset(){this.store={},this.componentStates={},this.listeners=[]}}const p={class:"className",id:"id",value:"value",checked:"checked",disabled:"disabled",selected:"selected",readOnly:"readOnly",href:"href",src:"src",type:"type",name:"name",title:"title",placeholder:"placeholder"};class y{constructor(e,t,n){this.eventManager=e,this.lifecycleQueue=t,this.diffEngine=n}_s(e){if(!e||"object"!=typeof e||!e.e)return 1;if("string"!=typeof e.e||e.m||e.up||e.un||e.d)return 0;if(e.a&&e.a.events)return 0;if(e.c)for(let t=0;t<e.c.length;t++)if(!this._s(e.c[t]))return 0;return 1}_e(e){return String(e).replace(/[&<>"]/g,e=>"&"===e?"&":"<"===e?"<":">"===e?">":""")}_r(e){if(null==e||"boolean"==typeof e)return"";const t=typeof e;if("string"===t||"number"===t)return this._e(e);if("object"!==t||!e.e||"string"!=typeof e.e)return null;let n="<"+e.e,s=e.a;if(s)for(let e in s){if("key"===e||"events"===e)continue;let t=s[e];if("style"===e){let e="";for(let n in t)e+=n.replace(/[A-Z]/g,e=>"-"+e.toLowerCase())+":"+t[n]+";";n+=' style="'+this._e(e)+'"'}else"checked"===e||"disabled"===e?t&&(n+=" "+e):n+=" "+e+'="'+this._e(t)+'"'}if(~",br,col,hr,img,input,link,meta,area,base,param,source,track,wbr,".indexOf(","+e.e+","))return n+" />";if(n+=">",e.c)for(let t=0;t<e.c.length;t++){let s=this._r(e.c[t]);null!=s&&(n+=s)}return n+"</"+e.e+">"}mount(e,t){if(this._s(t)){const n=this._r(t);if(null!=n)return void(e.innerHTML=n)}const n=this.createElement(t);n&&e.appendChild(n)}createElement(e){const t=typeof e;return"string"===t||"number"===t?document.createTextNode(e):null==e||"boolean"===t?document.createTextNode(""):"object"===t&&e.e?this._createElementNode(e):null}_createElementNode(e){const t=document.createElement(e.e),n=e.a;if("object"==typeof n)for(let s in n){const r=n[s];if("events"===s)e.eid=this.eventManager.bindEvents(t,r);else if("style"===s)this._addStyles(r,t);else{if("key"===s)continue;{const e=p[s];e?t[e]=r:t.setAttribute(s,r)}}}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;let n="";for(let t in e)n+=t.replace(/[A-Z]/g,e=>"-"+e.toLowerCase())+": "+e[t]+";";n&&(t.style.cssText=n)}patch(e,t,n,s){const r=typeof n;if(void 0===s)this._appendChild(e,n);else if("undefined"===r)this._unmountNodeAndChildren(s,t);else if(this.diffEngine.areNodesEqual(n,s)){if("object"===r&&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{const e=s.c?s.c.length:0,r=Array.isArray(n.c)?n.c.length:0;if(0===r){if(s.c)for(let t=0;t<e;t++)this._unmountNodeAndChildren(s.c[t]);t.innerHTML=""}else if(e>r){const i=Array.from(t.childNodes);for(let t=e-1;t>=r;t--){const e=i[t];e&&this._unmountNodeAndChildren(s.c[t],e)}for(let e=0;e<r;e++)this.patch(t,i[e]||null,n.c[e],s.c?s.c[e]:void 0)}else if(Array.isArray(n.c)){const e=Array.from(t.childNodes);for(let r=0,i=n.c.length;r<i;r++)this.patch(t,e[r]||null,n.c[r],s.c?s.c[r]:void 0)}}}}else if(!t||3!==t.nodeType||"string"!==r&&"number"!==r)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){this.lifecycleQueue.updated(t);const s=t.a,r=n.a;if(s===r)return;if(!s||!r)return;let i,o;for(let c in s)if(o=s[c],i=r[c],"style"===c){if(this._stylesAreEqual(o,i))continue;e.removeAttribute(c),this._addStyles(o,e)}else if("events"===c)this.eventManager.unbindEvents(e,n.eid),t.eid=this.eventManager.bindEvents(e,o);else{if("key"===c)continue;{const t=p[c];t?void 0!==i&&o===i||(e[t]=o):void 0!==i&&o===i||e.setAttribute(c,o)}}}_stylesAreEqual(e,t){if(e===t)return!0;if("object"!=typeof e||"object"!=typeof t)return!1;for(let n in e)if(e[n]!==t[n])return!1;for(let n in t)if(!(n in e))return!1;return!0}_unmountNodeAndChildren(e,t){if(null!=e){if(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]);t&&t.remove()}}unmount(e,t){this._unmountNodeAndChildren(t),e&&(e.innerHTML="")}}class _{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)}if(t.hasAttribute&&!t.hasAttribute("data-eid")){t=t.parentNode;continue}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 m={abort:1,blur:1,error:1,focus:1,load:1,mouseenter:1,mouseleave:1,resize:1,scroll:1,unload:1},g="eid";class b{constructor(){this.cache={},this.rootNode=null,this._elementToUUID=new WeakMap,this.eventBus=new _(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-"+g);return"string"==typeof t?t:this.getEventUUID(e.parentNode)}bindEvents(e,t){const n=i();e.setAttribute("data-"+g,n),this.cache[n]=t,this._elementToUUID.set(e,n);const s=Object.keys(t),r=this.cache[n],o=this.rootNode;for(let t=s.length;t--;){const n=s[t];m[n]?e.addEventListener(n,r[n]):this.eventBus.attach(o,n)}return n}unbindEvents(e,t){if(!this.cache[t])return;const n=Object.keys(this.cache[t]);for(let s=n.length;s--;)m[n[s]]&&e.removeEventListener(n[s],this.cache[t][n[s]]);this.deleteCachedEvent(t),this._elementToUUID.delete(e),e.removeAttribute("data-"+g)}addEvents(e,t,n){n.setAttribute("data-"+g,e),this.cache[e]=t,this._elementToUUID.set(n,e);const s=Object.keys(t);for(let t=s.length;t--;)m[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--;)m[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 v{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(){for(let e=0;e<this.queue.length;e++)this.queue[e]();this.queue.length=0}clear(){this.queue.length=0}}function S(t,n){if(null==t)return t;if("function"==typeof t.e){let e;e=t.a&&t.a.props?a({},t.a.props):t.a?a({},t.a):{},delete(t={e:t.e,a:a({},t.a),c:t.c}).a.props,t=t.e(e)}if(t&&"function"==typeof t.bind){const s=t,r=n.getState(s.key);if(s._cachedProps&&o(s._props,s._cachedProps)&&o(r,s._cachedState)&&o(n.store,s._cachedStore))t=a({},s._cachedRender),Array.isArray(t.c)&&(t.c=t.c.slice());else{const r=s.bind(n);let i;try{i=r.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(s.definitionId||r.definitionId,s.instanceId||r.instanceId),i.withLifecycle({mounted:r.mounted,updated:r.updated,unmounted:r.unmounted,deleteState:r.deleteState}),t=i;else if(i&&"function"==typeof i.bind)t=S(i,n);else if("object"==typeof i&&null!==i){const n=new e(i.e,i.a,i.c);n.withIdentity(s.definitionId||r.definitionId,s.instanceId||r.instanceId),n.withLifecycle({mounted:r.mounted,updated:r.updated,unmounted:r.unmounted,deleteState:r.deleteState}),t=n}else t=i;s._cachedProps=s._props,s._cachedState=n.getState(s.key),s._cachedStore=n.store,s._cachedRender=a({},t),Array.isArray(t.c)&&(s._cachedRender.c=t.c.slice())}}if(t&&Array.isArray(t.c))for(let e=0,s=t.c.length;e<s;e++)t.c[e]=S(t.c[e],n);return t}class N{areNodesEqual(e,t){const n=typeof e;return n===typeof t&&("object"===n&&e&&t&&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 r=t.length;if(0===r){if(n)for(let e=0;e<n.length;e++)s._unmountNodeAndChildren(n[e]);return e.innerHTML="",0}const i=Array.from(e.childNodes);for(let o=0;o<r;o++)s.patch(e,i[o]||null,t[o],n?n[o]:void 0);return r}_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 r=n?n.length:0,i=t.length;if(0===i){for(let e=0;e<r;e++)s._unmountNodeAndChildren(n[e]);return e.innerHTML="",0}const o=Object.create(null),c=Object.create(null),h=Array.from(e.childNodes),u=new Set;for(let e=0;e<r;e++){const t=n[e].a?n[e].a.key:null,s=h[e];null!=t&&s&&(o[t]=s,c[t]=n[e])}const l=new Array(i),a=new Array(i);for(let e=0;e<i;e++){const n=t[e].a?t[e].a.key:null;null!=n&&o[n]?(l[e]=o[n],a[e]=c[n],u.add(o[n]),delete o[n]):(l[e]=null,a[e]=null)}let d=0;for(const t in o){const n=o[t];n&&(s.patch(e,n,void 0,c[t]),n.remove(),d++)}for(let t=0;t<h.length;t++){const r=h[t];r&&!u.has(r)&&(s.patch(e,r,void 0,n[t]),r.remove(),d++)}const f=new Map;for(let e=0;e<h.length;e++)f.set(h[e],e);const p=new Array(i);for(let e=0;e<i;e++)if(l[e]){const t=f.get(l[e]);p[e]=void 0!==t?t:-1}else p[e]=-1;const y=[],_=[];for(let e=0;e<i;e++)-1!==p[e]&&(y.push(p[e]),_.push(e));const m=this._findLIS(y),g=Object.create(null);for(let e=0;e<m.length;e++)g[_[m[e]]]=!0;let b=!1,v=null,S=null;for(let n=i-1;n>=0;n--)if(g[n])v=l[n],S=null;else if(l[n])e.insertBefore(l[n],v||S),S=l[n],b=!0;else{const r=s.createElement(t[n]);r&&(e.insertBefore(r,v||S),S=r,b=!0)}const N=b||d>0?Array.from(e.childNodes):h;for(let n=0;n<i;n++)a[n]&&s.patch(e,N[n],t[n],a[n]);return i}_findLIS(e){const t=e.length;if(0===t)return[];const n=[],s=new Array(t).fill(-1);for(let r=0;r<t;r++){let t=0,i=n.length;for(;t<i;){const s=t+i>>1;e[n[s]]<e[r]?t=s+1:i=s}t>0&&(s[r]=n[t-1]),t===n.length?n.push(r):n[t]=r}const r=[];let i=n[n.length-1];for(;-1!==i;)r.unshift(i),i=s[i];return r}removeExtraChildren(e,t,n,s){const r=t.c?t.c.length:0;if(!(r<=n))for(let i=r;i-- >n;){const n=e.lastChild;n&&s._unmountNodeAndChildren(t.c[i],n)}}}class A{constructor(e,t,n){this.generateTree=e||S,this.renderer=t,this.lifecycleQueue=n}mount(e,t,n){u();const s=this.generateTree(t(),n);return this.renderer.mount(e,s),this.lifecycleQueue.flush(),s}update(e,t,n,s){u();const r=this.generateTree(t(),n);return this.renderer.patch(e,e.firstChild,r,s),this.lifecycleQueue.flush(),r}unmount(e,t){t&&(this.renderer.unmount(e,t),this.lifecycleQueue.flush())}}class E{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 k{constructor(e,t={}){this.rootComponent=e,this.savedTree=e,this.currentTree=null,this.rootNode=null,this.stateManager=new f(t),this.lifecycleQueue=new v,this.eventManager=new b,this.renderer=new y(this.eventManager,this.lifecycleQueue,new N),this.mountOrchestrator=new A(S,this.renderer,this.lifecycleQueue),this.updateLoop=new E(()=>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._boundSchedule),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 w(e,t,n){const s=new k(e,n);return s.mount(t),s}function C(e,t,n={}){const s=new k(e,n);return s.mount(t),s}export{k as App,b as EventManager,v as LifecycleQueue,y as Renderer,f as StateManager,u as advanceRenderCycle,l as component,w as createApp,t as flatten,S as generateTree,i as generateUUID,n as h,s as isVNode,C as render};
|
|
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,t){const n=[{arr:t,i:0}];for(;n.length;){const t=n[n.length-1],s=t.arr,r=s.length;if(t.i>=r)n.pop();else for(let i=t.i;i<r;i++){const o=s[i];if(Array.isArray(o)){t.i=i+1,n.push({arr:o,i:0});break}null!=o&&"boolean"!=typeof o&&e.push(o),i===r-1&&n.pop()}}return e}function n(n,s,...r){const i={};for(let e in s)i[e]=s[e];return new e(n,i,t([],r))}function s(t){return t instanceof e||null!==t&&"object"==typeof t&&"e"in t}let r=0;function i(){return"_id_"+ ++r}function o(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}class c{constructor(e,t,n,s,r,i){this.definitionId=e,this.instanceId=t,this.name=n,this.key=`${e}_${t}_${n}`,this.props=s,this._stateManager=i,this._config=r,i.createState(this.key,r.state),this.mounted=r.mounted.bind(this),this.updated=r.updated.bind(this),this.unmounted=r.unmounted.bind(this),this.deleteState=()=>{this._stateManager.deleteState(this.key)},this._renderedVNode=null,this._previousProps=null;const o=r.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.props===this._previousProps||this._previousProps&&o(this.props,this._previousProps))}bindMethods(e){"function"==typeof e&&(e=e.bind(this)());for(let t in e)this[t]=e[t].bind(this)}}let h=0;function u(){h++}function l({name:e="",state:t={},mounted:n=function(){},updated:s=function(){},unmounted:r=function(){},methods:o=function(){},render:u=function(){}}={}){const l=i(),a=new Map;let d=0,f=-1;return function(p={}){f!==h&&(d=0,f=h);const y=null!=p.key?String(p.key):`__auto_${d++}__`;if(!a.has(y)){const h=i(),d=e+(null!=p.key?"_"+String(p.key):"");a.set(y,{definitionId:l,instanceId:h,key:`${l}_${h}_${d}`,_props:null,_cachedRender:null,bind(e){const i=new c(l,h,d,this._props,{state:t,mounted:n,updated:s,unmounted:r,methods:o,render:u},e);return i.bindMethods(o),i}})}const _=a.get(y);return _._props=p,_}}function a(){return a=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},a.apply(null,arguments)}function d(e,t){for(let n in t)e[n]=t[n];return e}class f{constructor(e={}){this.store=a({},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 a({},this.store)}getStoreRef(){return this.store}setStore(e,t){"function"==typeof e&&(e=e(this.store)),this.store=d(d({},this.store),e),this._notify(),"function"==typeof t&&t()}createState(e,t){e in this.componentStates||(this.componentStates[e]=a({},t))}getState(e){return a({},this.componentStates[e])}getStateRef(e){return this.componentStates[e]}setState(e,t,n){"function"==typeof t&&(t=t(this.componentStates[e])),this.componentStates[e]=d(d({},this.componentStates[e]),t),this._notify(),"function"==typeof n&&n()}deleteState(e){delete this.componentStates[e]}reset(){this.store={},this.componentStates={},this.listeners=[]}}const p={class:"className",id:"id",value:"value",checked:"checked",disabled:"disabled",selected:"selected",readOnly:"readOnly",href:"href",src:"src",type:"type",name:"name",title:"title",placeholder:"placeholder"};class y{constructor(e,t,n){this.eventManager=e,this.lifecycleQueue=t,this.diffEngine=n}_s(e){if(!e||"object"!=typeof e||!e.e)return 1;if("string"!=typeof e.e||e.m||e.up||e.un||e.d)return 0;if(e.a&&e.a.events)return 0;if(e.c)for(let t=0;t<e.c.length;t++)if(!this._s(e.c[t]))return 0;return 1}_e(e){return String(e).replace(/[&<>"]/g,e=>"&"===e?"&":"<"===e?"<":">"===e?">":""")}_r(e){if(null==e||"boolean"==typeof e)return"";const t=typeof e;if("string"===t||"number"===t)return this._e(e);if("object"!==t||!e.e||"string"!=typeof e.e)return null;let n="<"+e.e,s=e.a;if(s)for(let e in s){if("key"===e||"events"===e)continue;let t=s[e];if("style"===e){let e="";for(let n in t)e+=n.replace(/[A-Z]/g,e=>"-"+e.toLowerCase())+":"+t[n]+";";n+=' style="'+this._e(e)+'"'}else"checked"===e||"disabled"===e?t&&(n+=" "+e):n+=" "+e+'="'+this._e(t)+'"'}if(~",br,col,hr,img,input,link,meta,area,base,param,source,track,wbr,".indexOf(","+e.e+","))return n+" />";if(n+=">",e.c)for(let t=0;t<e.c.length;t++){let s=this._r(e.c[t]);null!=s&&(n+=s)}return n+"</"+e.e+">"}mount(e,t){if(this._s(t)){const n=this._r(t);if(null!=n)return void(e.innerHTML=n)}const n=this.createElement(t);n&&e.appendChild(n)}createElement(e){const t=typeof e;return"string"===t||"number"===t?document.createTextNode(e):null==e||"boolean"===t?document.createTextNode(""):"object"===t&&e.e?this._createElementNode(e):null}_createElementNode(e){const t=document.createElement(e.e),n=e.a;if("object"==typeof n)for(let s in n){const r=n[s];if("events"===s)e.eid=this.eventManager.bindEvents(t,r);else if("style"===s)this._addStyles(r,t);else{if("key"===s)continue;{const e=p[s];e?t[e]=r:t.setAttribute(s,r)}}}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;let n="";for(let t in e)n+=t.replace(/[A-Z]/g,e=>"-"+e.toLowerCase())+": "+e[t]+";";n&&(t.style.cssText=n)}patch(e,t,n,s){const r=typeof n;if(void 0===s)this._appendChild(e,n);else if("undefined"===r)this._unmountNodeAndChildren(s,t);else if(this.diffEngine.areNodesEqual(n,s)){if("object"===r&&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{const e=s.c?s.c.length:0,r=Array.isArray(n.c)?n.c.length:0;if(0===r){if(s.c)for(let t=0;t<e;t++)this._unmountNodeAndChildren(s.c[t]);t.innerHTML=""}else if(e>r){const i=Array.from(t.childNodes);for(let t=e-1;t>=r;t--){const e=i[t];e&&this._unmountNodeAndChildren(s.c[t],e)}for(let e=0;e<r;e++)this.patch(t,i[e]||null,n.c[e],s.c?s.c[e]:void 0)}else if(Array.isArray(n.c)){const e=Array.from(t.childNodes);for(let r=0,i=n.c.length;r<i;r++)this.patch(t,e[r]||null,n.c[r],s.c?s.c[r]:void 0)}}}}else if(!t||3!==t.nodeType||"string"!==r&&"number"!==r)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){this.lifecycleQueue.updated(t);const s=t.a,r=n.a;if(s===r)return;if(!s||!r)return;let i,o;for(let c in s)if(o=s[c],i=r[c],"style"===c){if(this._stylesAreEqual(o,i))continue;e.removeAttribute(c),this._addStyles(o,e)}else if("events"===c)this.eventManager.unbindEvents(e,n.eid),t.eid=this.eventManager.bindEvents(e,o);else{if("key"===c)continue;{const t=p[c];t?void 0!==i&&o===i||(e[t]=o):void 0!==i&&o===i||e.setAttribute(c,o)}}}_stylesAreEqual(e,t){if(e===t)return!0;if("object"!=typeof e||"object"!=typeof t)return!1;for(let n in e)if(e[n]!==t[n])return!1;for(let n in t)if(!(n in e))return!1;return!0}_unmountNodeAndChildren(e,t){if(null!=e){if(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]);t&&t.remove()}}unmount(e,t){this._unmountNodeAndChildren(t),e&&(e.innerHTML="")}}class _{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)}if(t.hasAttribute&&!t.hasAttribute("data-eid")){t=t.parentNode;continue}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 m={abort:1,blur:1,error:1,focus:1,load:1,mouseenter:1,mouseleave:1,resize:1,scroll:1,unload:1},g="eid";class b{constructor(){this.cache={},this.rootNode=null,this._elementToUUID=new WeakMap,this.eventBus=new _(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-"+g);return"string"==typeof t?t:this.getEventUUID(e.parentNode)}bindEvents(e,t){const n=i();e.setAttribute("data-"+g,n),this.cache[n]=t,this._elementToUUID.set(e,n);const s=Object.keys(t),r=this.cache[n],o=this.rootNode;for(let t=s.length;t--;){const n=s[t];m[n]?e.addEventListener(n,r[n]):this.eventBus.attach(o,n)}return n}unbindEvents(e,t){if(!this.cache[t])return;const n=Object.keys(this.cache[t]);for(let s=n.length;s--;)m[n[s]]&&e.removeEventListener(n[s],this.cache[t][n[s]]);this.deleteCachedEvent(t),this._elementToUUID.delete(e),e.removeAttribute("data-"+g)}addEvents(e,t,n){n.setAttribute("data-"+g,e),this.cache[e]=t,this._elementToUUID.set(n,e);const s=Object.keys(t);for(let t=s.length;t--;)m[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--;)m[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 v{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(){for(let e=0;e<this.queue.length;e++)this.queue[e]();this.queue.length=0}clear(){this.queue.length=0}}function S(e){if(!e||"object"!=typeof e||"function"==typeof e.bind)return e;const t=a({},e);return Array.isArray(e.c)&&(t.c=e.c.map(e=>S(e))),t}function N(t,n){if(null==t)return t;if("function"==typeof t.e){let e;e=t.a&&t.a.props?a({},t.a.props):t.a?a({},t.a):{},delete(t={e:t.e,a:a({},t.a),c:t.c}).a.props,t=t.e(e)}if(t&&"function"==typeof t.bind){const s=t,r=n.getState(s.key);if(s._cachedProps&&o(s._props,s._cachedProps)&&o(r,s._cachedState)&&o(n.store,s._cachedStore))(t=s._cachedRender)&&"function"==typeof t.bind||(t=S(t));else{const r=s.bind(n);let i;try{i=r.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(s.definitionId||r.definitionId,s.instanceId||r.instanceId),i.withLifecycle({mounted:r.mounted,updated:r.updated,unmounted:r.unmounted,deleteState:r.deleteState}),t=i;else if(i&&"function"==typeof i.bind)t=i;else if("object"==typeof i&&null!==i){const n=new e(i.e,i.a,i.c);n.withIdentity(s.definitionId||r.definitionId,s.instanceId||r.instanceId),n.withLifecycle({mounted:r.mounted,updated:r.updated,unmounted:r.unmounted,deleteState:r.deleteState}),t=n}else t=i;s._cachedProps=s._props,s._cachedState=n.getState(s.key),s._cachedStore=n.store,s._cachedRender=t&&"function"==typeof t.bind?t:S(t)}}if(t&&"function"==typeof t.bind&&(t=N(t,n)),t&&Array.isArray(t.c))for(let e=0,s=t.c.length;e<s;e++)t.c[e]=N(t.c[e],n);return t}class A{areNodesEqual(e,t){const n=typeof e;return n===typeof t&&("object"===n&&e&&t&&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 r=t.length;if(0===r){if(n)for(let e=0;e<n.length;e++)s._unmountNodeAndChildren(n[e]);return e.innerHTML="",0}const i=Array.from(e.childNodes);for(let o=0;o<r;o++)s.patch(e,i[o]||null,t[o],n?n[o]:void 0);return r}_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 r=n?n.length:0,i=t.length;if(0===i){for(let e=0;e<r;e++)s._unmountNodeAndChildren(n[e]);return e.innerHTML="",0}const o=Object.create(null),c=Object.create(null),h=Array.from(e.childNodes),u=new Set;for(let e=0;e<r;e++){const t=n[e].a?n[e].a.key:null,s=h[e];null!=t&&s&&(o[t]=s,c[t]=n[e])}const l=new Array(i),a=new Array(i);for(let e=0;e<i;e++){const n=t[e].a?t[e].a.key:null;null!=n&&o[n]?(l[e]=o[n],a[e]=c[n],u.add(o[n]),delete o[n]):(l[e]=null,a[e]=null)}let d=0;for(const t in o){const n=o[t];n&&(s.patch(e,n,void 0,c[t]),n.remove(),d++)}for(let t=0;t<h.length;t++){const r=h[t];r&&!u.has(r)&&(s.patch(e,r,void 0,n[t]),r.remove(),d++)}const f=new Map;for(let e=0;e<h.length;e++)f.set(h[e],e);const p=new Array(i);for(let e=0;e<i;e++)if(l[e]){const t=f.get(l[e]);p[e]=void 0!==t?t:-1}else p[e]=-1;const y=[],_=[];for(let e=0;e<i;e++)-1!==p[e]&&(y.push(p[e]),_.push(e));const m=this._findLIS(y),g=Object.create(null);for(let e=0;e<m.length;e++)g[_[m[e]]]=!0;let b=!1,v=null,S=null;for(let n=i-1;n>=0;n--)if(g[n])v=l[n],S=null;else if(l[n])e.insertBefore(l[n],v||S),S=l[n],b=!0;else{const r=s.createElement(t[n]);r&&(e.insertBefore(r,v||S),S=r,b=!0)}const N=b||d>0?Array.from(e.childNodes):h;for(let n=0;n<i;n++)a[n]&&s.patch(e,N[n],t[n],a[n]);return i}_findLIS(e){const t=e.length;if(0===t)return[];const n=[],s=new Array(t).fill(-1);for(let r=0;r<t;r++){let t=0,i=n.length;for(;t<i;){const s=t+i>>1;e[n[s]]<e[r]?t=s+1:i=s}t>0&&(s[r]=n[t-1]),t===n.length?n.push(r):n[t]=r}const r=[];let i=n[n.length-1];for(;-1!==i;)r.unshift(i),i=s[i];return r}removeExtraChildren(e,t,n,s){const r=t.c?t.c.length:0;if(!(r<=n))for(let i=r;i-- >n;){const n=e.lastChild;n&&s._unmountNodeAndChildren(t.c[i],n)}}}class E{constructor(e,t,n){this.generateTree=e||N,this.renderer=t,this.lifecycleQueue=n}mount(e,t,n){u();const s=this.generateTree(t(),n);return this.renderer.mount(e,s),this.lifecycleQueue.flush(),s}update(e,t,n,s){u();const r=this.generateTree(t(),n);return this.renderer.patch(e,e.firstChild,r,s),this.lifecycleQueue.flush(),r}unmount(e,t){t&&(this.renderer.unmount(e,t),this.lifecycleQueue.flush())}}class k{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 w{constructor(e,t={}){this.rootComponent=e,this.savedTree=e,this.currentTree=null,this.rootNode=null,this.stateManager=new f(t),this.lifecycleQueue=new v,this.eventManager=new b,this.renderer=new y(this.eventManager,this.lifecycleQueue,new A),this.mountOrchestrator=new E(N,this.renderer,this.lifecycleQueue),this.updateLoop=new k(()=>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._boundSchedule),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 C(e,t,n){const s=new w(e,n);return s.mount(t),s}function I(e,t,n={}){const s=new w(e,n);return s.mount(t),s}export{w as App,b as EventManager,v as LifecycleQueue,y as Renderer,f as StateManager,u as advanceRenderCycle,l as component,C as createApp,t as flatten,N as generateTree,i as generateUUID,n as h,s as isVNode,I as render};
|
package/build/index.umd.js
CHANGED
|
@@ -1 +1 @@
|
|
|
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){const n=[{arr:t,i:0}];for(;n.length;){const t=n[n.length-1],s=t.arr,i=s.length;if(t.i>=i)n.pop();else for(let r=t.i;r<i;r++){const o=s[r];if(Array.isArray(o)){t.i=r+1,n.push({arr:o,i:0});break}null!=o&&"boolean"!=typeof o&&e.push(o),r===i-1&&n.pop()}}return e}let s=0;function i(){return"_id_"+ ++s}function r(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}class o{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.props===this._previousProps||this._previousProps&&r(this.props,this._previousProps))}bindMethods(e){"function"==typeof e&&(e=e.bind(this)());for(let t in e)this[t]=e[t].bind(this)}}let c=0;function h(){c++}function d(e,t){for(let n in t)e[n]=t[n];return e}class l{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}}getStoreRef(){return this.store}setStore(e,t){"function"==typeof e&&(e=e(this.store)),this.store=d(d({},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]}}getStateRef(e){return this.componentStates[e]}setState(e,t,n){"function"==typeof t&&(t=t(this.componentStates[e])),this.componentStates[e]=d(d({},this.componentStates[e]),t),this._notify(),"function"==typeof n&&n()}deleteState(e){delete this.componentStates[e]}reset(){this.store={},this.componentStates={},this.listeners=[]}}const u={class:"className",id:"id",value:"value",checked:"checked",disabled:"disabled",selected:"selected",readOnly:"readOnly",href:"href",src:"src",type:"type",name:"name",title:"title",placeholder:"placeholder"};class a{constructor(e,t,n){this.eventManager=e,this.lifecycleQueue=t,this.diffEngine=n}_s(e){if(!e||"object"!=typeof e||!e.e)return 1;if("string"!=typeof e.e||e.m||e.up||e.un||e.d)return 0;if(e.a&&e.a.events)return 0;if(e.c)for(let t=0;t<e.c.length;t++)if(!this._s(e.c[t]))return 0;return 1}_e(e){return String(e).replace(/[&<>"]/g,e=>"&"===e?"&":"<"===e?"<":">"===e?">":""")}_r(e){if(null==e||"boolean"==typeof e)return"";const t=typeof e;if("string"===t||"number"===t)return this._e(e);if("object"!==t||!e.e||"string"!=typeof e.e)return null;let n="<"+e.e,s=e.a;if(s)for(let e in s){if("key"===e||"events"===e)continue;let t=s[e];if("style"===e){let e="";for(let n in t)e+=n.replace(/[A-Z]/g,e=>"-"+e.toLowerCase())+":"+t[n]+";";n+=' style="'+this._e(e)+'"'}else"checked"===e||"disabled"===e?t&&(n+=" "+e):n+=" "+e+'="'+this._e(t)+'"'}if(~",br,col,hr,img,input,link,meta,area,base,param,source,track,wbr,".indexOf(","+e.e+","))return n+" />";if(n+=">",e.c)for(let t=0;t<e.c.length;t++){let s=this._r(e.c[t]);null!=s&&(n+=s)}return n+"</"+e.e+">"}mount(e,t){if(this._s(t)){const n=this._r(t);if(null!=n)return void(e.innerHTML=n)}const n=this.createElement(t);n&&e.appendChild(n)}createElement(e){const t=typeof e;return"string"===t||"number"===t?document.createTextNode(e):null==e||"boolean"===t?document.createTextNode(""):"object"===t&&e.e?this._createElementNode(e):null}_createElementNode(e){const t=document.createElement(e.e),n=e.a;if("object"==typeof n)for(let s in n){const i=n[s];if("events"===s)e.eid=this.eventManager.bindEvents(t,i);else if("style"===s)this._addStyles(i,t);else{if("key"===s)continue;{const e=u[s];e?t[e]=i:t.setAttribute(s,i)}}}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;let n="";for(let t in e)n+=t.replace(/[A-Z]/g,e=>"-"+e.toLowerCase())+": "+e[t]+";";n&&(t.style.cssText=n)}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);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{const e=s.c?s.c.length:0,i=Array.isArray(n.c)?n.c.length:0;if(0===i){if(s.c)for(let t=0;t<e;t++)this._unmountNodeAndChildren(s.c[t]);t.innerHTML=""}else if(e>i){const r=Array.from(t.childNodes);for(let t=e-1;t>=i;t--){const e=r[t];e&&this._unmountNodeAndChildren(s.c[t],e)}for(let e=0;e<i;e++)this.patch(t,r[e]||null,n.c[e],s.c?s.c[e]:void 0)}else if(Array.isArray(n.c)){const e=Array.from(t.childNodes);for(let i=0,r=n.c.length;i<r;i++)this.patch(t,e[i]||null,n.c[i],s.c?s.c[i]: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){this.lifecycleQueue.updated(t);const s=t.a,i=n.a;if(s===i)return;if(!s||!i)return;let r,o;for(let c in s)if(o=s[c],r=i[c],"style"===c){if(this._stylesAreEqual(o,r))continue;e.removeAttribute(c),this._addStyles(o,e)}else if("events"===c)this.eventManager.unbindEvents(e,n.eid),t.eid=this.eventManager.bindEvents(e,o);else{if("key"===c)continue;{const t=u[c];t?void 0!==r&&o===r||(e[t]=o):void 0!==r&&o===r||e.setAttribute(c,o)}}}_stylesAreEqual(e,t){if(e===t)return!0;if("object"!=typeof e||"object"!=typeof t)return!1;for(let n in e)if(e[n]!==t[n])return!1;for(let n in t)if(!(n in e))return!1;return!0}_unmountNodeAndChildren(e,t){if(null!=e){if(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]);t&&t.remove()}}unmount(e,t){this._unmountNodeAndChildren(t),e&&(e.innerHTML="")}}class f{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)}if(t.hasAttribute&&!t.hasAttribute("data-eid")){t=t.parentNode;continue}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 p={abort:1,blur:1,error:1,focus:1,load:1,mouseenter:1,mouseleave:1,resize:1,scroll:1,unload:1},y="eid";class _{constructor(){this.cache={},this.rootNode=null,this._elementToUUID=new WeakMap,this.eventBus=new f(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-"+y);return"string"==typeof t?t:this.getEventUUID(e.parentNode)}bindEvents(e,t){const n=i();e.setAttribute("data-"+y,n),this.cache[n]=t,this._elementToUUID.set(e,n);const s=Object.keys(t),r=this.cache[n],o=this.rootNode;for(let t=s.length;t--;){const n=s[t];p[n]?e.addEventListener(n,r[n]):this.eventBus.attach(o,n)}return n}unbindEvents(e,t){if(!this.cache[t])return;const n=Object.keys(this.cache[t]);for(let s=n.length;s--;)p[n[s]]&&e.removeEventListener(n[s],this.cache[t][n[s]]);this.deleteCachedEvent(t),this._elementToUUID.delete(e),e.removeAttribute("data-"+y)}addEvents(e,t,n){n.setAttribute("data-"+y,e),this.cache[e]=t,this._elementToUUID.set(n,e);const s=Object.keys(t);for(let t=s.length;t--;)p[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--;)p[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 m{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(){for(let e=0;e<this.queue.length;e++)this.queue[e]();this.queue.length=0}clear(){this.queue.length=0}}function g(e,n){if(null==e)return e;if("function"==typeof e.e){let t;t=e.a&&e.a.props?{...e.a.props}:e.a?{...e.a}:{},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,i=n.getState(s.key);if(s._cachedProps&&r(s._props,s._cachedProps)&&r(i,s._cachedState)&&r(n.store,s._cachedStore))e={...s._cachedRender},Array.isArray(e.c)&&(e.c=e.c.slice());else{const i=s.bind(n);let r;try{r=i.render()}catch(e){console.error("Component render error:",e),r={e:"div",a:{class:"error-boundary"},c:[`${e.name}: ${e.message}`]}}if("object"==typeof r&&null!==r&&r instanceof t)r.withIdentity(s.definitionId||i.definitionId,s.instanceId||i.instanceId),r.withLifecycle({mounted:i.mounted,updated:i.updated,unmounted:i.unmounted,deleteState:i.deleteState}),e=r;else if(r&&"function"==typeof r.bind)e=g(r,n);else if("object"==typeof r&&null!==r){const n=new t(r.e,r.a,r.c);n.withIdentity(s.definitionId||i.definitionId,s.instanceId||i.instanceId),n.withLifecycle({mounted:i.mounted,updated:i.updated,unmounted:i.unmounted,deleteState:i.deleteState}),e=n}else e=r;s._cachedProps=s._props,s._cachedState=n.getState(s.key),s._cachedStore=n.store,s._cachedRender={...e},Array.isArray(e.c)&&(s._cachedRender.c=e.c.slice())}}if(e&&Array.isArray(e.c))for(let t=0,s=e.c.length;t<s;t++)e.c[t]=g(e.c[t],n);return e}class v{areNodesEqual(e,t){const n=typeof e;return n===typeof t&&("object"===n&&e&&t&&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=Array.from(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),c=Object.create(null),h=Array.from(e.childNodes),d=new Set;for(let e=0;e<i;e++){const t=n[e].a?n[e].a.key:null,s=h[e];null!=t&&s&&(o[t]=s,c[t]=n[e])}const l=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]?(l[e]=o[n],u[e]=c[n],d.add(o[n]),delete o[n]):(l[e]=null,u[e]=null)}let a=0;for(const t in o){const n=o[t];n&&(s.patch(e,n,void 0,c[t]),n.remove(),a++)}for(let t=0;t<h.length;t++){const i=h[t];i&&!d.has(i)&&(s.patch(e,i,void 0,n[t]),i.remove(),a++)}const f=new Map;for(let e=0;e<h.length;e++)f.set(h[e],e);const p=new Array(r);for(let e=0;e<r;e++)if(l[e]){const t=f.get(l[e]);p[e]=void 0!==t?t:-1}else p[e]=-1;const y=[],_=[];for(let e=0;e<r;e++)-1!==p[e]&&(y.push(p[e]),_.push(e));const m=this._findLIS(y),g=Object.create(null);for(let e=0;e<m.length;e++)g[_[m[e]]]=!0;let v=!1,b=null,S=null;for(let n=r-1;n>=0;n--)if(g[n])b=l[n],S=null;else if(l[n])e.insertBefore(l[n],b||S),S=l[n],v=!0;else{const i=s.createElement(t[n]);i&&(e.insertBefore(i,b||S),S=i,v=!0)}const N=v||a>0?Array.from(e.childNodes):h;for(let n=0;n<r;n++)u[n]&&s.patch(e,N[n],t[n],u[n]);return r}_findLIS(e){const t=e.length;if(0===t)return[];const n=[],s=new Array(t).fill(-1);for(let i=0;i<t;i++){let t=0,r=n.length;for(;t<r;){const s=t+r>>1;e[n[s]]<e[i]?t=s+1:r=s}t>0&&(s[i]=n[t-1]),t===n.length?n.push(i):n[t]=i}const i=[];let r=n[n.length-1];for(;-1!==r;)i.unshift(r),r=s[r];return i}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)}}}class b{constructor(e,t,n){this.generateTree=e||g,this.renderer=t,this.lifecycleQueue=n}mount(e,t,n){h();const s=this.generateTree(t(),n);return this.renderer.mount(e,s),this.lifecycleQueue.flush(),s}update(e,t,n,s){h();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 S{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 N{constructor(e,t){void 0===t&&(t={}),this.rootComponent=e,this.savedTree=e,this.currentTree=null,this.rootNode=null,this.stateManager=new l(t),this.lifecycleQueue=new m,this.eventManager=new _,this.renderer=new a(this.eventManager,this.lifecycleQueue,new v),this.mountOrchestrator=new b(g,this.renderer,this.lifecycleQueue),this.updateLoop=new S(()=>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._boundSchedule),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=N,e.EventManager=_,e.LifecycleQueue=m,e.Renderer=a,e.StateManager=l,e.advanceRenderCycle=h,e.component=function(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,l=void 0===d?function(){}:d,u=t.updated,a=void 0===u?function(){}:u,f=t.unmounted,p=void 0===f?function(){}:f,y=t.methods,_=void 0===y?function(){}:y,m=t.render,g=void 0===m?function(){}:m;const v=i(),b=new Map;let S=0,N=-1;return function(e){void 0===e&&(e={}),N!==c&&(S=0,N=c);const t=null!=e.key?String(e.key):`__auto_${S++}__`;if(!b.has(t)){const n=i(),r=s+(null!=e.key?"_"+String(e.key):"");b.set(t,{definitionId:v,instanceId:n,key:`${v}_${n}_${r}`,_props:null,_cachedRender:null,bind(e){const t=new o(v,n,r,this._props,{state:h,mounted:l,updated:a,unmounted:p,methods:_,render:g},e);return t.bindMethods(_),t}})}const n=b.get(t);return n._props=e,n}},e.createApp=function(e,t,n){const s=new N(e,n);return s.mount(t),s},e.flatten=n,e.generateTree=g,e.generateUUID=i,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 N(e,n);return s.mount(t),s}});
|
|
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){const n=[{arr:t,i:0}];for(;n.length;){const t=n[n.length-1],s=t.arr,i=s.length;if(t.i>=i)n.pop();else for(let r=t.i;r<i;r++){const o=s[r];if(Array.isArray(o)){t.i=r+1,n.push({arr:o,i:0});break}null!=o&&"boolean"!=typeof o&&e.push(o),r===i-1&&n.pop()}}return e}let s=0;function i(){return"_id_"+ ++s}function r(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}class o{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.props===this._previousProps||this._previousProps&&r(this.props,this._previousProps))}bindMethods(e){"function"==typeof e&&(e=e.bind(this)());for(let t in e)this[t]=e[t].bind(this)}}let c=0;function h(){c++}function d(e,t){for(let n in t)e[n]=t[n];return e}class u{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}}getStoreRef(){return this.store}setStore(e,t){"function"==typeof e&&(e=e(this.store)),this.store=d(d({},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]}}getStateRef(e){return this.componentStates[e]}setState(e,t,n){"function"==typeof t&&(t=t(this.componentStates[e])),this.componentStates[e]=d(d({},this.componentStates[e]),t),this._notify(),"function"==typeof n&&n()}deleteState(e){delete this.componentStates[e]}reset(){this.store={},this.componentStates={},this.listeners=[]}}const l={class:"className",id:"id",value:"value",checked:"checked",disabled:"disabled",selected:"selected",readOnly:"readOnly",href:"href",src:"src",type:"type",name:"name",title:"title",placeholder:"placeholder"};class a{constructor(e,t,n){this.eventManager=e,this.lifecycleQueue=t,this.diffEngine=n}_s(e){if(!e||"object"!=typeof e||!e.e)return 1;if("string"!=typeof e.e||e.m||e.up||e.un||e.d)return 0;if(e.a&&e.a.events)return 0;if(e.c)for(let t=0;t<e.c.length;t++)if(!this._s(e.c[t]))return 0;return 1}_e(e){return String(e).replace(/[&<>"]/g,e=>"&"===e?"&":"<"===e?"<":">"===e?">":""")}_r(e){if(null==e||"boolean"==typeof e)return"";const t=typeof e;if("string"===t||"number"===t)return this._e(e);if("object"!==t||!e.e||"string"!=typeof e.e)return null;let n="<"+e.e,s=e.a;if(s)for(let e in s){if("key"===e||"events"===e)continue;let t=s[e];if("style"===e){let e="";for(let n in t)e+=n.replace(/[A-Z]/g,e=>"-"+e.toLowerCase())+":"+t[n]+";";n+=' style="'+this._e(e)+'"'}else"checked"===e||"disabled"===e?t&&(n+=" "+e):n+=" "+e+'="'+this._e(t)+'"'}if(~",br,col,hr,img,input,link,meta,area,base,param,source,track,wbr,".indexOf(","+e.e+","))return n+" />";if(n+=">",e.c)for(let t=0;t<e.c.length;t++){let s=this._r(e.c[t]);null!=s&&(n+=s)}return n+"</"+e.e+">"}mount(e,t){if(this._s(t)){const n=this._r(t);if(null!=n)return void(e.innerHTML=n)}const n=this.createElement(t);n&&e.appendChild(n)}createElement(e){const t=typeof e;return"string"===t||"number"===t?document.createTextNode(e):null==e||"boolean"===t?document.createTextNode(""):"object"===t&&e.e?this._createElementNode(e):null}_createElementNode(e){const t=document.createElement(e.e),n=e.a;if("object"==typeof n)for(let s in n){const i=n[s];if("events"===s)e.eid=this.eventManager.bindEvents(t,i);else if("style"===s)this._addStyles(i,t);else{if("key"===s)continue;{const e=l[s];e?t[e]=i:t.setAttribute(s,i)}}}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;let n="";for(let t in e)n+=t.replace(/[A-Z]/g,e=>"-"+e.toLowerCase())+": "+e[t]+";";n&&(t.style.cssText=n)}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);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{const e=s.c?s.c.length:0,i=Array.isArray(n.c)?n.c.length:0;if(0===i){if(s.c)for(let t=0;t<e;t++)this._unmountNodeAndChildren(s.c[t]);t.innerHTML=""}else if(e>i){const r=Array.from(t.childNodes);for(let t=e-1;t>=i;t--){const e=r[t];e&&this._unmountNodeAndChildren(s.c[t],e)}for(let e=0;e<i;e++)this.patch(t,r[e]||null,n.c[e],s.c?s.c[e]:void 0)}else if(Array.isArray(n.c)){const e=Array.from(t.childNodes);for(let i=0,r=n.c.length;i<r;i++)this.patch(t,e[i]||null,n.c[i],s.c?s.c[i]: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){this.lifecycleQueue.updated(t);const s=t.a,i=n.a;if(s===i)return;if(!s||!i)return;let r,o;for(let c in s)if(o=s[c],r=i[c],"style"===c){if(this._stylesAreEqual(o,r))continue;e.removeAttribute(c),this._addStyles(o,e)}else if("events"===c)this.eventManager.unbindEvents(e,n.eid),t.eid=this.eventManager.bindEvents(e,o);else{if("key"===c)continue;{const t=l[c];t?void 0!==r&&o===r||(e[t]=o):void 0!==r&&o===r||e.setAttribute(c,o)}}}_stylesAreEqual(e,t){if(e===t)return!0;if("object"!=typeof e||"object"!=typeof t)return!1;for(let n in e)if(e[n]!==t[n])return!1;for(let n in t)if(!(n in e))return!1;return!0}_unmountNodeAndChildren(e,t){if(null!=e){if(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]);t&&t.remove()}}unmount(e,t){this._unmountNodeAndChildren(t),e&&(e.innerHTML="")}}class f{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)}if(t.hasAttribute&&!t.hasAttribute("data-eid")){t=t.parentNode;continue}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 p={abort:1,blur:1,error:1,focus:1,load:1,mouseenter:1,mouseleave:1,resize:1,scroll:1,unload:1},y="eid";class _{constructor(){this.cache={},this.rootNode=null,this._elementToUUID=new WeakMap,this.eventBus=new f(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-"+y);return"string"==typeof t?t:this.getEventUUID(e.parentNode)}bindEvents(e,t){const n=i();e.setAttribute("data-"+y,n),this.cache[n]=t,this._elementToUUID.set(e,n);const s=Object.keys(t),r=this.cache[n],o=this.rootNode;for(let t=s.length;t--;){const n=s[t];p[n]?e.addEventListener(n,r[n]):this.eventBus.attach(o,n)}return n}unbindEvents(e,t){if(!this.cache[t])return;const n=Object.keys(this.cache[t]);for(let s=n.length;s--;)p[n[s]]&&e.removeEventListener(n[s],this.cache[t][n[s]]);this.deleteCachedEvent(t),this._elementToUUID.delete(e),e.removeAttribute("data-"+y)}addEvents(e,t,n){n.setAttribute("data-"+y,e),this.cache[e]=t,this._elementToUUID.set(n,e);const s=Object.keys(t);for(let t=s.length;t--;)p[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--;)p[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 m{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(){for(let e=0;e<this.queue.length;e++)this.queue[e]();this.queue.length=0}clear(){this.queue.length=0}}function g(e){if(!e||"object"!=typeof e||"function"==typeof e.bind)return e;const t={...e};return Array.isArray(e.c)&&(t.c=e.c.map(e=>g(e))),t}function v(e,n){if(null==e)return e;if("function"==typeof e.e){let t;t=e.a&&e.a.props?{...e.a.props}:e.a?{...e.a}:{},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,i=n.getState(s.key);if(s._cachedProps&&r(s._props,s._cachedProps)&&r(i,s._cachedState)&&r(n.store,s._cachedStore))(e=s._cachedRender)&&"function"==typeof e.bind||(e=g(e));else{const i=s.bind(n);let r;try{r=i.render()}catch(e){console.error("Component render error:",e),r={e:"div",a:{class:"error-boundary"},c:[`${e.name}: ${e.message}`]}}if("object"==typeof r&&null!==r&&r instanceof t)r.withIdentity(s.definitionId||i.definitionId,s.instanceId||i.instanceId),r.withLifecycle({mounted:i.mounted,updated:i.updated,unmounted:i.unmounted,deleteState:i.deleteState}),e=r;else if(r&&"function"==typeof r.bind)e=r;else if("object"==typeof r&&null!==r){const n=new t(r.e,r.a,r.c);n.withIdentity(s.definitionId||i.definitionId,s.instanceId||i.instanceId),n.withLifecycle({mounted:i.mounted,updated:i.updated,unmounted:i.unmounted,deleteState:i.deleteState}),e=n}else e=r;s._cachedProps=s._props,s._cachedState=n.getState(s.key),s._cachedStore=n.store,s._cachedRender=e&&"function"==typeof e.bind?e:g(e)}}if(e&&"function"==typeof e.bind&&(e=v(e,n)),e&&Array.isArray(e.c))for(let t=0,s=e.c.length;t<s;t++)e.c[t]=v(e.c[t],n);return e}class b{areNodesEqual(e,t){const n=typeof e;return n===typeof t&&("object"===n&&e&&t&&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=Array.from(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),c=Object.create(null),h=Array.from(e.childNodes),d=new Set;for(let e=0;e<i;e++){const t=n[e].a?n[e].a.key:null,s=h[e];null!=t&&s&&(o[t]=s,c[t]=n[e])}const u=new Array(r),l=new Array(r);for(let e=0;e<r;e++){const n=t[e].a?t[e].a.key:null;null!=n&&o[n]?(u[e]=o[n],l[e]=c[n],d.add(o[n]),delete o[n]):(u[e]=null,l[e]=null)}let a=0;for(const t in o){const n=o[t];n&&(s.patch(e,n,void 0,c[t]),n.remove(),a++)}for(let t=0;t<h.length;t++){const i=h[t];i&&!d.has(i)&&(s.patch(e,i,void 0,n[t]),i.remove(),a++)}const f=new Map;for(let e=0;e<h.length;e++)f.set(h[e],e);const p=new Array(r);for(let e=0;e<r;e++)if(u[e]){const t=f.get(u[e]);p[e]=void 0!==t?t:-1}else p[e]=-1;const y=[],_=[];for(let e=0;e<r;e++)-1!==p[e]&&(y.push(p[e]),_.push(e));const m=this._findLIS(y),g=Object.create(null);for(let e=0;e<m.length;e++)g[_[m[e]]]=!0;let v=!1,b=null,S=null;for(let n=r-1;n>=0;n--)if(g[n])b=u[n],S=null;else if(u[n])e.insertBefore(u[n],b||S),S=u[n],v=!0;else{const i=s.createElement(t[n]);i&&(e.insertBefore(i,b||S),S=i,v=!0)}const N=v||a>0?Array.from(e.childNodes):h;for(let n=0;n<r;n++)l[n]&&s.patch(e,N[n],t[n],l[n]);return r}_findLIS(e){const t=e.length;if(0===t)return[];const n=[],s=new Array(t).fill(-1);for(let i=0;i<t;i++){let t=0,r=n.length;for(;t<r;){const s=t+r>>1;e[n[s]]<e[i]?t=s+1:r=s}t>0&&(s[i]=n[t-1]),t===n.length?n.push(i):n[t]=i}const i=[];let r=n[n.length-1];for(;-1!==r;)i.unshift(r),r=s[r];return i}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)}}}class S{constructor(e,t,n){this.generateTree=e||v,this.renderer=t,this.lifecycleQueue=n}mount(e,t,n){h();const s=this.generateTree(t(),n);return this.renderer.mount(e,s),this.lifecycleQueue.flush(),s}update(e,t,n,s){h();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 N{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 A{constructor(e,t){void 0===t&&(t={}),this.rootComponent=e,this.savedTree=e,this.currentTree=null,this.rootNode=null,this.stateManager=new u(t),this.lifecycleQueue=new m,this.eventManager=new _,this.renderer=new a(this.eventManager,this.lifecycleQueue,new b),this.mountOrchestrator=new S(v,this.renderer,this.lifecycleQueue),this.updateLoop=new N(()=>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._boundSchedule),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=A,e.EventManager=_,e.LifecycleQueue=m,e.Renderer=a,e.StateManager=u,e.advanceRenderCycle=h,e.component=function(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,u=void 0===d?function(){}:d,l=t.updated,a=void 0===l?function(){}:l,f=t.unmounted,p=void 0===f?function(){}:f,y=t.methods,_=void 0===y?function(){}:y,m=t.render,g=void 0===m?function(){}:m;const v=i(),b=new Map;let S=0,N=-1;return function(e){void 0===e&&(e={}),N!==c&&(S=0,N=c);const t=null!=e.key?String(e.key):`__auto_${S++}__`;if(!b.has(t)){const n=i(),r=s+(null!=e.key?"_"+String(e.key):"");b.set(t,{definitionId:v,instanceId:n,key:`${v}_${n}_${r}`,_props:null,_cachedRender:null,bind(e){const t=new o(v,n,r,this._props,{state:h,mounted:u,updated:a,unmounted:p,methods:_,render:g},e);return t.bindMethods(_),t}})}const n=b.get(t);return n._props=e,n}},e.createApp=function(e,t,n){const s=new A(e,n);return s.mount(t),s},e.flatten=n,e.generateTree=v,e.generateUUID=i,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 A(e,n);return s.mount(t),s}});
|