From 638278b334199f17e052a54a0837c97624940c0c Mon Sep 17 00:00:00 2001 From: Eduardo San Martin Morote Date: Fri, 23 Aug 2019 18:44:48 +0200 Subject: [PATCH] refactor: remove circular dependency --- src/util/push-state.js | 55 +++++++++++++++--------------------------- src/util/scroll.js | 2 +- src/util/state-key.js | 22 +++++++++++++++++ 3 files changed, 42 insertions(+), 37 deletions(-) create mode 100644 src/util/state-key.js diff --git a/src/util/push-state.js b/src/util/push-state.js index 136d6f9ec..823c98dc4 100644 --- a/src/util/push-state.js +++ b/src/util/push-state.js @@ -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() @@ -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) diff --git a/src/util/scroll.js b/src/util/scroll.js index 1d444f3ec..683adc456 100644 --- a/src/util/scroll.js +++ b/src/util/scroll.js @@ -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) diff --git a/src/util/state-key.js b/src/util/state-key.js new file mode 100644 index 000000000..be9144d74 --- /dev/null +++ b/src/util/state-key.js @@ -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) +}