fix query abstraction

This commit is contained in:
Florian Dold 2016-01-05 15:42:46 +01:00
parent b459ffb424
commit 3bf1846ed8
9 changed files with 63 additions and 18 deletions

View File

@ -20,6 +20,8 @@
* Declarations and helpers for * Declarations and helpers for
* things that are stored in the wallet's * things that are stored in the wallet's
* database. * database.
* @module Db
* @author Florian Dold
*/ */

View File

@ -13,6 +13,12 @@
You should have received a copy of the GNU General Public License along with You should have received a copy of the GNU General Public License along with
TALER; see the file COPYING. If not, If not, see <http://www.gnu.org/licenses/> TALER; see the file COPYING. If not, If not, see <http://www.gnu.org/licenses/>
*/ */
/**
* High-level interface to emscripten-compiled modules used
* by the wallet.
* @module EmscriptIf
* @author Florian Dold
*/
"use strict"; "use strict";
// Size of a native pointer. // Size of a native pointer.
const PTR_SIZE = 4; const PTR_SIZE = 4;

View File

@ -14,6 +14,14 @@
TALER; see the file COPYING. If not, If not, see <http://www.gnu.org/licenses/> TALER; see the file COPYING. If not, If not, see <http://www.gnu.org/licenses/>
*/ */
/**
* High-level interface to emscripten-compiled modules used
* by the wallet.
* @module EmscriptIf
* @author Florian Dold
*/
"use strict"; "use strict";
declare var Module: EmscModule; declare var Module: EmscModule;

View File

@ -14,6 +14,12 @@
TALER; see the file COPYING. If not, If not, see <http://www.gnu.org/licenses/> TALER; see the file COPYING. If not, If not, see <http://www.gnu.org/licenses/>
*/ */
/**
* Helpers for doing XMLHttpRequest-s that are based on ES6 promises.
* @module Http
* @author Florian Dold
*/
"use strict"; "use strict";
interface HttpResponse { interface HttpResponse {
@ -37,6 +43,8 @@ function httpReq(method: string,
myRequest.open(method, urlString); myRequest.open(method, urlString);
if (options && options.req) { if (options && options.req) {
myRequest.send(options.req); myRequest.send(options.req);
} else {
myRequest.send();
} }
myRequest.addEventListener("readystatechange", (e) => { myRequest.addEventListener("readystatechange", (e) => {
if (myRequest.readyState == XMLHttpRequest.DONE) { if (myRequest.readyState == XMLHttpRequest.DONE) {

View File

@ -34,7 +34,7 @@ let handlers = {
exportDb(db).then(sendResponse); exportDb(db).then(sendResponse);
return true; return true;
}, },
["reset-db"]: function(db, detail, sendResponse) { ["reset"]: function(db, detail, sendResponse) {
let tx = db.transaction(db.objectStoreNames, 'readwrite'); let tx = db.transaction(db.objectStoreNames, 'readwrite');
for (let i = 0; i < db.objectStoreNames.length; i++) { for (let i = 0; i < db.objectStoreNames.length; i++) {
tx.objectStore(db.objectStoreNames[i]).clear(); tx.objectStore(db.objectStoreNames[i]).clear();
@ -47,7 +47,7 @@ let handlers = {
}, },
["confirm-reserve"]: function(db, detail, sendResponse) { ["confirm-reserve"]: function(db, detail, sendResponse) {
return confirmReserveHandler(db, detail, sendResponse); return confirmReserveHandler(db, detail, sendResponse);
} },
}; };

View File

@ -187,7 +187,7 @@ class QueryRoot {
function doPut() { function doPut() {
this.tx.objectStore(storeName).put(val); this.tx.objectStore(storeName).put(val);
} }
this.work.push(doPut); this.work.push(doPut.bind(this));
return this; return this;
} }
@ -198,7 +198,7 @@ class QueryRoot {
this.tx.objectStore(storeName).put(obj); this.tx.objectStore(storeName).put(obj);
} }
} }
this.work.push(doPutAll); this.work.push(doPutAll.bind(this));
return this; return this;
} }
@ -207,7 +207,7 @@ class QueryRoot {
function doAdd() { function doAdd() {
this.tx.objectStore(storeName).add(val); this.tx.objectStore(storeName).add(val);
} }
this.work.push(doAdd); this.work.push(doAdd.bind(this));
return this; return this;
} }
@ -224,11 +224,13 @@ class QueryRoot {
function doGet() { function doGet() {
let req = this.tx.objectStore(storeName).get(key); let req = this.tx.objectStore(storeName).get(key);
req.onsuccess = (r) => { req.onsuccess = (r) => {
leakedResolve(r); leakedResolve(req.result);
}; };
} }
this.work.push(doGet); this.work.push(doGet.bind(this));
return p; return Promise.resolve().then(() => {
return this.finish().then(() => p);
});
} }
finish(): Promise<void> { finish(): Promise<void> {
@ -253,7 +255,7 @@ class QueryRoot {
function doDelete() { function doDelete() {
this.tx.objectStore(storeName).delete(key); this.tx.objectStore(storeName).delete(key);
} }
this.work.push(doDelete); this.work.push(doDelete.bind(this));
return this; return this;
} }
} }

