diff options
author | Florian Dold <florian.dold@gmail.com> | 2017-12-10 23:02:00 +0100 |
---|---|---|
committer | Florian Dold <florian.dold@gmail.com> | 2017-12-10 23:02:00 +0100 |
commit | 3c882c44b5aba1ae397a2b89f99f4cdb82fbbbfa (patch) | |
tree | 842d51eb84dcc48ed4240c27551c973bd369ef6d /src | |
parent | 0469abd4a9c9270a1fdc962969e36e63699af8b4 (diff) |
fix problems found by newer TypeScript compiler
Diffstat (limited to 'src')
-rw-r--r-- | src/crypto/cryptoApi.ts | 2 | ||||
-rw-r--r-- | src/query.ts | 30 | ||||
-rw-r--r-- | src/wallet.ts | 30 | ||||
-rw-r--r-- | src/webex/notify.ts | 14 | ||||
-rw-r--r-- | src/webex/pages/add-auditor.tsx | 4 | ||||
-rw-r--r-- | src/webex/pages/auditors.tsx | 6 | ||||
-rw-r--r-- | src/webex/pages/confirm-contract.tsx | 4 | ||||
-rw-r--r-- | src/webex/pages/logs.tsx | 6 | ||||
-rw-r--r-- | src/webex/pages/payback.tsx | 6 | ||||
-rw-r--r-- | src/webex/pages/return-coins.tsx | 6 | ||||
-rw-r--r-- | src/webex/pages/tree.tsx | 6 | ||||
-rw-r--r-- | src/webex/wxBackend.ts | 3 |
12 files changed, 66 insertions, 51 deletions
diff --git a/src/crypto/cryptoApi.ts b/src/crypto/cryptoApi.ts index 5300c1370..94083d622 100644 --- a/src/crypto/cryptoApi.ts +++ b/src/crypto/cryptoApi.ts @@ -218,7 +218,7 @@ export class CryptoApi { ...args: any[]): Promise<T> { const start = timer.performanceNow(); - const p = new Promise((resolve, reject) => { + const p: Promise<T> = new Promise<T>((resolve, reject) => { const rpcId = this.nextRpcId++; const workItem: WorkItem = {operation, args, resolve, reject, rpcId}; diff --git a/src/query.ts b/src/query.ts index 554f937a5..9889ed13e 100644 --- a/src/query.ts +++ b/src/query.ts @@ -127,9 +127,15 @@ export interface QueryStream<T> { filter(f: (x: T) => boolean): QueryStream<T>; /** - * Reduce the stream, resulting in a single value. + * Fold the stream, resulting in a single value. */ - reduce<S>(f: (v: T, acc?: S) => S, start?: S): Promise<S>; + fold<S>(f: (v: T, acc: S) => S, start: S): Promise<S>; + + /** + * Execute a function for every value of the stream, for the + * side-effects of the function. + */ + forEach(f: (v: T) => void): Promise<void>; /** * Map each element of the stream using a function, resulting in another @@ -324,7 +330,7 @@ abstract class QueryStreamBase<T> implements QueryStream<T> { .then(() => promise); } - reduce<A>(f: (x: any, acc?: A) => A, init?: A): Promise<any> { + fold<A>(f: (x: T, acc: A) => A, init: A): Promise<A> { const {resolve, promise} = openPromise(); let acc = init; @@ -341,6 +347,22 @@ abstract class QueryStreamBase<T> implements QueryStream<T> { .then(() => promise); } + forEach(f: (x: T) => void): Promise<void> { + const {resolve, promise} = openPromise(); + + this.subscribe((isDone, value) => { + if (isDone) { + resolve(); + return; + } + f(value); + }); + + return Promise.resolve() + .then(() => this.root.finish()) + .then(() => promise); + } + run(): Promise<void> { const {resolve, promise} = openPromise(); @@ -699,7 +721,7 @@ export class QueryRoot { * If the mutation function throws AbortTransaction, the whole transaction will be aborted. * If the mutation function returns undefined or null, no modification will be made. */ - mutate<T>(store: Store<T>, key: any, f: (v: T|undefined) => T|undefined): QueryRoot { + mutate<T>(store: Store<T>, key: any, f: (v: T) => T|undefined): QueryRoot { this.checkFinished(); const doPut = (tx: IDBTransaction) => { const req = tx.objectStore(store.name).openCursor(IDBKeyRange.only(key)); diff --git a/src/wallet.ts b/src/wallet.ts index 13f4dc6c2..1966db768 100644 --- a/src/wallet.ts +++ b/src/wallet.ts @@ -730,34 +730,34 @@ export class Wallet { this.q() .iter(Stores.reserves) - .reduce((reserve) => { + .forEach((reserve) => { console.log("resuming reserve", reserve.reserve_pub); this.processReserve(reserve); }); this.q() .iter(Stores.precoins) - .reduce((preCoin) => { + .forEach((preCoin) => { console.log("resuming precoin"); this.processPreCoin(preCoin); }); this.q() .iter(Stores.refresh) - .reduce((r: RefreshSessionRecord) => { + .forEach((r: RefreshSessionRecord) => { this.continueRefreshSession(r); }); this.q() .iter(Stores.coinsReturns) - .reduce((r: CoinsReturnRecord) => { + .forEach((r: CoinsReturnRecord) => { this.depositReturnedCoins(r); }); // FIXME: optimize via index this.q() .iter(Stores.coins) - .reduce((c: CoinRecord) => { + .forEach((c: CoinRecord) => { if (c.status === CoinStatus.Dirty) { console.log("resuming pending refresh for coin", c); this.refresh(c.coinPub); @@ -1536,7 +1536,6 @@ export class Wallet { ); let allValid = false; - let currentPossibleDenoms = possibleDenoms; let selectedDenoms: DenominationRecord[]; @@ -1561,7 +1560,6 @@ export class Wallet { nextPossibleDenoms.push(denom); } } - currentPossibleDenoms = nextPossibleDenoms; } while (selectedDenoms.length > 0 && !allValid); return selectedDenoms; @@ -1709,7 +1707,7 @@ export class Wallet { .iterIndex(Stores.coins.exchangeBaseUrlIndex, exchangeInfo.baseUrl) .indexJoinLeft(Stores.denominations.exchangeBaseUrlIndex, (e) => e.exchangeBaseUrl) - .reduce((cd: JoinLeftResult<CoinRecord, DenominationRecord>, + .fold((cd: JoinLeftResult<CoinRecord, DenominationRecord>, suspendedCoins: CoinRecord[]) => { if ((!cd.right) || (!cd.right.isOffered)) { return Array.prototype.concat(suspendedCoins, [cd.left]); @@ -1718,7 +1716,7 @@ export class Wallet { }, [])); const q = this.q(); - resultSuspendedCoins.map((c) => { + resultSuspendedCoins.map((c: CoinRecord) => { console.log("suspending coin", c); c.suspended = true; q.put(Stores.coins, c); @@ -1851,7 +1849,7 @@ export class Wallet { const existingDenoms: {[denomPub: string]: DenominationRecord} = await ( this.q().iterIndex(Stores.denominations.exchangeBaseUrlIndex, exchangeInfo.baseUrl) - .reduce((x: DenominationRecord, + .fold((x: DenominationRecord, acc: typeof existingDenoms) => (acc[x.denomPub] = x, acc), {}) ); @@ -1995,19 +1993,19 @@ export class Wallet { .iter(Stores.exchanges) .indexJoin(Stores.denominations.exchangeBaseUrlIndex, (x) => x.baseUrl) - .reduce(collectSmallestWithdraw, {})); + .fold(collectSmallestWithdraw, {})); const tx = this.q(); tx.iter(Stores.coins) - .reduce(collectBalances, balanceStore); + .fold(collectBalances, balanceStore); tx.iter(Stores.refresh) - .reduce(collectPendingRefresh, balanceStore); + .fold(collectPendingRefresh, balanceStore); tx.iter(Stores.reserves) - .reduce(collectPendingWithdraw, balanceStore); + .fold(collectPendingWithdraw, balanceStore); tx.iter(Stores.reserves) - .reduce(collectPaybacks, balanceStore); + .fold(collectPaybacks, balanceStore); tx.iter(Stores.purchases) - .reduce(collectPayments, balanceStore); + .fold(collectPayments, balanceStore); await tx.finish(); return balanceStore; } diff --git a/src/webex/notify.ts b/src/webex/notify.ts index 0531717b8..1da8af1cd 100644 --- a/src/webex/notify.ts +++ b/src/webex/notify.ts @@ -49,7 +49,7 @@ if (document.documentElement.getAttribute("data-taler-nojs")) { interface Handler { type: string; - listener: (e: CustomEvent) => void|Promise<void>; + listener: (e: Event) => void|Promise<void>; } const handlers: Handler[] = []; @@ -230,13 +230,6 @@ async function processProposal(proposal: any) { return; } - let merchantName = "(unknown)"; - try { - merchantName = proposal.data.merchant.name; - } catch (e) { - // bad contract / name not included - } - const proposalId = await wxApi.saveProposal({ contractTerms: proposal.data, contractTermsHash: proposal.hash, @@ -400,7 +393,10 @@ function registerHandlers() { * handles adding sequence numbers to responses. */ function addHandler(type: string, handler: HandlerFn) { - const handlerWrap = (e: CustomEvent) => { + const handlerWrap = (e: Event) => { + if (!(e instanceof CustomEvent)) { + throw Error(`invariant violated`); + } if (e.type !== type) { throw Error(`invariant violated`); } diff --git a/src/webex/pages/add-auditor.tsx b/src/webex/pages/add-auditor.tsx index dbe761a5f..4b898b13c 100644 --- a/src/webex/pages/add-auditor.tsx +++ b/src/webex/pages/add-auditor.tsx @@ -44,8 +44,8 @@ interface ConfirmAuditorProps { class ConfirmAuditor extends ImplicitStateComponent<ConfirmAuditorProps> { private addDone: StateHolder<boolean> = this.makeState(false); - constructor() { - super(); + constructor(props: ConfirmAuditorProps) { + super(props); } async add() { diff --git a/src/webex/pages/auditors.tsx b/src/webex/pages/auditors.tsx index 8850cdd87..9d57218ad 100644 --- a/src/webex/pages/auditors.tsx +++ b/src/webex/pages/auditors.tsx @@ -39,9 +39,9 @@ interface CurrencyListState { currencies?: CurrencyRecord[]; } -class CurrencyList extends React.Component<any, CurrencyListState> { - constructor() { - super(); +class CurrencyList extends React.Component<{}, CurrencyListState> { + constructor(props: {}) { + super(props); const port = chrome.runtime.connect(); port.onMessage.addListener((msg: any) => { if (msg.notify) { diff --git a/src/webex/pages/confirm-contract.tsx b/src/webex/pages/confirm-contract.tsx index f2e22bec4..e41b0a1df 100644 --- a/src/webex/pages/confirm-contract.tsx +++ b/src/webex/pages/confirm-contract.tsx @@ -118,8 +118,8 @@ interface ContractPromptState { } class ContractPrompt extends React.Component<ContractPromptProps, ContractPromptState> { - constructor() { - super(); + constructor(props: ContractPromptProps) { + super(props); this.state = { alreadyPaid: false, error: null, diff --git a/src/webex/pages/logs.tsx b/src/webex/pages/logs.tsx index 2853b2a06..c4fe670a2 100644 --- a/src/webex/pages/logs.tsx +++ b/src/webex/pages/logs.tsx @@ -55,9 +55,9 @@ interface LogsState { logs: LogEntry[]|undefined; } -class Logs extends React.Component<any, LogsState> { - constructor() { - super(); +class Logs extends React.Component<{}, LogsState> { + constructor(props: {}) { + super({}); this.update(); this.state = {} as any; } diff --git a/src/webex/pages/payback.tsx b/src/webex/pages/payback.tsx index c2a092460..a380a33d0 100644 --- a/src/webex/pages/payback.tsx +++ b/src/webex/pages/payback.tsx @@ -38,10 +38,10 @@ import { import * as React from "react"; import * as ReactDOM from "react-dom"; -class Payback extends ImplicitStateComponent<any> { +class Payback extends ImplicitStateComponent<{}> { private reserves: StateHolder<ReserveRecord[]|null> = this.makeState(null); - constructor() { - super(); + constructor(props: {}) { + super(props); const port = chrome.runtime.connect(); port.onMessage.addListener((msg: any) => { if (msg.notify) { diff --git a/src/webex/pages/return-coins.tsx b/src/webex/pages/return-coins.tsx index 453ae4784..5bcb2252a 100644 --- a/src/webex/pages/return-coins.tsx +++ b/src/webex/pages/return-coins.tsx @@ -189,9 +189,9 @@ interface ReturnCoinsState { lastConfirmedDetail: SelectedDetail | undefined; } -class ReturnCoins extends React.Component<any, ReturnCoinsState> { - constructor() { - super(); +class ReturnCoins extends React.Component<{}, ReturnCoinsState> { + constructor(props: {}) { + super(props); const port = chrome.runtime.connect(); port.onMessage.addListener((msg: any) => { if (msg.notify) { diff --git a/src/webex/pages/tree.tsx b/src/webex/pages/tree.tsx index c8035c597..2ac0b8631 100644 --- a/src/webex/pages/tree.tsx +++ b/src/webex/pages/tree.tsx @@ -360,9 +360,9 @@ interface ExchangesListState { exchanges?: ExchangeRecord[]; } -class ExchangesList extends React.Component<any, ExchangesListState> { - constructor() { - super(); +class ExchangesList extends React.Component<{}, ExchangesListState> { + constructor(props: {}) { + super(props); const port = chrome.runtime.connect(); port.onMessage.addListener((msg: any) => { if (msg.notify) { diff --git a/src/webex/wxBackend.ts b/src/webex/wxBackend.ts index a17f516a8..fd5df7e47 100644 --- a/src/webex/wxBackend.ts +++ b/src/webex/wxBackend.ts @@ -497,9 +497,8 @@ function handleBankRequest(wallet: Wallet, headerList: chrome.webRequest.HttpHea console.log("202 not understood (X-Taler-Callback-Url missing)"); return; } - let amountParsed; try { - amountParsed = JSON.parse(amount); + JSON.parse(amount); } catch (e) { const errUri = new URI(chrome.extension.getURL("/src/webex/pages/error.html")); const p = { |