Do more mutation-y status reducing.

This commit is contained in:
Roger Braun 2016-11-17 18:31:26 +01:00
parent d10a58f26a
commit 4fcb60487c
2 changed files with 17 additions and 4 deletions

View file

@ -1,4 +1,4 @@
import { reduce, map, slice, last, intersectionBy, sortBy, unionBy, toInteger, groupBy, differenceBy, each, find, flatten, maxBy } from 'lodash' import { reduce, map, slice, last, intersectionBy, sortBy, unionBy, toInteger, groupBy, differenceBy, each, find, flatten, maxBy, merge } from 'lodash'
import moment from 'moment' import moment from 'moment'
import apiService from '../services/api/api.service.js' import apiService from '../services/api/api.service.js'
// import parse from '../services/status_parser/status_parser.js' // import parse from '../services/status_parser/status_parser.js'
@ -54,6 +54,19 @@ export const prepareStatus = (status) => {
return status return status
} }
// Merges old and new status collections.
const mergeStatuses = (oldStatuses, newStatuses) => {
each(newStatuses, (status) => {
let oldStatus = find(oldStatuses, { id: status.id })
if (oldStatus) {
merge(oldStatus, status)
} else {
oldStatuses.push(status)
}
})
return oldStatuses
}
const addStatusesToTimeline = (addedStatuses, showImmediately, { statuses, visibleStatuses, newStatusCount, faves, loading, maxId }) => { const addStatusesToTimeline = (addedStatuses, showImmediately, { statuses, visibleStatuses, newStatusCount, faves, loading, maxId }) => {
const statusesAndFaves = groupBy(addedStatuses, statusType) const statusesAndFaves = groupBy(addedStatuses, statusType)
const addedFaves = statusesAndFaves['fave'] || [] const addedFaves = statusesAndFaves['fave'] || []
@ -150,7 +163,7 @@ export const mutations = {
// const statusesByType = groupStatusesByType(statuses) // const statusesByType = groupStatusesByType(statuses)
state.timelines[timeline] = addStatusesToTimeline(statuses, showImmediately, state.timelines[timeline]) state.timelines[timeline] = addStatusesToTimeline(statuses, showImmediately, state.timelines[timeline])
state.allStatuses = unionBy(state.timelines[timeline].statuses, state.allStatuses, 'id') mergeStatuses(state.allStatuses, state.timelines[timeline].statuses)
// Set up retweets with most current status // Set up retweets with most current status
const getRetweets = (result, status) => { const getRetweets = (result, status) => {

View file

@ -90,7 +90,7 @@ describe('The Statuses module', () => {
// It refers to the modified status. // It refers to the modified status.
mutations.addNewStatuses(state, { statuses: [modStatus], timeline: 'public' }) mutations.addNewStatuses(state, { statuses: [modStatus], timeline: 'public' })
expect(state.allStatuses).to.eql([retweet, modStatus]) expect(state.allStatuses).to.eql([modStatus, retweet])
expect(retweet.retweeted_status).to.eql(modStatus) expect(retweet.retweeted_status).to.eql(modStatus)
}) })
@ -108,7 +108,7 @@ describe('The Statuses module', () => {
mutations.addNewStatuses(state, { statuses: [modStatus], showImmediately: true, timeline: 'public' }) mutations.addNewStatuses(state, { statuses: [modStatus], showImmediately: true, timeline: 'public' })
expect(state.timelines.public.visibleStatuses).to.have.length(1) expect(state.timelines.public.visibleStatuses).to.have.length(1)
expect(state.allStatuses).to.have.length(1) expect(state.allStatuses).to.have.length(1)
expect(state.allStatuses[0]).to.equal(modStatus) expect(state.allStatuses[0]).to.eql(modStatus)
}) })
it('replaces existing statuses with the same id, coming from a retweet', () => { it('replaces existing statuses with the same id, coming from a retweet', () => {