Mailspring/internal_packages/thread-list/lib/thread-list-store.coffee
Ben Gotow 8d6dcfe549 perf(thread-list): Tailored SQLite indexes, ListTabular / ScrollRegion optimizations galore
Summary:
Allow Database models to create indexes, but don't autocreate bad ones

fix minor bug in error-reporter

Fix index on message list to make thread list lookups use proper index

Developer bar ignores state changes unless it's open

DatabaseView now asks for metadata for a set of items rather than calling a function for every item. Promise.props was cute but we really needed to make a single database query for all message metadata.

New "in" matcher so you can say `thread_id IN (1,2,3)`

Add .scroll-region-content-inner which is larger than the viewport by 1 page size, and uses transform(0,0,0) trick

ScrollRegion exposes `onScrollEnd` so listTabular, et al don't need to re-implement it with more timers. Also removing requestAnimationFrame which was causing us to request scrollTop when it was not ready, and caching the values of...

...clientHeight/scrollHeight while scrolling is in-flight

Updating rendered content 10 rows at a time (RangeChunkSize) was a bad idea. Instead, add every row in a render: pass as it comes in (less work all the time vs more work intermittently). Also remove bad requestAnimationFrame, and prevent calls to...

...updateRangeState from triggering additional calls to updateRangeState by removing `componentDidUpdate => updateRangeState `

Turning off hover (pointer-events:none) is now standard in ScrollRegion

Loading text in the scroll tooltip, instead of random date shown

Handle query parse errors by catching error and throwing a better more explanatory error

Replace "quick action" retina images with background images to make React render easier

Replace hasTagId with a faster implementation which doesn't call functions and doesn't build a temporary array

Print query durations when printing to console instead of only in metadata

Remove headers from support from ListTabular, we'll never use it

Making columns part of state was a good idea but changing the array causes the entire ListTabular to re-render.  To avoid this, be smarter about updating columns. This logic could potentially go in `componentDidReceiveProps` too.

Fix specs and add 6 more for new database store functionality

Test Plan: Run 6 new specs. More in the works?

Reviewers: evan

Reviewed By: evan

Differential Revision: https://phab.nylas.com/D1651
2015-06-17 20:12:48 -07:00

195 lines
5.7 KiB
CoffeeScript

