atomic-router
Advanced tools
Comparing version 0.1.5 to 0.2.0-dev.0
@@ -8,3 +8,2 @@ 'use strict'; | ||
var qs = require('qs'); | ||
var history = require('history'); | ||
@@ -958,2 +957,6 @@ function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { | ||
var historyPushFx = /*#__PURE__*/effector.createEffect(function (pushParams) { | ||
if (!pushParams.history) { | ||
throw new Error('[Routing] No history provided'); | ||
} | ||
pushParams.history[pushParams.method](pushParams.path, {}); | ||
@@ -963,9 +966,17 @@ return pushParams; | ||
var createHistoryRouter = function createHistoryRouter(params) { | ||
var actualHistory = params.history || history.createBrowserHistory(); // historyPushFx for the current history | ||
var setHistory = effector.createEvent(); // @ts-expect-error | ||
var $history = effector.createStore(null).on(setHistory, function (_, nextHistory) { | ||
return nextHistory; | ||
}); // historyPushFx for the current history | ||
var pushFx = effector.attach({ | ||
source: { | ||
history: $history | ||
}, | ||
effect: historyPushFx, | ||
mapParams: function mapParams(params) { | ||
mapParams: function mapParams(params, _ref) { | ||
var history = _ref.history; | ||
return _extends({ | ||
history: actualHistory | ||
history: history | ||
}, params); | ||
@@ -975,6 +986,6 @@ } | ||
var enteredFx = effector.createEffect(function (_ref) { | ||
var route = _ref.route, | ||
params = _ref.params, | ||
query = _ref.query; | ||
var enteredFx = effector.createEffect(function (_ref2) { | ||
var route = _ref2.route, | ||
params = _ref2.params, | ||
query = _ref2.query; | ||
var path = buildPath({ | ||
@@ -993,5 +1004,5 @@ pathCreator: route.path, | ||
var recheckFx = effector.createEffect(function (_ref2) { | ||
var path = _ref2.path, | ||
query = _ref2.query; | ||
var recheckFx = effector.createEffect(function (_ref3) { | ||
var path = _ref3.path, | ||
query = _ref3.query; | ||
var entered = []; | ||
@@ -1030,8 +1041,8 @@ var left = []; | ||
}); | ||
var routesEntered = recheckFx.doneData.map(function (_ref3) { | ||
var entered = _ref3.entered; | ||
var routesEntered = recheckFx.doneData.map(function (_ref4) { | ||
var entered = _ref4.entered; | ||
return entered; | ||
}); | ||
var routesLeft = recheckFx.doneData.map(function (_ref4) { | ||
var left = _ref4.left; | ||
var routesLeft = recheckFx.doneData.map(function (_ref5) { | ||
var left = _ref5.left; | ||
return left; | ||
@@ -1045,5 +1056,5 @@ }); | ||
clock: routeObj.route.navigate.doneData, | ||
fn: function fn(_ref6) { | ||
var params = _ref6.params, | ||
query = _ref6.query; | ||
fn: function fn(_ref9) { | ||
var params = _ref9.params, | ||
query = _ref9.query; | ||
return { | ||
@@ -1083,17 +1094,85 @@ route: routeObj, | ||
_loop(); | ||
} | ||
} // Takes current path from history and triggers recalculate | ||
var recheck = function recheck() { | ||
var _ref5 = [actualHistory.location.pathname, qs.parse(actualHistory.location.search.slice(1))], | ||
path = _ref5[0], | ||
query = _ref5[1]; | ||
recheckFx({ | ||
path: path, | ||
query: query | ||
}); | ||
}; | ||
actualHistory.listen(recheck); | ||
recheck(); | ||
var recheck = effector.attach({ | ||
source: { | ||
history: $history | ||
}, | ||
effect: function () { | ||
var _effect = _asyncToGenerator( /*#__PURE__*/runtime_1.mark(function _callee(_ref6) { | ||
var history, _ref7, path, query; | ||
return runtime_1.wrap(function _callee$(_context) { | ||
while (1) { | ||
switch (_context.prev = _context.next) { | ||
case 0: | ||
history = _ref6.history; | ||
_ref7 = [history.location.pathname, qs.parse(history.location.search.slice(1))], path = _ref7[0], query = _ref7[1]; | ||
return _context.abrupt("return", { | ||
path: path, | ||
query: query | ||
}); | ||
case 3: | ||
case "end": | ||
return _context.stop(); | ||
} | ||
} | ||
}, _callee); | ||
})); | ||
function effect(_x) { | ||
return _effect.apply(this, arguments); | ||
} | ||
return effect; | ||
}() | ||
}); | ||
effector.sample({ | ||
source: recheck.doneData, | ||
target: recheckFx | ||
}); // Triggered whenever history is changed | ||
var subscribeHistory = effector.attach({ | ||
source: { | ||
history: $history | ||
}, | ||
effect: function () { | ||
var _effect2 = _asyncToGenerator( /*#__PURE__*/runtime_1.mark(function _callee2(_ref8) { | ||
var history; | ||
return runtime_1.wrap(function _callee2$(_context2) { | ||
while (1) { | ||
switch (_context2.prev = _context2.next) { | ||
case 0: | ||
history = _ref8.history; | ||
history.listen(function () { | ||
return recheck(); | ||
}); | ||
recheck(); | ||
return _context2.abrupt("return", true); | ||
case 4: | ||
case "end": | ||
return _context2.stop(); | ||
} | ||
} | ||
}, _callee2); | ||
})); | ||
function effect(_x2) { | ||
return _effect2.apply(this, arguments); | ||
} | ||
return effect; | ||
}() | ||
}); | ||
effector.sample({ | ||
clock: $history, | ||
target: subscribeHistory | ||
}); | ||
return { | ||
$history: $history, | ||
setHistory: setHistory, | ||
push: pushFx, | ||
routes: params.routes | ||
@@ -1100,0 +1179,0 @@ }; |
@@ -1,2 +0,2 @@ | ||
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var t=require("effector"),r=require("path-to-regexp"),e=require("qs"),n=require("history");function o(t,r,e,n,o,a,i){try{var u=t[a](i),c=u.value}catch(t){return void e(t)}u.done?r(c):Promise.resolve(c).then(n,o)}function a(){return(a=Object.assign||function(t){for(var r=1;r<arguments.length;r++){var e=arguments[r];for(var n in e)Object.prototype.hasOwnProperty.call(e,n)&&(t[n]=e[n])}return t}).apply(this,arguments)}function i(t,r){(null==r||r>t.length)&&(r=t.length);for(var e=0,n=new Array(r);e<r;e++)n[e]=t[e];return n}function u(t,r){var e="undefined"!=typeof Symbol&&t[Symbol.iterator]||t["@@iterator"];if(e)return(e=e.call(t)).next.bind(e);if(Array.isArray(t)||(e=function(t,r){if(t){if("string"==typeof t)return i(t,void 0);var e=Object.prototype.toString.call(t).slice(8,-1);return"Object"===e&&t.constructor&&(e=t.constructor.name),"Map"===e||"Set"===e?Array.from(t):"Arguments"===e||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(e)?i(t,void 0):void 0}}(t))||r&&t&&"number"==typeof t.length){e&&(t=e);var n=0;return function(){return n>=t.length?{done:!0}:{done:!1,value:t[n++]}}}throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}var c,f=(function(t){var r=function(t){var r=Object.prototype,e=r.hasOwnProperty,n="function"==typeof Symbol?Symbol:{},o=n.iterator||"@@iterator",a=n.asyncIterator||"@@asyncIterator",i=n.toStringTag||"@@toStringTag";function u(t,r,e){return Object.defineProperty(t,r,{value:e,enumerable:!0,configurable:!0,writable:!0}),t[r]}try{u({},"")}catch(t){u=function(t,r,e){return t[r]=e}}function c(t,r,e,n){var o=Object.create((r&&r.prototype instanceof h?r:h).prototype),a=new L(n||[]);return o._invoke=function(t,r,e){var n="suspendedStart";return function(o,a){if("executing"===n)throw new Error("Generator is already running");if("completed"===n){if("throw"===o)throw a;return{value:void 0,done:!0}}for(e.method=o,e.arg=a;;){var i=e.delegate;if(i){var u=b(i,e);if(u){if(u===s)continue;return u}}if("next"===e.method)e.sent=e._sent=e.arg;else if("throw"===e.method){if("suspendedStart"===n)throw n="completed",e.arg;e.dispatchException(e.arg)}else"return"===e.method&&e.abrupt("return",e.arg);n="executing";var c=f(t,r,e);if("normal"===c.type){if(n=e.done?"completed":"suspendedYield",c.arg===s)continue;return{value:c.arg,done:e.done}}"throw"===c.type&&(n="completed",e.method="throw",e.arg=c.arg)}}}(t,e,a),o}function f(t,r,e){try{return{type:"normal",arg:t.call(r,e)}}catch(t){return{type:"throw",arg:t}}}t.wrap=c;var s={};function h(){}function l(){}function p(){}var v={};u(v,o,(function(){return this}));var y=Object.getPrototypeOf,d=y&&y(y(O([])));d&&d!==r&&e.call(d,o)&&(v=d);var m=p.prototype=h.prototype=Object.create(v);function g(t){["next","throw","return"].forEach((function(r){u(t,r,(function(t){return this._invoke(r,t)}))}))}function w(t,r){var n;this._invoke=function(o,a){function i(){return new r((function(n,i){!function n(o,a,i,u){var c=f(t[o],t,a);if("throw"!==c.type){var s=c.arg,h=s.value;return h&&"object"==typeof h&&e.call(h,"__await")?r.resolve(h.__await).then((function(t){n("next",t,i,u)}),(function(t){n("throw",t,i,u)})):r.resolve(h).then((function(t){s.value=t,i(s)}),(function(t){return n("throw",t,i,u)}))}u(c.arg)}(o,a,n,i)}))}return n=n?n.then(i,i):i()}}function b(t,r){var e=t.iterator[r.method];if(void 0===e){if(r.delegate=null,"throw"===r.method){if(t.iterator.return&&(r.method="return",r.arg=void 0,b(t,r),"throw"===r.method))return s;r.method="throw",r.arg=new TypeError("The iterator does not provide a 'throw' method")}return s}var n=f(e,t.iterator,r.arg);if("throw"===n.type)return r.method="throw",r.arg=n.arg,r.delegate=null,s;var o=n.arg;return o?o.done?(r[t.resultName]=o.value,r.next=t.nextLoc,"return"!==r.method&&(r.method="next",r.arg=void 0),r.delegate=null,s):o:(r.method="throw",r.arg=new TypeError("iterator result is not an object"),r.delegate=null,s)}function x(t){var r={tryLoc:t[0]};1 in t&&(r.catchLoc=t[1]),2 in t&&(r.finallyLoc=t[2],r.afterLoc=t[3]),this.tryEntries.push(r)}function E(t){var r=t.completion||{};r.type="normal",delete r.arg,t.completion=r}function L(t){this.tryEntries=[{tryLoc:"root"}],t.forEach(x,this),this.reset(!0)}function O(t){if(t){var r=t[o];if(r)return r.call(t);if("function"==typeof t.next)return t;if(!isNaN(t.length)){var n=-1,a=function r(){for(;++n<t.length;)if(e.call(t,n))return r.value=t[n],r.done=!1,r;return r.value=void 0,r.done=!0,r};return a.next=a}}return{next:j}}function j(){return{value:void 0,done:!0}}return l.prototype=p,u(m,"constructor",p),u(p,"constructor",l),l.displayName=u(p,i,"GeneratorFunction"),t.isGeneratorFunction=function(t){var r="function"==typeof t&&t.constructor;return!!r&&(r===l||"GeneratorFunction"===(r.displayName||r.name))},t.mark=function(t){return Object.setPrototypeOf?Object.setPrototypeOf(t,p):(t.__proto__=p,u(t,i,"GeneratorFunction")),t.prototype=Object.create(m),t},t.awrap=function(t){return{__await:t}},g(w.prototype),u(w.prototype,a,(function(){return this})),t.AsyncIterator=w,t.async=function(r,e,n,o,a){void 0===a&&(a=Promise);var i=new w(c(r,e,n,o),a);return t.isGeneratorFunction(e)?i:i.next().then((function(t){return t.done?t.value:i.next()}))},g(m),u(m,i,"Generator"),u(m,o,(function(){return this})),u(m,"toString",(function(){return"[object Generator]"})),t.keys=function(t){var r=[];for(var e in t)r.push(e);return r.reverse(),function e(){for(;r.length;){var n=r.pop();if(n in t)return e.value=n,e.done=!1,e}return e.done=!0,e}},t.values=O,L.prototype={constructor:L,reset:function(t){if(this.prev=0,this.next=0,this.sent=this._sent=void 0,this.done=!1,this.delegate=null,this.method="next",this.arg=void 0,this.tryEntries.forEach(E),!t)for(var r in this)"t"===r.charAt(0)&&e.call(this,r)&&!isNaN(+r.slice(1))&&(this[r]=void 0)},stop:function(){this.done=!0;var t=this.tryEntries[0].completion;if("throw"===t.type)throw t.arg;return this.rval},dispatchException:function(t){if(this.done)throw t;var r=this;function n(e,n){return i.type="throw",i.arg=t,r.next=e,n&&(r.method="next",r.arg=void 0),!!n}for(var o=this.tryEntries.length-1;o>=0;--o){var a=this.tryEntries[o],i=a.completion;if("root"===a.tryLoc)return n("end");if(a.tryLoc<=this.prev){var u=e.call(a,"catchLoc"),c=e.call(a,"finallyLoc");if(u&&c){if(this.prev<a.catchLoc)return n(a.catchLoc,!0);if(this.prev<a.finallyLoc)return n(a.finallyLoc)}else if(u){if(this.prev<a.catchLoc)return n(a.catchLoc,!0)}else{if(!c)throw new Error("try statement without catch or finally");if(this.prev<a.finallyLoc)return n(a.finallyLoc)}}}},abrupt:function(t,r){for(var n=this.tryEntries.length-1;n>=0;--n){var o=this.tryEntries[n];if(o.tryLoc<=this.prev&&e.call(o,"finallyLoc")&&this.prev<o.finallyLoc){var a=o;break}}a&&("break"===t||"continue"===t)&&a.tryLoc<=r&&r<=a.finallyLoc&&(a=null);var i=a?a.completion:{};return i.type=t,i.arg=r,a?(this.method="next",this.next=a.finallyLoc,s):this.complete(i)},complete:function(t,r){if("throw"===t.type)throw t.arg;return"break"===t.type||"continue"===t.type?this.next=t.arg:"return"===t.type?(this.rval=this.arg=t.arg,this.method="return",this.next="end"):"normal"===t.type&&r&&(this.next=r),s},finish:function(t){for(var r=this.tryEntries.length-1;r>=0;--r){var e=this.tryEntries[r];if(e.finallyLoc===t)return this.complete(e.completion,e.afterLoc),E(e),s}},catch:function(t){for(var r=this.tryEntries.length-1;r>=0;--r){var e=this.tryEntries[r];if(e.tryLoc===t){var n=e.completion;if("throw"===n.type){var o=n.arg;E(e)}return o}}throw new Error("illegal catch attempt")},delegateYield:function(t,r,e){return this.delegate={iterator:O(t),resultName:r,nextLoc:e},"next"===this.method&&(this.arg=void 0),s}},t}(t.exports);try{regeneratorRuntime=r}catch(t){"object"==typeof globalThis?globalThis.regeneratorRuntime=r:Function("r","regeneratorRuntime = r")(r)}}(c={exports:{}}),c.exports);function s(t){var e=t.params,n=t.query;return r.compile(t.pathCreator)(e)+(Object.keys(n).length?"?"+new URLSearchParams(n):"")}function h(t){var e=t.actualPath,n=r.match(t.pathCreator)(e);return n?{matches:!0,params:n.params}:{matches:!1}}var l=t.createEffect((function(t){return t.history[t.method](t.path,{}),t}));exports.buildPath=s,exports.createHistoryRouter=function(r){var o=r.history||n.createBrowserHistory(),i=t.attach({effect:l,mapParams:function(t){return a({history:o},t)}}),c=t.createEffect((function(t){var r=t.params,e=t.query;return{path:s({pathCreator:t.route.path,params:r,query:e}),params:r,query:e,method:"push"}})),f=t.createEffect((function(t){for(var e,n=t.path,o=t.query,a=[],i=[],c=u(r.routes);!(e=c()).done;){var f=e.value,s=h({pathCreator:f.path,actualPath:n});(s.matches?a:i).push({route:f,params:s.params,query:o})}return{entered:a,left:i}}));t.sample({clock:c.doneData,target:i}),t.sample({clock:i.doneData,target:f});for(var p,v=f.doneData.map((function(t){return t.entered})),y=f.doneData.map((function(t){return t.left})),d=function(){var r=p.value;t.sample({clock:r.route.navigate.doneData,fn:function(t){return{route:r,params:t.params,query:t.query}},target:c}),t.guard({clock:v.filterMap((function(t){return t.find((function(t){return t.route===r}))})),filter:r.route.$isOpened.map((function(t){return!t})),target:r.route.opened}),t.guard({clock:y.filterMap((function(t){return t.find((function(t){return t.route===r}))})),filter:r.route.$isOpened,target:r.route.left})},m=u(r.routes);!(p=m()).done;)d();var g=function(){var t=[o.location.pathname,e.parse(o.location.search.slice(1))];f({path:t[0],query:t[1]})};return o.listen(g),g(),{routes:r.routes}},exports.createRoute=function(){var r=t.createEffect(function(){var t,r=(t=f.mark((function t(r){return f.wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.abrupt("return",{params:r.params||{},query:r.query||{}});case 2:case"end":return t.stop()}}),t)})),function(){var r=this,e=arguments;return new Promise((function(n,a){var i=t.apply(r,e);function u(t){o(i,n,a,u,c,"next",t)}function c(t){o(i,n,a,u,c,"throw",t)}u(void 0)}))});return function(t){return r.apply(this,arguments)}}()),e=t.attach({effect:r,mapParams:function(t){return{params:t||{},query:{}}}}),n=t.createStore(!1),a=t.createStore({}),i=t.createStore({}),u=t.createEvent(),c=t.createEvent();return n.on(u,(function(){return!0})).on(c,(function(){return!1})),a.on(u,(function(t,r){return r.params})),i.on(u,(function(t,r){return r.query})),t.sample({clock:r.doneData,target:u}),{$isOpened:n,$params:a,$query:i,opened:u,left:c,navigate:r,open:e}},exports.matchPath=h; | ||
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var t=require("effector"),r=require("path-to-regexp"),e=require("qs");function n(t,r,e,n,o,a,i){try{var u=t[a](i),c=u.value}catch(t){return void e(t)}u.done?r(c):Promise.resolve(c).then(n,o)}function o(t){return function(){var r=this,e=arguments;return new Promise((function(o,a){var i=t.apply(r,e);function u(t){n(i,o,a,u,c,"next",t)}function c(t){n(i,o,a,u,c,"throw",t)}u(void 0)}))}}function a(){return(a=Object.assign||function(t){for(var r=1;r<arguments.length;r++){var e=arguments[r];for(var n in e)Object.prototype.hasOwnProperty.call(e,n)&&(t[n]=e[n])}return t}).apply(this,arguments)}function i(t,r){(null==r||r>t.length)&&(r=t.length);for(var e=0,n=new Array(r);e<r;e++)n[e]=t[e];return n}function u(t,r){var e="undefined"!=typeof Symbol&&t[Symbol.iterator]||t["@@iterator"];if(e)return(e=e.call(t)).next.bind(e);if(Array.isArray(t)||(e=function(t,r){if(t){if("string"==typeof t)return i(t,void 0);var e=Object.prototype.toString.call(t).slice(8,-1);return"Object"===e&&t.constructor&&(e=t.constructor.name),"Map"===e||"Set"===e?Array.from(t):"Arguments"===e||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(e)?i(t,void 0):void 0}}(t))||r&&t&&"number"==typeof t.length){e&&(t=e);var n=0;return function(){return n>=t.length?{done:!0}:{done:!1,value:t[n++]}}}throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}var c,s=(function(t){var r=function(t){var r=Object.prototype,e=r.hasOwnProperty,n="function"==typeof Symbol?Symbol:{},o=n.iterator||"@@iterator",a=n.asyncIterator||"@@asyncIterator",i=n.toStringTag||"@@toStringTag";function u(t,r,e){return Object.defineProperty(t,r,{value:e,enumerable:!0,configurable:!0,writable:!0}),t[r]}try{u({},"")}catch(t){u=function(t,r,e){return t[r]=e}}function c(t,r,e,n){var o=Object.create((r&&r.prototype instanceof h?r:h).prototype),a=new L(n||[]);return o._invoke=function(t,r,e){var n="suspendedStart";return function(o,a){if("executing"===n)throw new Error("Generator is already running");if("completed"===n){if("throw"===o)throw a;return{value:void 0,done:!0}}for(e.method=o,e.arg=a;;){var i=e.delegate;if(i){var u=b(i,e);if(u){if(u===f)continue;return u}}if("next"===e.method)e.sent=e._sent=e.arg;else if("throw"===e.method){if("suspendedStart"===n)throw n="completed",e.arg;e.dispatchException(e.arg)}else"return"===e.method&&e.abrupt("return",e.arg);n="executing";var c=s(t,r,e);if("normal"===c.type){if(n=e.done?"completed":"suspendedYield",c.arg===f)continue;return{value:c.arg,done:e.done}}"throw"===c.type&&(n="completed",e.method="throw",e.arg=c.arg)}}}(t,e,a),o}function s(t,r,e){try{return{type:"normal",arg:t.call(r,e)}}catch(t){return{type:"throw",arg:t}}}t.wrap=c;var f={};function h(){}function l(){}function p(){}var y={};u(y,o,(function(){return this}));var v=Object.getPrototypeOf,d=v&&v(v(O([])));d&&d!==r&&e.call(d,o)&&(y=d);var m=p.prototype=h.prototype=Object.create(y);function g(t){["next","throw","return"].forEach((function(r){u(t,r,(function(t){return this._invoke(r,t)}))}))}function w(t,r){var n;this._invoke=function(o,a){function i(){return new r((function(n,i){!function n(o,a,i,u){var c=s(t[o],t,a);if("throw"!==c.type){var f=c.arg,h=f.value;return h&&"object"==typeof h&&e.call(h,"__await")?r.resolve(h.__await).then((function(t){n("next",t,i,u)}),(function(t){n("throw",t,i,u)})):r.resolve(h).then((function(t){f.value=t,i(f)}),(function(t){return n("throw",t,i,u)}))}u(c.arg)}(o,a,n,i)}))}return n=n?n.then(i,i):i()}}function b(t,r){var e=t.iterator[r.method];if(void 0===e){if(r.delegate=null,"throw"===r.method){if(t.iterator.return&&(r.method="return",r.arg=void 0,b(t,r),"throw"===r.method))return f;r.method="throw",r.arg=new TypeError("The iterator does not provide a 'throw' method")}return f}var n=s(e,t.iterator,r.arg);if("throw"===n.type)return r.method="throw",r.arg=n.arg,r.delegate=null,f;var o=n.arg;return o?o.done?(r[t.resultName]=o.value,r.next=t.nextLoc,"return"!==r.method&&(r.method="next",r.arg=void 0),r.delegate=null,f):o:(r.method="throw",r.arg=new TypeError("iterator result is not an object"),r.delegate=null,f)}function x(t){var r={tryLoc:t[0]};1 in t&&(r.catchLoc=t[1]),2 in t&&(r.finallyLoc=t[2],r.afterLoc=t[3]),this.tryEntries.push(r)}function E(t){var r=t.completion||{};r.type="normal",delete r.arg,t.completion=r}function L(t){this.tryEntries=[{tryLoc:"root"}],t.forEach(x,this),this.reset(!0)}function O(t){if(t){var r=t[o];if(r)return r.call(t);if("function"==typeof t.next)return t;if(!isNaN(t.length)){var n=-1,a=function r(){for(;++n<t.length;)if(e.call(t,n))return r.value=t[n],r.done=!1,r;return r.value=void 0,r.done=!0,r};return a.next=a}}return{next:j}}function j(){return{value:void 0,done:!0}}return l.prototype=p,u(m,"constructor",p),u(p,"constructor",l),l.displayName=u(p,i,"GeneratorFunction"),t.isGeneratorFunction=function(t){var r="function"==typeof t&&t.constructor;return!!r&&(r===l||"GeneratorFunction"===(r.displayName||r.name))},t.mark=function(t){return Object.setPrototypeOf?Object.setPrototypeOf(t,p):(t.__proto__=p,u(t,i,"GeneratorFunction")),t.prototype=Object.create(m),t},t.awrap=function(t){return{__await:t}},g(w.prototype),u(w.prototype,a,(function(){return this})),t.AsyncIterator=w,t.async=function(r,e,n,o,a){void 0===a&&(a=Promise);var i=new w(c(r,e,n,o),a);return t.isGeneratorFunction(e)?i:i.next().then((function(t){return t.done?t.value:i.next()}))},g(m),u(m,i,"Generator"),u(m,o,(function(){return this})),u(m,"toString",(function(){return"[object Generator]"})),t.keys=function(t){var r=[];for(var e in t)r.push(e);return r.reverse(),function e(){for(;r.length;){var n=r.pop();if(n in t)return e.value=n,e.done=!1,e}return e.done=!0,e}},t.values=O,L.prototype={constructor:L,reset:function(t){if(this.prev=0,this.next=0,this.sent=this._sent=void 0,this.done=!1,this.delegate=null,this.method="next",this.arg=void 0,this.tryEntries.forEach(E),!t)for(var r in this)"t"===r.charAt(0)&&e.call(this,r)&&!isNaN(+r.slice(1))&&(this[r]=void 0)},stop:function(){this.done=!0;var t=this.tryEntries[0].completion;if("throw"===t.type)throw t.arg;return this.rval},dispatchException:function(t){if(this.done)throw t;var r=this;function n(e,n){return i.type="throw",i.arg=t,r.next=e,n&&(r.method="next",r.arg=void 0),!!n}for(var o=this.tryEntries.length-1;o>=0;--o){var a=this.tryEntries[o],i=a.completion;if("root"===a.tryLoc)return n("end");if(a.tryLoc<=this.prev){var u=e.call(a,"catchLoc"),c=e.call(a,"finallyLoc");if(u&&c){if(this.prev<a.catchLoc)return n(a.catchLoc,!0);if(this.prev<a.finallyLoc)return n(a.finallyLoc)}else if(u){if(this.prev<a.catchLoc)return n(a.catchLoc,!0)}else{if(!c)throw new Error("try statement without catch or finally");if(this.prev<a.finallyLoc)return n(a.finallyLoc)}}}},abrupt:function(t,r){for(var n=this.tryEntries.length-1;n>=0;--n){var o=this.tryEntries[n];if(o.tryLoc<=this.prev&&e.call(o,"finallyLoc")&&this.prev<o.finallyLoc){var a=o;break}}a&&("break"===t||"continue"===t)&&a.tryLoc<=r&&r<=a.finallyLoc&&(a=null);var i=a?a.completion:{};return i.type=t,i.arg=r,a?(this.method="next",this.next=a.finallyLoc,f):this.complete(i)},complete:function(t,r){if("throw"===t.type)throw t.arg;return"break"===t.type||"continue"===t.type?this.next=t.arg:"return"===t.type?(this.rval=this.arg=t.arg,this.method="return",this.next="end"):"normal"===t.type&&r&&(this.next=r),f},finish:function(t){for(var r=this.tryEntries.length-1;r>=0;--r){var e=this.tryEntries[r];if(e.finallyLoc===t)return this.complete(e.completion,e.afterLoc),E(e),f}},catch:function(t){for(var r=this.tryEntries.length-1;r>=0;--r){var e=this.tryEntries[r];if(e.tryLoc===t){var n=e.completion;if("throw"===n.type){var o=n.arg;E(e)}return o}}throw new Error("illegal catch attempt")},delegateYield:function(t,r,e){return this.delegate={iterator:O(t),resultName:r,nextLoc:e},"next"===this.method&&(this.arg=void 0),f}},t}(t.exports);try{regeneratorRuntime=r}catch(t){"object"==typeof globalThis?globalThis.regeneratorRuntime=r:Function("r","regeneratorRuntime = r")(r)}}(c={exports:{}}),c.exports);function f(t){var e=t.params,n=t.query;return r.compile(t.pathCreator)(e)+(Object.keys(n).length?"?"+new URLSearchParams(n):"")}function h(t){var e=t.actualPath,n=r.match(t.pathCreator)(e);return n?{matches:!0,params:n.params}:{matches:!1}}var l=t.createEffect((function(t){if(!t.history)throw new Error("[Routing] No history provided");return t.history[t.method](t.path,{}),t}));exports.buildPath=f,exports.createHistoryRouter=function(r){var n=t.createEvent(),i=t.createStore(null).on(n,(function(t,r){return r})),c=t.attach({source:{history:i},effect:l,mapParams:function(t,r){return a({history:r.history},t)}}),p=t.createEffect((function(t){var r=t.params,e=t.query;return{path:f({pathCreator:t.route.path,params:r,query:e}),params:r,query:e,method:"push"}})),y=t.createEffect((function(t){for(var e,n=t.path,o=t.query,a=[],i=[],c=u(r.routes);!(e=c()).done;){var s=e.value,f=h({pathCreator:s.path,actualPath:n});(f.matches?a:i).push({route:s,params:f.params,query:o})}return{entered:a,left:i}}));t.sample({clock:p.doneData,target:c}),t.sample({clock:c.doneData,target:y});for(var v,d=y.doneData.map((function(t){return t.entered})),m=y.doneData.map((function(t){return t.left})),g=function(){var r=v.value;t.sample({clock:r.route.navigate.doneData,fn:function(t){return{route:r,params:t.params,query:t.query}},target:p}),t.guard({clock:d.filterMap((function(t){return t.find((function(t){return t.route===r}))})),filter:r.route.$isOpened.map((function(t){return!t})),target:r.route.opened}),t.guard({clock:m.filterMap((function(t){return t.find((function(t){return t.route===r}))})),filter:r.route.$isOpened,target:r.route.left})},w=u(r.routes);!(v=w()).done;)g();var b,x=t.attach({source:{history:i},effect:(b=o(s.mark((function t(r){var n,o;return s.wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return o=[(n=r.history).location.pathname,e.parse(n.location.search.slice(1))],t.abrupt("return",{path:o[0],query:o[1]});case 3:case"end":return t.stop()}}),t)}))),function(t){return b.apply(this,arguments)})});t.sample({source:x.doneData,target:y});var E,L=t.attach({source:{history:i},effect:(E=o(s.mark((function t(r){return s.wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r.history.listen((function(){return x()})),x(),t.abrupt("return",!0);case 4:case"end":return t.stop()}}),t)}))),function(t){return E.apply(this,arguments)})});return t.sample({clock:i,target:L}),{$history:i,setHistory:n,push:c,routes:r.routes}},exports.createRoute=function(){var r=t.createEffect(function(){var t=o(s.mark((function t(r){return s.wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.abrupt("return",{params:r.params||{},query:r.query||{}});case 2:case"end":return t.stop()}}),t)})));return function(r){return t.apply(this,arguments)}}()),e=t.attach({effect:r,mapParams:function(t){return{params:t||{},query:{}}}}),n=t.createStore(!1),a=t.createStore({}),i=t.createStore({}),u=t.createEvent(),c=t.createEvent();return n.on(u,(function(){return!0})).on(c,(function(){return!1})),a.on(u,(function(t,r){return r.params})),i.on(u,(function(t,r){return r.query})),t.sample({clock:r.doneData,target:u}),{$isOpened:n,$params:a,$query:i,opened:u,left:c,navigate:r,open:e}},exports.matchPath=h; | ||
//# sourceMappingURL=atomic-router.cjs.production.min.js.map |
import { createEffect, attach, createStore, createEvent, sample, guard } from 'effector'; | ||
import { compile, match } from 'path-to-regexp'; | ||
import { parse } from 'qs'; | ||
import { createBrowserHistory } from 'history'; | ||
@@ -953,2 +952,6 @@ function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { | ||
var historyPushFx = /*#__PURE__*/createEffect(function (pushParams) { | ||
if (!pushParams.history) { | ||
throw new Error('[Routing] No history provided'); | ||
} | ||
pushParams.history[pushParams.method](pushParams.path, {}); | ||
@@ -958,9 +961,17 @@ return pushParams; | ||
var createHistoryRouter = function createHistoryRouter(params) { | ||
var actualHistory = params.history || createBrowserHistory(); // historyPushFx for the current history | ||
var setHistory = createEvent(); // @ts-expect-error | ||
var $history = createStore(null).on(setHistory, function (_, nextHistory) { | ||
return nextHistory; | ||
}); // historyPushFx for the current history | ||
var pushFx = attach({ | ||
source: { | ||
history: $history | ||
}, | ||
effect: historyPushFx, | ||
mapParams: function mapParams(params) { | ||
mapParams: function mapParams(params, _ref) { | ||
var history = _ref.history; | ||
return _extends({ | ||
history: actualHistory | ||
history: history | ||
}, params); | ||
@@ -970,6 +981,6 @@ } | ||
var enteredFx = createEffect(function (_ref) { | ||
var route = _ref.route, | ||
params = _ref.params, | ||
query = _ref.query; | ||
var enteredFx = createEffect(function (_ref2) { | ||
var route = _ref2.route, | ||
params = _ref2.params, | ||
query = _ref2.query; | ||
var path = buildPath({ | ||
@@ -988,5 +999,5 @@ pathCreator: route.path, | ||
var recheckFx = createEffect(function (_ref2) { | ||
var path = _ref2.path, | ||
query = _ref2.query; | ||
var recheckFx = createEffect(function (_ref3) { | ||
var path = _ref3.path, | ||
query = _ref3.query; | ||
var entered = []; | ||
@@ -1025,8 +1036,8 @@ var left = []; | ||
}); | ||
var routesEntered = recheckFx.doneData.map(function (_ref3) { | ||
var entered = _ref3.entered; | ||
var routesEntered = recheckFx.doneData.map(function (_ref4) { | ||
var entered = _ref4.entered; | ||
return entered; | ||
}); | ||
var routesLeft = recheckFx.doneData.map(function (_ref4) { | ||
var left = _ref4.left; | ||
var routesLeft = recheckFx.doneData.map(function (_ref5) { | ||
var left = _ref5.left; | ||
return left; | ||
@@ -1040,5 +1051,5 @@ }); | ||
clock: routeObj.route.navigate.doneData, | ||
fn: function fn(_ref6) { | ||
var params = _ref6.params, | ||
query = _ref6.query; | ||
fn: function fn(_ref9) { | ||
var params = _ref9.params, | ||
query = _ref9.query; | ||
return { | ||
@@ -1078,17 +1089,85 @@ route: routeObj, | ||
_loop(); | ||
} | ||
} // Takes current path from history and triggers recalculate | ||
var recheck = function recheck() { | ||
var _ref5 = [actualHistory.location.pathname, parse(actualHistory.location.search.slice(1))], | ||
path = _ref5[0], | ||
query = _ref5[1]; | ||
recheckFx({ | ||
path: path, | ||
query: query | ||
}); | ||
}; | ||
actualHistory.listen(recheck); | ||
recheck(); | ||
var recheck = attach({ | ||
source: { | ||
history: $history | ||
}, | ||
effect: function () { | ||
var _effect = _asyncToGenerator( /*#__PURE__*/runtime_1.mark(function _callee(_ref6) { | ||
var history, _ref7, path, query; | ||
return runtime_1.wrap(function _callee$(_context) { | ||
while (1) { | ||
switch (_context.prev = _context.next) { | ||
case 0: | ||
history = _ref6.history; | ||
_ref7 = [history.location.pathname, parse(history.location.search.slice(1))], path = _ref7[0], query = _ref7[1]; | ||
return _context.abrupt("return", { | ||
path: path, | ||
query: query | ||
}); | ||
case 3: | ||
case "end": | ||
return _context.stop(); | ||
} | ||
} | ||
}, _callee); | ||
})); | ||
function effect(_x) { | ||
return _effect.apply(this, arguments); | ||
} | ||
return effect; | ||
}() | ||
}); | ||
sample({ | ||
source: recheck.doneData, | ||
target: recheckFx | ||
}); // Triggered whenever history is changed | ||
var subscribeHistory = attach({ | ||
source: { | ||
history: $history | ||
}, | ||
effect: function () { | ||
var _effect2 = _asyncToGenerator( /*#__PURE__*/runtime_1.mark(function _callee2(_ref8) { | ||
var history; | ||
return runtime_1.wrap(function _callee2$(_context2) { | ||
while (1) { | ||
switch (_context2.prev = _context2.next) { | ||
case 0: | ||
history = _ref8.history; | ||
history.listen(function () { | ||
return recheck(); | ||
}); | ||
recheck(); | ||
return _context2.abrupt("return", true); | ||
case 4: | ||
case "end": | ||
return _context2.stop(); | ||
} | ||
} | ||
}, _callee2); | ||
})); | ||
function effect(_x2) { | ||
return _effect2.apply(this, arguments); | ||
} | ||
return effect; | ||
}() | ||
}); | ||
sample({ | ||
clock: $history, | ||
target: subscribeHistory | ||
}); | ||
return { | ||
$history: $history, | ||
setHistory: setHistory, | ||
push: pushFx, | ||
routes: params.routes | ||
@@ -1095,0 +1174,0 @@ }; |
import { History } from 'history'; | ||
import { RouteInstance, RouteParams } from './types'; | ||
import { RouteInstance, RouteParams, RouteQuery } from './types'; | ||
declare type RouteObject<Params extends RouteParams> = { | ||
@@ -7,8 +7,17 @@ route: RouteInstance<Params>; | ||
}; | ||
declare type HistoryPushParams = { | ||
history: History; | ||
path: string; | ||
params: RouteParams; | ||
query: RouteQuery; | ||
method: 'replace' | 'push'; | ||
}; | ||
export declare const createHistoryRouter: (params: { | ||
history?: History; | ||
routes: RouteObject<any>[]; | ||
}) => { | ||
$history: import("effector").Store<History<import("history").State>>; | ||
setHistory: import("effector").Event<History<import("history").State>>; | ||
push: import("effector").Effect<Pick<HistoryPushParams, "path" | "params" | "query" | "method">, HistoryPushParams, Error>; | ||
routes: RouteObject<any>[]; | ||
}; | ||
export {}; |
{ | ||
"version": "0.1.5", | ||
"version": "0.2.0-dev.0", | ||
"license": "MIT", | ||
@@ -4,0 +4,0 @@ "main": "dist/index.js", |
import { parse } from 'qs'; | ||
import { buildPath, matchPath } from './build-path'; | ||
import { createBrowserHistory, History } from 'history'; | ||
import { attach, createEffect, guard, sample } from 'effector'; | ||
import { History } from 'history'; | ||
import { RouteInstance, RouteParams, RouteQuery } from './types'; | ||
import { | ||
attach, | ||
createEffect, | ||
guard, | ||
sample, | ||
createStore, | ||
createEvent, | ||
} from 'effector'; | ||
@@ -22,2 +29,5 @@ type RouteObject<Params extends RouteParams> = { | ||
pushParams => { | ||
if (!pushParams.history) { | ||
throw new Error('[Routing] No history provided'); | ||
} | ||
pushParams.history[pushParams.method](pushParams.path, {}); | ||
@@ -28,6 +38,3 @@ return pushParams; | ||
export const createHistoryRouter = (params: { | ||
history?: History; | ||
routes: RouteObject<any>[]; | ||
}) => { | ||
export const createHistoryRouter = (params: { routes: RouteObject<any>[] }) => { | ||
type PushParams = Omit<HistoryPushParams, 'history'>; | ||
@@ -45,10 +52,17 @@ type EnterParams<Params extends RouteParams> = { | ||
const actualHistory = params.history || createBrowserHistory(); | ||
const setHistory = createEvent<History>(); | ||
// @ts-expect-error | ||
const $history = createStore<History>(null).on( | ||
setHistory, | ||
(_, nextHistory) => nextHistory | ||
); | ||
// historyPushFx for the current history | ||
const pushFx = attach({ | ||
source: { history: $history }, | ||
effect: historyPushFx, | ||
mapParams: (params: PushParams) => { | ||
mapParams: (params: PushParams, { history }) => { | ||
return { | ||
history: actualHistory, | ||
history, | ||
...params, | ||
@@ -130,17 +144,40 @@ }; | ||
const recheck = () => { | ||
const [path, query] = [ | ||
actualHistory.location.pathname, | ||
parse(actualHistory.location.search.slice(1)) as RouteQuery, | ||
]; | ||
recheckFx({ path, query }); | ||
}; | ||
// Takes current path from history and triggers recalculate | ||
const recheck = attach({ | ||
source: { history: $history }, | ||
effect: async ({ history }) => { | ||
const [path, query] = [ | ||
history.location.pathname, | ||
parse(history.location.search.slice(1)) as RouteQuery, | ||
]; | ||
return { path, query }; | ||
}, | ||
}); | ||
actualHistory.listen(recheck); | ||
sample({ | ||
source: recheck.doneData, | ||
target: recheckFx, | ||
}); | ||
recheck(); | ||
// Triggered whenever history is changed | ||
const subscribeHistory = attach({ | ||
source: { history: $history }, | ||
effect: async ({ history }) => { | ||
history.listen(() => recheck()); | ||
recheck(); | ||
return true; | ||
}, | ||
}); | ||
sample({ | ||
clock: $history, | ||
target: subscribeHistory, | ||
}); | ||
return { | ||
$history, | ||
setHistory, | ||
push: pushFx, | ||
routes: params.routes, | ||
}; | ||
}; |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
263706
2397