@umijs/history
Version:
Manage session history with JavaScript
3 lines (2 loc) • 5.38 kB
JavaScript
import{parse as t,stringify as e}from"query-string";var n;!function(t){t.Pop="POP",t.Push="PUSH",t.Replace="REPLACE"}(n||(n={}));function a(e={}){let{window:a=document.defaultView}=e,r=a.history;function i(){let{pathname:e,search:n,hash:i}=a.location,l=r.state||{},u=t(n)||{};return[l.idx,{pathname:e,search:n,query:u,hash:i,state:l.usr||null,key:l.key||"default"}]}let l=null;a.addEventListener("popstate",(function(){if(l)d.call(l),l=null;else{let t=n.Pop,[e,a]=i();if(d.length){if(null!=e){let n=p-e;n&&(l={action:t,location:a,retry(){x(-1*n)}},x(n))}}else P(t)}}));let f=n.Pop,[p,g]=i(),y=o(),d=o();function b(t){return"string"==typeof t?t:s(t)}function m(t,e=null){return Object.assign(Object.assign({pathname:g.pathname,hash:"",search:"",query:{}},"string"==typeof t?h(t):t),{state:e,key:c()})}function k(t,e){return[{usr:t.state,key:t.key,idx:e},b(t)]}function O(t,e,n){return!d.length||(d.call({action:t,location:e,retry:n}),!1)}function P(t){f=t,[p,g]=i(),y.call({action:f,location:g})}function x(t){r.go(t)}return null==p&&(p=0,r.replaceState(Object.assign(Object.assign({},r.state),{idx:p}),"")),{get action(){return f},get location(){return g},createHref:b,push:function t(e,i){let l=n.Push,u=m(e,i);if(O(l,u,(function(){t(e,i)}))){let[t,e]=k(u,p+1);try{r.pushState(t,"",e)}catch(o){a.location.assign(e)}P(l)}},replace:function t(e,a){let i=n.Replace,l=m(e,a);if(O(i,l,(function(){t(e,a)}))){let[t,e]=k(l,p);r.replaceState(t,"",e),P(i)}},go:x,back(){x(-1)},forward(){x(1)},listen:t=>y.push(t),block(t){let e=d.push(t);return 1===d.length&&a.addEventListener("beforeunload",u),function(){e(),d.length||a.removeEventListener("beforeunload",u)}}}}function r(e={}){let{window:a=document.defaultView}=e,r=a.history;function i(){let{pathname:e="/",search:n="",hash:i=""}=h(a.location.hash.substr(1)),l=r.state||{};return[l.idx,{pathname:e,search:n,query:t(n)||{},hash:i,state:l.usr||null,key:l.key||"default"}]}let l=null;function f(){if(l)b.call(l),l=null;else{let t=n.Pop,[e,a]=i();if(b.length){if(null!=e){let n=g-e;n&&(l={action:t,location:a,retry(){j(-1*n)}},j(n))}}else x(t)}}a.addEventListener("popstate",f),a.addEventListener("hashchange",(()=>{let[,t]=i();s(t)!==s(y)&&f()}));let p=n.Pop,[g,y]=i(),d=o(),b=o();function m(t){return function(){let t=document.querySelector("base"),e="";if(t&&t.getAttribute("href")){let t=a.location.href,n=t.indexOf("#");e=-1===n?t:t.slice(0,n)}return e}()+"#"+("string"==typeof t?t:s(t))}function k(t,e=null){return Object.assign(Object.assign({pathname:y.pathname,hash:"",search:"",query:{}},"string"==typeof t?h(t):t),{state:e,key:c()})}function O(t,e){return[{usr:t.state,key:t.key,idx:e},m(t)]}function P(t,e,n){return!b.length||(b.call({action:t,location:e,retry:n}),!1)}function x(t){p=t,[g,y]=i(),d.call({action:p,location:y})}function j(t){r.go(t)}return null==g&&(g=0,r.replaceState(Object.assign(Object.assign({},r.state),{idx:g}),"")),{get action(){return p},get location(){return y},createHref:m,push:function t(e,i){let l=n.Push,u=k(e,i);if(P(l,u,(function(){t(e,i)}))){let[t,e]=O(u,g+1);try{r.pushState(t,"",e)}catch(o){a.location.assign(e)}x(l)}},replace:function t(e,a){let i=n.Replace,l=k(e,a);if(P(i,l,(function(){t(e,a)}))){let[t,e]=O(l,g);r.replaceState(t,"",e),x(i)}},go:j,back(){j(-1)},forward(){j(1)},listen:t=>d.push(t),block(t){let e=b.push(t);return 1===b.length&&a.addEventListener("beforeunload",u),function(){e(),b.length||a.removeEventListener("beforeunload",u)}}}}function i(t={}){let{initialEntries:e=["/"],initialIndex:a}=t,r=e.map((t=>Object.assign({pathname:"/",search:"",hash:"",query:{},state:null,key:c()},"string"==typeof t?h(t):t))),i=l(null==a?r.length-1:a,0,r.length-1),u=n.Pop,f=r[i],p=o(),g=o();function y(t,e=null){return Object.assign(Object.assign({pathname:f.pathname,search:"",query:{},hash:""},"string"==typeof t?h(t):t),{state:e,key:c()})}function d(t,e,n){return!g.length||(g.call({action:t,location:e,retry:n}),!1)}function b(t,e){u=t,f=e,p.call({action:u,location:f})}function m(t){let e=l(i+t,0,r.length-1),a=n.Pop,u=r[e];d(a,u,(function(){m(t)}))&&(i=e,b(a,u))}return{get index(){return i},get action(){return u},get location(){return f},createHref:function(t){return"string"==typeof t?t:s(t)},push:function t(e,a){let l=n.Push,u=y(e,a);d(l,u,(function(){t(e,a)}))&&(i+=1,r.splice(i,r.length,u),b(l,u))},replace:function t(e,a){let l=n.Replace,u=y(e,a);d(l,u,(function(){t(e,a)}))&&(r[i]=u,b(l,u))},go:m,back(){m(-1)},forward(){m(1)},listen:t=>p.push(t),block:t=>g.push(t)}}function l(t,e,n){return Math.min(Math.max(t,e),n)}function u(t){t.preventDefault(),t.returnValue=""}function o(){let t=[];return{get length(){return t.length},push:e=>(t.push(e),function(){t=t.filter((t=>t!==e))}),call(e){t.forEach((t=>t&&t(e)))}}}function c(){return Math.random().toString(36).substr(2,8)}function s({pathname:t="/",search:n="",query:a,hash:r=""}){if(n&&"?"!==n)t+="?"===n.charAt(0)?n:"?"+n;else if(a){const n=e(a);n&&(t+="?"+n)}return r&&"#"!==r&&(t+="#"===r.charAt(0)?r:"#"+r),t}function h(e){let n={};if(e){let a=e.indexOf("#");a>=0&&(n.hash=e.substr(a),e=e.substr(0,a));let r=e.indexOf("?");r>=0&&(n.search=e.substr(r),n.query=n.query||t(n.search||"")||{},e=e.substr(0,r)),e&&(n.pathname=e)}return n}export{n as Action,a as createBrowserHistory,r as createHashHistory,i as createMemoryHistory,s as createPath,h as parsePath};
//# sourceMappingURL=history.production.min.js.map