Reflux = require 'reflux'
_ = require 'underscore'
{DatabaseStore,
DatabaseView,
SearchView,
NamespaceStore,
WorkspaceStore,
AddRemoveTagsTask,
FocusedTagStore,
FocusedContentStore,
Actions,
Utils,
Thread,
Message} = require 'nylas-exports'
# Public: A mutable text container with undo/redo support and the ability to
# annotate logical regions in the text.
#
module.exports =
ThreadListStore = Reflux.createStore
init: ->
@_resetInstanceVars()
@_afterViewUpdate = []
@listenTo Actions.searchQueryCommitted, @_onSearchCommitted
@listenTo Actions.selectLayoutMode, @_autofocusForLayoutMode
@listenTo Actions.archiveAndPrevious, @_onArchiveAndPrev
@listenTo Actions.archiveAndNext, @_onArchiveAndNext
@listenTo Actions.archiveSelection, @_onArchiveSelection
@listenTo Actions.archive, @_onArchive
@listenTo Actions.toggleStarSelection, @_onToggleStarSelection
@listenTo DatabaseStore, @_onDataChanged
@listenTo FocusedTagStore, @_onTagChanged
@listenTo NamespaceStore, @_onNamespaceChanged
@createView()
_resetInstanceVars: ->
@_lastQuery = null
@_searchQuery = null
view: ->
@_view
setView: (view) ->
@_viewUnlisten() if @_viewUnlisten
@_view = view
@_viewUnlisten = view.listen ->
@trigger(@)
fn() for fn in @_afterViewUpdate
@_afterViewUpdate = []
@_autofocusForLayoutMode()
,@
@trigger(@)
createView: ->
tagId = FocusedTagStore.tagId()
namespaceId = NamespaceStore.current()?.id
if @_searchQuery
@setView(new SearchView(@_searchQuery, namespaceId))
else if namespaceId and tagId
matchers = []
matchers.push Thread.attributes.namespaceId.equal(namespaceId)
matchers.push Thread.attributes.tags.contains(tagId) if tagId isnt "*"
view = new DatabaseView Thread, {matchers}, (ids) =>
DatabaseStore.findAll(Message).where(Message.attributes.threadId.in(ids)).then (messages) ->
messagesByThread = {}
for id in ids
messagesByThread[id] = []
for message in messages
messagesByThread[message.threadId].push message
messagesByThread
@setView(view)
Actions.setFocus(collection: 'thread', item: null)
# Inbound Events
_onTagChanged: -> @createView()
_onNamespaceChanged: ->
namespaceId = NamespaceStore.current()?.id
namespaceMatcher = (m) ->
m.attribute() is Thread.attributes.namespaceId and m.value() is namespaceId
return if @view and _.find(@view.matchers, namespaceMatcher)
@createView()
_onSearchCommitted: (query) ->
return if @_searchQuery is query
@_searchQuery = query
@createView()
_onDataChanged: (change) ->
if change.objectClass is Thread.name
@_view.invalidate({change: change, shallow: true})
if change.objectClass is Message.name
threadIds = _.uniq _.map change.objects, (m) -> m.threadId
@_view.invalidateMetadataFor(threadIds)
_onToggleStarSelection: ->
selected = @_view.selection.items()
focusedId = FocusedContentStore.focusedId('thread')
keyboardId = FocusedContentStore.keyboardCursorId('thread')
oneAlreadyStarred = false
for thread in selected
if thread.hasTagId('starred')
oneAlreadyStarred = true
for thread in selected
if oneAlreadyStarred
task = new AddRemoveTagsTask(thread, [], ['starred'])
else
task = new AddRemoveTagsTask(thread, ['starred'], [])
Actions.queueTask(task)
_onArchive: ->
@_archiveAndShiftBy('auto')
_onArchiveSelection: ->
selected = @_view.selection.items()
focusedId = FocusedContentStore.focusedId('thread')
keyboardId = FocusedContentStore.keyboardCursorId('thread')
for thread in selected
task = new AddRemoveTagsTask(thread, ['archive'], ['inbox'])
Actions.queueTask(task)
if thread.id is focusedId
Actions.setFocus(collection: 'thread', item: null)
if thread.id is keyboardId
Actions.setCursorPosition(collection: 'thread', item: null)
@_view.selection.clear()
_onArchiveAndPrev: ->
@_archiveAndShiftBy(-1)
_onArchiveAndNext: ->
@_archiveAndShiftBy(1)
_archiveAndShiftBy: (offset) ->
layoutMode = WorkspaceStore.layoutMode()
focused = FocusedContentStore.focused('thread')
explicitOffset = if offset is "auto" then false else true
return unless focused
# Determine the current index
index = @_view.indexOfId(focused.id)
return if index is -1
# Determine the next index we want to move to
if offset is 'auto'
if @_view.get(index - 1)?.isUnread()
offset = -1
else
offset = 1
index = Math.min(Math.max(index + offset, 0), @_view.count() - 1)
nextKeyboard = nextFocus = @_view.get(index)
# Archive the current thread
task = new AddRemoveTagsTask(focused, ['archive'], ['inbox'])
Actions.queueTask(task)
# Remove the current thread from selection
@_view.selection.remove(focused)
# If the user is in list mode and archived without specifically saying
# "archive and next" or "archive and prev", return to the thread list
# instead of focusing on the next message.
if layoutMode is 'list' and not explicitOffset
nextFocus = null
@_afterViewUpdate.push ->
Actions.setFocus(collection: 'thread', item: nextFocus)
Actions.setCursorPosition(collection: 'thread', item: nextKeyboard)
_autofocusForLayoutMode: ->
layoutMode = WorkspaceStore.layoutMode()
focused = FocusedContentStore.focused('thread')
if layoutMode is 'split' and not focused and @_view.selection.count() is 0
item = @_view.get(0)
_.defer =>
Actions.setFocus({collection: 'thread', item: item})