perf(*): Make AccountStore / ContactRankingStore less noisy

This commit is contained in:
Ben Gotow 2016-04-05 16:15:47 -07:00
parent d047cae9ba
commit b0768d337d
3 changed files with 22 additions and 8 deletions

View file

@ -1,6 +1,7 @@
_ = require 'underscore'
Model = require './model'
Attributes = require '../attributes'
AccountStore = null
# We look for a few standard categories and display them in the Mailboxes
# portion of the left sidebar. Note that these may not all be present on
@ -97,7 +98,7 @@ class Category extends Model
@
displayType: =>
AccountStore = require '../stores/account-store'
AccountStore ?= require '../stores/account-store'
if AccountStore.accountForId(@accountId).usesLabels()
return 'label'
else

View file

@ -83,7 +83,7 @@ class AccountStore extends NylasStore
return unless NylasEnv.isMainWindow()
idx = _.findIndex @_accounts, (a) -> a.id is id
account = @_accounts[idx]
return if !account
return if !account or _.isEqual(account, updated)
account = _.extend(account, updated)
@_caches = {}
@_accounts[idx] = account

View file

@ -8,16 +8,29 @@ class ContactRankingStore extends NylasStore
constructor: ->
@_values = {}
@_valuesAllAccounts = null
@_disposables = []
@_disposables = {}
@listenTo AccountStore, @_onAccountsChanged
@_registerObservables(AccountStore.accounts())
_registerObservables: (accounts) =>
@_disposables.forEach (disp) -> disp.dispose()
@_disposables = accounts.map ({accountId}) =>
query = DatabaseStore.findJSONBlob("ContactRankingsFor#{accountId}")
return Rx.Observable.fromQuery(query)
.subscribe @_onRankingsChanged.bind(@, accountId)
nextDisposables = {}
# Create new observables, reusing existing ones when possible
# (so they don't trigger with initial state unnecesarily)
for acct in accounts
if @_disposables[acct.id]
nextDisposables[acct.id] = @_disposables[acct.id]
delete @_disposables[acct.id]
else
query = DatabaseStore.findJSONBlob("ContactRankingsFor#{acct.id}")
callback = @_onRankingsChanged.bind(@, acct.id)
nextDisposables[acct.id] = Rx.Observable.fromQuery(query).subscribe(callback)
# Remove unused observables in the old set
for key, disposable of @_disposables
disposable.dispose()
@_disposables = nextDisposables
_onRankingsChanged: (accountId, json) =>
@_values[accountId] = if json then json.value else null