Skip to content

Commit 638278b

Browse files
committed
refactor: remove circular dependency
1 parent fdf5756 commit 638278b

File tree

3 files changed

+42
-37
lines changed

3 files changed

+42
-37
lines changed

src/util/push-state.js

+19-36
Original file line numberDiff line numberDiff line change
@@ -2,40 +2,24 @@
22

33
import { inBrowser } from './dom'
44
import { saveScrollPosition } from './scroll'
5+
import { genStateKey, setStateKey, getStateKey } from './state-key'
6+
7+
export const supportsPushState =
8+
inBrowser &&
9+
(function () {
10+
const ua = window.navigator.userAgent
11+
12+
if (
13+
(ua.indexOf('Android 2.') !== -1 || ua.indexOf('Android 4.0') !== -1) &&
14+
ua.indexOf('Mobile Safari') !== -1 &&
15+
ua.indexOf('Chrome') === -1 &&
16+
ua.indexOf('Windows Phone') === -1
17+
) {
18+
return false
19+
}
520

6-
export const supportsPushState = inBrowser && (function () {
7-
const ua = window.navigator.userAgent
8-
9-
if (
10-
(ua.indexOf('Android 2.') !== -1 || ua.indexOf('Android 4.0') !== -1) &&
11-
ua.indexOf('Mobile Safari') !== -1 &&
12-
ua.indexOf('Chrome') === -1 &&
13-
ua.indexOf('Windows Phone') === -1
14-
) {
15-
return false
16-
}
17-
18-
return window.history && 'pushState' in window.history
19-
})()
20-
21-
// use User Timing api (if present) for more accurate key precision
22-
const Time = inBrowser && window.performance && window.performance.now
23-
? window.performance
24-
: Date
25-
26-
let _key: string = genKey()
27-
28-
function genKey (): string {
29-
return Time.now().toFixed(3)
30-
}
31-
32-
export function getStateKey () {
33-
return _key
34-
}
35-
36-
export function setStateKey (key: string) {
37-
_key = key
38-
}
21+
return window.history && 'pushState' in window.history
22+
})()
3923

4024
export function pushState (url?: string, replace?: boolean) {
4125
saveScrollPosition()
@@ -44,10 +28,9 @@ export function pushState (url?: string, replace?: boolean) {
4428
const history = window.history
4529
try {
4630
if (replace) {
47-
history.replaceState({ key: _key }, '', url)
31+
history.replaceState({ key: getStateKey() }, '', url)
4832
} else {
49-
_key = genKey()
50-
history.pushState({ key: _key }, '', url)
33+
history.pushState({ key: setStateKey(genStateKey()) }, '', url)
5134
}
5235
} catch (e) {
5336
window.location[replace ? 'replace' : 'assign'](url)

src/util/scroll.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import type Router from '../index'
44
import { assert } from './warn'
5-
import { getStateKey, setStateKey } from './push-state'
5+
import { getStateKey, setStateKey } from './state-key'
66

77
const positionStore = Object.create(null)
88

src/util/state-key.js

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/* @flow */
2+
import { inBrowser } from './dom'
3+
4+
// use User Timing api (if present) for more accurate key precision
5+
const Time =
6+
inBrowser && window.performance && window.performance.now
7+
? window.performance
8+
: Date
9+
10+
export function genStateKey (): string {
11+
return Time.now().toFixed(3)
12+
}
13+
14+
let _key: string = genStateKey()
15+
16+
export function getStateKey () {
17+
return _key
18+
}
19+
20+
export function setStateKey (key: string) {
21+
return (_key = key)
22+
}

0 commit comments

Comments
 (0)