Skip to content

Commit

Permalink
Update automatic trash emptying after 3872e64
Browse files Browse the repository at this point in the history
And restore 'limit' parameter used for auto-empty
  • Loading branch information
dstillman committed Sep 16, 2017
1 parent e25af42 commit fb61819
Showing 1 changed file with 25 additions and 17 deletions.
42 changes: 25 additions & 17 deletions chrome/content/zotero/xpcom/data/items.js
Expand Up @@ -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) {
Expand All @@ -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 = {
Expand Down Expand Up @@ -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) => {
Expand All @@ -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;
}
}
}
Expand Down

0 comments on commit fb61819

Please sign in to comment.