diff --git a/chrome/content/zotero/xpcom/data/items.js b/chrome/content/zotero/xpcom/data/items.js index e6b9247d37..77860139a7 100644 --- a/chrome/content/zotero/xpcom/data/items.js +++ b/chrome/content/zotero/xpcom/data/items.js @@ -872,11 +872,13 @@ Zotero.Items = function() { * @param {Object} [options] * @param {Function} [options.onProgress] - fn(progress, progressMax) * @param {Integer} [options.days] - Only delete items deleted more than this many days ago + * @param {Integer} [options.limit] - Number of items to delete */ this.emptyTrash = async function (libraryID, options = {}) { - if (typeof arguments[1] == 'number') { + if (arguments.length > 2 || typeof arguments[1] == 'number') { Zotero.warn("Zotero.Items.emptyTrash() has changed -- update your code"); options.days = arguments[1]; + options.limit = arguments[2]; } if (!libraryID) { @@ -886,6 +888,11 @@ Zotero.Items = function() { var t = new Date(); var deleted = await this.getDeleted(libraryID, false, options.days); + + if (options.limit) { + deleted = deleted.slice(0, options.limit); + } + var processed = 0; if (deleted.length) { let toDelete = { @@ -925,7 +932,7 @@ Zotero.Items = function() { * Start idle observer to delete trashed items older than a certain number of days */ this._emptyTrashIdleObserver = null; - this._emptyTrashTimer = null; + this._emptyTrashTimeoutID = null; this.startEmptyTrashTimer = function () { this._emptyTrashIdleObserver = { observe: (subject, topic, data) => { @@ -941,30 +948,31 @@ Zotero.Items = function() { // // TODO: increase number after dealing with slow // tag.getLinkedItems() call during deletes - var num = 10; - this.emptyTrash(Zotero.Libraries.userLibraryID, days, num) - .then(deleted => { + let num = 50; + this.emptyTrash( + Zotero.Libraries.userLibraryID, + { + days, + limit: num + } + ) + .then((deleted) => { if (!deleted) { - this._emptyTrashTimer = null; + this._emptyTrashTimeoutID = null; return; } // Set a timer to do more every few seconds - if (!this._emptyTrashTimer) { - this._emptyTrashTimer = Components.classes["@mozilla.org/timer;1"] - .createInstance(Components.interfaces.nsITimer); - } - this._emptyTrashTimer.init( - this._emptyTrashIdleObserver.observe, - 5 * 1000, - Components.interfaces.nsITimer.TYPE_ONE_SHOT - ); + this._emptyTrashTimeoutID = setTimeout(() => { + this._emptyTrashIdleObserver.observe(null, 'timer-callback', null); + }, 2500); }); } // When no longer idle, cancel timer else if (topic == 'back') { - if (this._emptyTrashTimer) { - this._emptyTrashTimer.cancel(); + if (this._emptyTrashTimeoutID) { + clearTimeout(this._emptyTrashTimeoutID); + this._emptyTrashTimeoutID = null; } } }