From 0289de243aacc88aa17a87a58d05ba7ddc3fd7a3 Mon Sep 17 00:00:00 2001 From: Ben Gotow Date: Tue, 15 Sep 2015 14:27:18 -0700 Subject: [PATCH] fix(message-list): Optimize draft deletion along with draft creation Summary: I honestly can't wait until we refactor this logic into a sort of "LiveQuery" class so that it can apply to all of the queries in the whole app. These optimizations make deleting / creating drafts quite a bit more responsive because there's no delay between the database store emitting an event and the view updating. Fixes T3456 No explicit tests for now because we'll pull this into a nice class soon Test Plan: Run tests Reviewers: dillon, evan Reviewed By: evan Maniphest Tasks: T3456 Differential Revision: https://phab.nylas.com/D2019 --- src/flux/stores/message-store.coffee | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/src/flux/stores/message-store.coffee b/src/flux/stores/message-store.coffee index 0d008fb26..8be04cb26 100644 --- a/src/flux/stores/message-store.coffee +++ b/src/flux/stores/message-store.coffee @@ -86,17 +86,27 @@ class MessageStore extends NylasStore if change.objectClass is Message.name inDisplayedThread = _.some change.objects, (obj) => obj.threadId is @_thread.id - if inDisplayedThread + return unless inDisplayedThread + if change.objects.length is 1 and change.objects[0].draft is true item = change.objects[0] - itemAlreadyExists = _.some @_items, (msg) -> msg.id is item.id or msg.clientId is item.clientId - if change.objects.length is 1 and item.draft is true and not itemAlreadyExists + itemIndex = _.findIndex @_items, (msg) -> msg.id is item.id or msg.clientId is item.clientId + + if change.type is 'persist' and itemIndex is -1 @_items = [].concat(@_items, [item]) @_items = @_sortItemsForDisplay(@_items) @_expandItemsToDefault() @trigger() - else - @_fetchFromCache() + return + + if change.type is 'unpersist' and itemIndex isnt -1 + @_items = [].concat(@_items) + @_items.splice(itemIndex, 1) + @_expandItemsToDefault() + @trigger() + return + + @_fetchFromCache() if change.objectClass is Thread.name updatedThread = _.find change.objects, (obj) => obj.id is @_thread.id