[gnome-documents] all: turn managers cache into associative arrays
- From: Cosimo Cecchi <cosimoc src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gnome-documents] all: turn managers cache into associative arrays
- Date: Tue, 30 Aug 2011 18:06:43 +0000 (UTC)
commit 41d68d5ac9b6dca5c0bf114d17aadcf264b34061
Author: Cosimo Cecchi <cosimoc gnome org>
Date: Tue Aug 30 14:02:27 2011 -0400
all: turn managers cache into associative arrays
Makes code way simpler.
src/categories.js | 30 +++++++++++++-----------
src/documents.js | 30 ++++++++++--------------
src/sidebar.js | 14 +++++-----
src/sources.js | 64 ++++++++++++++++++++++++++++------------------------
4 files changed, 70 insertions(+), 68 deletions(-)
---
diff --git a/src/categories.js b/src/categories.js
index 8fc11f8..7cb35a0 100644
--- a/src/categories.js
+++ b/src/categories.js
@@ -63,30 +63,28 @@ function CategoryManager() {
CategoryManager.prototype = {
_init: function() {
- this.categories = [];
+ this._categories = {};
- this.categories.push(new Category('recent', _("New and Recent"), ''));
-
- this.categories.push(new Category('favorites', _("Favorites"), 'emblem-favorite-symbolic'));
- this.categories.push(new Category('shared', _("Shared with you"), 'emblem-shared-symbolic'));
+ let category;
+ category = new Category('recent', _("New and Recent"), '');
+ this._categories[category.id] = category;
+ category = new Category('favorites', _("Favorites"), 'emblem-favorite-symbolic');
+ this._categories[category.id] = category;
+ category = new Category('shared', _("Shared with you"), 'emblem-shared-symbolic');
+ this._categories[category.id] = category;
// unimplemented
- this.categories.push(new Category('private', _("Private"), 'channel-secure-symbolic'));
+ category = new Category('private', _("Private"), 'channel-secure-symbolic');
+ this._categories[category.id] = category;
this.setActiveCategoryId('recent');
},
setActiveCategoryId: function(id) {
- let matched = this.categories.filter(Lang.bind(this,
- function(category) {
- return (category.id == id);
- }));
-
- if (!matched.length)
+ if (!this._categories[id])
return;
- this.activeCategory = matched[0];
-
+ this.activeCategory = this._categories[id];
this.emit('active-category-changed');
},
@@ -100,6 +98,10 @@ CategoryManager.prototype = {
getActiveCategoryFilter: function(subject) {
return this.activeCategory.getFilter(subject);
+ },
+
+ getCategories: function() {
+ return this._categories;
}
};
Signals.addSignalMethods(CategoryManager.prototype);
diff --git a/src/documents.js b/src/documents.js
index 2c11c24..3e691b5 100644
--- a/src/documents.js
+++ b/src/documents.js
@@ -438,7 +438,7 @@ function DocumentManager() {
DocumentManager.prototype = {
_init: function() {
- this._docs = [];
+ this._docs = {};
this._pixbufFrame = GdkPixbuf.Pixbuf.new_from_file(Path.ICONS_DIR + 'thumbnail-frame.png');
},
@@ -461,15 +461,15 @@ DocumentManager.prototype = {
else
doc = new LocalDocument(cursor);
- this._docs.push(doc);
+ this._docs[doc.urn] = doc;
this.emit('new-document', doc);
},
clear: function() {
- this._docs.forEach(function(doc) {
- doc.destroy();
- });
- this._docs = [];
+ for (idx in this._docs) {
+ this._docs[idx].destroy();
+ };
+ this._docs = {};
this.emit('clear');
},
@@ -478,15 +478,12 @@ DocumentManager.prototype = {
},
lookupDocument: function(urn) {
- let matched = this._docs.filter(
- function(doc) {
- return (doc.urn == urn);
- });
+ let document = null;
- if (!matched.length)
- return null;
+ if (this._docs[urn])
+ document = this._docs[urn];
- return matched[0];
+ return document;
}
};
Signals.addSignalMethods(DocumentManager.prototype);
@@ -510,10 +507,9 @@ DocumentModel.prototype = {
this._documentManager.connect('clear', Lang.bind(this, this._onManagerClear));
this._documentManager.connect('new-document', Lang.bind(this, this._onNewDocument));
- this._documentManager.getDocuments().forEach(Lang.bind(this,
- function(document) {
- this._onNewDocument(this._documentManager, document);
- }));
+ let documents = this._documentManager.getDocuments();
+ for (idx in this._documentManager.getDocuments())
+ this._onNewDocument(this._documentManager, documents[idx]);
},
_onManagerClear: function() {
diff --git a/src/sidebar.js b/src/sidebar.js
index c81a021..e5e1874 100644
--- a/src/sidebar.js
+++ b/src/sidebar.js
@@ -53,13 +53,13 @@ SidebarModel.prototype = {
this.model = Gd.create_sidebar_store();
this._categoryManager = Global.categoryManager;
- let categories = this._categoryManager.categories;
- categories.forEach(Lang.bind(this,
- function(category) {
- iter = this.model.append();
- Gd.sidebar_store_set(this.model, iter,
- category.id, category.name, category.icon, false);
- }));
+ let categories = this._categoryManager.getCategories();
+ for (idx in categories) {
+ let category = categories[idx];
+ iter = this.model.append();
+ Gd.sidebar_store_set(this.model, iter,
+ category.id, category.name, category.icon, false);
+ };
}
};
diff --git a/src/sources.js b/src/sources.js
index d9ed694..2f7525a 100644
--- a/src/sources.js
+++ b/src/sources.js
@@ -98,18 +98,22 @@ function SourceManager(initCallback) {
SourceManager.prototype = {
_init: function(initCallback) {
this._client = null;
- this.sources = [];
+ this._sources = {};
this._initCallback = initCallback;
// two outstanding ops for the local sources, and one for the GOA client
this._outstandingOps = 3;
- this.sources.push(new Source({ id: 'all',
- name: _("All"),
- initCallback: Lang.bind(this, this._initSourceCollector) }));
- this.sources.push(new Source({ id: 'local',
- name: _("Local"),
- initCallback: Lang.bind(this, this._initSourceCollector) }));
+
+ let source = new Source({ id: 'all',
+ name: _("All"),
+ initCallback: Lang.bind(this, this._initSourceCollector) });
+ this._sources[source.id] = source;
+
+ source = new Source({ id: 'local',
+ name: _("Local"),
+ initCallback: Lang.bind(this, this._initSourceCollector) });
+ this._sources[source.id] = source;
Goa.Client.new(null, Lang.bind(this, this._onGoaClientCreated));
},
@@ -134,8 +138,9 @@ SourceManager.prototype = {
return;
this._outstandingOps++;
- this.sources.push(new Source({ object: object,
- initCallback: Lang.bind(this, this._initSourceCollector) }));
+ let source = new Source({ object: object,
+ initCallback: Lang.bind(this, this._initSourceCollector) });
+ this._sources.push[source.id] = source;
}));
let activeSourceId = Global.settings.get_string('active-source');
@@ -152,15 +157,10 @@ SourceManager.prototype = {
},
setActiveSourceId: function(id) {
- let matched = this.sources.filter(Lang.bind(this,
- function(source) {
- return (source.id == id);
- }));
-
- if (!matched.length)
+ if (!this._sources[id])
return;
- this.activeSource = matched[0];
+ this.activeSource = this._sources[id];
Global.settings.set_string('active-source', this.activeSource.id);
this.emit('active-source-changed');
@@ -175,15 +175,19 @@ SourceManager.prototype = {
},
getSourceByUrn: function(resourceUrn) {
- let matched = this.sources.filter(Lang.bind(this,
- function(source) {
- return (source.resourceUrn == resourceUrn);
- }));
+ let source = null;
+ for (idx in this._sources) {
+ if (this._sources[idx].resourceUrn == resourceUrn) {
+ source = this._sources[idx];
+ break;
+ }
+ }
- if (!matched.length)
- return null;
+ return source;
+ },
- return matched[0];
+ getSources: function() {
+ return this._sources;
}
};
Signals.addSignalMethods(SourceManager.prototype);
@@ -209,13 +213,13 @@ SourceModel.prototype = {
Gd.sources_store_set(this.model, iter,
'', _("Sources"), true);
- let sources = this._sourceManager.sources;
- sources.forEach(Lang.bind(this,
- function(source) {
- iter = this.model.append();
- Gd.sources_store_set(this.model, iter,
- source.id, source.name, false);
- }));
+ let sources = this._sourceManager.getSources();
+ for (idx in sources) {
+ let source = sources[idx];
+ iter = this.model.append();
+ Gd.sources_store_set(this.model, iter,
+ source.id, source.name, false);
+ };
}
};
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]