[ci skip] Improve performance of getting last note

This commit is contained in:
Filipa Lacerda 2017-08-10 12:25:59 +01:00
parent 3e0a76dae6
commit c438106732

View file

@ -1,3 +1,5 @@
import _ from 'underscore';
export const notes = state => state.notes; export const notes = state => state.notes;
export const targetNoteHash = state => state.targetNoteHash; export const targetNoteHash = state => state.targetNoteHash;
@ -16,15 +18,11 @@ export const notesById = state => state.notes.reduce((acc, note) => {
}, {}); }, {});
const reverseNotes = array => array.slice(0).reverse(); const reverseNotes = array => array.slice(0).reverse();
const isLastNote = (note, userId) => !note.system && note.author.id === userId; const isLastNote = (note, state) => !note.system && note.author.id === state.userData.id;
export const getCurrentUserLastNote = state => userId => reverseNotes(state.notes) export const getCurrentUserLastNote = state => _.flatten(reverseNotes(state.notes)
.reduce((acc, note) => { .map(note => note.notes))
acc.push(reverseNotes(note.notes).find(el => isLastNote(el, userId))); .find(el => isLastNote(el, state));
return acc;
}, []).filter(el => el !== undefined)[0];
// eslint-disable-next-line no-unused-vars
export const getDiscussionLastNote = state => (discussion, userId) => reverseNotes(discussion.notes)
.find(el => isLastNote(el, userId));
export const getDiscussionLastNote = state => discussion => reverseNotes(discussion.notes)
.find(el => isLastNote(el, state));