aboutsummaryrefslogtreecommitdiff
path: root/src/query.ts
diff options
context:
space:
mode:
authorFlorian Dold <florian.dold@gmail.com>2017-12-10 23:02:00 +0100
committerFlorian Dold <florian.dold@gmail.com>2017-12-10 23:02:00 +0100
commit3c882c44b5aba1ae397a2b89f99f4cdb82fbbbfa (patch)
tree842d51eb84dcc48ed4240c27551c973bd369ef6d /src/query.ts
parent0469abd4a9c9270a1fdc962969e36e63699af8b4 (diff)
fix problems found by newer TypeScript compiler
Diffstat (limited to 'src/query.ts')
-rw-r--r--src/query.ts30
1 files changed, 26 insertions, 4 deletions
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));