www

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | Submodules | README | LICENSE

commit d5439363070f5f0f48b02fc778a591ac65955d3e
parent c95223724dd1d9d417aed7cf032bd27b90ae4100
Author: Dan Stillman <dstillman@gmail.com>
Date:   Wed, 14 Aug 2013 22:00:32 -0700

Merge pull request #374 from simonster/lazy

Add Zotero.lazy() and use in _getConnectionAsync
Diffstat:
Mchrome/content/zotero/xpcom/db.js | 15+++------------
Mchrome/content/zotero/xpcom/zotero.js | 19+++++++++++++++++++
2 files changed, 22 insertions(+), 12 deletions(-)

diff --git a/chrome/content/zotero/xpcom/db.js b/chrome/content/zotero/xpcom/db.js @@ -70,7 +70,6 @@ Zotero.DBConnection = function(dbName) { this._dbName = dbName; this._shutdown = false; this._connection = null; - this._connectionAsync = null; this._transactionDate = null; this._lastTransactionDate = null; this._transactionRollback = false; @@ -1021,23 +1020,15 @@ Zotero.DBConnection.prototype.asyncResult = function (val) { /** * Asynchronously return a connection object for the current DB */ -Zotero.DBConnection.prototype._getConnectionAsync = function () { - if (this._connectionAsync) { - return Q(this._connectionAsync); - } - +Zotero.DBConnection.prototype._getConnectionAsync = Zotero.lazy(function() { var db = this._getDBConnection(); var options = { path: db.databaseFile.path }; var self = this; Zotero.debug("Asynchronously opening DB connection"); - return Q(this.Sqlite.openConnection(options) - .then(function(conn) { - self._connectionAsync = conn; - return conn; - })); -}; + return Q(this.Sqlite.openConnection(options)); +}); /* diff --git a/chrome/content/zotero/xpcom/zotero.js b/chrome/content/zotero/xpcom/zotero.js @@ -1519,6 +1519,25 @@ Components.utils.import("resource://gre/modules/Services.jsm"); }; /** + * Generate a function that produces a static output + * + * Zotero.lazy(fn) returns a function. The first time this function + * is called, it calls fn() and returns its output. Subsequent + * calls return the same output as the first without calling fn() + * again. + */ + this.lazy = function(fn) { + var x, called = false; + return function() { + if(!called) { + x = fn.apply(this); + called = true; + } + return x; + }; + }; + + /** * Pumps a generator until it yields false. See itemTreeView.js for an example. * * If errorHandler is specified, exceptions in the generator will be caught