diff --git a/spec/models/category-spec.coffee b/spec/models/category-spec.coffee index eeacd2651..729ac44c0 100644 --- a/spec/models/category-spec.coffee +++ b/spec/models/category-spec.coffee @@ -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 diff --git a/src/flux/models/category.coffee b/src/flux/models/category.coffee index fdd9dc0c2..841437a2f 100644 --- a/src/flux/models/category.coffee +++ b/src/flux/models/category.coffee @@ -98,7 +98,7 @@ class Category extends Model StandardCategories[@name]? and @name isnt 'important' isLockedCategory: -> - LockedCategories[@name]? + LockedCategories[@name]? isHiddenCategory: -> HiddenCategories[@name]? diff --git a/src/flux/stores/category-store.coffee b/src/flux/stores/category-store.coffee index b4da97c5b..94f04f064 100644 --- a/src/flux/stores/category-store.coffee +++ b/src/flux/stores/category-store.coffee @@ -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()