From 3bf1846ed81b7c137a3d751400a74546f502a37b Mon Sep 17 00:00:00 2001 From: Florian Dold Date: Tue, 5 Jan 2016 15:42:46 +0100 Subject: fix query abstraction --- extension/background/db.ts | 2 ++ extension/background/emscriptif.js | 6 ++++++ extension/background/emscriptif.ts | 8 ++++++++ extension/background/http.ts | 8 ++++++++ extension/background/messaging.ts | 4 ++-- extension/background/query.ts | 16 +++++++++------- extension/background/wallet.js | 15 +++++++++++---- extension/background/wallet.ts | 19 +++++++++++++++---- 8 files changed, 61 insertions(+), 17 deletions(-) (limited to 'extension/background') diff --git a/extension/background/db.ts b/extension/background/db.ts index 1dd399907..df24e1471 100644 --- a/extension/background/db.ts +++ b/extension/background/db.ts @@ -20,6 +20,8 @@ * Declarations and helpers for * things that are stored in the wallet's * database. + * @module Db + * @author Florian Dold */ diff --git a/extension/background/emscriptif.js b/extension/background/emscriptif.js index 83bf8b5bb..6cf22420e 100644 --- a/extension/background/emscriptif.js +++ b/extension/background/emscriptif.js @@ -13,6 +13,12 @@ You should have received a copy of the GNU General Public License along with TALER; see the file COPYING. If not, If not, see */ +/** + * High-level interface to emscripten-compiled modules used + * by the wallet. + * @module EmscriptIf + * @author Florian Dold + */ "use strict"; // Size of a native pointer. const PTR_SIZE = 4; diff --git a/extension/background/emscriptif.ts b/extension/background/emscriptif.ts index 46937d088..0e35bc9b1 100644 --- a/extension/background/emscriptif.ts +++ b/extension/background/emscriptif.ts @@ -14,6 +14,14 @@ TALER; see the file COPYING. If not, If not, see */ +/** + * High-level interface to emscripten-compiled modules used + * by the wallet. + * @module EmscriptIf + * @author Florian Dold + */ + + "use strict"; declare var Module: EmscModule; diff --git a/extension/background/http.ts b/extension/background/http.ts index 9a064e974..9355add6f 100644 --- a/extension/background/http.ts +++ b/extension/background/http.ts @@ -14,6 +14,12 @@ TALER; see the file COPYING. If not, If not, see */ +/** + * Helpers for doing XMLHttpRequest-s that are based on ES6 promises. + * @module Http + * @author Florian Dold + */ + "use strict"; interface HttpResponse { @@ -37,6 +43,8 @@ function httpReq(method: string, myRequest.open(method, urlString); if (options && options.req) { myRequest.send(options.req); + } else { + myRequest.send(); } myRequest.addEventListener("readystatechange", (e) => { if (myRequest.readyState == XMLHttpRequest.DONE) { diff --git a/extension/background/messaging.ts b/extension/background/messaging.ts index fc513bd04..4bd5ec24e 100644 --- a/extension/background/messaging.ts +++ b/extension/background/messaging.ts @@ -34,7 +34,7 @@ let handlers = { exportDb(db).then(sendResponse); return true; }, - ["reset-db"]: function(db, detail, sendResponse) { + ["reset"]: function(db, detail, sendResponse) { let tx = db.transaction(db.objectStoreNames, 'readwrite'); for (let i = 0; i < db.objectStoreNames.length; i++) { tx.objectStore(db.objectStoreNames[i]).clear(); @@ -47,7 +47,7 @@ let handlers = { }, ["confirm-reserve"]: function(db, detail, sendResponse) { return confirmReserveHandler(db, detail, sendResponse); - } + }, }; diff --git a/extension/background/query.ts b/extension/background/query.ts index 1a61c66ca..75201c2d4 100644 --- a/extension/background/query.ts +++ b/extension/background/query.ts @@ -187,7 +187,7 @@ class QueryRoot { function doPut() { this.tx.objectStore(storeName).put(val); } - this.work.push(doPut); + this.work.push(doPut.bind(this)); return this; } @@ -198,7 +198,7 @@ class QueryRoot { this.tx.objectStore(storeName).put(obj); } } - this.work.push(doPutAll); + this.work.push(doPutAll.bind(this)); return this; } @@ -207,7 +207,7 @@ class QueryRoot { function doAdd() { this.tx.objectStore(storeName).add(val); } - this.work.push(doAdd); + this.work.push(doAdd.bind(this)); return this; } @@ -224,11 +224,13 @@ class QueryRoot { function doGet() { let req = this.tx.objectStore(storeName).get(key); req.onsuccess = (r) => { - leakedResolve(r); + leakedResolve(req.result); }; } - this.work.push(doGet); - return p; + this.work.push(doGet.bind(this)); + return Promise.resolve().then(() => { + return this.finish().then(() => p); + }); } finish(): Promise { @@ -253,7 +255,7 @@ class QueryRoot { function doDelete() { this.tx.objectStore(storeName).delete(key); } - this.work.push(doDelete); + this.work.push(doDelete.bind(this)); return this; } } \ No newline at end of file diff --git a/extension/background/wallet.js b/extension/background/wallet.js index 971da7195..beb66c57a 100644 --- a/extension/background/wallet.js +++ b/extension/background/wallet.js @@ -13,6 +13,12 @@ You should have received a copy of the GNU General Public License along with TALER; see the file COPYING. If not, If not, see */ +/** + * High-level wallet operations that should be indepentent from the underlying + * browser extension interface. + * @module Wallet + * @author Florian Dold + */ /// /// 'use strict'; @@ -247,12 +253,13 @@ function confirmReserveHandler(db, detail, sendResponse) { .then(() => { // Do this in the background updateMintFromUrl(db, reserveRecord.mint_base_url) - .then((mint) => { - updateReserve(db, reservePub, mint) - .then((reserve) => depleteReserve(db, reserve, mint)); - }); + .then((mint) => updateReserve(db, reservePub, mint) + .then((reserve) => depleteReserve(db, reserve, mint))); return resp; }); + }) + .then((resp) => { + sendResponse(resp); }); // Allow async response return true; diff --git a/extension/background/wallet.ts b/extension/background/wallet.ts index b3593f682..6231baad0 100644 --- a/extension/background/wallet.ts +++ b/extension/background/wallet.ts @@ -14,6 +14,14 @@ TALER; see the file COPYING. If not, If not, see */ +/** + * High-level wallet operations that should be indepentent from the underlying + * browser extension interface. + * @module Wallet + * @author Florian Dold + */ + + /// /// 'use strict'; @@ -364,12 +372,15 @@ function confirmReserveHandler(db, detail, sendResponse) { .then(() => { // Do this in the background updateMintFromUrl(db, reserveRecord.mint_base_url) - .then((mint) => { + .then((mint) => updateReserve(db, reservePub, mint) - .then((reserve) => depleteReserve(db, reserve, mint)); - }); + .then((reserve) => depleteReserve(db, reserve, mint)) + ); return resp; }); + }) + .then((resp) => { + sendResponse(resp); }); // Allow async response @@ -516,7 +527,7 @@ function withdraw(db, denom, reserve): Promise { /** * 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 remaining = new Amount(reserve.current_amount); denoms.sort(rankDenom); -- cgit v1.2.3