diff --git a/README.md b/README.md index 6860df3a..e438ae20 100644 --- a/README.md +++ b/README.md @@ -18,7 +18,7 @@ mithril.js [![NPM Version](https://img.shields.io/npm/v/mithril.svg)](https://ww ## What is Mithril? -A modern client-side Javascript framework for building Single Page Applications. It's small (8.68 KB gzipped), fast and provides routing and XHR utilities out of the box. +A modern client-side Javascript framework for building Single Page Applications. It's small (8.74 KB gzipped), fast and provides routing and XHR utilities out of the box. Mithril is used by companies like Vimeo and Nike, and open source platforms like Lichess 👍. diff --git a/mithril.js b/mithril.js index 87176674..4f50a73e 100644 --- a/mithril.js +++ b/mithril.js @@ -174,6 +174,20 @@ PromisePolyfill.prototype.then = function(onFulfilled, onRejection) { PromisePolyfill.prototype.catch = function(onRejection) { return this.then(null, onRejection) } +PromisePolyfill.prototype.finally = function(callback) { + return this.then( + function(value) { + return PromisePolyfill.resolve(callback()).then(function() { + return value + }) + }, + function(reason) { + return PromisePolyfill.resolve(callback()).then(function() { + return PromisePolyfill.reject(reason); + }) + } + ) +} PromisePolyfill.resolve = function(value) { if (value instanceof PromisePolyfill) return value return new PromisePolyfill(function(resolve) {resolve(value)}) @@ -208,10 +222,18 @@ PromisePolyfill.race = function(list) { }) } if (typeof window !== "undefined") { - if (typeof window.Promise === "undefined") window.Promise = PromisePolyfill + if (typeof window.Promise === "undefined") { + window.Promise = PromisePolyfill + } else if (!window.Promise.prototype.finally) { + window.Promise.prototype.finally = PromisePolyfill.prototype.finally + } var PromisePolyfill = window.Promise } else if (typeof global !== "undefined") { - if (typeof global.Promise === "undefined") global.Promise = PromisePolyfill + if (typeof global.Promise === "undefined") { + global.Promise = PromisePolyfill + } else if (!global.Promise.prototype.finally) { + global.Promise.prototype.finally = PromisePolyfill.prototype.finally + } var PromisePolyfill = global.Promise } else { } diff --git a/mithril.min.js b/mithril.min.js index 4b3ed3ed..3b95ad15 100644 --- a/mithril.min.js +++ b/mithril.min.js @@ -1,47 +1,47 @@ -(function(){function x(b,d,e,h,p,l){return{tag:b,key:d,attrs:e,children:h,text:p,dom:l,domSize:void 0,state:void 0,events:void 0,instance:void 0,skip:!1}}function R(b){for(var d in b)if(G.call(b,d))return!1;return!0}function w(b){var d=arguments[1],e=2;if(null==b||"string"!==typeof b&&"function"!==typeof b&&"function"!==typeof b.view)throw Error("The selector must be either a string or a component.");if("string"===typeof b){var h;if(!(h=S[b])){var p="div";for(var l=[],k={};h=W.exec(b);){var q=h[1], -r=h[2];""===q&&""!==r?p=r:"#"===q?k.id=r:"."===q?l.push(r):"["===h[3][0]&&((q=h[6])&&(q=q.replace(/\\(["'])/g,"$1").replace(/\\\\/g,"\\")),"class"===h[4]?l.push(q):k[h[4]]=""===q?q:q||!0)}0a.indexOf("?")?"?":"&";a+=e+d}return a}function k(a){try{return""!==a?JSON.parse(a):null}catch(B){throw Error(a);}}function q(a){return a.responseText}function r(a,b){if("function"===typeof a)if(Array.isArray(b))for(var d=0;dm.status||304===m.status||Z.test(a.url))d(r(a.type,b));else{var h=Error(m.responseText);h.code=m.status;h.response=b;e(h)}}catch(I){e(I)}};h&&null!=a.data?m.send(a.data):m.send()});return!0===a.background?B:A(B)},jsonp:function(a,k){var A=e();a=h(a, -k);var q=new d(function(d,e){var h=a.callbackName||"_mithril_"+Math.round(1E16*Math.random())+"_"+m++,k=b.document.createElement("script");b[h]=function(e){k.parentNode.removeChild(k);d(r(a.type,e));delete b[h]};k.onerror=function(){k.parentNode.removeChild(k);e(Error("JSONP request failed"));delete b[h]};null==a.data&&(a.data={});a.url=p(a.url,a.data);a.data[a.callbackKey||"callback"]=h;k.src=l(a.url,a.data);b.document.documentElement.appendChild(k)});return!0===a.background?q:A(q)},setCompletionCallback:function(a){z= -a}}}(window,t),V=function(b){function d(g,c){if(g.state!==c)throw Error("`vnode.state` must not be modified");}function e(g){var c=g.state;try{return this.apply(c,arguments)}finally{d(g,c)}}function h(g,c,f,a,b,d,e){for(;f'+c.children+"",d=d.firstChild):d.innerHTML=c.children;c.dom=d.firstChild;c.domSize=d.childNodes.length;for(c=C.createDocumentFragment();f=d.firstChild;)c.appendChild(f);a(g,c,b);return c}function k(g,c){if("function"===typeof g.tag.view){g.state=Object.create(g.tag);var f=g.state.view;if(null!=f.$$reentrantLock$$)return I;f.$$reentrantLock$$=!0}else{g.state=void 0;f=g.tag;if(null!=f.$$reentrantLock$$)return I; -f.$$reentrantLock$$=!0;g.state=null!=g.tag.prototype&&"function"===typeof g.tag.prototype.view?new g.tag(g):g.tag(g)}null!=g.attrs&&L(g.attrs,g,c);L(g.state,g,c);g.instance=x.normalize(e.call(g.state.view,g));if(g.instance===g)throw Error("A view cannot return the vnode it received as argument");f.$$reentrantLock$$=null}function q(g,c,f,b,d,e,k){if(!(c===f&&!b||null==c&&null==f))if(null==c)h(g,f,0,f.length,d,e,k);else if(null==f)A(c,0,c.length,f,b);else{for(var n=0,l=Math.min(c.length,f.length),q= -c.length,B=!1,D=!1;n=E&&t>=n;)if(v=c[E],y=f[n],u=B&&E>=q,v===y&&!u&&!b||null==v&&null==y)E++,n++;else if(null==v)(D||null==y.key)&&p(g,f[n],d,k,z(c,++n,q,e)),E++;else if(null==y){if(D||null==v.key)A(c,n,n+1,f,b),E++;n++}else if(v.key===y.key)E++,n++,r(g,v,y,d,z(c,E,q,e),u||b,k),u&&v.tag===y.tag&&a(g,m(y),e);else if(v=c[l],u=B&&l>=q,v!==y||u||b)if(null==v)l--;else if(null== -y)n++;else if(v.key===y.key)r(g,v,y,d,z(c,l+1,q,e),u||b,k),(u&&v.tag===y.tag||n=E&&t>=n;){v=c[l];y=f[t];u=B&&l>=q;if(v!==y||u||b)if(null==v)l--;else{if(null!=y)if(v.key===y.key)r(g,v,y,d,z(c,l+1,q,e),u||b,k),u&&v.tag===y.tag&&a(g,m(y),e),null!=v.dom&&(e=v.dom),l--;else{if(!x){x=c;D=l;v={};for(u=0;u=q,r(g,v,y,d,z(c,l+1,q,e),u||b,k), -a(g,m(y),e),v.skip=!0,null!=v.dom&&(e=v.dom)):e=p(g,y,d,k,e))}t--}else l--,t--;if(t=g;l--)c[l].skip?c[l].skip=!1:J(c[l],f)}}}function r(g,c,f,a,b,d,h){var n=c.tag;if(n===f.tag){f.state=c.state;f.events=c.events;var A;if(A=!d){var z,D;null!=f.attrs&&"function"===typeof f.attrs.onbeforeupdate&&(z=e.call(f.attrs.onbeforeupdate,f,c));"string"!==typeof f.tag&&"function"===typeof f.state.onbeforeupdate&&(D=e.call(f.state.onbeforeupdate, -f,c));void 0===z&&void 0===D||z||D?A=!1:(f.dom=c.dom,f.domSize=c.domSize,f.instance=c.instance,A=!0)}if(!A)if("string"===typeof n)switch(null!=f.attrs&&(d?(f.state={},L(f.attrs,f,a)):N(f.attrs,f,a)),n){case "#":c.children.toString()!==f.children.toString()&&(c.dom.nodeValue=f.children);f.dom=c.dom;break;case "<":c.children!==f.children?(m(c),l(g,f,h,b)):(f.dom=c.dom,f.domSize=c.domSize);break;case "[":q(g,c.children,f.children,d,a,b,h);c=0;a=f.children;f.dom=null;if(null!=a){for(d=0;da.indexOf("?")?"?":"&";a+=e+c}return a}function k(a){try{return""!==a?JSON.parse(a):null}catch(B){throw Error(a);}}function q(a){return a.responseText}function t(a,b){if("function"=== +typeof a)if(Array.isArray(b))for(var c=0;cm.status||304===m.status||Z.test(a.url))c(t(a.type,b));else{var h=Error(m.responseText);h.code=m.status;h.response=b;e(h)}}catch(I){e(I)}};h&&null!=a.data?m.send(a.data):m.send()});return!0===a.background?B:z(B)},jsonp:function(a,k){var z=e();a=h(a,k);var q=new c(function(c,e){var h=a.callbackName||"_mithril_"+Math.round(1E16*Math.random())+"_"+m++,k=b.document.createElement("script");b[h]=function(e){k.parentNode.removeChild(k);c(t(a.type,e));delete b[h]};k.onerror=function(){k.parentNode.removeChild(k); +e(Error("JSONP request failed"));delete b[h]};null==a.data&&(a.data={});a.url=p(a.url,a.data);a.data[a.callbackKey||"callback"]=h;k.src=l(a.url,a.data);b.document.documentElement.appendChild(k)});return!0===a.background?q:z(q)},setCompletionCallback:function(a){A=a}}}(window,r),V=function(b){function c(g,d){if(g.state!==d)throw Error("`vnode.state` must not be modified");}function e(g){var d=g.state;try{return this.apply(d,arguments)}finally{c(g,d)}}function h(g,d,f,a,b,c,e){for(;f'+d.children+"",c=c.firstChild):c.innerHTML=d.children;d.dom=c.firstChild;d.domSize=c.childNodes.length;for(d=C.createDocumentFragment();f= +c.firstChild;)d.appendChild(f);a(g,d,b);return d}function k(g,d){if("function"===typeof g.tag.view){g.state=Object.create(g.tag);var f=g.state.view;if(null!=f.$$reentrantLock$$)return I;f.$$reentrantLock$$=!0}else{g.state=void 0;f=g.tag;if(null!=f.$$reentrantLock$$)return I;f.$$reentrantLock$$=!0;g.state=null!=g.tag.prototype&&"function"===typeof g.tag.prototype.view?new g.tag(g):g.tag(g)}null!=g.attrs&&L(g.attrs,g,d);L(g.state,g,d);g.instance=x.normalize(e.call(g.state.view,g));if(g.instance===g)throw Error("A view cannot return the vnode it received as argument"); +f.$$reentrantLock$$=null}function q(g,d,f,b,c,e,k){if(!(d===f&&!b||null==d&&null==f))if(null==d)h(g,f,0,f.length,c,e,k);else if(null==f)z(d,0,d.length,f,b);else{for(var n=0,l=Math.min(d.length,f.length),q=d.length,B=!1,D=!1;n=r&&F>=n;)if(v=d[r],y=f[n],u=B&&r>=q,v===y&&!u&&!b||null==v&&null==y)r++,n++;else if(null==v)(D||null==y.key)&&p(g,f[n],c,k,A(d,++n,q,e)),r++;else if(null== +y){if(D||null==v.key)z(d,n,n+1,f,b),r++;n++}else if(v.key===y.key)r++,n++,t(g,v,y,c,A(d,r,q,e),u||b,k),u&&v.tag===y.tag&&a(g,m(y),e);else if(v=d[l],u=B&&l>=q,v!==y||u||b)if(null==v)l--;else if(null==y)n++;else if(v.key===y.key)t(g,v,y,c,A(d,l+1,q,e),u||b,k),(u&&v.tag===y.tag||n=r&&F>=n;){v=d[l];y=f[F];u=B&&l>=q;if(v!==y||u||b)if(null==v)l--;else{if(null!=y)if(v.key===y.key)t(g,v,y,c,A(d,l+1,q,e),u||b,k),u&&v.tag===y.tag&&a(g,m(y),e), +null!=v.dom&&(e=v.dom),l--;else{if(!x){x=d;D=l;v={};for(u=0;u=q,t(g,v,y,c,A(d,l+1,q,e),u||b,k),a(g,m(y),e),v.skip=!0,null!=v.dom&&(e=v.dom)):e=p(g,y,c,k,e))}F--}else l--,F--;if(F=g;l--)d[l].skip?d[l].skip=!1:J(d[l],f)}}}function t(g,d,f,b,a,c,h){var n=d.tag;if(n===f.tag){f.state=d.state;f.events=d.events;var z;if(z= +!c){var A,D;null!=f.attrs&&"function"===typeof f.attrs.onbeforeupdate&&(A=e.call(f.attrs.onbeforeupdate,f,d));"string"!==typeof f.tag&&"function"===typeof f.state.onbeforeupdate&&(D=e.call(f.state.onbeforeupdate,f,d));void 0===A&&void 0===D||A||D?z=!1:(f.dom=d.dom,f.domSize=d.domSize,f.instance=d.instance,z=!0)}if(!z)if("string"===typeof n)switch(null!=f.attrs&&(c?(f.state={},L(f.attrs,f,b)):N(f.attrs,f,b)),n){case "#":d.children.toString()!==f.children.toString()&&(d.dom.nodeValue=f.children);f.dom= +d.dom;break;case "<":d.children!==f.children?(m(d),l(g,f,h,a)):(f.dom=d.dom,f.domSize=d.domSize);break;case "[":q(g,d.children,f.children,c,b,a,h);d=0;b=f.children;f.dom=null;if(null!=b){for(c=0;c