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

View File

@ -1127,23 +1127,20 @@ Zotero.DataObject.prototype.erase = Zotero.Promise.coroutine(function* (options)
env.options.tx = true;
}
var proceed = yield this._eraseInit(env);
if (!proceed) return false;
Zotero.debug('Deleting ' + this.objectType + ' ' + this.id);
if (env.options.tx) {
return Zotero.DB.executeTransaction(function* () {
Zotero.DataObject.prototype._initErase.call(this, env);
yield this._eraseData(env);
yield this._erasePreCommit(env);
return this._erasePostCommit(env);
Zotero.DataObject.prototype._finalizeErase.call(this, env);
}.bind(this))
}
else {
Zotero.DB.requireTransaction();
Zotero.DataObject.prototype._initErase.call(this, env);
yield this._eraseData(env);
yield this._erasePreCommit(env);
return this._erasePostCommit(env);
Zotero.DataObject.prototype._finalizeErase.call(this, env);
}
});
@ -1153,23 +1150,28 @@ Zotero.DataObject.prototype.eraseTx = function (options) {
return this.erase(options);
};
Zotero.DataObject.prototype._eraseInit = function(env) {
if (!this.id) return Zotero.Promise.resolve(false);
Zotero.DataObject.prototype._initErase = function (env) {
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);
};
Zotero.DataObject.prototype._eraseData = function(env) {
throw new Error("Zotero.DataObject.prototype._eraseData is an abstract method");
};
Zotero.DataObject.prototype._erasePreCommit = function(env) {
return Zotero.Promise.resolve();
};
Zotero.DataObject.prototype._erasePostCommit = function(env) {
return Zotero.Promise.resolve();
};
if (!env.options.skipNotifier) {
Zotero.Notifier.queue(
'delete',
this._objectType,
Object.keys(env.notifierData).map(id => parseInt(id)),
env.notifierData
);
}
}
/**
* 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.DB.requireTransaction();
@ -3792,21 +3779,11 @@ Zotero.Item.prototype._eraseData = Zotero.Promise.coroutine(function* (env) {
//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);
if (env.parentItem) {
yield env.parentItem.reload(['primaryData', 'childItems'], true);
env.parentItem.clearBestAttachmentState();
}
this.ObjectsClass.unload(this.id);
if (!env.options.skipNotifier) {
Zotero.Notifier.queue('delete', 'item', this.id, env.deletedItemNotifierData);
if (parentItem) {
yield parentItem.reload(['primaryData', 'childItems'], true);
parentItem.clearBestAttachmentState();
}
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.DB.requireTransaction();
var notifierData = {};
notifierData[this.id] = {
libraryID: this.libraryID,
key: this.key
};
var sql = "DELETE FROM savedSearchConditions WHERE savedSearchID=?";
yield Zotero.DB.queryAsync(sql, this.id);
var sql = "DELETE FROM savedSearches WHERE savedSearchID=?";
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);
}
});
});
})
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 () {
it("should not allow an id change", function* () {