-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
619ad26
commit dccc74d
Showing
3 changed files
with
109 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
import { PubSub } from 'parallel-universe'; | ||
import { toLocation } from '../utils'; | ||
import { History, SearchParamsAdapter } from './types'; | ||
import { Location } from '../types'; | ||
import { urlSearchParamsAdapter } from './urlSearchParamsAdapter'; | ||
import { parseURL, toURL } from './utils'; | ||
|
||
/** | ||
* Options of {@link createHashHistory}. | ||
*/ | ||
export interface HashHistoryOptions { | ||
/** | ||
* A default URL base used by {@link History.toURL}. | ||
*/ | ||
base?: URL | string; | ||
|
||
/** | ||
* An adapter that extracts params from a URL search string and stringifies them back. By default, an adapter that | ||
* relies on {@link !URLSearchParams} is used. | ||
*/ | ||
searchParamsAdapter?: SearchParamsAdapter; | ||
} | ||
|
||
/** | ||
* Create the history adapter that reads and writes location to a browser's session history. | ||
* | ||
* @param options History options. | ||
*/ | ||
export function createHashHistory(options: HashHistoryOptions = {}): History { | ||
const { base: defaultBase, searchParamsAdapter = urlSearchParamsAdapter } = options; | ||
const pubSub = new PubSub(); | ||
const handlePopstate = () => pubSub.publish(); | ||
|
||
let prevHref: string; | ||
let location: Location; | ||
|
||
return { | ||
get location() { | ||
const href = decodeURIComponent(window.location.hash.substring(1)); | ||
|
||
return prevHref === href ? location : (location = parseURL((prevHref = href), searchParamsAdapter)); | ||
}, | ||
|
||
toURL(location, base = defaultBase) { | ||
const url = '#' + encodeURIComponent(toURL(location, searchParamsAdapter)); | ||
|
||
return base === undefined ? url : new URL(url, base).toString(); | ||
}, | ||
|
||
push(to) { | ||
location = toLocation(to); | ||
history.pushState(location.state, '', '#' + encodeURIComponent(toURL(location, searchParamsAdapter))); | ||
pubSub.publish(); | ||
}, | ||
|
||
replace(to) { | ||
location = toLocation(to); | ||
history.replaceState(location.state, '', '#' + encodeURIComponent(toURL(location, searchParamsAdapter))); | ||
pubSub.publish(); | ||
}, | ||
|
||
back() { | ||
history.back(); | ||
}, | ||
|
||
subscribe(listener) { | ||
if (pubSub.listenerCount === 0) { | ||
window.addEventListener('popstate', handlePopstate); | ||
} | ||
|
||
const unsubscribe = pubSub.subscribe(listener); | ||
|
||
return () => { | ||
unsubscribe(); | ||
|
||
if (pubSub.listenerCount === 0) { | ||
window.removeEventListener('popstate', handlePopstate); | ||
} | ||
}; | ||
}, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters