From e3cc9c59bcc36eee8c3234574cfdfda3f5eea658 Mon Sep 17 00:00:00 2001 From: Florian Dold Date: Mon, 12 Sep 2016 17:41:12 +0200 Subject: stricter type checking --- lib/wallet/query.ts | 140 ++++++++++++++++++++++++++++------------------------ 1 file changed, 76 insertions(+), 64 deletions(-) (limited to 'lib/wallet/query.ts') diff --git a/lib/wallet/query.ts b/lib/wallet/query.ts index 4eccb696b..c7420a3f7 100644 --- a/lib/wallet/query.ts +++ b/lib/wallet/query.ts @@ -24,7 +24,7 @@ "use strict"; -export function Query(db) { +export function Query(db: IDBDatabase) { return new QueryRoot(db); } @@ -36,24 +36,27 @@ export interface QueryStream { indexJoin(storeName: string, indexName: string, keyFn: (obj: any) => any): QueryStream<[T,S]>; - filter(f: (any) => boolean): QueryStream; + filter(f: (x: any) => boolean): QueryStream; reduce(f: (v: T, acc: S) => S, start?: S): Promise; - flatMap(f: (T) => T[]): QueryStream; + flatMap(f: (x: T) => T[]): QueryStream; } /** * Get an unresolved promise together with its extracted resolve / reject * function. - * - * @returns {{resolve: any, reject: any, promise: Promise}} */ function openPromise() { - let resolve, reject; + let resolve: ((value?: T | PromiseLike) => void) | null = null; + let reject: ((reason?: any) => void) | null = null; const promise = new Promise((res, rej) => { resolve = res; reject = rej; }); + if (!(resolve && reject)) { + // Never happens, unless JS implementation is broken + throw Error(); + } return {resolve, reject, promise}; } @@ -61,7 +64,7 @@ function openPromise() { abstract class QueryStreamBase implements QueryStream { abstract subscribe(f: (isDone: boolean, value: any, - tx: IDBTransaction) => void); + tx: IDBTransaction) => void): void; root: QueryRoot; @@ -69,30 +72,28 @@ abstract class QueryStreamBase implements QueryStream { this.root = root; } - flatMap(f: (T) => T[]): QueryStream { + flatMap(f: (x: T) => T[]): QueryStream { return new QueryStreamFlatMap(this, f); } indexJoin(storeName: string, indexName: string, key: any): QueryStream<[T,S]> { - this.root.addWork(null, storeName, false); + this.root.addStoreAccess(storeName, false); return new QueryStreamIndexJoin(this, storeName, indexName, key); } - filter(f: (any) => boolean): QueryStream { + filter(f: (x: any) => boolean): QueryStream { return new QueryStreamFilter(this, f); } - reduce(f, acc?): Promise { - let leakedResolve; - let p = new Promise((resolve, reject) => { - leakedResolve = resolve; - }); + reduce(f: (x: any, acc?: A) => A, init?: A): Promise { + let {resolve, promise} = openPromise(); + let acc = init; this.subscribe((isDone, value) => { if (isDone) { - leakedResolve(acc); + resolve(acc); return; } acc = f(value, acc); @@ -100,22 +101,28 @@ abstract class QueryStreamBase implements QueryStream { return Promise.resolve() .then(() => this.root.finish()) - .then(() => p); + .then(() => promise); } } +type FilterFn = (e: any) => boolean; +type SubscribeFn = (done: boolean, value: any, tx: IDBTransaction) => void; + +interface FlatMapFn { + (v: T): T[]; +} class QueryStreamFilter extends QueryStreamBase { s: QueryStreamBase; - filterFn; + filterFn: FilterFn; - constructor(s: QueryStreamBase, filterFn) { + constructor(s: QueryStreamBase, filterFn: FilterFn) { super(s.root); this.s = s; this.filterFn = filterFn; } - subscribe(f) { + subscribe(f: SubscribeFn) { this.s.subscribe((isDone, value, tx) => { if (isDone) { f(true, undefined, tx); @@ -131,15 +138,15 @@ class QueryStreamFilter extends QueryStreamBase { class QueryStreamFlatMap extends QueryStreamBase { s: QueryStreamBase; - flatMapFn; + flatMapFn: (v: T) => T[]; - constructor(s: QueryStreamBase, flatMapFn) { + constructor(s: QueryStreamBase, flatMapFn: (v: T) => T[]) { super(s.root); this.s = s; - this.flatMap = flatMapFn; + this.flatMapFn = flatMapFn; } - subscribe(f) { + subscribe(f: SubscribeFn) { this.s.subscribe((isDone, value, tx) => { if (isDone) { f(true, undefined, tx); @@ -154,13 +161,13 @@ class QueryStreamFlatMap extends QueryStreamBase { } -class QueryStreamIndexJoin extends QueryStreamBase { +class QueryStreamIndexJoin extends QueryStreamBase<[T, S]> { s: QueryStreamBase; - storeName; - key; - indexName; + storeName: string; + key: any; + indexName: string; - constructor(s, storeName: string, indexName: string, key: any) { + constructor(s: QueryStreamBase, storeName: string, indexName: string, key: any) { super(s.root); this.s = s; this.storeName = storeName; @@ -168,7 +175,7 @@ class QueryStreamIndexJoin extends QueryStreamBase { this.indexName = indexName; } - subscribe(f) { + subscribe(f: SubscribeFn) { this.s.subscribe((isDone, value, tx) => { if (isDone) { f(true, undefined, tx); @@ -192,31 +199,31 @@ class QueryStreamIndexJoin extends QueryStreamBase { class IterQueryStream extends QueryStreamBase { - private storeName; - private options; - private subscribers; + private storeName: string; + private options: any; + private subscribers: SubscribeFn[]; - constructor(qr, storeName, options) { + constructor(qr: QueryRoot, storeName: string, options: any) { super(qr); this.options = options; this.storeName = storeName; this.subscribers = []; - let doIt = (tx) => { + let doIt = (tx: IDBTransaction) => { const {indexName = void 0, only = void 0} = this.options; - let s; + let s: any; if (indexName !== void 0) { s = tx.objectStore(this.storeName) .index(this.options.indexName); } else { s = tx.objectStore(this.storeName); } - let kr = undefined; - if (only !== void 0) { + let kr: IDBKeyRange|undefined = undefined; + if (only !== undefined) { kr = IDBKeyRange.only(this.options.only); } let req = s.openCursor(kr); - req.onsuccess = (e) => { + req.onsuccess = () => { let cursor: IDBCursorWithValue = req.result; if (cursor) { for (let f of this.subscribers) { @@ -231,32 +238,33 @@ class IterQueryStream extends QueryStreamBase { } }; - this.root.addWork(doIt, null, false); + this.root.addWork(doIt); } - subscribe(f) { + subscribe(f: SubscribeFn) { this.subscribers.push(f); } } class QueryRoot { - private work = []; + private work: ((t: IDBTransaction) => void)[] = []; private db: IDBDatabase; private stores = new Set(); - private kickoffPromise; + private kickoffPromise: Promise; /** * Some operations is a write operation, * and we need to do a "readwrite" transaction/ */ - private hasWrite; + private hasWrite: boolean; - constructor(db) { + constructor(db: IDBDatabase) { this.db = db; } - iter(storeName, {only = void 0, indexName = void 0} = {}): QueryStream { + iter(storeName: string, + {only = undefined, indexName = undefined} = {}): QueryStream { this.stores.add(storeName); return new IterQueryStream(this, storeName, {only, indexName}); } @@ -266,7 +274,7 @@ class QueryRoot { * Overrides if an existing object with the same key exists * in the store. */ - put(storeName, val): QueryRoot { + put(storeName: string, val: any): QueryRoot { let doPut = (tx: IDBTransaction) => { tx.objectStore(storeName).put(val); }; @@ -280,7 +288,7 @@ class QueryRoot { * Fails if the object's key is already present * in the object store. */ - putAll(storeName, iterable): QueryRoot { + putAll(storeName: string, iterable: any[]): QueryRoot { const doPutAll = (tx: IDBTransaction) => { for (const obj of iterable) { tx.objectStore(storeName).put(obj); @@ -295,7 +303,7 @@ class QueryRoot { * Fails if the object's key is already present * in the object store. */ - add(storeName, val): QueryRoot { + add(storeName: string, val: any): QueryRoot { const doAdd = (tx: IDBTransaction) => { tx.objectStore(storeName).add(val); }; @@ -306,16 +314,16 @@ class QueryRoot { /** * Get one object from a store by its key. */ - get(storeName, key): Promise { + get(storeName: any, key: any): Promise { if (key === void 0) { throw Error("key must not be undefined"); } const {resolve, promise} = openPromise(); - const doGet = (tx) => { + const doGet = (tx: IDBTransaction) => { const req = tx.objectStore(storeName).get(key); - req.onsuccess = (r) => { + req.onsuccess = () => { resolve(req.result); }; }; @@ -329,16 +337,16 @@ class QueryRoot { /** * Get one object from a store by its key. */ - getIndexed(storeName, indexName, key): Promise { + getIndexed(storeName: string, indexName: string, key: any): Promise { if (key === void 0) { throw Error("key must not be undefined"); } const {resolve, promise} = openPromise(); - const doGetIndexed = (tx) => { + const doGetIndexed = (tx: IDBTransaction) => { const req = tx.objectStore(storeName).index(indexName).get(key); - req.onsuccess = (r) => { + req.onsuccess = () => { resolve(req.result); }; }; @@ -356,7 +364,7 @@ class QueryRoot { if (this.kickoffPromise) { return this.kickoffPromise; } - this.kickoffPromise = new Promise((resolve, reject) => { + this.kickoffPromise = new Promise((resolve, reject) => { if (this.work.length == 0) { resolve(); return; @@ -376,8 +384,8 @@ class QueryRoot { /** * Delete an object by from the given object store. */ - delete(storeName: string, key): QueryRoot { - const doDelete = (tx) => { + delete(storeName: string, key: any): QueryRoot { + const doDelete = (tx: IDBTransaction) => { tx.objectStore(storeName).delete(key); }; this.addWork(doDelete, storeName, true); @@ -387,17 +395,21 @@ class QueryRoot { /** * Low-level function to add a task to the internal work queue. */ - addWork(workFn: (IDBTransaction) => void, - storeName: string, - isWrite: boolean) { + addWork(workFn: (t: IDBTransaction) => void, + storeName?: string, + isWrite?: boolean) { + this.work.push(workFn); + if (storeName) { + this.addStoreAccess(storeName, isWrite); + } + } + + addStoreAccess(storeName: string, isWrite?: boolean) { if (storeName) { this.stores.add(storeName); } if (isWrite) { this.hasWrite = true; } - if (workFn) { - this.work.push(workFn); - } } } \ No newline at end of file -- cgit v1.2.3