UNPKG

hele

Version:
1 lines 7.04 kB
class Reference{constructor(){this.current=void 0}}function isEqual(t,e){if(t===e||t!=t&&e!=e)return!0;if(!(t instanceof Object&&e instanceof Object)||t+""!=e+"")return!1;for(const n in t)if(!(n in e&&isEqual(t[n],e[n])))return!1;for(const n in e)if(!(n in t))return!1;return!0}function _clrChd(t,e=!1){[...t.childNodes].forEach(n=>{e&&_clrChd(n,!0),t.removeChild(n)})}function _flatten(t){const e=[];return t.forEach(t=>{t instanceof Array?t.forEach(t=>{e.push(t)}):e.push(t)}),e}function _copy(t){return t instanceof Object?Object.create(t):t}const t=t=>t instanceof Object&&t+""=="[object Object]",e=new Map,n=new Map([["svg","http://www.w3.org/2000/svg"]]),o="_xmlns";function _toEle(t){if(t instanceof HElement)return t;{const e=typeof t;return"string"===e?new HElement(null,{children:[t]}):"number"===e?new HElement(null,{children:[t.toString()]}):t instanceof Array?t.map(_toEle):null}}function _toNode(t){return t instanceof HElement?t.toNode():t instanceof Array?_flatten(t.map(_toNode)):document.createTextNode("")}class HElement{constructor(t,e){this.type=t,this.parent=void 0,this.node=void 0,this.context=void 0,this.props=t&&"string"!=typeof t&&t.prototype instanceof Component?Object.assign({},t.defaultProps,e):e}toNode(){const{type:t,props:r}=this;let c;if(null===t)c=document.createTextNode(r.children[0]);else if("string"==typeof t){const e=_copy(this.context),i=!r["no-xmlns"]&&(r.xmlns||n.get(t)||e[o]);c=i?document.createElementNS(i,t):document.createElement(t),i&&(e[o]=i),_crtNode(r,c,e)}else{const{element:n,component:o}=_getCom(t,r,_copy(this.context)),i=_toEle(n);if(t===Context&&(this.context=o.state),i){const{context:t}=this;_flatten([i]).forEach(e=>{e&&(e.parent=this,e.context=_copy(t))})}o&&e.set(o,this);try{c=i instanceof Array?_flatten(i.map(_toNode)):_toNode(i),o&&o.onDidMount()}catch(t){if(c=document.createTextNode(""),!o)throw t;o.onCaughtError(t)}}return this.node=c}}function _upCom(t){const n=e.get(t);if(n){const{node:o}=n;if(o){const{parent:r}=n,c=_flatten([o]);c.forEach((o,i)=>{const{parentNode:s}=o;if(_clrChd(o,!0),s)if(0===i){const i=t.toElement();if(i instanceof HElement){i.parent=r,i.context=n.context,e.set(t,i);const a=i.toNode(),l=_flatten([a]),u=document.createDocumentFragment();r&&(r.node instanceof Array?c.forEach((t,e)=>{const n=r.node.indexOf(t);0===e?r.node.splice(n,1,...l):r.node.splice(n,1)}):r.node=a),l.forEach(t=>{u.appendChild(t)}),s.replaceChild(u,o)}else e.delete(t),s.removeChild(o)}else s.removeChild(o)})}}}const r=new Set,c=t=>{requestAnimationFrame(t)},i={tickMethod:c,maxUpdateTime:12,maxClearTime:3,_willTick:!1,_tick(){i._willTick||(i.tickMethod(()=>{i._willTick=!1;const{maxUpdateTime:t,maxClearTime:n}=i;let o=Date.now();r.forEach(e=>{if(Date.now()-o<t){r.delete(e);const{state:t,updateRequestCallbacks:n,_forceUp:o}=e,c=n.slice(0),i=c.length;let s,a=_copy(t);c.forEach(t=>{void 0!==(s=t(a))&&(a=s)}),e.updateRequestCallbacks=n.slice(i);try{if(o||e.shouldUpdate(t,a)){e._forceUp=!1;const n=e.onWillUpdate(t);e.state=a,_upCom(e),e.onDidUpdate(n)}}catch(t){e.onCaughtError(t)}}}),r.size&&i._tick(),o=Date.now();let c=!0;for(;c&&Date.now()-o<n;)c=!1,e.forEach((t,n)=>{const{node:o}=t;if(o){const t=o instanceof Array?o[0]:o;if(!t||!t.parentNode){c=!0;try{n.onWillUnmount(),e.delete(n),n.onDidUnmount()}catch(t){n.onCaughtError(t)}}}})}),i._willTick=!0)},_mark(t){r.add(t),i._tick()}};class Component{constructor(t,e){this.context=e,this.state={},this.refs=new Map,this.updateRequestCallbacks=[],this._forceUp=!1,this.props=t}toElement(){this.refs.forEach(t=>{t.current=void 0});try{const t=this.render(),e=typeof t;return"string"===e?new HElement(null,{children:[t]}):"number"===e?new HElement(null,{children:[t.toString()]}):t}catch(t){return this.onCaughtError(t),null}}onWillMount(){}onDidMount(){}shouldUpdate(t,e){return!isEqual(t,e)}onWillUpdate(t){}onDidUpdate(t){}onWillUnmount(){}onDidUnmount(){}onCaughtError(t){throw t}createRef(t){const{refs:e}=this;if(e.has(t))return e.get(t);{const n=new Reference;return e.set(t,n),n}}requestUpdate(t){return this.updateRequestCallbacks.push(t),i._mark(this),this}update(e){return this.requestUpdate(n=>{if(!t(e)||!t(n))return e;Object.assign(n,e)})}forceUpdate(t){return this._forceUp=!0,arguments.length?this.update(t):i._mark(this),this}}Component.defaultProps={};const s=t=>_flatten(t.children);class Context extends Component{constructor(t,e){super(t,e),this.state=Object.assign({},e,t.value)}render(){return _flatten(this.props.children)}}function render(t,e,n=!0){_clrChd(e,n),i._tick();const{context:o}=e;_flatten([t]).forEach(t=>{if(t instanceof HElement)t.context=o,_flatten([t.toNode()]).forEach(t=>{e.appendChild(t)});else{const n=typeof t;"string"===n?e.appendChild(document.createTextNode(t)):"number"===n&&e.appendChild(document.createTextNode(t.toString()))}})}const a=new Map([["children",(t,e)=>{render(t,e,!1)}],["style",(t,e)=>{if(e instanceof HTMLElement)if(t instanceof Object)for(const n in t)e.style[n]=t[n];else e.style=t}],["ref",(t,e)=>{t.current=e}],["class",(t,e)=>{"setAttribute"in e&&e.setAttribute("class","string"==typeof t?t:t.filter(t=>"string"==typeof t).join(" "))}],["no-xmlns",()=>{}]]),l=new Map([["ref",(t,e)=>{t.current=e}]]),u=new Map([["ref",t=>{t.current=void 0}]]),d=/^on(\w+)$/i,f=/capture/i,p=/nonpassive/i,h=/once/i;function _getEName(t,e,n,o){let r=0;return e&&(r+=7),n&&(r+=7),o&&(r+=4),r>0?t.slice(0,-r):t}function _getEOpt(t,e,n){return!!(e||t||n)&&{capture:t,passive:!e,once:n}}function _crtNode(t,e,n){e.context=n;for(const n in t){const o=t[n],r=a.get(n);if(r)r(o,e);else if(n.match(d)){const t=RegExp.$1,n=f.test(t),r=p.test(t),c=h.test(t),i=_getEName(t,n,r,c);e.addEventListener(i,o,_getEOpt(n,r,c))}else if(!(n in e)&&"setAttribute"in e)e.setAttribute(n,o);else try{e[n]=o}catch(t){e.setAttribute(n,o)}}}function _getCom(t,e,n){const o={element:null,component:null};if(t.prototype instanceof Component){const r=o.component=new t(e,n);for(const t in e){const n=l.get(t);n&&n(e[t],r)}try{r.onWillMount()}catch(t){r.onCaughtError(t)}o.element=r.toElement()}else{for(const t in e){const n=u.get(t);n&&n(e[t],void 0)}const r=t(e,n);o.element=r}return o}function createElement(t,e,...n){return new HElement(t,{...e||{},children:_flatten(n)})}class Portal extends Component{constructor(t,e){super(t,e),t.container?this.container=t.container:(this.container=document.createElement("div"),document.body.appendChild(this.container))}render(){return null}onWillMount(){render(createElement(Context,{value:this.context},this.props.children),this.container,this.props.deepClear)}}function createFactory(t){return function(e,...n){return createElement(t,e,...n)}}export{a as specialNodePropProcessors,l as specialComponentPropProcessors,u as specialFactoryPropProcessors,_getEName,_getEOpt,_crtNode,_getCom,isEqual,_clrChd,_flatten,_copy,t as _isNorObj,_upCom,r as _expired,c as defaultTickMethod,i as Ticker,Component,s as Fragment,Context,e as _eleMap,n as namespaces,o as _nsCtxName,_toEle,_toNode,HElement,Reference,render,Portal,createElement,createFactory};