choo-shortcache
Version:
choo nanocomponent cache shortcut
1 lines • 16.1 kB
JavaScript
!function(e){if("object"==typeof exports&&"undefined"!=typeof module)module.exports=e();else if("function"==typeof define&&define.amd)define([],e);else{("undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof self?self:this).Choo=e()}}(function(){var e={};function t(e){if(!(this instanceof t))return new t(e);"number"==typeof e&&(e={max:e}),e||(e={}),this.cache={},this.head=this.tail=null,this.length=0,this.max=e.max||1e3,this.maxAge=e.maxAge||0}e=t,Object.defineProperty(t.prototype,"keys",{get:function(){return Object.keys(this.cache)}}),t.prototype.clear=function(){this.cache={},this.head=this.tail=null,this.length=0},t.prototype.remove=function(e){if("string"!=typeof e&&(e=""+e),this.cache.hasOwnProperty(e)){var t=this.cache[e];return delete this.cache[e],this._unlink(e,t.prev,t.next),t.value}},t.prototype._unlink=function(e,t,n){this.length--,0===this.length?this.head=this.tail=null:this.head===e?(this.head=t,this.cache[this.head].next=null):this.tail===e?(this.tail=n,this.cache[this.tail].prev=null):(this.cache[t].next=n,this.cache[n].prev=t)},t.prototype.peek=function(e){if(this.cache.hasOwnProperty(e)){var t=this.cache[e];if(this._checkAge(e,t))return t.value}},t.prototype.set=function(e,t){var n;if("string"!=typeof e&&(e=""+e),this.cache.hasOwnProperty(e)){if((n=this.cache[e]).value=t,this.maxAge&&(n.modified=Date.now()),e===this.head)return t;this._unlink(e,n.prev,n.next)}else n={value:t,modified:0,next:null,prev:null},this.maxAge&&(n.modified=Date.now()),this.cache[e]=n,this.length===this.max&&this.evict();return this.length++,n.next=null,n.prev=this.head,this.head&&(this.cache[this.head].next=e),this.head=e,this.tail||(this.tail=e),t},t.prototype._checkAge=function(e,t){return!(this.maxAge&&Date.now()-t.modified>this.maxAge&&(this.remove(e),1))},t.prototype.get=function(e){if("string"!=typeof e&&(e=""+e),this.cache.hasOwnProperty(e)){var t=this.cache[e];if(this._checkAge(e,t))return this.head!==e&&(e===this.tail?(this.tail=t.next,this.cache[this.tail].prev=null):this.cache[t.prev].next=t.next,this.cache[t.next].prev=t.prev,this.cache[this.head].next=e,t.prev=this.head,t.next=null,this.head=e),t.value}},t.prototype.evict=function(){this.tail&&this.remove(this.tail)};var n={};function i(t,n,i){this.cache="number"==typeof i?new e(i):i||new e(100),this.state=t,this.emit=n}function r(e){return new(e.bind.apply(e,arguments))}n=i,i.prototype.render=function(e,t){var n=this.cache.get(t);if(!n){for(var i=[],o=2,s=arguments.length;o<s;o++)i.push(arguments[o]);i.unshift(e,t,this.state,this.emit),n=r.apply(r,i),this.cache.set(t,n)}return n};var o=function(e){var t=document.readyState;if("complete"===t||"interactive"===t)return setTimeout(e,0);document.addEventListener("DOMContentLoaded",function(){e()})},s="undefined"!=typeof window;function a(e){this.hasWindow=e,this.hasIdle=this.hasWindow&&window.requestIdleCallback,this.method=this.hasIdle?window.requestIdleCallback.bind(window):this.setTimeout,this.scheduled=!1,this.queue=[]}a.prototype.push=function(e){this.hasWindow&&(this.queue.push(e),this.schedule())},a.prototype.schedule=function(){if(!this.scheduled){this.scheduled=!0;var e=this;this.method(function(t){for(;e.queue.length&&t.timeRemaining()>0;)e.queue.shift()(t);e.scheduled=!1,e.queue.length&&e.schedule()})}},a.prototype.setTimeout=function(e){setTimeout(e,0,{timeRemaining:function(){return 1}})};var h,u=function(){var e;return s?(window._nanoScheduler||(window._nanoScheduler=new a(!0)),e=window._nanoScheduler):e=new a,e}();c.disabled=!0;try{h=window.performance,c.disabled="true"===window.localStorage.DISABLE_NANOTIMING||!h.mark}catch(e){}function c(e){if(c.disabled)return l;var t=(1e4*h.now()).toFixed()%Number.MAX_SAFE_INTEGER,n="start-"+t+"-"+e;function i(i){var r="end-"+t+"-"+e;h.mark(r),u.push(function(){var o=null;try{var s=e+" ["+t+"]";h.measure(s,n,r),h.clearMarks(n),h.clearMarks(r)}catch(e){o=e}i&&i(o,e)})}return h.mark(n),i.uuid=t,i}function l(e){e&&u.push(function(){e(new Error("nanotiming: performance API unavailable"))})}var d=c,f=function(e,t,n){var i,r=e.length;if(!(t>=r||0===n)){var o=r-(n=t+n>r?r-t:n);for(i=t;i<o;++i)e[i]=e[i+n];e.length=o}},p={};function v(e){if(!(this instanceof v))return new v(e);this._name=e||"nanobus",this._starListeners=[],this._listeners={}}p=v,v.prototype.emit=function(e){for(var t=[],n=1,i=arguments.length;n<i;n++)t.push(arguments[n]);var r=d(this._name+"('"+e+"')"),o=this._listeners[e];return o&&o.length>0&&this._emit(this._listeners[e],t),this._starListeners.length>0&&this._emit(this._starListeners,e,t,r.uuid),r(),this},v.prototype.on=v.prototype.addListener=function(e,t){return"*"===e?this._starListeners.push(t):(this._listeners[e]||(this._listeners[e]=[]),this._listeners[e].push(t)),this},v.prototype.prependListener=function(e,t){return"*"===e?this._starListeners.unshift(t):(this._listeners[e]||(this._listeners[e]=[]),this._listeners[e].unshift(t)),this},v.prototype.once=function(e,t){var n=this;return this.on(e,function i(){t.apply(n,arguments),n.removeListener(e,i)}),this},v.prototype.prependOnceListener=function(e,t){var n=this;return this.prependListener(e,function i(){t.apply(n,arguments),n.removeListener(e,i)}),this},v.prototype.removeListener=function(e,t){return"*"===e?(this._starListeners=this._starListeners.slice(),n(this._starListeners,t)):(void 0!==this._listeners[e]&&(this._listeners[e]=this._listeners[e].slice()),n(this._listeners[e],t));function n(e,t){if(e){var n=e.indexOf(t);return-1!==n?(f(e,n,1),!0):void 0}}},v.prototype.removeAllListeners=function(e){return e?"*"===e?this._starListeners=[]:this._listeners[e]=[]:(this._starListeners=[],this._listeners={}),this},v.prototype.listeners=function(e){var t="*"!==e?this._listeners[e]:this._starListeners,n=[];if(t)for(var i=t.length,r=0;r<i;r++)n.push(t[r]);return n},v.prototype._emit=function(e,t,n,i){if(void 0!==e&&0!==e.length){void 0===n&&(n=t,t=null),t&&(n=void 0!==i?[t].concat(n,i):[t].concat(n));for(var r=e.length,o=0;o<r;o++){var s=e[o];s.apply(s,n)}}};var m=/(noopener|noreferrer) (noopener|noreferrer)/,w=/^[\w-_]+:/,y=["onclick","ondblclick","onmousedown","onmouseup","onmouseover","onmousemove","onmouseout","onmouseenter","onmouseleave","ontouchcancel","ontouchend","ontouchmove","ontouchstart","ondragstart","ondrag","ondragenter","ondragleave","ondragover","ondrop","ondragend","onkeydown","onkeypress","onkeyup","onunload","onabort","onerror","onresize","onscroll","onselect","onchange","onsubmit","onreset","onfocus","onblur","oninput","oncontextmenu","onfocusin","onfocusout"],_=y.length;function g(e,t,n){e[n]!==t[n]&&(t[n]=e[n],e[n]?t.setAttribute(n,""):t.removeAttribute(n))}var b=function(e,t){var n=e.nodeType,i=e.nodeName;1===n&&function(e,t){for(var n=t.attributes,i=e.attributes,r=null,o=null,s=null,a=null,h=i.length-1;h>=0;--h)s=(a=i[h]).name,r=a.namespaceURI,o=a.value,r?(s=a.localName||s,t.getAttributeNS(r,s)!==o&&t.setAttributeNS(r,s,o)):t.hasAttribute(s)?t.getAttribute(s)!==o&&("null"===o||"undefined"===o?t.removeAttribute(s):t.setAttribute(s,o)):t.setAttribute(s,o);for(var u=n.length-1;u>=0;--u)!1!==(a=n[u]).specified&&(s=a.name,(r=a.namespaceURI)?(s=a.localName||s,e.hasAttributeNS(r,s)||t.removeAttributeNS(r,s)):e.hasAttributeNS(null,s)||t.removeAttribute(s))}(e,t),3!==n&&8!==n||t.nodeValue!==e.nodeValue&&(t.nodeValue=e.nodeValue),"INPUT"===i?function(e,t){var n=e.value,i=t.value;g(e,t,"checked"),g(e,t,"disabled"),n!==i&&(t.setAttribute("value",n),t.value=n),"null"===n&&(t.value="",t.removeAttribute("value")),e.hasAttributeNS(null,"value")?"range"===t.type&&(t.value=n):t.removeAttribute("value")}(e,t):"OPTION"===i?function(e,t){g(e,t,"selected")}(e,t):"TEXTAREA"===i&&function(e,t){var n=e.value;if(n!==t.value&&(t.value=n),t.firstChild&&t.firstChild.nodeValue!==n){if(""===n&&t.firstChild.nodeValue===t.placeholder)return;t.firstChild.nodeValue=n}}(e,t),function(e,t){for(var n=0;n<_;n++){var i=y[n];e[i]?t[i]=e[i]:t[i]&&(t[i]=void 0)}}(e,t)},A=3;function E(e,t){return e.id?e.id===t.id:e.isSameNode?e.isSameNode(t):e.tagName===t.tagName&&e.type===A&&e.nodeValue===t.nodeValue}var N=function(e,t){return function e(t,n){return n?t?t.isSameNode&&t.isSameNode(n)?n:t.tagName!==n.tagName?t:(b(t,n),function(t,n){for(var i,r,o,s,a=0,h=0;i=n.childNodes[h],r=t.childNodes[h-a],i||r;h++)if(r)if(i)if(E(r,i))(o=e(r,i))!==i&&(n.replaceChild(o,i),a++);else{s=null;for(var u=h;u<n.childNodes.length;u++)if(E(n.childNodes[u],r)){s=n.childNodes[u];break}s?((o=e(r,s))!==s&&a++,n.insertBefore(o,i)):r.id||i.id?(n.insertBefore(r,i),a++):(o=e(r,i))!==i&&(n.replaceChild(o,i),a++)}else n.appendChild(r),a++;else n.removeChild(i),h--}(t,n),n):null:t}(t,e)},T=/([^?=&]+)(=([^&]*))?/g,S=function(){for(var e={},t=0;t<arguments.length;t++){var n=arguments[t];for(var i in n)L.call(n,i)&&(e[i]=n[i])}return e},L=Object.prototype.hasOwnProperty,O=function(e){for(var t=1;t<arguments.length;t++){var n=arguments[t];for(var i in n)x.call(n,i)&&(e[i]=n[i])}return e},x=Object.prototype.hasOwnProperty,C={};function R(){if(!(this instanceof R))return new R;this.trie={nodes:{}}}C=R,R.prototype.create=function(e){var t=e.replace(/^\//,"").split("/");return function e(n,i){var r=t.hasOwnProperty(n)&&t[n];if(!1===r)return i;var o=null;return/^:|^\*/.test(r)?(i.nodes.hasOwnProperty("$$")?o=i.nodes.$$:(o={nodes:{}},i.nodes.$$=o),"*"===r[0]&&(i.wildcard=!0),i.name=r.replace(/^:|^\*/,"")):i.nodes.hasOwnProperty(r)?o=i.nodes[r]:(o={nodes:{}},i.nodes[r]=o),e(n+1,o)}(0,this.trie)},R.prototype.match=function(e){var t=e.replace(/^\//,"").split("/"),n={},i=function e(i,r){if(void 0!==r){var o=t[i];if(void 0===o)return r;if(r.nodes.hasOwnProperty(o))return e(i+1,r.nodes[o]);if(r.name){try{n[r.name]=decodeURIComponent(o)}catch(t){return e(i,void 0)}return e(i+1,r.nodes.$$)}if(r.wildcard){try{n.wildcard=decodeURIComponent(t.slice(i).join("/"))}catch(t){return e(i,void 0)}return r.nodes.$$}return e(i+1)}}(0,this.trie);if(i)return(i=S(i)).params=n,i},R.prototype.mount=function(e,t){var n=e.replace(/^\//,"").split("/"),i=null,r=null;if(1===n.length)r=n[0],i=this.create(r);else{var o=n.join("/");r=n[0],i=this.create(o)}O(i.nodes,t.nodes),t.name&&(i.name=t.name),i.nodes[""]&&(Object.keys(i.nodes[""]).forEach(function(e){"nodes"!==e&&(i[e]=i.nodes[""][e])}),O(i.nodes,i.nodes[""].nodes),delete i.nodes[""].nodes)};var k=function e(t){if(!(this instanceof e))return new e(t);var n=(t||"").replace(/^\//,""),i=C();return r._trie=i,r.on=function(e,t){(e=e||"/",t.route=e,t&&t._wayfarer&&t._trie)?i.mount(e,t._trie.trie):i.create(e).cb=t;return r},r.emit=r,r.match=o,r._wayfarer=!0,r;function r(e){var t=o(e),n=new Array(arguments.length);n[0]=t.params;for(var i=1;i<n.length;i++)n[i]=arguments[i];return t.cb.apply(t.cb,n)}function o(e){var t=i.match(e);if(t&&t.cb)return new s(t);var r=i.match(n);if(r&&r.cb)return new s(r);throw new Error("route '"+e+"' did not match")}function s(e){this.cb=e.cb,this.route=e.cb.route,this.params=e.params}},I={},P=/file:\/\//.test("object"==typeof window&&window.location&&window.location.origin),D=new RegExp("^(file://|/)(.*.html?/?)?"),V=new RegExp("^(http(s)?(://))?(www.)?[a-zA-Z0-9-_.]+(:[0-9]{1,5})?(/{1})?"),$=new RegExp("#"),M=new RegExp("[?].*$");function q(e){if(!(this instanceof q))return new q(e);e=e||{},this.router=k(e.default||"/404")}function U(e,t){return e=t?e.replace(D,""):e.replace(V,""),decodeURI(e.replace(M,"").replace($,"/"))}I=q,q.prototype.on=function(e,t){e=e.replace(/^[#/]/,""),this.router.on(e,t)},q.prototype.emit=function(e){return e=U(e,P),this.router.emit(e)},q.prototype.match=function(e){return e=U(e,P),this.router.match(e)};var j=function(e,t){if(e)try{var n=document.querySelector(e);n&&n.scrollIntoView(t)}catch(e){}},G=W,H={};function W(e){if(!(this instanceof W))return new W(e);e=e||{};var t=this;this._events={DOMCONTENTLOADED:"DOMContentLoaded",DOMTITLECHANGE:"DOMTitleChange",REPLACESTATE:"replaceState",PUSHSTATE:"pushState",NAVIGATE:"navigate",POPSTATE:"popState",RENDER:"render"},this._historyEnabled=void 0===e.history||e.history,this._hrefEnabled=void 0===e.href||e.href,this._hashEnabled=void 0===e.hash||e.hash,this._hasWindow="undefined"!=typeof window,this._cache=e.cache,this._loaded=!1,this._stores=[],this._tree=null;var n={events:this._events,components:{}};this._hasWindow?(this.state=window.initialState?S(window.initialState,n):n,delete window.initialState):this.state=n,this.router=I({curry:!0}),this.emitter=p("choo.emit"),this.emit=this.emitter.emit.bind(this.emitter),this._hasWindow&&(this.state.title=document.title),this.emitter.prependListener(this._events.DOMTITLECHANGE,function(e){t.state.title=e,t._hasWindow&&(document.title=e)})}return W.prototype.route=function(e,t){this.router.on(e,t)},W.prototype.use=function(e){var t=this;this._stores.push(function(n){var i="choo.use";i=e.storeName?i+"("+e.storeName+")":i;var r=d(i);e(n,t.emitter,t),r()})},W.prototype.start=function(){var e,t,n=this;return this._historyEnabled&&(this.emitter.prependListener(this._events.NAVIGATE,function(){n._matchRoute(),n._loaded&&(n.emitter.emit(n._events.RENDER),setTimeout(j.bind(null,window.location.hash),0))}),this.emitter.prependListener(this._events.POPSTATE,function(){n.emitter.emit(n._events.NAVIGATE)}),this.emitter.prependListener(this._events.PUSHSTATE,function(e){window.history.pushState(H,null,e),n.emitter.emit(n._events.NAVIGATE)}),this.emitter.prependListener(this._events.REPLACESTATE,function(e){window.history.replaceState(H,null,e),n.emitter.emit(n._events.NAVIGATE)}),window.onpopstate=function(){n.emitter.emit(n._events.POPSTATE)},n._hrefEnabled&&(e=function(e){var t=e.href,i=e.hash;t!==window.location.href?n.emitter.emit(n._events.PUSHSTATE,t):!n._hashEnabled&&i&&j(i)},t=t||window.document,window.addEventListener("click",function(n){if(!(n.button&&0!==n.button||n.ctrlKey||n.metaKey||n.altKey||n.shiftKey||n.defaultPrevented)){var i=function e(n){if(n&&n!==t)return"a"!==n.localName||void 0===n.href?e(n.parentNode):n}(n.target);i&&(window.location.origin!==i.origin||i.hasAttribute("download")||"_blank"===i.getAttribute("target")&&m.test(i.getAttribute("rel"))||w.test(i.getAttribute("href"))||(n.preventDefault(),e(i)))}}))),this._setCache(this.state),this._stores.forEach(function(e){e(n.state)}),this._matchRoute(),this._tree=this._prerender(this.state),this.emitter.prependListener(n._events.RENDER,function(e,t){t||(t=window.requestAnimationFrame);var n=!1,i=null;return function(){null!==i||n||(n=!0,t(function(){n=!1;for(var t=i.length,r=new Array(t),o=0;o<t;o++)r[o]=i[o];e.apply(e,r),i=null})),i=arguments}}(function(){var e=d("choo.render"),t=n._prerender(n.state),i=d("choo.morph");N(n._tree,t),i(),e()})),o(function(){n.emitter.emit(n._events.DOMCONTENTLOADED),n._loaded=!0}),this._tree},W.prototype.mount=function(e){if("object"!=typeof window)return this.selector=e,this;var t=this;o(function(){var n=d("choo.render"),i=t.start();t._tree="string"==typeof e?document.querySelector(e):e;var r=d("choo.morph");N(t._tree,i),r(),n()})},W.prototype.toString=function(e,t){this.state=S(this.state,t||{});var n=this;this._setCache(this.state),this._stores.forEach(function(e){e(n.state)}),this._matchRoute(e);var i=this._prerender(this.state);return"string"==typeof i.outerHTML?i.outerHTML:i.toString()},W.prototype._matchRoute=function(e){var t,n;e?(t=e.replace(/\?.+$/,"").replace(/\/$/,""),this._hashEnabled||(t=t.replace(/#.+$/,"")),n=e):(t=window.location.pathname.replace(/\/$/,""),this._hashEnabled&&(t+=window.location.hash.replace(/^#/,"/")),n=window.location.search);var i,r=this.router.match(t);return this._handler=r.cb,this.state.href=t,this.state.query=(i={},n.replace(/^.*\?/,"").replace(T,function(e,t,n,r){i[decodeURIComponent(t)]=decodeURIComponent(r)}),i),this.state.route=r.route,this.state.params=r.params,this.state},W.prototype._prerender=function(e){var t=d("choo.prerender('"+e.route+"')"),n=this._handler(e,this.emit);return t(),n},W.prototype._setCache=function(e){var t=new n(e,this.emitter.emit.bind(this.emitter),this._cache);function i(e,n){for(var i=[],r=0,o=arguments.length;r<o;r++)i.push(arguments[r]);return t.render.apply(t,i)}e.cache=i,i.toJSON=function(){return null}},G});