UNPKG

vjsrouter

Version:

A modern, file-system based router for vanilla JavaScript with SSR support.

3 lines (2 loc) 1.43 kB
import{a as t,l as e,b as a,c as n}from"./Logger-79661028.js";var o=new WeakMap;class r{constructor(){t(this,o,null),this.handlePopState=this.handlePopState.bind(this)}start(){e.debug("HistoryManager.start","Attaching popstate event listener to the window."),window.addEventListener("popstate",this.handlePopState)}stop(){e.debug("HistoryManager.stop","Removing popstate event listener from the window."),window.removeEventListener("popstate",this.handlePopState)}onURLChange(t){var n="HistoryManager.onURLChange";"function"==typeof t?(a(o,this,t),e.debug(n,"URL change callback has been registered.")):e.error(n,"Provided callback is not a function. Cannot register.")}push(t){var a=arguments.length>1&&void 0!==arguments[1]?arguments[1]:"",n="HistoryManager.push";"string"==typeof t?t!==this.getCurrentPath()?(e.info(n,'Pushing new state to history: "'.concat(t,'".')),window.history.pushState({},a,t)):e.debug(n,"Path is the same as the current one. Skipping pushState.",{path:t}):e.error(n,"Invalid path provided. Must be a string.",{path:t})}handlePopState(t){var a="HistoryManager.handlePopState",r=this.getCurrentPath();e.info(a,"Popstate event detected. URL changed to:",{newPath:r,event:t}),n(o,this)?n(o,this).call(this,r):e.warn(a,"Popstate event occurred, but no URL change callback is registered.")}getCurrentPath(){return window.location.pathname}}export{r as HistoryManager}; //# sourceMappingURL=HistoryManager.js.map