From 493d37d1c7650ff645213e7fc36844f2eafb3048 Mon Sep 17 00:00:00 2001 From: Dan Stillman Date: Mon, 12 Oct 2015 19:00:05 -0400 Subject: [PATCH] Fix 100% CPU usage with pane open (since 9e356a7e6) And make Zotero.Item.prototype.fileExists() check the file regardless of whether there's a cached state. --- chrome/content/zotero/xpcom/data/item.js | 5 +-- test/tests/itemTest.js | 51 ++++++++++++++++++++++++ 2 files changed, 52 insertions(+), 4 deletions(-) diff --git a/chrome/content/zotero/xpcom/data/item.js b/chrome/content/zotero/xpcom/data/item.js index c36ad821d..e89e86b1a 100644 --- a/chrome/content/zotero/xpcom/data/item.js +++ b/chrome/content/zotero/xpcom/data/item.js @@ -2090,6 +2090,7 @@ Zotero.Item.prototype.getFilePathAsync = Zotero.Promise.coroutine(function* () { return false; } + this._updateAttachmentStates(true); return path; } @@ -2215,10 +2216,6 @@ Zotero.Item.prototype.getFilename = function () { * This is updated only initially and on subsequent getFilePathAsync() calls. */ Zotero.Item.prototype.fileExists = Zotero.Promise.coroutine(function* () { - if (this._fileExists !== null) { - return this._fileExists; - } - if (!this.isAttachment()) { throw new Error("Zotero.Item.fileExists() can only be called on attachment items"); } diff --git a/test/tests/itemTest.js b/test/tests/itemTest.js index 2cda00e12..5960fab11 100644 --- a/test/tests/itemTest.js +++ b/test/tests/itemTest.js @@ -616,6 +616,57 @@ describe("Zotero.Item", function () { }) }) + + describe("#getBestAttachmentState()", function () { + it("should cache state for an existing file", function* () { + var parentItem = yield createDataObject('item'); + var file = getTestDataDirectory(); + file.append('test.png'); + var childItem = yield Zotero.Attachments.importFromFile({ + file, + parentItemID: parentItem.id + }); + yield parentItem.getBestAttachmentState(); + assert.equal(parentItem.getBestAttachmentStateCached(), 1); + }) + + it("should cache state for a missing file", function* () { + var parentItem = yield createDataObject('item'); + var file = getTestDataDirectory(); + file.append('test.png'); + var childItem = yield Zotero.Attachments.importFromFile({ + file, + parentItemID: parentItem.id + }); + let path = yield childItem.getFilePathAsync(); + yield OS.File.remove(path); + yield parentItem.getBestAttachmentState(); + assert.equal(parentItem.getBestAttachmentStateCached(), -1); + }) + }) + + + describe("#fileExists()", function () { + it("should cache state for an existing file", function* () { + var file = getTestDataDirectory(); + file.append('test.png'); + var item = yield Zotero.Attachments.importFromFile({ file }); + yield item.fileExists(); + assert.equal(item.fileExistsCached(), true); + }) + + it("should cache state for a missing file", function* () { + var file = getTestDataDirectory(); + file.append('test.png'); + var item = yield Zotero.Attachments.importFromFile({ file }); + let path = yield item.getFilePathAsync(); + yield OS.File.remove(path); + yield item.fileExists(); + assert.equal(item.fileExistsCached(), false); + }) + }) + + describe("#setTags", function () { it("should save an array of tags in API JSON format", function* () { var tags = [