forked from cybrespace/pinafore
add markers, tweak how streaming is processed
This commit is contained in:
parent
c8c7c03864
commit
0c9757a941
|
@ -3,6 +3,8 @@ import identity from 'lodash/identity'
|
||||||
import { database } from '../_database/database'
|
import { database } from '../_database/database'
|
||||||
import { store } from '../_store/store'
|
import { store } from '../_store/store'
|
||||||
import { scheduleIdleTask } from '../_utils/scheduleIdleTask'
|
import { scheduleIdleTask } from '../_utils/scheduleIdleTask'
|
||||||
|
import throttle from 'lodash/throttle'
|
||||||
|
import { mark, stop } from '../_utils/marks'
|
||||||
|
|
||||||
async function removeDuplicates (instanceName, timelineName, updates) {
|
async function removeDuplicates (instanceName, timelineName, updates) {
|
||||||
// remove duplicates, including duplicates due to reblogs
|
// remove duplicates, including duplicates due to reblogs
|
||||||
|
@ -15,7 +17,8 @@ async function removeDuplicates (instanceName, timelineName, updates) {
|
||||||
return updates.filter(update => !existingItemIds.has(update.id))
|
return updates.filter(update => !existingItemIds.has(update.id))
|
||||||
}
|
}
|
||||||
|
|
||||||
async function handleFreshChanges (instanceName, timelineName) {
|
async function processFreshChanges (instanceName, timelineName) {
|
||||||
|
mark('processFreshChanges')
|
||||||
let freshChanges = store.getForTimeline(instanceName, timelineName, 'freshChanges')
|
let freshChanges = store.getForTimeline(instanceName, timelineName, 'freshChanges')
|
||||||
if (freshChanges.updates && freshChanges.updates.length) {
|
if (freshChanges.updates && freshChanges.updates.length) {
|
||||||
let updates = freshChanges.updates.slice()
|
let updates = freshChanges.updates.slice()
|
||||||
|
@ -29,21 +32,29 @@ async function handleFreshChanges (instanceName, timelineName) {
|
||||||
let itemIdsToAdd = store.getForTimeline(instanceName, timelineName, 'itemIdsToAdd') || []
|
let itemIdsToAdd = store.getForTimeline(instanceName, timelineName, 'itemIdsToAdd') || []
|
||||||
if (updates && updates.length) {
|
if (updates && updates.length) {
|
||||||
itemIdsToAdd = itemIdsToAdd.concat(updates.map(_ => _.id))
|
itemIdsToAdd = itemIdsToAdd.concat(updates.map(_ => _.id))
|
||||||
|
console.log('adding ', itemIdsToAdd.length, 'items to itemIdsToAdd')
|
||||||
store.setForTimeline(instanceName, timelineName, {itemIdsToAdd: itemIdsToAdd})
|
store.setForTimeline(instanceName, timelineName, {itemIdsToAdd: itemIdsToAdd})
|
||||||
}
|
}
|
||||||
|
stop('processFreshChanges')
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const lazilyProcessFreshChanges = throttle((instanceName, timelineName) => {
|
||||||
|
scheduleIdleTask(() => {
|
||||||
|
processFreshChanges(instanceName, timelineName)
|
||||||
|
})
|
||||||
|
}, 5000)
|
||||||
|
|
||||||
function handleStreamMessage (instanceName, timelineName, message) {
|
function handleStreamMessage (instanceName, timelineName, message) {
|
||||||
|
mark('handleStreamMessage')
|
||||||
let { event, payload } = message
|
let { event, payload } = message
|
||||||
let key = event === 'update' ? 'updates' : 'deletes'
|
let key = event === 'update' ? 'updates' : 'deletes'
|
||||||
let freshChanges = store.getForTimeline(instanceName, timelineName, 'freshChanges') || {}
|
let freshChanges = store.getForTimeline(instanceName, timelineName, 'freshChanges') || {}
|
||||||
freshChanges[key] = freshChanges[key] || []
|
freshChanges[key] = freshChanges[key] || []
|
||||||
freshChanges[key].push(JSON.parse(payload))
|
freshChanges[key].push(JSON.parse(payload))
|
||||||
store.setForTimeline(instanceName, timelineName, {freshChanges: freshChanges})
|
store.setForTimeline(instanceName, timelineName, {freshChanges: freshChanges})
|
||||||
scheduleIdleTask(() => {
|
lazilyProcessFreshChanges(instanceName, timelineName)
|
||||||
handleFreshChanges(instanceName, timelineName)
|
stop('handleStreamMessage')
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export function createStream (streamingApi, instanceName, accessToken, timelineName) {
|
export function createStream (streamingApi, instanceName, accessToken, timelineName) {
|
||||||
|
|
|
@ -83,6 +83,7 @@ export async function fetchTimelineItemsOnScrollToBottom () {
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function showMoreItemsForCurrentTimeline() {
|
export async function showMoreItemsForCurrentTimeline() {
|
||||||
|
mark('showMoreItemsForCurrentTimeline')
|
||||||
let instanceName = store.get('currentInstance')
|
let instanceName = store.get('currentInstance')
|
||||||
let timelineName = store.get('currentTimeline')
|
let timelineName = store.get('currentTimeline')
|
||||||
let itemIdsToAdd = store.get('itemIdsToAdd')
|
let itemIdsToAdd = store.get('itemIdsToAdd')
|
||||||
|
@ -92,4 +93,5 @@ export async function showMoreItemsForCurrentTimeline() {
|
||||||
shouldShowHeader: false,
|
shouldShowHeader: false,
|
||||||
showHeader: false
|
showHeader: false
|
||||||
})
|
})
|
||||||
|
stop('showMoreItemsForCurrentTimeline')
|
||||||
}
|
}
|
|
@ -73,6 +73,7 @@
|
||||||
import { showMoreItemsForCurrentTimeline } from '../../_actions/timeline'
|
import { showMoreItemsForCurrentTimeline } from '../../_actions/timeline'
|
||||||
import { virtualListStore } from '../virtualList/virtualListStore' // TODO: hacky, need better way to expose scrollTop
|
import { virtualListStore } from '../virtualList/virtualListStore' // TODO: hacky, need better way to expose scrollTop
|
||||||
import { scheduleIdleTask } from '../../_utils/scheduleIdleTask'
|
import { scheduleIdleTask } from '../../_utils/scheduleIdleTask'
|
||||||
|
import { mark, stop } from '../../_utils/marks'
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
oncreate() {
|
oncreate() {
|
||||||
|
@ -191,6 +192,7 @@
|
||||||
if (!itemIdsToAdd || !itemIdsToAdd.length) {
|
if (!itemIdsToAdd || !itemIdsToAdd.length) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
mark('handleItemIdsToAdd')
|
||||||
let scrollTop = virtualListStore.get('scrollTop')
|
let scrollTop = virtualListStore.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')
|
||||||
|
@ -203,13 +205,10 @@
|
||||||
// user hasn't scrolled to the top, show a header instead
|
// user hasn't scrolled to the top, show a header instead
|
||||||
this.store.setForTimeline(instanceName, timelineName, {shouldShowHeader: true})
|
this.store.setForTimeline(instanceName, timelineName, {shouldShowHeader: true})
|
||||||
}
|
}
|
||||||
|
stop('handleItemIdsToAdd')
|
||||||
}
|
}
|
||||||
this.observe('itemIdsToAddStringified', itemIdsToAddStringified => {
|
this.observe('itemIdsToAddStringified', itemIdsToAddStringified => {
|
||||||
let itemIdsToAdd = typeof itemIdsToAddStringified === 'undefined' ? undefined :
|
|
||||||
JSON.parse(itemIdsToAddStringified)
|
|
||||||
if (itemIdsToAdd && itemIdsToAdd.length) {
|
|
||||||
scheduleIdleTask(handleItemIdsToAdd)
|
scheduleIdleTask(handleItemIdsToAdd)
|
||||||
}
|
|
||||||
})
|
})
|
||||||
},
|
},
|
||||||
setupFocus() {
|
setupFocus() {
|
||||||
|
|
Loading…
Reference in New Issue