UNPKG

hooked-elements

Version:
2 lines (1 loc) • 4.87 kB
var hookedElements=function(e){"use strict";var t=Promise;let n=null,s=new Set;const o=e=>{const{$:t,r:n,h:s}=e;u(n)&&(l.get(s).delete(e),n()),u(e.r=t())&&l.get(s).add(e)},r=()=>{const e=s;s=new Set,e.forEach((({h:e,c:t,a:n,e:s})=>{s&&e.apply(t,n)}))},l=new WeakMap,a=[],c=[];function i(e,t){return e!==this[t]}const d=()=>n,u=e=>"function"==typeof e,h=e=>{const t={h:s,c:null,a:null,e:0,i:0,s:[]};return s;function s(){const s=n;n=t,t.e=t.i=0;try{return e.apply(t.c=this,t.a=arguments)}finally{n=s,a.length&&f.then(a.forEach.bind(a.splice(0),o)),c.length&&c.splice(0).forEach(o)}}},f=new t((e=>e()));function p(e){const{_:t,value:n}=this;n!==e&&(this._=new Set,this.value=e,t.forEach((({h:e,c:t,a:n})=>{e.apply(t,n)})))}const g=(e,t)=>{const n=d(),{i:s,s:o}=n;return s!==o.length&&t&&!t.some(i,o[s]._)||(o[s]={$:e(),_:t}),o[n.i++].$},w=e=>(t,n)=>{const s=d(),{i:o,s:r,h:a}=s,c=o===r.length;s.i++,c&&(l.has(a)||l.set(a,new Set),r[o]={$:t,_:n,r:null,h:a}),(c||!n||n.some(i,r[o]._))&&e.push(r[o]),r[o].$=t,r[o]._=n},b=w(a),m=w(c),v=(e,t)=>u(t)?t(e):t,y=(e,t,n)=>{const o=d(),{i:l,s:a}=o;l===a.length&&a.push({$:u(n)?n(t):v(void 0,t),set:t=>{a[l].$=e(a[l].$,t),(e=>{s.has(e)||(e.e=1,s.add(e),f.then(r))})(o)}});const{$:c,set:i}=a[o.i++];return[c,i]},S=!0,E=!1,$="querySelectorAll";function _(e){this.observe(e,{subtree:S,childList:S})}const M="querySelectorAll",{document:k,Element:A,MutationObserver:O,Set:C,WeakMap:q}=self,L=e=>M in e,{filter:N}=[];const{create:x,keys:W}=Object,R=new WeakMap,V=new Set,j=[],D={},F={},P=(e,t)=>{for(let n=R.get(t),s=0,{length:o}=e;s<o;s++){const{target:t,attributeName:o,oldValue:r}=e[s],l=t.getAttribute(o);n.attributeChanged(o,r,l)}},{drop:z,flush:B,parse:G}=(e=>{const t=new q,n=(n,s)=>{let r;if(s)for(let l,a=(e=>e.matches||e.webkitMatchesSelector||e.msMatchesSelector)(n),c=0,{length:i}=o;c<i;c++)a.call(n,l=o[c])&&(t.has(n)||t.set(n,new C),r=t.get(n),r.has(l)||(r.add(l),e.handle(n,s,l)));else t.has(n)&&(r=t.get(n),t.delete(n),r.forEach((t=>{e.handle(n,s,t)})))},s=(e,t=!0)=>{for(let s=0,{length:o}=e;s<o;s++)n(e[s],t)},{query:o}=e,r=e.root||k,l=((e,t,n)=>{const s=(t,n,o,r,l)=>{for(let a=0,{length:c}=t;a<c;a++){const c=t[a];(l||$ in c)&&(r?n.has(c)||(n.add(c),o.delete(c),e(c,r)):o.has(c)||(o.add(c),n.delete(c),e(c,r)),l||s(c[$]("*"),n,o,r,S))}},o=new(n||MutationObserver)((e=>{for(let t=new Set,n=new Set,o=0,{length:r}=e;o<r;o++){const{addedNodes:r,removedNodes:l}=e[o];s(l,t,n,E,E),s(r,t,n,S,E)}}));return o.add=_,o.add(t||document),o})(n,r,O),{attachShadow:a}=A.prototype;return a&&(A.prototype.attachShadow=function(e){const t=a.call(this,e);return l.add(t),t}),o.length&&s(r[M](o)),{drop:e=>{for(let n=0,{length:s}=e;n<s;n++)t.delete(e[n])},flush:()=>{const e=l.takeRecords();for(let t=0,{length:n}=e;t<n;t++)s(N.call(e[t].removedNodes,L),!1),s(N.call(e[t].addedNodes,L),!0)},observer:l,parse:s}})({query:j,handle(e,t,n){const{m:s,l:o,o:r}=D[n],l=s.get(e)||((e,t,n,s)=>{const o=x(s,{element:{enumerable:!0,value:e}});for(let t=0,{length:s}=n;t<s;t++)e.addEventListener(n[t].t,o,n[t].o);t.set(e,o),o.init&&o.init();const{observedAttributes:r}=s;if(r){const t=new MutationObserver(P);t.observe(e,{attributes:!0,attributeOldValue:!0,attributeFilter:r.map((t=>(e.hasAttribute(t)&&o.attributeChanged(t,null,e.getAttribute(t)),t)))}),R.set(t,o)}return o})(e,s,o,r),a=t?"connected":"disconnected";a in l&&l[a]()}}),H=(e,t)=>{if(-1<j.indexOf(e))throw new Error("duplicated: "+e);B();const n=[],s=x(null);for(let e=W(t),o=0,{length:r}=e;o<r;o++){const r=e[o];if(/^on/.test(r)&&!/Options$/.test(r)){const e=t[r+"Options"]||!1,o=r.toLowerCase();let l=o.slice(2);n.push({t:l,o:e}),s[l]=r,o!==r&&(l=r.slice(2,3).toLowerCase()+r.slice(3),s[l]=r,n.push({t:l,o:e}))}}n.length&&(t.handleEvent=function(e){this[s[e.type]](e)}),j.push(e),D[e]={m:new WeakMap,l:n,o:t},G(document.querySelectorAll(e)),I(e),V.has(e)||F[e]._()},I=e=>{if(!(e in F)){let n,s=new t((e=>{n=e}));F[e]={_:n,$:s}}return F[e].$};function J(){Q(this)}const K=(e,t)=>{H(e,"function"==typeof t?{init:J,render:t}:(t.init||(t.init=J),t))},Q=e=>{const{disconnected:t,element:n,render:s}=e,o=h(s.bind(e,n));return e.disconnected=()=>{(e=>l.has(e))(o)&&(e=>{const t=l.get(e);t&&f.then((()=>{t.forEach((e=>{e.r(),e.r=null})),t.clear()}))})(o),t&&t.call(e)},(e.render=o)()};return e.createContext=e=>({_:new Set,provide:p,value:e}),e.define=K,e.defineAsync=(e,t)=>{((e,t,n)=>{V.add(e),H(e,{init(){V.has(e)&&(V.delete(e),t().then((({default:t})=>{j.splice(j.indexOf(e),1),z(document.querySelectorAll(e)),(n||H)(e,t)})))}})})(e,t,K)},e.get=e=>(D[e]||R).o,e.render=Q,e.upgrade=e=>{j.length&&(B(),G([e]))},e.useCallback=(e,t)=>g((()=>e),t),e.useContext=({_:e,value:t})=>(e.add(d()),t),e.useEffect=b,e.useLayoutEffect=m,e.useMemo=g,e.useReducer=y,e.useRef=e=>{const t=d(),{i:n,s:s}=t;return n===s.length&&s.push({current:e}),s[t.i++]},e.useState=e=>y(v,e),e.whenDefined=I,e}({});