UNPKG

@nimel/directorr-router

Version:
2 lines (1 loc) 9.12 kB
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports,require("path-to-regexp"),require("query-string"),require("@nimel/directorr"),require("history")):"function"==typeof define&&define.amd?define(["exports","path-to-regexp","query-string","@nimel/directorr","history"],e):e((t=t||self).index={},t.pathToRegexp,t.qs,t.directorr,t.history)}(this,(function(t,e,o,r,n){"use strict";o=o&&Object.prototype.hasOwnProperty.call(o,"default")?o.default:o;var i=function(){function t(t){void 0===t&&(t=1e4),this.store=new Map,this.cacheSize=t}return t.prototype.set=function(t,e){return this.store.size>this.cacheSize&&this.store.delete(this.store.keys().next().value),this.store.set(t,e)},t.prototype.get=function(t){return this.store.get(t)},t.prototype.has=function(t){return this.store.has(t)},Object.defineProperty(t.prototype,"size",{get:function(){return this.store.size},enumerable:!1,configurable:!0}),t}(),c=new Map,a=new i;function s(t,o,n,a){void 0===n&&(n=!0),void 0===a&&(a=!1);var s=function(t,o,n){var a=r.EMPTY_STRING+o+n;c.has(a)||c.set(a,new i);var s=c.get(a);if(s.has(t))return s.get(t);var u=[],f={reg:e.pathToRegexp(t,u,{end:o,strict:n}),keys:u};return s.set(t,f),f}(o,n,a),u=s.reg,f=s.keys,p=u.exec(t);if(p)return{patterns:p,keys:f}}function u(t,e){for(var o,r={},n=0,i=e.length,c=void 0;n<i;++n)o=e[n].name,c=t[n+1],o&&(r[o]=c);return r}function f(t,e){if(e){var r=o.stringify(e);return r?"".concat(t,"?").concat(r):t}return t}var p,y,h,d,l,R,b,O,v,P,g,E,T=function(){return(T=Object.assign||function(t){for(var e,o=1,r=arguments.length;o<r;o++)for(var n in e=arguments[o])Object.prototype.hasOwnProperty.call(e,n)&&(t[n]=e[n]);return t}).apply(this,arguments)};function j(t,e,o,r){var n,i=arguments.length,c=i<3?e:null===r?r=Object.getOwnPropertyDescriptor(e,o):r;if("object"==typeof Reflect&&"function"==typeof Reflect.decorate)c=Reflect.decorate(t,e,o,r);else for(var a=t.length-1;a>=0;a--)(n=t[a])&&(c=(i<3?n(c):i>3?n(e,o,c):n(e,o))||c);return i>3&&c&&Object.defineProperty(e,o,c),c}function S(t,e){if("object"==typeof Reflect&&"function"==typeof Reflect.metadata)return Reflect.metadata(t,e)}function A(t,e){var o="function"==typeof Symbol&&t[Symbol.iterator];if(!o)return t;var r,n,i=o.call(t),c=[];try{for(;(void 0===e||e-- >0)&&!(r=i.next()).done;)c.push(r.value)}catch(t){n={error:t}}finally{try{r&&!r.done&&(o=i.return)&&o.call(i)}finally{if(n)throw n.error}}return c}function k(t,e,o){if(o||2===arguments.length)for(var r,n=0,i=e.length;n<i;n++)!r&&n in e||(r||(r=Array.prototype.slice.call(e,0,n)),r[n]=e[n]);return t.concat(r||Array.prototype.slice.call(e))}function H(){return!0}function C(t,e){return T(T({},t),{store:e})}function w(t,e){return e===t.store}var m=(p=A(r.createActionAndEffect("ROUTER.PUSH"),2))[0],U=p[1],B=(y=A(r.createActionAndEffect("ROUTER.REPLACE"),2))[0],q=y[1],x=(h=A(r.createActionAndEffect("ROUTER.BACK"),2))[0],_=h[1],Y=(d=A(r.createActionAndEffect("ROUTER.FORWARD"),2))[0],M=d[1],N=(l=A(r.createActionAndEffect("ROUTER.GOTO"),2))[0],F=l[1],I=(R=A(r.createActionAndEffect("ROUTER.RELOAD"),2))[0],L=R[1],D=(b=A(r.createActionAndEffect("ROUTER.BLOCK"),2))[0],G=b[1],z=(O=A(r.createActionAndEffect("ROUTER.CANCEL_BLOCK"),2))[0],J=O[1],K=(v=A(r.createActionAndEffect("ROUTER.STATE"),2))[0],W=v[1],Q=r.action("ROUTER.IS_PATTERN",C),V=r.effect(Q.type),X=r.action("ROUTER.IS_PATTERN_SUCCESS"),Z=r.composePropertyDecorators([r.effect(X.type),r.whenState(w)]),$=(P=A(r.createActionAndEffect("HISTORY.POP"),2))[0],tt=P[1],et=(g=A(r.createActionAndEffect("HISTORY.PUSH"),2))[0],ot=g[1],rt=(E=A(r.createActionAndEffect("HISTORY.REPLACE"),2))[0],nt=E[1],it={exact:!0,strict:!0};var ct={POP:"POP",PUSH:"PUSH",REPLACE:"REPLACE"},at=function(){function t(t){void 0===t&&(t=n.createBrowserHistory());var e=this;this.unsubHistory=r.EMPTY_FUNC,this.handlersStack=[],this.blockState=[r.EMPTY_FUNC,r.EMPTY_FUNC],this.subscribe=function(t){e.handlersStack=k(k([],A(e.handlersStack),!1),[t],!1)},this.unsubscribe=function(t){e.handlersStack=e.handlersStack.filter((function(e){return e!==t}))},this.toSetState=function(t){var o=t.path,r=t.queryObject,n=t.state;e.path=o,e.queryObject=r,e.state=n},this.toDestroy=function(){e.unsubHistory()},this.toHistoryPush=function(t){return t},this.toHistoryPop=function(t){return t},this.toHistoryReplace=function(t){return t},this.toPop=function(t){var o=t.path,r=t.queryObject,n=t.state,i=t.action;e.path=o,e.queryObject=r,e.state=n,e.action=i},this.push=function(t,e,o){return void 0===e&&(e=r.EMPTY_OBJECT),{path:t,queryObject:e,state:o}},this.replace=function(t,e,o){return void 0===e&&(e=r.EMPTY_OBJECT),{path:t,queryObject:e,state:o}},this.back=r.EMPTY_FUNC,this.reload=r.EMPTY_FUNC,this.toReload=function(){window.location.reload()},this.toPush=function(t){var o=t.path,r=t.queryObject,n=t.state;return e.history.push(f(o,r),n)},this.toReplace=function(t){var o=t.path,r=t.queryObject,n=t.state;return e.history.replace(f(o,r),n)},this.toBack=function(){return e.history.back()},this.goTo=function(t){return{index:t}},this.toGoTo=function(t){var o=t.index;return e.history.go(o)},this.forward=r.EMPTY_FUNC,this.toForward=function(){return e.history.forward()},this.block=function(t){return{blocker:t}},this.toBlock=function(t){var o=t.blocker;e.blockState=[o,e.history.block(o)]},this.cancelBlock=function(t){return{blocker:t}},this.toCancelBlock=function(t){var o=t.blocker,r=A(e.blockState,2),n=r[0],i=r[1];o===n&&i()},this.dispatchAction=function(t){var r,n,i=t.location,c=t.action,a=T(T({path:i.pathname,queryObject:o.parse(i.search)},i.state&&{state:i.state}),{action:c});switch(c){case ct.POP:e.toHistoryPop(a);break;case ct.REPLACE:e.toHistoryReplace(a);break;default:e.toHistoryPush(a)}try{for(var s=function(t){var e="function"==typeof Symbol&&Symbol.iterator,o=e&&t[e],r=0;if(o)return o.call(t);if(t&&"number"==typeof t.length)return{next:function(){return t&&r>=t.length&&(t=void 0),{value:t&&t[r++],done:!t}}};throw new TypeError(e?"Object is not iterable.":"Symbol.iterator is not defined.")}(e.handlersStack),u=s.next();!u.done;u=s.next()){(0,u.value)(t)}}catch(t){r={error:t}}finally{try{u&&!u.done&&(n=s.return)&&n.call(s)}finally{if(r)throw r.error}}},this.history=t;var i=this.history,c=i.location,a=c.pathname,s=c.search,u=c.state,p=i.action;this.unsubHistory=this.history.listen(this.dispatchAction),this.path=a,this.queryObject=o.parse(s),this.state=u,this.action=p}return t.prototype.toJSON=function(){},j([W,S("design:type",Object)],t.prototype,"toSetState",void 0),j([r.whenDestroy,S("design:type",Object)],t.prototype,"toDestroy",void 0),j([et,S("design:type",Object)],t.prototype,"toHistoryPush",void 0),j([$,S("design:type",Object)],t.prototype,"toHistoryPop",void 0),j([rt,S("design:type",Object)],t.prototype,"toHistoryReplace",void 0),j([tt,ot,nt,S("design:type",Object)],t.prototype,"toPop",void 0),j([m,S("design:type",Object)],t.prototype,"push",void 0),j([B,S("design:type",Object)],t.prototype,"replace",void 0),j([x,S("design:type",Object)],t.prototype,"back",void 0),j([I,S("design:type",Object)],t.prototype,"reload",void 0),j([L,S("design:type",Object)],t.prototype,"toReload",void 0),j([U,S("design:type",Object)],t.prototype,"toPush",void 0),j([q,S("design:type",Object)],t.prototype,"toReplace",void 0),j([_,S("design:type",Object)],t.prototype,"toBack",void 0),j([N,S("design:type",Object)],t.prototype,"goTo",void 0),j([F,S("design:type",Object)],t.prototype,"toGoTo",void 0),j([Y,S("design:type",Object)],t.prototype,"forward",void 0),j([M,S("design:type",Object)],t.prototype,"toForward",void 0),j([D,S("design:type",Object)],t.prototype,"block",void 0),j([G,S("design:type",Object)],t.prototype,"toBlock",void 0),j([z,S("design:type",Object)],t.prototype,"cancelBlock",void 0),j([J,S("design:type",Object)],t.prototype,"toCancelBlock",void 0),t}();t.ACTION=ct,t.ANY_PATH="(.*)",t.HistoryStore=at,t.actionHistoryPop=$,t.actionHistoryPush=et,t.actionHistoryReplace=rt,t.actionRouterBack=x,t.actionRouterBlock=D,t.actionRouterCancelBlock=z,t.actionRouterForward=Y,t.actionRouterGoTo=N,t.actionRouterIsPattern=Q,t.actionRouterIsPatternSuccess=X,t.actionRouterPush=m,t.actionRouterReload=I,t.actionRouterReplace=B,t.actionRouterState=K,t.addStoreToPayload=C,t.calcPath=f,t.effectHistoryPop=tt,t.effectHistoryPush=ot,t.effectHistoryReplace=nt,t.effectRouterBack=_,t.effectRouterBlock=G,t.effectRouterCancelBlock=J,t.effectRouterForward=M,t.effectRouterGoTo=F,t.effectRouterIsPattern=V,t.effectRouterIsPatternSuccess=Z,t.effectRouterPush=U,t.effectRouterReload=L,t.effectRouterReplace=q,t.effectRouterState=W,t.generatePath=function(t,o){return void 0===t&&(t="/"),void 0===o&&(o=r.EMPTY_OBJECT),"/"===t?t:function(t){if(a.has(t))return a.get(t);var o=e.compile(t);return a.set(t,o),o}(t)(o)},t.historyChange=function(t,e){var o=void 0===e?it:e,n=o.exact,i=o.strict;return r.composePropertyDecorators([tt,ot,nt,r.whenPayload(H,(function(e){var o=s(e.path,t,n,i);return T(T({},e),{match:!!o,queryObject:T(T({},e.queryObject),(null==o?void 0:o.keys.length)&&{params:u(o.patterns,o.keys)})})}))])},t.matchPath=s,t.pickSameStore=w,Object.defineProperty(t,"__esModule",{value:!0})}));