Clean up DataObject erasing, and fix search unloading

This commit is contained in:
Dan Stillman 2015-06-16 19:50:25 -04:00
parent 4f155e3432
commit 0d59bde186
5 changed files with 43 additions and 74 deletions

View File

@ -561,12 +561,6 @@ Zotero.Collection.prototype._eraseData = Zotero.Promise.coroutine(function* (env
var descendents = yield this.getDescendents(false, null, true); var descendents = yield this.getDescendents(false, null, true);
var items = []; var items = [];
var notifierData = {};
notifierData[this.id] = {
libraryID: this.libraryID,
key: this.key
};
var del = []; var del = [];
for(var i=0, len=descendents.length; i<len; i++) { for(var i=0, len=descendents.length; i<len; i++) {
// Descendent collections // Descendent collections
@ -574,7 +568,7 @@ Zotero.Collection.prototype._eraseData = Zotero.Promise.coroutine(function* (env
collections.push(descendents[i].id); collections.push(descendents[i].id);
var c = yield this.ObjectsClass.getAsync(descendents[i].id); var c = yield this.ObjectsClass.getAsync(descendents[i].id);
if (c) { if (c) {
notifierData[c.id] = { env.notifierData[c.id] = {
libraryID: c.libraryID, libraryID: c.libraryID,
key: c.key key: c.key
}; };
@ -607,13 +601,7 @@ Zotero.Collection.prototype._eraseData = Zotero.Promise.coroutine(function* (env
+ '(' + placeholders + ')', collections); + '(' + placeholders + ')', collections);
// TODO: Update member items // TODO: Update member items
// Clear deleted collection from internal memory env.deletedObjectIDs = collections;
this.ObjectsClass.unload(collections);
//return Zotero.Collections.reloadAll();
if (!env.options.skipNotifier) {
Zotero.Notifier.queue('delete', 'collection', collections, notifierData);
}
}); });

View File

@ -1127,23 +1127,20 @@ Zotero.DataObject.prototype.erase = Zotero.Promise.coroutine(function* (options)
env.options.tx = true; env.options.tx = true;
} }
var proceed = yield this._eraseInit(env);
if (!proceed) return false;
Zotero.debug('Deleting ' + this.objectType + ' ' + this.id); Zotero.debug('Deleting ' + this.objectType + ' ' + this.id);
if (env.options.tx) { if (env.options.tx) {
return Zotero.DB.executeTransaction(function* () { return Zotero.DB.executeTransaction(function* () {
Zotero.DataObject.prototype._initErase.call(this, env);
yield this._eraseData(env); yield this._eraseData(env);
yield this._erasePreCommit(env); Zotero.DataObject.prototype._finalizeErase.call(this, env);
return this._erasePostCommit(env);
}.bind(this)) }.bind(this))
} }
else { else {
Zotero.DB.requireTransaction(); Zotero.DB.requireTransaction();
Zotero.DataObject.prototype._initErase.call(this, env);
yield this._eraseData(env); yield this._eraseData(env);
yield this._erasePreCommit(env); Zotero.DataObject.prototype._finalizeErase.call(this, env);
return this._erasePostCommit(env);
} }
}); });
@ -1153,23 +1150,28 @@ Zotero.DataObject.prototype.eraseTx = function (options) {
return this.erase(options); return this.erase(options);
}; };
Zotero.DataObject.prototype._eraseInit = function(env) { Zotero.DataObject.prototype._initErase = function (env) {
if (!this.id) return Zotero.Promise.resolve(false); env.notifierData = {};
env.notifierData[this.id] = {
libraryID: this.libraryID,
key: this.key
};
};
Zotero.DataObject.prototype._finalizeErase = function (env) {
Zotero.DB.addCurrentCallback("commit", function () {
this.ObjectsClass.unload(env.deletedObjectIDs || this.id);
}.bind(this));
return Zotero.Promise.resolve(true); if (!env.options.skipNotifier) {
}; Zotero.Notifier.queue(
'delete',
Zotero.DataObject.prototype._eraseData = function(env) { this._objectType,
throw new Error("Zotero.DataObject.prototype._eraseData is an abstract method"); Object.keys(env.notifierData).map(id => parseInt(id)),
}; env.notifierData
);
Zotero.DataObject.prototype._erasePreCommit = function(env) { }
return Zotero.Promise.resolve(); }
};
Zotero.DataObject.prototype._erasePostCommit = function(env) {
return Zotero.Promise.resolve();
};
/** /**
* Generates data object key * Generates data object key

View File

@ -3720,19 +3720,6 @@ Zotero.Item.prototype.copy = Zotero.Promise.coroutine(function* () {
});; });;
Zotero.Item.prototype._eraseInit = Zotero.Promise.coroutine(function* (env) {
var proceed = yield Zotero.Item._super.prototype._eraseInit.apply(this, arguments);
if (!proceed) return false;
env.deletedItemNotifierData = {};
env.deletedItemNotifierData[this.id] = {
libraryID: this.libraryID,
key: this.key
};
return true;
});
Zotero.Item.prototype._eraseData = Zotero.Promise.coroutine(function* (env) { Zotero.Item.prototype._eraseData = Zotero.Promise.coroutine(function* (env) {
Zotero.DB.requireTransaction(); Zotero.DB.requireTransaction();
@ -3792,21 +3779,11 @@ Zotero.Item.prototype._eraseData = Zotero.Promise.coroutine(function* (env) {
//Zotero.Fulltext.clearItemContent(this.id); //Zotero.Fulltext.clearItemContent(this.id);
} }
env.parentItem = parentItem;
});
Zotero.Item.prototype._erasePreCommit = Zotero.Promise.coroutine(function* (env) {
yield Zotero.DB.queryAsync('DELETE FROM items WHERE itemID=?', this.id); yield Zotero.DB.queryAsync('DELETE FROM items WHERE itemID=?', this.id);
if (env.parentItem) { if (parentItem) {
yield env.parentItem.reload(['primaryData', 'childItems'], true); yield parentItem.reload(['primaryData', 'childItems'], true);
env.parentItem.clearBestAttachmentState(); parentItem.clearBestAttachmentState();
}
this.ObjectsClass.unload(this.id);
if (!env.options.skipNotifier) {
Zotero.Notifier.queue('delete', 'item', this.id, env.deletedItemNotifierData);
} }
Zotero.Prefs.set('purge.items', true); Zotero.Prefs.set('purge.items', true);

View File

@ -220,21 +220,11 @@ Zotero.Search.prototype.clone = function (libraryID) {
Zotero.Search.prototype._eraseData = Zotero.Promise.coroutine(function* (env) { Zotero.Search.prototype._eraseData = Zotero.Promise.coroutine(function* (env) {
Zotero.DB.requireTransaction(); Zotero.DB.requireTransaction();
var notifierData = {};
notifierData[this.id] = {
libraryID: this.libraryID,
key: this.key
};
var sql = "DELETE FROM savedSearchConditions WHERE savedSearchID=?"; var sql = "DELETE FROM savedSearchConditions WHERE savedSearchID=?";
yield Zotero.DB.queryAsync(sql, this.id); yield Zotero.DB.queryAsync(sql, this.id);
var sql = "DELETE FROM savedSearches WHERE savedSearchID=?"; var sql = "DELETE FROM savedSearches WHERE savedSearchID=?";
yield Zotero.DB.queryAsync(sql, this.id); yield Zotero.DB.queryAsync(sql, this.id);
if (!env.options.skipNotifier) {
Zotero.Notifier.queue('delete', 'search', this.id, notifierData);
}
}); });

View File

@ -42,7 +42,19 @@ describe("Zotero.DataObjects", function () {
assert.isFalse(libraryKey); assert.isFalse(libraryKey);
} }
}); });
}); })
describe("#exists()", function () {
it("should return false after object is deleted", function* () {
for (let type of types) {
let objectsClass = Zotero.DataObjectUtilities.getObjectsClassForObjectType(type);
let obj = yield createDataObject(type);
let id = obj.id;
yield obj.eraseTx();
assert.isFalse(objectsClass.exists(id), type + " does not exist");
}
})
})
describe("#_setIdentifier", function () { describe("#_setIdentifier", function () {
it("should not allow an id change", function* () { it("should not allow an id change", function* () {