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

Add fastdom to improve performance #174

Open
wants to merge 4 commits into
base: master
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
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
"eslint-plugin-promise": "^4.0.1",
"eslint-plugin-standard": "^4.0.0",
"eslint-plugin-vue": "^6.2.2",
"fastdom": "^1.0.9",
"rollup": "^2.2.0",
"rollup-plugin-babel": "^4.4.0",
"rollup-plugin-commonjs": "^10.1.0",
Expand Down
19 changes: 15 additions & 4 deletions src/components/DynamicScrollerItem.vue
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<script>
import fastdom from 'fastdom'
export default {
name: 'DynamicScrollerItem',

Expand Down Expand Up @@ -161,12 +162,22 @@ export default {
this.updateSize()
},

getBounds () {
return new Promise((resolve, reject) => {
fastdom.measure(() => {
resolve({
width: this.$el.offsetWidth,
height: this.$el.offsetHeight,
})
})
})
},

computeSize (id) {
this.$nextTick(() => {
this.$nextTick(async () => {
if (this.id === id) {
const width = this.$el.offsetWidth
const height = this.$el.offsetHeight
this.applySize(width, height)
const bounds = await this.getBounds()
this.applySize(bounds.width, bounds.height)
}
this.$_pendingSizeUpdate = null
})
Expand Down
91 changes: 57 additions & 34 deletions src/components/RecycleScroller.vue
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
import { ResizeObserver } from 'vue-resize'
import { ObserveVisibility } from 'vue-observe-visibility'
import ScrollParent from 'scrollparent'
import fastdom from 'fastdom'
import config from '../config'
import { props, simpleArray } from './common'
import { supportsPassive } from '../utils'
Expand Down Expand Up @@ -265,7 +266,7 @@ export default {
}
},

updateVisibleItems (checkItem) {
async updateVisibleItems (checkItem) {
const itemSize = this.itemSize
const typeField = this.typeField
const keyField = this.simpleArray ? null : this.keyField
Expand All @@ -285,7 +286,7 @@ export default {
endIndex = this.prerender
totalSize = null
} else {
const scroll = this.getScroll()
const scroll = await this.getScroll()
const buffer = this.buffer
scroll.start -= buffer
scroll.end += buffer
Expand Down Expand Up @@ -471,39 +472,61 @@ export default {
},

getScroll () {
const { $el: el, direction } = this
const isVertical = direction === 'vertical'
let scrollState

if (this.pageMode) {
const bounds = el.getBoundingClientRect()
const boundsSize = isVertical ? bounds.height : bounds.width
let start = -(isVertical ? bounds.top : bounds.left)
let size = isVertical ? window.innerHeight : window.innerWidth
if (start < 0) {
size += start
start = 0
}
if (start + size > boundsSize) {
size = boundsSize - start
}
scrollState = {
start,
end: start + size,
}
} else if (isVertical) {
scrollState = {
start: el.scrollTop,
end: el.scrollTop + el.clientHeight,
}
} else {
scrollState = {
start: el.scrollLeft,
end: el.scrollLeft + el.clientWidth,
return new Promise((resolve, reject) => {
const { $el: el, direction } = this
const isVertical = direction === 'vertical'
let scrollState

if (this.pageMode) {
fastdom.measure(() => {
const bounds = el.getBoundingClientRect()
const boundsSize = isVertical ? bounds.height : bounds.width
let start = -(isVertical ? bounds.top : bounds.left)
let size = isVertical ? window.innerHeight : window.innerWidth

fastdom.mutate(() => {
if (start < 0) {
size += start
start = 0
}
if (start + size > boundsSize) {
size = boundsSize - start
}
scrollState = {
start,
end: start + size,
}
resolve(scrollState)
})
})
} else if (isVertical) {
fastdom.measure(() => {
const start = el.scrollTop
const end = el.scrollTop + el.clientHeight

fastdom.mutate(() => {
scrollState = {
start,
end,
}
resolve(scrollState)
})
})
} else {
fastdom.measure(() => {
const start = el.scrollLeft
const end = el.scrollLeft + el.clientWidth

fastdom.mutate(() => {
scrollState = {
start,
end,
}
resolve(scrollState)
})
})
}
}

return scrollState
})
},

applyPageMode () {
Expand Down
12 changes: 12 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1773,6 +1773,13 @@ fast-levenshtein@~2.0.6:
resolved "https://registry.yarnpkg.com/fast-levenshtein/-/fast-levenshtein-2.0.6.tgz#3d8a5c66883a16a30ca8643e851f19baa7797917"
integrity sha1-PYpcZog6FqMMqGQ+hR8Zuqd5eRc=

fastdom@^1.0.9:
version "1.0.9"
resolved "https://registry.yarnpkg.com/fastdom/-/fastdom-1.0.9.tgz#b395fab11a3701173c02a054fe769d8f596a0a26"
integrity sha512-SSp4fbVzu8JkkG01NUX+0iOwe9M5PN3MGIQ84txLf4TkkJG4q30khkzumKgi4hUqO1+jX6wLHfnCPoZ6eSZ6Tg==
dependencies:
strictdom "^1.0.1"

fastparse@^1.1.2:
version "1.1.2"
resolved "https://registry.yarnpkg.com/fastparse/-/fastparse-1.1.2.tgz#91728c5a5942eced8531283c79441ee4122c35a9"
Expand Down Expand Up @@ -3429,6 +3436,11 @@ sshpk@^1.7.0:
safer-buffer "^2.0.2"
tweetnacl "~0.14.0"

strictdom@^1.0.1:
version "1.0.1"
resolved "https://registry.yarnpkg.com/strictdom/-/strictdom-1.0.1.tgz#189de91649f73d44d59b8432efa68ef9d2659460"
integrity sha1-GJ3pFkn3PUTVm4Qy76aO+dJllGA=

string-hash@^1.1.0:
version "1.1.3"
resolved "https://registry.yarnpkg.com/string-hash/-/string-hash-1.1.3.tgz#e8aafc0ac1855b4666929ed7dd1275df5d6c811b"
Expand Down