From e2cbfbd0fecfa2afee127f9e85382a03f0edd45a Mon Sep 17 00:00:00 2001 From: Dan Stillman Date: Thu, 21 Apr 2016 11:07:16 -0400 Subject: [PATCH] Deasyncify Zotero.Tags.getID()/getAsync(), and add Zotero.Tags.create() --- .../content/zotero/bindings/tagselector.xml | 4 +- chrome/content/zotero/xpcom/data/item.js | 8 +- chrome/content/zotero/xpcom/data/tags.js | 87 ++++++++++++++++--- chrome/content/zotero/xpcom/zotero.js | 1 + test/tests/supportTest.js | 2 +- test/tests/tagSelectorTest.js | 2 +- test/tests/tagsTest.js | 14 +-- test/tests/tagsboxTest.js | 4 +- 8 files changed, 90 insertions(+), 32 deletions(-) diff --git a/chrome/content/zotero/bindings/tagselector.xml b/chrome/content/zotero/bindings/tagselector.xml index 03bb70a42..c4cca673d 100644 --- a/chrome/content/zotero/bindings/tagselector.xml +++ b/chrome/content/zotero/bindings/tagselector.xml @@ -669,7 +669,7 @@ this.selection.delete(oldName); } - if (yield Zotero.Tags.getID(oldName)) { + if (Zotero.Tags.getID(oldName)) { yield Zotero.Tags.rename(this.libraryID, oldName, newName.value); } // Colored tags don't need to exist, so in that case @@ -707,7 +707,7 @@ return; } - var tagID = yield Zotero.Tags.getID(name); + var tagID = Zotero.Tags.getID(name); if (tagID) { yield Zotero.Tags.removeFromLibrary(this.libraryID, tagID); } diff --git a/chrome/content/zotero/xpcom/data/item.js b/chrome/content/zotero/xpcom/data/item.js index 43004ff6e..9544e3705 100644 --- a/chrome/content/zotero/xpcom/data/item.js +++ b/chrome/content/zotero/xpcom/data/item.js @@ -1621,7 +1621,7 @@ Zotero.Item.prototype._saveData = Zotero.Promise.coroutine(function* (env) { for (let i=0; i} - A tag name, or false if tag with id not found */ this.getName = function (tagID) { - return Zotero.DB.valueQueryAsync("SELECT name FROM tags WHERE tagID=?", tagID); - } + if (!_initialized) { + throw new Zotero.Exception.UnloadedDataException("Tags not yet loaded"); + } + + var name = _tagsByID.get(tagID); + return name !== undefined ? name : false; + }; /** - * Returns the tagID matching given fields, or creates a new tag and returns its id + * Returns the tagID matching given fields, or false if none * * @param {String} name - Tag data in API JSON format - * @param {Boolean} [create=false] - If no matching tag, create one; - * requires a wrapping transaction - * @return {Promise} tagID + * @return {Integer} tagID */ - this.getID = Zotero.Promise.coroutine(function* (name, create) { - if (create) { - Zotero.DB.requireTransaction(); + this.getID = function (name) { + if (!_initialized) { + throw new Zotero.Exception.UnloadedDataException("Tags not yet loaded"); } + if (arguments.length > 1) { + throw new Error("Zotero.Tags.getID() no longer takes a second parameter -- use Zotero.Tags.create()"); + } + data = this.cleanData({ tag: name }); - var sql = "SELECT tagID FROM tags WHERE name=?"; - var id = yield Zotero.DB.valueQueryAsync(sql, data.tag); - if (!id && create) { + var id = _idsByTag.get(data.tag); + return id !== undefined ? id : false; + }; + + + /** + * Returns the tagID matching given fields, or creates one and returns its id + * + * Requires a wrapping transaction + * + * @param {String} name - Tag data in API JSON format + * @return {Promise} tagID + */ + this.create = Zotero.Promise.coroutine(function* (name) { + if (!_initialized) { + throw new Zotero.Exception.UnloadedDataException("Tags not yet loaded"); + } + + Zotero.DB.requireTransaction(); + data = this.cleanData({ + tag: name + }); + var id = this.getID(data.tag); + if (!id) { id = Zotero.ID.get('tags'); let sql = "INSERT INTO tags (tagID, name) VALUES (?, ?)"; yield Zotero.DB.queryAsync(sql, [id, data.tag]); + _tagsByID.set(id, data.tag); + _idsByTag.set(data.tag, id); } return id; }); @@ -252,7 +302,7 @@ Zotero.Tags = new function() { var notifierData = {}; for (let i=0; i { + _tagsByID.delete(row.id); + _idsByTag.delete(row.name); + }); + ids.push(row.id); notifierData[row.id] = { old: { diff --git a/chrome/content/zotero/xpcom/zotero.js b/chrome/content/zotero/xpcom/zotero.js index f3362f1f5..d340c33f2 100644 --- a/chrome/content/zotero/xpcom/zotero.js +++ b/chrome/content/zotero/xpcom/zotero.js @@ -625,6 +625,7 @@ Components.utils.import("resource://gre/modules/osfile.jsm"); yield Zotero.Collections.init(); yield Zotero.Items.init(); yield Zotero.Searches.init(); + yield Zotero.Tags.init(); yield Zotero.Creators.init(); yield Zotero.Groups.init(); yield Zotero.Relations.init(); diff --git a/test/tests/supportTest.js b/test/tests/supportTest.js index 89a43c377..33a48d557 100644 --- a/test/tests/supportTest.js +++ b/test/tests/supportTest.js @@ -72,7 +72,7 @@ describe("Support Functions for Unit Testing", function() { let tags = data.itemWithTags.tags; for (let i=0; i