Add Zotero.lazy() and use in _getConnectionAsync
I tried to describe what this function does earlier today and failed. Hopefully the code is clearer.
This commit is contained in:
parent
c95223724d
commit
857fcfa31d
2 changed files with 22 additions and 12 deletions
|
@ -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));
|
||||
});
|
||||
|
||||
|
||||
/*
|
||||
|
|
|
@ -1518,6 +1518,25 @@ Components.utils.import("resource://gre/modules/Services.jsm");
|
|||
return;
|
||||
};
|
||||
|
||||
/**
|
||||
* 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.
|
||||
*
|
||||
|
|
Loading…
Reference in a new issue