Wait for items list to refresh before handling notifications

This fixes an error if New Item is used before the items list has
loaded.
This commit is contained in:
Dan Stillman 2015-05-10 04:18:40 -04:00
parent 6faa2caff8
commit e584dbf5dd
2 changed files with 124 additions and 99 deletions

View file

@ -2141,9 +2141,11 @@ Zotero.CollectionTreeCache = {
"clear": Zotero.Promise.coroutine(function* () { "clear": Zotero.Promise.coroutine(function* () {
this.lastTreeRow = null; this.lastTreeRow = null;
this.lastSearch = null; this.lastSearch = null;
yield Zotero.DB.waitForTransaction();
if(this.lastTempTable) { if(this.lastTempTable) {
yield Zotero.DB.queryAsync("DROP TABLE IF EXISTS " + this.lastTempTable); // Drop the last temp table when we can. We don't wait on this because it can cause a
// deadlock: this waits on open transactions, but a transaction could be waiting on
// ItemTreeView::notify(), which waits on ItemTreeView::refresh(), which calls this.
Zotero.DB.queryTx("DROP TABLE IF EXISTS " + this.lastTempTable).done();
} }
this.lastTempTable = null; this.lastTempTable = null;
this.lastResults = null; this.lastResults = null;

View file

@ -51,6 +51,8 @@ Zotero.ItemTreeView = function (collectionTreeRow, sourcesOnly) {
this._cellTextCache = {}; this._cellTextCache = {};
this._itemImages = {}; this._itemImages = {};
this._refreshPromise = Zotero.Promise.resolve();
this._unregisterID = Zotero.Notifier.registerObserver( this._unregisterID = Zotero.Notifier.registerObserver(
this, ['item', 'collection-item', 'item-tag', 'share-items', 'bucket'], 'itemTreeView' this, ['item', 'collection-item', 'item-tag', 'share-items', 'bucket'], 'itemTreeView'
); );
@ -289,6 +291,13 @@ Zotero.ItemTreeView.prototype.refresh = Zotero.serial(Zotero.Promise.coroutine(f
return false; return false;
} }
var resolve, reject;
this._refreshPromise = new Zotero.Promise(function () {
resolve = arguments[0];
reject = arguments[1];
});
try {
for (let i=0; i<visibleFields.length; i++) { for (let i=0; i<visibleFields.length; i++) {
let field = visibleFields[i]; let field = visibleFields[i];
switch (field) { switch (field) {
@ -396,8 +405,20 @@ Zotero.ItemTreeView.prototype.refresh = Zotero.serial(Zotero.Promise.coroutine(f
//this._treebox.endUpdateBatch(); //this._treebox.endUpdateBatch();
this.selection.selectEventsSuppressed = false; this.selection.selectEventsSuppressed = false;
} }
setTimeout(function () {
resolve();
});
}
catch (e) {
setTimeout(function () {
reject(e);
});
throw e;
}
})); }));
/** /**
* Generator used internally for refresh * Generator used internally for refresh
*/ */
@ -409,6 +430,8 @@ Zotero.ItemTreeView._haveCachedFields = false;
*/ */
Zotero.ItemTreeView.prototype.notify = Zotero.Promise.coroutine(function* (action, type, ids, extraData) Zotero.ItemTreeView.prototype.notify = Zotero.Promise.coroutine(function* (action, type, ids, extraData)
{ {
yield this._refreshPromise;
if (!this._treebox || !this._treebox.treeBody) { if (!this._treebox || !this._treebox.treeBody) {
Components.utils.reportError("Treebox didn't exist in itemTreeView.notify()"); Components.utils.reportError("Treebox didn't exist in itemTreeView.notify()");
return; return;