View File

@ -13,6 +13,12 @@
You should have received a copy of the GNU General Public License along with You should have received a copy of the GNU General Public License along with
TALER; see the file COPYING. If not, If not, see <http://www.gnu.org/licenses/> TALER; see the file COPYING. If not, If not, see <http://www.gnu.org/licenses/>
*/ */
/**
* High-level wallet operations that should be indepentent from the underlying
* browser extension interface.
* @module Wallet
* @author Florian Dold
*/
/// <reference path="../decl/urijs/URIjs.d.ts" /> /// <reference path="../decl/urijs/URIjs.d.ts" />
/// <reference path="../decl/chrome/chrome.d.ts" /> /// <reference path="../decl/chrome/chrome.d.ts" />
'use strict'; 'use strict';
@ -247,12 +253,13 @@ function confirmReserveHandler(db, detail, sendResponse) {
.then(() => { .then(() => {
// Do this in the background // Do this in the background
updateMintFromUrl(db, reserveRecord.mint_base_url) updateMintFromUrl(db, reserveRecord.mint_base_url)
.then((mint) => { .then((mint) => updateReserve(db, reservePub, mint)
updateReserve(db, reservePub, mint) .then((reserve) => depleteReserve(db, reserve, mint)));
.then((reserve) => depleteReserve(db, reserve, mint));
});
return resp; return resp;
}); });
})
.then((resp) => {
sendResponse(resp);
}); });
// Allow async response // Allow async response
return true; return true;

View File

@ -14,6 +14,14 @@
TALER; see the file COPYING. If not, If not, see <http://www.gnu.org/licenses/> TALER; see the file COPYING. If not, If not, see <http://www.gnu.org/licenses/>
*/ */
/**
* High-level wallet operations that should be indepentent from the underlying
* browser extension interface.
* @module Wallet
* @author Florian Dold
*/
/// <reference path="../decl/urijs/URIjs.d.ts" /> /// <reference path="../decl/urijs/URIjs.d.ts" />
/// <reference path="../decl/chrome/chrome.d.ts" /> /// <reference path="../decl/chrome/chrome.d.ts" />
'use strict'; 'use strict';
@ -364,12 +372,15 @@ function confirmReserveHandler(db, detail, sendResponse) {
.then(() => { .then(() => {
// Do this in the background // Do this in the background
updateMintFromUrl(db, reserveRecord.mint_base_url) updateMintFromUrl(db, reserveRecord.mint_base_url)
.then((mint) => { .then((mint) =>
updateReserve(db, reservePub, mint) updateReserve(db, reservePub, mint)
.then((reserve) => depleteReserve(db, reserve, mint)); .then((reserve) => depleteReserve(db, reserve, mint))
}); );
return resp; return resp;
}); });
})
.then((resp) => {
sendResponse(resp);
}); });
// Allow async response // Allow async response
@ -516,7 +527,7 @@ function withdraw(db, denom, reserve): Promise<void> {
/** /**
* Withdraw coins from a reserve until it is empty. * Withdraw coins from a reserve until it is empty.
*/ */
function depleteReserve(db, reserve, mint) { function depleteReserve(db, reserve, mint): void {
let denoms = copy(mint.keys.denoms); let denoms = copy(mint.keys.denoms);
let remaining = new Amount(reserve.current_amount); let remaining = new Amount(reserve.current_amount);
denoms.sort(rankDenom); denoms.sort(rankDenom);

View File

@ -1,7 +1,8 @@
{ {
"compilerOptions": { "compilerOptions": {
"target": "es6", "target": "es6",
"jsx": "react" "jsx": "react",
"experimentalDecorators": true
}, },
"files": [ "files": [
"background/wallet.ts", "background/wallet.ts",