From 9f340daa3a7ae1ad557c6ec1bc0a1f6120765fab Mon Sep 17 00:00:00 2001 From: Leo Horie Date: Mon, 5 Dec 2016 12:32:11 -0500 Subject: [PATCH] reduce router code size --- README.md | 2 +- api/router.js | 30 +++++++++---------- mithril.js | 17 +++++------ mithril.min.js | 81 +++++++++++++++++++++++++------------------------- 4 files changed, 64 insertions(+), 66 deletions(-) diff --git a/README.md b/README.md index ef729c3b..4676e66b 100644 --- a/README.md +++ b/README.md @@ -34,6 +34,6 @@ There are over 4000 assertions in the test suite, and tests cover even difficult ## Modularity -Despite the huge improvements in performance and modularity, the new codebase is smaller than v0.2.x, currently clocking at 7.44 KB min+gzip +Despite the huge improvements in performance and modularity, the new codebase is smaller than v0.2.x, currently clocking at 7.40 KB min+gzip In addition, Mithril is now completely modular: you can import only the modules that you need and easily integrate 3rd party modules if you wish to use a different library for routing, ajax, and even rendering diff --git a/api/router.js b/api/router.js index 96ad71bc..95ef6afd 100644 --- a/api/router.js +++ b/api/router.js @@ -7,32 +7,32 @@ module.exports = function($window, redrawService) { var routeService = coreRouter($window) var identity = function(v) {return v} - var current = {} + var resolver, component, attrs, currentPath, resolve var route = function(root, defaultRoute, routes) { if (root == null) throw new Error("Ensure the DOM element that was passed to `m.route` is not undefined") - var update = function(resolver, component, params, path) { - current = {resolver: resolver, component: component, params: params, path: path, resolve: null} - current.resolver.render = resolver.render || identity + var update = function(routeResolver, comp, params, path) { + resolver = routeResolver, component = comp, attrs = params, currentPath = path, resolve = null + resolver.render = routeResolver.render || identity render() } var render = function() { - if (current.resolver != null) redrawService.render(root, current.resolver.render(Vnode(current.component, current.params.key, current.params))) + if (resolver != null) redrawService.render(root, resolver.render(Vnode(component, attrs.key, attrs))) } - routeService.defineRoutes(routes, function(component, params, path) { - if (component.view) update({}, component, params, path) + routeService.defineRoutes(routes, function(payload, params, path) { + if (payload.view) update({}, payload, params, path) else { - if (component.onmatch) { - if (current.resolve != null) update(component, current.component, params, path) + if (payload.onmatch) { + if (resolve != null) update(payload, component, params, path) else { - current.resolve = function(resolved) { - update(component, resolved, params, path) + resolve = function(resolved) { + update(payload, resolved, params, path) } - component.onmatch(function(resolved) { - if (current.resolve != null) current.resolve(resolved) + payload.onmatch(function(resolved) { + if (resolve != null) resolve(resolved) }, params, path) } } - else update(component, "div", params, path) + else update(payload, "div", params, path) } }, function() { routeService.setPath(defaultRoute) @@ -40,7 +40,7 @@ module.exports = function($window, redrawService) { redrawService.subscribe(root, render) } route.set = routeService.setPath - route.get = function() {return current.path} + route.get = function() {return currentPath} route.prefix = routeService.setPrefix route.link = routeService.link return route diff --git a/mithril.js b/mithril.js index 35531c81..b214a003 100644 --- a/mithril.js +++ b/mithril.js @@ -1092,28 +1092,27 @@ var _20 = function($window, redrawService0) { var routeService = coreRouter($window) var identity = function(v) {return v} - var current = {} + var currentResolver, currentComponent, currentParams, currentPath, currentResolve var route = function(root, defaultRoute, routes) { - if (root == null) throw new Error("Ensure the DOM element that was passed to `m.route` is not undefined") var update = function(resolver, component, params, path) { - current = {resolver: resolver, component: component, params: params, path: path, resolve: null} - current.resolver.render = resolver.render || identity + currentResolver = resolver, currentComponent = component, currentParams = params, currentPath = path, currentResolve = null + currentResolver.render = resolver.render || identity render1() } var render1 = function() { - if (current.resolver != null) redrawService0.render(root, current.resolver.render(Vnode(current.component, current.params.key, current.params))) + if (currentResolver != null) redrawService0.render(root, currentResolver.render(Vnode(currentComponent, currentParams.key, currentParams))) } routeService.defineRoutes(routes, function(component, params, path) { if (component.view) update({}, component, params, path) else { if (component.onmatch) { - if (current.resolve != null) update(component, current.component, params, path) + if (currentResolve != null) update(component, currentComponent, params, path) else { - current.resolve = function(resolved) { + currentResolve = function(resolved) { update(component, resolved, params, path) } component.onmatch(function(resolved) { - if (current.resolve != null) current.resolve(resolved) + if (currentResolve != null) currentResolve(resolved) }, params, path) } } @@ -1125,7 +1124,7 @@ var _20 = function($window, redrawService0) { redrawService0.subscribe(root, render1) } route.set = routeService.setPath - route.get = function() {return current.path} + route.get = function() {return currentPath} route.prefix = routeService.setPrefix route.link = routeService.link return route diff --git a/mithril.min.js b/mithril.min.js index dd7f0936..1a81dc22 100644 --- a/mithril.min.js +++ b/mithril.min.js @@ -1,41 +1,40 @@ -new function(){function m(a,c,h,g,d,f){return{tag:a,key:c,attrs:h,children:g,text:d,dom:f,domSize:void 0,state:{},events:void 0,instance:void 0,skip:!1}}function y(a){if(null==a||"string"!==typeof a&&null==a.view)throw Error("The selector must be either a string or a component.");if("string"===typeof a&&void 0===I[a]){for(var c,h,g=[],d={};c=O.exec(a);){var f=c[1],n=c[2];""===f&&""!==n?h=n:"#"===f?d.id=n:"."===f?g.push(n):"["===c[3][0]&&((f=c[6])&&(f=f.replace(/\\(["'])/g,"$1").replace(/\\\\/g,"\\")), -"class"===c[4]?g.push(f):d[c[4]]=f||!0)}0b.indexOf("?")?"?":"&";b+=d+c}return b}function n(b){try{return""!==b?JSON.parse(b):null}catch(B){throw Error(b);}}function r(b){return b.responseText}function q(b,a){if("function"=== -typeof b)if(a instanceof Array)for(var c=0;cl.status||304===l.status)c(q(b.type,a));else{var d=Error(l.responseText),f;for(f in a)d[f]=a[f];g(d)}}catch(D){g(D)}};h&&null!=b.data?l.send(b.data):l.send()});return!0===b.background?t:v(t)},jsonp:function(b,l){var n=h();b=g(b,l);var t=new c(function(c,g){var l=b.callbackName|| -"_mithril_"+Math.round(1E16*Math.random())+"_"+m++,h=a.document.createElement("script");a[l]=function(d){h.parentNode.removeChild(h);c(q(b.type,d));delete a[l]};h.onerror=function(){h.parentNode.removeChild(h);g(Error("JSONP request failed"));delete a[l]};null==b.data&&(b.data={});b.url=d(b.url,b.data);b.data[b.callbackKey||"callback"]=l;h.src=f(b.url,b.data);a.document.documentElement.appendChild(h)});return!0===b.background?t:n(t)},setCompletionCallback:function(a){l=a}}}(window,"undefined"!==typeof Promise? -Promise:u),N=function(a){function c(e,k,a,b,c,d,f){for(;a=w&&v>=t;){var x=k[w],m=a[t];if(x!==m||p)if(null==x)w++;else if(null==m)t++;else if(x.key===m.key)w++,t++,f(e,x,m,b,r(k,w,d),p,g),p&&x.tag===m.tag&&q(e,n(x),d);else if(x=k[z],x!==m||p)if(null==x)z--;else if(null==m)t++;else if(x.key===m.key)f(e,x,m,b,r(k,z+1,d),p,g),(p||t=w&&v>=t;){x=k[z];m=a[v];if(x!==m||p)if(null==x)z--;else{if(null!= -m)if(x.key===m.key)f(e,x,m,b,r(k,z+1,d),p,g),p&&x.tag===m.tag&&q(e,n(x),d),null!=x.dom&&(d=x.dom),z--;else{if(!E){E=k;var x=z,B={},u;for(u=0;ua.indexOf("?")?"?":"&";a+=d+b}return a}function m(a){try{return""!==a?JSON.parse(a):null}catch(M){throw Error(a);}}function A(a){return a.responseText}function n(a,c){if("function"=== +typeof a)if(c instanceof Array)for(var b=0;bk.status||304===k.status)c(n(b.type,a));else{var g=Error(k.responseText),h;for(h in a)g[h]=a[h];d(g)}}catch(F){d(F)}};h&&null!=b.data?k.send(b.data):k.send()});return!0===b.background?u:v(u)},jsonp:function(b,k){var m=h();b=d(b,k);var u=new c(function(c,d){var k=b.callbackName|| +"_mithril_"+Math.round(1E16*Math.random())+"_"+r++,h=a.document.createElement("script");a[k]=function(d){h.parentNode.removeChild(h);c(n(b.type,d));delete a[k]};h.onerror=function(){h.parentNode.removeChild(h);d(Error("JSONP request failed"));delete a[k]};null==b.data&&(b.data={});b.url=g(b.url,b.data);b.data[b.callbackKey||"callback"]=k;h.src=l(b.url,b.data);a.document.documentElement.appendChild(h)});return!0===b.background?u:m(u)},setCompletionCallback:function(a){k=a}}}(window,"undefined"!==typeof Promise? +Promise:t),O=function(a){function c(e,f,a,b,c,d,g){for(;a=w&&v>=u;){var x=f[w],p=a[u];if(x!==p||q)if(null==x)w++;else if(null==p)u++;else if(x.key===p.key)w++,u++,l(e,x,p,b,A(f,w,d),q,g),q&&x.tag===p.tag&&n(e,m(x),d);else if(x=f[z],x!==p||q)if(null==x)z--;else if(null==p)u++;else if(x.key===p.key)l(e,x,p,b,A(f,z+1,d),q,g),(q||u=w&&v>=u;){x=f[z];p=a[v];if(x!==p||q)if(null==x)z--;else{if(null!= +p)if(x.key===p.key)l(e,x,p,b,A(f,z+1,d),q,g),q&&x.tag===p.tag&&n(e,m(x),d),null!=x.dom&&(d=x.dom),z--;else{if(!D){D=f;var x=z,r={},C;for(C=0;C