From 3c882c44b5aba1ae397a2b89f99f4cdb82fbbbfa Mon Sep 17 00:00:00 2001 From: Florian Dold Date: Sun, 10 Dec 2017 23:02:00 +0100 Subject: fix problems found by newer TypeScript compiler --- src/query.ts | 30 ++++++++++++++++++++++++++---- 1 file changed, 26 insertions(+), 4 deletions(-) (limited to 'src/query.ts') 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 { filter(f: (x: T) => boolean): QueryStream; /** - * Reduce the stream, resulting in a single value. + * Fold the stream, resulting in a single value. */ - reduce(f: (v: T, acc?: S) => S, start?: S): Promise; + fold(f: (v: T, acc: S) => S, start: S): Promise; + + /** + * Execute a function for every value of the stream, for the + * side-effects of the function. + */ + forEach(f: (v: T) => void): Promise; /** * Map each element of the stream using a function, resulting in another @@ -324,7 +330,7 @@ abstract class QueryStreamBase implements QueryStream { .then(() => promise); } - reduce(f: (x: any, acc?: A) => A, init?: A): Promise { + fold(f: (x: T, acc: A) => A, init: A): Promise { const {resolve, promise} = openPromise(); let acc = init; @@ -341,6 +347,22 @@ abstract class QueryStreamBase implements QueryStream { .then(() => promise); } + forEach(f: (x: T) => void): Promise { + 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 { 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(store: Store, key: any, f: (v: T|undefined) => T|undefined): QueryRoot { + mutate(store: Store, key: any, f: (v: T) => T|undefined): QueryRoot { this.checkFinished(); const doPut = (tx: IDBTransaction) => { const req = tx.objectStore(store.name).openCursor(IDBKeyRange.only(key)); -- cgit v1.2.3