Fix CategoryStore sorting:

- Also fix Category model specs
This commit is contained in:
Juan Tejada 2016-01-13 14:20:44 -08:00
parent fe625d8f6d
commit c5d4b10c7d
3 changed files with 41 additions and 15 deletions

View file

@ -2,12 +2,11 @@
describe 'Category', ->
describe '_initCategoryTypes', ->
describe 'category types', ->
it 'assigns type correctly when it is a user category', ->
cat = new Label
cat.name = undefined
cat._initCategoryTypes()
expect(cat.isUserCategory()).toBe true
expect(cat.isStandardCategory()).toBe false
expect(cat.isHiddenCategory()).toBe false
@ -16,7 +15,6 @@ describe 'Category', ->
it 'assigns type correctly when it is a standard category', ->
cat = new Label
cat.name = 'inbox'
cat._initCategoryTypes()
expect(cat.isUserCategory()).toBe false
expect(cat.isStandardCategory()).toBe true
expect(cat.isHiddenCategory()).toBe false
@ -25,7 +23,6 @@ describe 'Category', ->
it 'assigns type for `important` category when should not show important', ->
cat = new Label
cat.name = 'important'
cat._initCategoryTypes()
expect(cat.isUserCategory()).toBe false
expect(cat.isStandardCategory(false)).toBe false
expect(cat.isHiddenCategory()).toBe true
@ -34,7 +31,6 @@ describe 'Category', ->
it 'assigns type correctly when it is a hidden category', ->
cat = new Label
cat.name = 'archive'
cat._initCategoryTypes()
expect(cat.isUserCategory()).toBe false
expect(cat.isStandardCategory()).toBe true
expect(cat.isHiddenCategory()).toBe true
@ -43,8 +39,7 @@ describe 'Category', ->
it 'assigns type correctly when it is a locked category', ->
cat = new Label
cat.name = 'sent'
cat._initCategoryTypes()
expect(cat.isUserCategory()).toBe false
expect(cat.isStandardCategory()).toBe true
expect(cat.isHiddenCategory()).toBe true
expect(cat.isLockedCategory()).toBe false
expect(cat.isLockedCategory()).toBe true

View file

@ -98,7 +98,7 @@ class Category extends Model
StandardCategories[@name]? and @name isnt 'important'
isLockedCategory: ->
LockedCategories[@name]?
LockedCategories[@name]?
isHiddenCategory: ->
HiddenCategories[@name]?

View file

@ -9,11 +9,14 @@ class CategoryStore extends NylasStore
constructor: ->
@_categoryCache = {}
@_standardCategories = {}
@_userCategories = {}
@_hiddenCategories = {}
@_registerObservables(AccountStore.accounts())
@listenTo AccountStore, @_onAccountsChanged
byId: (account, categoryId) -> @categories(account)[categoryId]
byId: (account, categoryId) ->
@categories(account)[categoryId]
# Public: Returns an array of all categories for an account, both
# standard and user generated. The items returned by this function will be
@ -31,16 +34,21 @@ class CategoryStore extends NylasStore
# Public: Returns all of the standard categories for the current account.
#
standardCategories: (account) ->
_.values(@categories(account)).filter (cat) -> cat.isStandardCategory()
return [] unless account
_.compact(
StandardCategoryNames.map (name) => @_standardCategories[account.id][name]
)
hiddenCategories: (account) ->
_.values(@categories(account)).filter (cat) -> cat.isHiddenCategory()
return [] unless account
@_hiddenCategories[account.id]
# Public: Returns all of the categories that are not part of the standard
# category set.
#
userCategories: (account) ->
_.values(@categories(account)).filter (cat) -> cat.isUserCategory()
return [] unless account
@_userCategories[account.id]
# Public: Returns the Folder or Label object for a standard category name and
# for a given account.
@ -71,20 +79,43 @@ class CategoryStore extends NylasStore
@getStandardCategory(account, "trash")
_onAccountsChanged: ->
@_registerObservables(AccountStore.accounts())
accounts = AccountStore.accounts()
@_removeStaleCategories(accounts)
@_registerObservables(accounts)
_onCategoriesChanged: (accountId, categories) =>
return unless categories
@_categoryCache[accountId] = {}
@_standardCategories[accountId] = {}
@_userCategories[accountId] = []
@_hiddenCategories[accountId] = []
for category in categories
@_categoryCache[accountId][category.id] = category
if category.isStandardCategory()
@_standardCategories[accountId][category.name] = category
if category.isUserCategory()
@_userCategories[accountId].push(category)
if category.isHiddenCategory()
@_hiddenCategories[accountId].push(category)
@trigger()
# Remove any category sets for removed accounts
# Will prevent memory leaks
_removeStaleCategories: (accounts) ->
accountIds = accounts.map (acc) -> acc.id
removedAccountIds = _.difference(_.keys(@_categoryCache), accountIds)
for accountId in removedAccountIds
delete @_categoryCache[accountId]
delete @_standardCategories[accountId]
delete @_userCategories[accountId]
delete @_hiddenCategories[accountId]
_registerObservables: (accounts) =>
@_disposables ?= []
@_disposables.forEach (disp) -> disp.dispose()
@_disposables = accounts.map (account) =>
Categories.forAccount(account)
Categories.forAccount(account).sort()
.subscribe(@_onCategoriesChanged.bind(@, account.id))
module.exports = new CategoryStore()