UNPKG

@nimel/directorr-router

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