sinuous
Version:
🧬 Small, fast, reactive render engine
2 lines (1 loc) • 3.1 kB
JavaScript
!function(r,n){"object"==typeof exports&&"undefined"!=typeof module?module.exports=n(require("sinuous")):"function"==typeof define&&define.amd?define(["sinuous"],n):(r=r||self).map=n(r.sinuous)}(this,function(r){var n="__g",f="nextSibling";function o(r,f,o){var e=r[n];if(e)for(r=r[f];r&&r[n]!==e;)r=r[f];return o?r:r[f]}function e(r,n,f){for(var o;n!==f;)o=n.nextSibling,r.removeChild(n),n=o}function i(r,n,f,o){for(var e;n!==f;)e=n.nextSibling,r.insertBefore(n,o),n=e}function u(r,n){var f=-1,o=r.length;if(o>0&&r[o-1]<=n)return o-1;for(;o-f>1;){var e=(f+o)/2|0;r[e]>n?o=e:f=e}return f}var t=0;return function(a,v){var c=r.api.subscribe,d=r.api.root,s=r.api.sample,b=r.api.cleanup,p=new Map,l=document.createDocumentFragment(),m=l.appendChild(document.createTextNode("")),g=l.appendChild(document.createTextNode(""));function y(){p.forEach(function(r){return r()}),p.clear()}function k(r){var n=p.get(r);n&&n(),p.delete(r)}function w(r,f,o,e,i){return d(function(u){var a=function(r,f,o,e){var i,u=typeof f;return"string"===u||"number"===u?f=document.createTextNode(f):11===f.nodeType&&(i=f.firstChild)&&i!==f.lastChild&&(i[n]=f.lastChild[n]=e),r.insertBefore(f,o||null),i||f}(r,v(f,o,e),i,++t);return p.set(a,u),a})}return b(c(function(r){r=r||[];var n=a()||[];return s(function(){return function r(n,t,a,v,c,d,s,b){var p=a.length;if(n=c.parentNode,0===p)return v.previousSibling||c.nextSibling?e(n,v.nextSibling,c):(n.textContent="",n.appendChild(v),n.appendChild(c)),s&&s(),[];if(0===t.length){for(var l=0;l<p;l++)d(n,a[l],l,a,c);return a.slice()}var m,g,y=0,k=0,w=!0,S=t.length-1,x=p-1,A=v.nextSibling,M=A,_=c.previousSibling,h=c;r:for(;w;){w=!1;var j=void 0;for(m=t[y],g=a[k];m===g;){if(y++,k++,M=A=o(A,f),S<y||x<k)break r;m=t[y],g=a[k]}for(m=t[S],g=a[x];m===g;){if(S--,x--,_=(h=o(_,"previousSibling",!0)).previousSibling,S<y||x<k)break r;m=t[S],g=a[x]}for(m=t[S],g=a[k];m===g;){w=!0;var q=o(_,"previousSibling",!0);if(j=q.previousSibling,M!==q&&(i(n,q,_.nextSibling,M),_=j),k++,--S<y||x<k)break r;m=t[S],g=a[k]}for(m=t[y],g=a[x];m===g;){if(w=!0,j=o(A,f),A!==h){var z=j.previousSibling;i(n,A,j,h),h=z,A=j}if(x--,S<++y||x<k)break r;m=t[y],g=a[x]}}if(x<k){if(y<=S)for(var B,C;y<=S;)B=(C=o(_,"previousSibling",!0)).previousSibling,e(n,C,_.nextSibling),b&&b(C),_=B,S--;return a.slice()}if(S<y){if(k<=x)for(;k<=x;)d(n,a[k],k,a,h),k++;return a.slice()}for(var D=new Array(x+1-k),E=k;E<=x;E++)D[E]=-1;for(var F=new Map,G=k;G<=x;G++)F.set(a[G],G);for(var H=0,I=[],J=y;J<=S;J++)F.has(t[J])?(D[F.get(t[J])]=J,H++):I.push(J);if(0===H)return r(n,r(n,t,[],v,c,d,s),a,v,c,d);for(var K=function(r,n){for(var f=[],o=[],e=-1,i=new Array(r.length),t=n,a=r.length;t<a;t++){var v=r[t];if(!(v<0)){var c=u(f,v);-1!==c&&(i[t]=o[c]),c===e?(f[++e]=v,o[e]=t):v<f[c+1]&&(f[c+1]=v,o[c+1]=t)}}for(t=o[e];e>=0;t=i[t],e--)f[e]=t;return f}(D,k),L=[],N=A,O=y;O<=S;O++)L[O]=N,N=o(N,f);for(var P=0;P<I.length;P++){var Q=L[I[P]];e(n,Q,o(Q,f)),b&&b(Q)}for(var R,T=K.length-1,U=x;U>=k;U--)K[T]===U?(h=L[D[K[T]]],T--):(-1===D[U]?R=d(n,a[U],U,a,h):i(n,R=L[D[U]],o(R,f),h),h=R);return a.slice()}(l,r,n,m,g,w,y,k)})})),b(y),l}});