2018-01-15 20:23:28 +00:00
|
|
|
import { Store } from 'svelte/store.js'
|
2018-01-25 16:23:14 +00:00
|
|
|
import { mark, stop } from '../../_utils/marks'
|
2018-01-15 20:23:28 +00:00
|
|
|
|
2018-01-20 23:37:40 +00:00
|
|
|
const VIEWPORT_RENDER_FACTOR = 4
|
|
|
|
|
2018-01-15 20:23:28 +00:00
|
|
|
class VirtualListStore extends Store {
|
2018-01-18 03:53:12 +00:00
|
|
|
constructor(state) {
|
|
|
|
super(state)
|
2018-01-18 03:41:37 +00:00
|
|
|
this._batches = {}
|
|
|
|
}
|
|
|
|
|
|
|
|
batchUpdate(key, subKey, value) {
|
|
|
|
let batch = this._batches[key]
|
|
|
|
if (!batch) {
|
|
|
|
batch = this._batches[key] = {}
|
|
|
|
}
|
|
|
|
batch[subKey] = value
|
|
|
|
|
|
|
|
requestAnimationFrame(() => {
|
2018-01-18 03:52:18 +00:00
|
|
|
let batch = this._batches[key]
|
|
|
|
if (!batch) {
|
|
|
|
return
|
|
|
|
}
|
2018-01-18 03:41:37 +00:00
|
|
|
let updatedKeys = Object.keys(batch)
|
|
|
|
if (!updatedKeys.length) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
mark('batchUpdate()')
|
|
|
|
let obj = this.get(key)
|
|
|
|
for (let otherKey of updatedKeys) {
|
|
|
|
obj[otherKey] = batch[otherKey]
|
|
|
|
}
|
|
|
|
delete this._batches[key]
|
|
|
|
let toSet = {}
|
|
|
|
toSet[key] = obj
|
|
|
|
this.set(toSet)
|
|
|
|
stop('batchUpdate()')
|
|
|
|
})
|
|
|
|
}
|
2018-01-27 16:13:28 +00:00
|
|
|
|
|
|
|
setForRealm(obj) {
|
|
|
|
let realmName = this.get('currentRealm')
|
|
|
|
let realms = this.get('realms') || {}
|
|
|
|
realms[realmName] = Object.assign(realms[realmName] || {}, obj)
|
|
|
|
this.set({realms: realms})
|
|
|
|
}
|
2018-01-15 20:23:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const virtualListStore = new VirtualListStore({
|
2018-01-27 16:13:28 +00:00
|
|
|
realms: {},
|
|
|
|
currentRealm: null,
|
2018-01-15 20:23:28 +00:00
|
|
|
itemHeights: {},
|
2018-01-22 00:07:11 +00:00
|
|
|
footerHeight: 0
|
2018-01-15 20:23:28 +00:00
|
|
|
})
|
|
|
|
|
2018-01-27 16:13:28 +00:00
|
|
|
virtualListStore.compute('items', ['currentRealm', 'realms'], (currentRealm, realms) => {
|
|
|
|
return realms[currentRealm] && realms[currentRealm].items || []
|
|
|
|
})
|
|
|
|
|
|
|
|
virtualListStore.compute('showFooter', ['currentRealm', 'realms'], (currentRealm, realms) => {
|
|
|
|
return realms[currentRealm] && realms[currentRealm].showFooter
|
|
|
|
})
|
|
|
|
|
|
|
|
virtualListStore.compute('scrollTop', ['currentRealm', 'realms'], (currentRealm, realms) => {
|
|
|
|
return realms[currentRealm] && realms[currentRealm].scrollTop || 0
|
|
|
|
})
|
|
|
|
|
|
|
|
virtualListStore.compute('scrollHeight', ['currentRealm', 'realms'], (currentRealm, realms) => {
|
|
|
|
return realms[currentRealm] && realms[currentRealm].scrollHeight || 0
|
|
|
|
})
|
|
|
|
|
|
|
|
virtualListStore.compute('offsetHeight', ['currentRealm', 'realms'], (currentRealm, realms) => {
|
|
|
|
return realms[currentRealm] && realms[currentRealm].offsetHeight || 0
|
|
|
|
})
|
|
|
|
|
2018-01-16 00:12:07 +00:00
|
|
|
virtualListStore.compute('visibleItems',
|
2018-01-17 05:43:31 +00:00
|
|
|
['items', 'scrollTop', 'itemHeights', 'offsetHeight'],
|
|
|
|
(items, scrollTop, itemHeights, offsetHeight) => {
|
2018-01-17 08:59:15 +00:00
|
|
|
mark('compute visibleItems')
|
2018-01-20 23:37:40 +00:00
|
|
|
let renderBuffer = VIEWPORT_RENDER_FACTOR * offsetHeight
|
2018-01-16 00:12:07 +00:00
|
|
|
let visibleItems = []
|
2018-01-16 01:25:32 +00:00
|
|
|
let totalOffset = 0
|
|
|
|
let len = items.length
|
|
|
|
let i = -1
|
|
|
|
while (++i < len) {
|
2018-01-24 02:19:03 +00:00
|
|
|
let key = items[i]
|
2018-01-16 00:35:08 +00:00
|
|
|
let height = itemHeights[key] || 0
|
2018-01-16 01:25:32 +00:00
|
|
|
let currentOffset = totalOffset
|
|
|
|
totalOffset += height
|
|
|
|
let isBelowViewport = (currentOffset < scrollTop)
|
|
|
|
if (isBelowViewport) {
|
2018-01-16 02:29:28 +00:00
|
|
|
if (scrollTop - renderBuffer > currentOffset) {
|
2018-01-16 01:25:32 +00:00
|
|
|
continue // below the area we want to render
|
|
|
|
}
|
2018-01-16 00:12:07 +00:00
|
|
|
} else {
|
2018-01-17 07:16:15 +00:00
|
|
|
if (currentOffset > (scrollTop + height + renderBuffer)) {
|
2018-01-16 01:25:32 +00:00
|
|
|
break // above the area we want to render
|
|
|
|
}
|
2018-01-16 00:12:07 +00:00
|
|
|
}
|
2018-01-16 01:25:32 +00:00
|
|
|
visibleItems.push({
|
|
|
|
offset: currentOffset,
|
|
|
|
key: key,
|
2018-01-17 07:16:15 +00:00
|
|
|
index: i
|
2018-01-16 01:25:32 +00:00
|
|
|
})
|
|
|
|
}
|
2018-01-17 08:59:15 +00:00
|
|
|
stop('compute visibleItems')
|
2018-01-16 00:12:07 +00:00
|
|
|
return visibleItems
|
2018-01-15 20:23:28 +00:00
|
|
|
})
|
|
|
|
|
2018-01-22 00:07:11 +00:00
|
|
|
virtualListStore.compute('heightWithoutFooter',
|
|
|
|
['items', 'itemHeights'],
|
|
|
|
(items, itemHeights) => {
|
2018-01-15 20:23:28 +00:00
|
|
|
let sum = 0
|
2018-01-16 01:25:32 +00:00
|
|
|
let i = -1
|
|
|
|
let len = items.length
|
|
|
|
while (++i < len) {
|
2018-01-24 02:19:03 +00:00
|
|
|
sum += itemHeights[items[i]] || 0
|
2018-01-16 01:25:32 +00:00
|
|
|
}
|
2018-01-15 20:23:28 +00:00
|
|
|
return sum
|
|
|
|
})
|
|
|
|
|
2018-01-22 00:07:11 +00:00
|
|
|
|
|
|
|
virtualListStore.compute('height',
|
|
|
|
['heightWithoutFooter', 'showFooter', 'footerHeight'],
|
|
|
|
(heightWithoutFooter, showFooter, footerHeight) => {
|
|
|
|
return showFooter ? (heightWithoutFooter + footerHeight) : heightWithoutFooter
|
|
|
|
})
|
|
|
|
|
2018-01-18 07:00:33 +00:00
|
|
|
virtualListStore.compute('numItems', ['items'], (items) => items.length)
|
|
|
|
|
2018-01-25 02:04:25 +00:00
|
|
|
virtualListStore.compute('allVisibleItemsHaveHeight',
|
|
|
|
['visibleItems', 'itemHeights'],
|
|
|
|
(visibleItems, itemHeights) => {
|
2018-01-25 02:58:10 +00:00
|
|
|
if (!visibleItems.length) {
|
|
|
|
return false
|
|
|
|
}
|
2018-01-25 02:04:25 +00:00
|
|
|
for (let visibleItem of visibleItems) {
|
|
|
|
if (!itemHeights[visibleItem.key]) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
})
|
|
|
|
|
2018-01-15 20:23:28 +00:00
|
|
|
if (process.browser && process.env.NODE_ENV !== 'production') {
|
|
|
|
window.virtualListStore = virtualListStore
|
|
|
|
}
|
|
|
|
|
|
|
|
export {
|
|
|
|
virtualListStore
|
|
|
|
}
|