From 3d9e412fda7a9cc09246823170684e015fdef638 Mon Sep 17 00:00:00 2001 From: Ben Gotow Date: Tue, 12 Jan 2016 09:32:34 -0800 Subject: [PATCH] Perf: Category Rather than computing the types ahead of time when the object is created and generating methods, keep a few standard methods around and change the hardcoded names to maps so the lookup is O[1] --- src/flux/models/category.coffee | 83 +++++++++++++-------------------- 1 file changed, 32 insertions(+), 51 deletions(-) diff --git a/src/flux/models/category.coffee b/src/flux/models/category.coffee index 6d027df6e..fdd9dc0c2 100644 --- a/src/flux/models/category.coffee +++ b/src/flux/models/category.coffee @@ -5,29 +5,29 @@ Attributes = require '../attributes' # 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 # a particular account. -StandardCategoryNames = [ - "inbox" - "important" - "sent" - "drafts" - "all" - "spam" - "archive" +StandardCategories = { + "inbox", + "important", + "sent", + "drafts", + "all", + "spam", + "archive", "trash" -] +} -LockedCategoryNames = [ +LockedCategories = { "sent" -] +} -HiddenCategoryNames = [ +HiddenCategories = { "sent" "drafts" "all" "archive" "starred" "important" -] +} AllMailName = "all" @@ -71,52 +71,17 @@ class Category extends Model User: 'user' Hidden: 'hidden' - @StandardCategoryNames: StandardCategoryNames - @LockedCategoryNames: LockedCategoryNames - @HiddenCategoryNames: HiddenCategoryNames + @StandardCategoryNames: Object.keys(StandardCategories) + @LockedCategoryNames: Object.keys(LockedCategories) + @HiddenCategoryNames: Object.keys(HiddenCategories) constructor: -> super - @_initCategoryTypes() fromJSON: (json) -> super - @_initCategoryTypes() @ - _initCategoryTypes: => - @types = [] - if not (@name in StandardCategoryNames) and not (@name in HiddenCategoryNames) - @types.push @constructor.Types.User - if @name in LockedCategoryNames - @types.push @constructor.Types.Hidden - if @name in StandardCategoryNames - @types.push @constructor.Types.Standard - if @name in HiddenCategoryNames - @types.push @constructor.Types.Hidden - - # Define getter for isStandardCategory. Must take into account important - # setting - Object.defineProperty @, "isStandardCategory", - enumerable: true - configurable: true - value: (showImportant)=> - showImportant ?= NylasEnv.config.get('core.workspace.showImportant') - val = @constructor.Types.Standard - if showImportant is true - val in @types - else - val in @types and @name isnt 'important' - - # Define getters for other category types - for key, val of @constructor.Types - continue if val is @constructor.Types.Standard - do (key, val) => - Object.defineProperty @, "is#{key}Category", - enumerable: true - configurable: true - value: => val in @types - hue: -> return 0 unless @displayName hue = 0 @@ -125,4 +90,20 @@ class Category extends Model hue = hue * (396.0/512.0) hue + isStandardCategory: (showImportant) -> + showImportant ?= NylasEnv.config.get('core.workspace.showImportant') + if showImportant is true + StandardCategories[@name]? + else + StandardCategories[@name]? and @name isnt 'important' + + isLockedCategory: -> + LockedCategories[@name]? + + isHiddenCategory: -> + HiddenCategories[@name]? + + isUserCategory: -> + not @isStandardCategory() and not @isHiddenCategory() + module.exports = Category