Load groups at startup, and make Zotero.Groups functions synchronous

Groups were already being loaded for the collections list, so we might
as well just store them initially and let Zotero.Libraries.getName() be
a synchronous call.
This commit is contained in:
Dan Stillman 2015-05-22 04:49:02 -04:00
parent f0b44b2047
commit 32abbe7c25
7 changed files with 61 additions and 49 deletions

View file

@ -48,12 +48,6 @@ var ZoteroAdvancedSearch = new function() {
io.dataIn.search.loadPrimaryData()
.then(function () {
return Zotero.Groups.getAll();
})
.then(function (groups) {
// Since the search box can be used as a modal dialog, which can't use promises,
// it expects groups to be passed in.
_searchBox.groups = groups;
_searchBox.search = io.dataIn.search;
});
}

View file

@ -95,8 +95,9 @@
menupopup.appendChild(menuitem);
// Add groups
for (let i = 0; i < this.groups.length; i++) {
let group = this.groups[i];
var groups = Zotero.Groups.getAll();
for (let i = 0; i < groups.length; i++) {
let group = groups[i];
let menuitem = document.createElement('menuitem');
menuitem.setAttribute('label', group.name);
menuitem.setAttribute('libraryID', group.libraryID);

View file

@ -200,7 +200,7 @@ Zotero.CollectionTreeView.prototype.refresh = Zotero.Promise.coroutine(function*
);
// Add groups
var groups = yield Zotero.Groups.getAll();
var groups = Zotero.Groups.getAll();
if (groups.length) {
this._addRowToArray(
newRows,

View file

@ -287,7 +287,7 @@ Zotero.Group.prototype.save = function () {
throw (e);
}
//Zotero.Groups.reload(this.id);
Zotero.Groups.register(this);
Zotero.Notifier.trigger('add', 'group', this.id);
}
@ -369,6 +369,7 @@ Zotero.Group.prototype.erase = Zotero.Promise.coroutine(function* () {
Zotero.Notifier.enable();
}
Zotero.Groups.unregister(this.id);
Zotero.Notifier.trigger('delete', 'group', this.id, notifierData);
});
@ -391,7 +392,7 @@ Zotero.Group.prototype.serialize = function() {
Zotero.Group.prototype._requireLoad = function () {
if (!this._loaded) {
if (!this._loaded && Zotero.Groups.exists(this.id)) {
throw new Error("Group has not been loaded");
}
}

View file

@ -27,39 +27,38 @@
Zotero.Groups = new function () {
this.__defineGetter__('addGroupURL', function () ZOTERO_CONFIG.WWW_BASE_URL + 'groups/new/');
var _cache = {};
var _groupIDsByLibraryID = {};
var _libraryIDsByGroupID = {};
this.init = function () {
_loadIDs();
this.init = Zotero.Promise.coroutine(function* () {
yield _load();
});
/**
* @param {Integer} id - Group id
* @return {Zotero.Group}
*/
this.get = function (id) {
if (!id) throw new Error("groupID not provided");
return _cache[id] ? _cache[id] : false;
}
this.get = Zotero.Promise.coroutine(function* (id) {
if (!id) {
throw new Error("groupID not provided");
}
var group = new Zotero.Group;
group.id = id;
if (!(yield group.load())) {
return false;
}
return group;
});
this.getAll = Zotero.Promise.coroutine(function* () {
var groups = [];
var sql = "SELECT groupID FROM groups ORDER BY name COLLATE locale";
var groupIDs = yield Zotero.DB.columnQueryAsync(sql);
if (!groupIDs.length) {
return groups;
}
for each(var groupID in groupIDs) {
groups.push(this.get(groupID));
}
return Zotero.Promise.all(groups);
});
/**
* Get all groups, sorted by name
*
* @return {Zotero.Group[]}
*/
this.getAll = function () {
var groups = [for (id of Object.keys(_cache)) _cache[id]];
var collation = Zotero.getLocaleCollation();
groups.sort(function(a, b) {
return collation.compareString(1, a.name, b.name);
});
return groups;
}
this.getByLibraryID = function (libraryID) {
@ -91,15 +90,32 @@ Zotero.Groups = new function () {
}
function _loadIDs() {
var sql = "SELECT libraryID, groupID FROM groups";
return Zotero.DB.queryAsync(sql)
.then(function (rows) {
for (let i=0; i<rows.length; i++) {
let row = rows[i];
_groupIDsByLibraryID[row.libraryID] = row.groupID;
_libraryIDsByGroupID[row.groupID] = row.libraryID;
}
}.bind(this));
this.register = function (group) {
_libraryIDsByGroupID[group.id] = group.libraryID;
_groupIDsByLibraryID[group.libraryID] = group.id;
_cache[group.id] = group;
}
this.unregister = function (id) {
var libraryID = _libraryIDsByGroupID[groupID];
delete _groupIDsByLibraryID[libraryID];
delete _libraryIDsByGroupID[groupID];
delete _cache[id];
}
var _load = Zotero.Promise.coroutine(function* () {
var sql = "SELECT libraryID, groupID FROM groups";
var rows = yield Zotero.DB.queryAsync(sql)
for (let i=0; i<rows.length; i++) {
let row = rows[i];
_groupIDsByLibraryID[row.libraryID] = row.groupID;
_libraryIDsByGroupID[row.groupID] = row.libraryID;
let group = new Zotero.Group;
group.id = row.groupID;
yield group.load();
_cache[row.groupID] = group;
}
});
}

View file

@ -604,7 +604,7 @@ Components.utils.import("resource://gre/modules/osfile.jsm");
Zotero.Collections.init();
Zotero.Items.init();
yield Zotero.Searches.init();
Zotero.Groups.init();
yield Zotero.Groups.init();
yield Zotero.QuickCopy.init();

View file

@ -596,7 +596,7 @@ function ZoteroProtocolHandler() {
// FIXME: Hack to exclude group libraries for now
var search = new Zotero.Search();
search.setScope(s);
var groups = yield Zotero.Groups.getAll();
var groups = Zotero.Groups.getAll();
for each(var group in groups) {
search.addCondition('libraryID', 'isNot', group.libraryID);
}