Skip to content

Commit

Permalink
refactor: remove circular dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
posva committed Aug 26, 2019
1 parent fdf5756 commit 638278b
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 37 deletions.
55 changes: 19 additions & 36 deletions src/util/push-state.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,40 +2,24 @@

import { inBrowser } from './dom'
import { saveScrollPosition } from './scroll'
import { genStateKey, setStateKey, getStateKey } from './state-key'

export const supportsPushState =
inBrowser &&
(function () {
const ua = window.navigator.userAgent

if (
(ua.indexOf('Android 2.') !== -1 || ua.indexOf('Android 4.0') !== -1) &&
ua.indexOf('Mobile Safari') !== -1 &&
ua.indexOf('Chrome') === -1 &&
ua.indexOf('Windows Phone') === -1
) {
return false
}

export const supportsPushState = inBrowser && (function () {
const ua = window.navigator.userAgent

if (
(ua.indexOf('Android 2.') !== -1 || ua.indexOf('Android 4.0') !== -1) &&
ua.indexOf('Mobile Safari') !== -1 &&
ua.indexOf('Chrome') === -1 &&
ua.indexOf('Windows Phone') === -1
) {
return false
}

return window.history && 'pushState' in window.history
})()

// use User Timing api (if present) for more accurate key precision
const Time = inBrowser && window.performance && window.performance.now
? window.performance
: Date

let _key: string = genKey()

function genKey (): string {
return Time.now().toFixed(3)
}

export function getStateKey () {
return _key
}

export function setStateKey (key: string) {
_key = key
}
return window.history && 'pushState' in window.history
})()

export function pushState (url?: string, replace?: boolean) {
saveScrollPosition()
Expand All @@ -44,10 +28,9 @@ export function pushState (url?: string, replace?: boolean) {
const history = window.history
try {
if (replace) {
history.replaceState({ key: _key }, '', url)
history.replaceState({ key: getStateKey() }, '', url)
} else {
_key = genKey()
history.pushState({ key: _key }, '', url)
history.pushState({ key: setStateKey(genStateKey()) }, '', url)
}
} catch (e) {
window.location[replace ? 'replace' : 'assign'](url)
Expand Down
2 changes: 1 addition & 1 deletion src/util/scroll.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import type Router from '../index'
import { assert } from './warn'
import { getStateKey, setStateKey } from './push-state'
import { getStateKey, setStateKey } from './state-key'

const positionStore = Object.create(null)

Expand Down
22 changes: 22 additions & 0 deletions src/util/state-key.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/* @flow */
import { inBrowser } from './dom'

// use User Timing api (if present) for more accurate key precision
const Time =
inBrowser && window.performance && window.performance.now
? window.performance
: Date

export function genStateKey (): string {
return Time.now().toFixed(3)
}

let _key: string = genStateKey()

export function getStateKey () {
return _key
}

export function setStateKey (key: string) {
return (_key = key)
}

0 comments on commit 638278b

Please sign in to comment.