clean up observers

This commit is contained in:
Nolan Lawson 2018-02-25 10:50:04 -08:00
parent 81174e636b
commit 3f5f016c32
2 changed files with 14 additions and 17 deletions

View file

@ -67,6 +67,7 @@
import { scheduleIdleTask } from '../../_utils/scheduleIdleTask' import { scheduleIdleTask } from '../../_utils/scheduleIdleTask'
import { mark, stop } from '../../_utils/marks' import { mark, stop } from '../../_utils/marks'
import { importPseudoVirtualList } from '../../_utils/asyncModules' import { importPseudoVirtualList } from '../../_utils/asyncModules'
import isEqual from 'lodash/isEqual'
export default { export default {
oncreate() { oncreate() {
@ -138,11 +139,9 @@
&& timelineValue && timelineValue
}, },
itemIdsToAdd: ($itemIdsToAdd) => $itemIdsToAdd, itemIdsToAdd: ($itemIdsToAdd) => $itemIdsToAdd,
// TODO: hack to avoid getting called too often
itemIdsToAddStringified: (itemIdsToAdd) => JSON.stringify(itemIdsToAdd),
headerProps: (itemIdsToAdd) => { headerProps: (itemIdsToAdd) => {
return { return {
count: (itemIdsToAdd && itemIdsToAdd.length) || 0, count: itemIdsToAdd ? itemIdsToAdd.length : 0,
onClick: showMoreItemsForCurrentTimeline onClick: showMoreItemsForCurrentTimeline
} }
} }
@ -188,15 +187,10 @@
let instanceName = this.store.get('currentInstance') let instanceName = this.store.get('currentInstance')
let timelineName = this.get('timeline') let timelineName = this.get('timeline')
let handleItemIdsToAdd = () => { let handleItemIdsToAdd = () => {
let itemIdsToAdd = this.get('itemIdsToAdd')
if (!itemIdsToAdd || !itemIdsToAdd.length) {
return
}
mark('handleItemIdsToAdd') mark('handleItemIdsToAdd')
let scrollTop = this.get('scrollTop') let scrollTop = this.get('scrollTop')
let shouldShowHeader = this.store.get('shouldShowHeader') let shouldShowHeader = this.store.get('shouldShowHeader')
let showHeader = this.store.get('showHeader') let showHeader = this.store.get('showHeader')
//console.log('handleItemIdsToAdd', (itemIdsToAdd && itemIdsToAdd.length) || 0)
if (scrollTop === 0 && !shouldShowHeader && !showHeader) { if (scrollTop === 0 && !shouldShowHeader && !showHeader) {
// if the user is scrolled to the top and we're not showing the header, then // if the user is scrolled to the top and we're not showing the header, then
// just insert the statuses. this is "chat room mode" // just insert the statuses. this is "chat room mode"
@ -207,10 +201,13 @@
} }
stop('handleItemIdsToAdd') stop('handleItemIdsToAdd')
} }
this.observe('itemIdsToAddStringified', itemIdsToAddStringified => { this.observe('itemIdsToAdd', (newItemIdsToAdd, oldItemIdsToAdd) => {
if (itemIdsToAddStringified) { if (!newItemIdsToAdd ||
scheduleIdleTask(handleItemIdsToAdd) !newItemIdsToAdd.length ||
isEqual(newItemIdsToAdd, oldItemIdsToAdd)) {
return
} }
scheduleIdleTask(handleItemIdsToAdd)
}) })
}, },
setupFocus() { setupFocus() {

View file

@ -30,6 +30,7 @@
import { virtualListStore } from './virtualListStore' import { virtualListStore } from './virtualListStore'
import throttle from 'lodash/throttle' import throttle from 'lodash/throttle'
import { mark, stop } from '../../_utils/marks' import { mark, stop } from '../../_utils/marks'
import isEqual from 'lodash/isEqual'
const DISTANCE_FROM_BOTTOM_TO_FIRE = 400 const DISTANCE_FROM_BOTTOM_TO_FIRE = 400
const SCROLL_EVENT_THROTTLE = 1000 const SCROLL_EVENT_THROTTLE = 1000
@ -52,11 +53,12 @@
this.store.setForRealm({showHeader: showHeader}) this.store.setForRealm({showHeader: showHeader})
stop('set showHeader') stop('set showHeader')
}) })
this.observe('itemsStringified', (itemsStringified) => { this.observe('items', (newItems, oldItems) => {
let items = typeof itemsStringified === 'undefined' ? undefined : if (!newItems || isEqual(newItems, oldItems)) {
JSON.parse(itemsStringified) return
}
mark('set items') mark('set items')
this.store.setForRealm({items: items}) this.store.setForRealm({items: newItems})
stop('set items') stop('set items')
}) })
this.observe('allVisibleItemsHaveHeight', allVisibleItemsHaveHeight => { this.observe('allVisibleItemsHaveHeight', allVisibleItemsHaveHeight => {
@ -102,8 +104,6 @@
scrollTop: ($scrollTop) => $scrollTop, scrollTop: ($scrollTop) => $scrollTop,
// TODO: bug in svelte store, shouldn't need to do this // TODO: bug in svelte store, shouldn't need to do this
allVisibleItemsHaveHeight: ($allVisibleItemsHaveHeight) => $allVisibleItemsHaveHeight, allVisibleItemsHaveHeight: ($allVisibleItemsHaveHeight) => $allVisibleItemsHaveHeight,
// TODO: hack to avoid getting called too often
itemsStringified: (items) => JSON.stringify(items)
} }
} }
</script> </script>