From 4fcb60487cbe034d12d42852354c7522033dc23c Mon Sep 17 00:00:00 2001 From: Roger Braun Date: Thu, 17 Nov 2016 18:31:26 +0100 Subject: [PATCH] Do more mutation-y status reducing. --- src/modules/statuses.js | 17 +++++++++++++++-- test/unit/specs/modules/statuses.spec.js | 4 ++-- 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/src/modules/statuses.js b/src/modules/statuses.js index d7607bf1..7fa8a7b2 100644 --- a/src/modules/statuses.js +++ b/src/modules/statuses.js @@ -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 apiService from '../services/api/api.service.js' // import parse from '../services/status_parser/status_parser.js' @@ -54,6 +54,19 @@ export const prepareStatus = (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 statusesAndFaves = groupBy(addedStatuses, statusType) const addedFaves = statusesAndFaves['fave'] || [] @@ -150,7 +163,7 @@ export const mutations = { // const statusesByType = groupStatusesByType(statuses) 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 const getRetweets = (result, status) => { diff --git a/test/unit/specs/modules/statuses.spec.js b/test/unit/specs/modules/statuses.spec.js index 28ecbdfb..02013039 100644 --- a/test/unit/specs/modules/statuses.spec.js +++ b/test/unit/specs/modules/statuses.spec.js @@ -90,7 +90,7 @@ describe('The Statuses module', () => { // It refers to the modified status. 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) }) @@ -108,7 +108,7 @@ describe('The Statuses module', () => { mutations.addNewStatuses(state, { statuses: [modStatus], showImmediately: true, timeline: 'public' }) expect(state.timelines.public.visibleStatuses).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', () => {