Skip to content

Commit a4b9ce2

Browse files
committed
fix(state-key): PositionStore memory leak(fix vuejs#3445)
1 parent 3e9739a commit a4b9ce2

File tree

3 files changed

+23
-11
lines changed

3 files changed

+23
-11
lines changed

Diff for: src/util/push-state.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/* @flow */
22

33
import { inBrowser } from './dom'
4-
import { saveScrollPosition } from './scroll'
4+
import { saveScrollPosition, clearPositionStore } from './scroll'
55
import { genStateKey, setStateKey, getStateKey } from './state-key'
66
import { extend } from './misc'
77

@@ -28,6 +28,7 @@ export function pushState (url?: string, replace?: boolean) {
2828
// DOM Exception 18 where it limits to 100 pushState calls
2929
const history = window.history
3030
try {
31+
clearPositionStore()
3132
if (replace) {
3233
// preserve existing history state as it could be overriden by the user
3334
const stateCopy = extend({}, history.state)

Diff for: src/util/scroll.js

+10-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 './state-key'
5+
import { getStateKey, setStateKey, getCurrentStateKey } from './state-key'
66
import { extend } from './misc'
77

88
const positionStore = Object.create(null)
@@ -173,3 +173,12 @@ function scrollToPosition (shouldScroll, position) {
173173
}
174174
}
175175
}
176+
177+
export function clearPositionStore () {
178+
const key = getCurrentStateKey()
179+
for (var i in positionStore) {
180+
if (Number(i) > key) {
181+
delete (positionStore[i])
182+
}
183+
}
184+
}

Diff for: src/util/state-key.js

+11-9
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,24 @@
11
/* @flow */
22
import { inBrowser } from './dom'
33

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
4+
export function getCurrentStateKey (): number {
5+
const state = window.history.state
6+
if (state && typeof state.key === 'number') {
7+
return state.key
8+
}
9+
return 1
10+
}
911

10-
export function genStateKey (): string {
11-
return Time.now().toFixed(3)
12+
export function genStateKey (): number {
13+
return getCurrentStateKey() + 1
1214
}
1315

14-
let _key: string = genStateKey()
16+
let _key: number = inBrowser ? getCurrentStateKey() : -1
1517

1618
export function getStateKey () {
1719
return _key
1820
}
1921

20-
export function setStateKey (key: string) {
22+
export function setStateKey (key: number) {
2123
return (_key = key)
2224
}

0 commit comments

Comments
 (0)