Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(state-key): PositionStore memory leak(fix #3445) #3485

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/util/push-state.js
@@ -1,7 +1,7 @@
/* @flow */

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

Expand All @@ -28,6 +28,7 @@ export function pushState (url?: string, replace?: boolean) {
// DOM Exception 18 where it limits to 100 pushState calls
const history = window.history
try {
clearPositionStore()
if (replace) {
// preserve existing history state as it could be overriden by the user
const stateCopy = extend({}, history.state)
Expand Down
11 changes: 10 additions & 1 deletion src/util/scroll.js
Expand Up @@ -2,7 +2,7 @@

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

const positionStore = Object.create(null)
Expand Down Expand Up @@ -173,3 +173,12 @@ function scrollToPosition (shouldScroll, position) {
}
}
}

export function clearPositionStore () {
const key = getCurrentStateKey()
for (var i in positionStore) {
if (Number(i) > key) {
delete (positionStore[i])
}
}
}
20 changes: 11 additions & 9 deletions src/util/state-key.js
@@ -1,22 +1,24 @@
/* @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 getCurrentStateKey (): number {
const state = window.history.state
if (state && typeof state.key === 'number') {
return state.key
}
return 1
}

export function genStateKey (): string {
return Time.now().toFixed(3)
export function genStateKey (): number {
return getCurrentStateKey() + 1
}

let _key: string = genStateKey()
let _key: number = inBrowser ? getCurrentStateKey() : -1

export function getStateKey () {
return _key
}

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