From 9ee0823b7e4a97a2b1812847eaabdf6cf846655e Mon Sep 17 00:00:00 2001 From: Florian Dold Date: Wed, 19 Oct 2016 23:55:58 +0200 Subject: introduce map for query streams --- lib/wallet/query.ts | 32 ++++++++++++++++++++++++++++++-- 1 file changed, 30 insertions(+), 2 deletions(-) (limited to 'lib/wallet/query.ts') diff --git a/lib/wallet/query.ts b/lib/wallet/query.ts index c369e4b67..acf9aa44d 100644 --- a/lib/wallet/query.ts +++ b/lib/wallet/query.ts @@ -66,7 +66,8 @@ export interface QueryStream { keyFn: (obj: T) => I): QueryStream>; filter(f: (T: any) => boolean): QueryStream; reduce(f: (v: T, acc: S) => S, start?: S): Promise; - flatMap(f: (x: T) => T[]): QueryStream; + map(f: (x:T) => S): QueryStream; + flatMap(f: (x: T) => S[]): QueryStream; toArray(): Promise; } @@ -102,10 +103,14 @@ abstract class QueryStreamBase implements QueryStream { this.root = root; } - flatMap(f: (x: T) => T[]): QueryStream { + flatMap(f: (x: T) => T[]): QueryStream { return new QueryStreamFlatMap(this, f); } + map(f: (x: T) => S): QueryStream { + return new QueryStreamMap(this, f); + } + indexJoin(index: Index, keyFn: (obj: T) => I): QueryStream> { this.root.addStoreAccess(index.storeName, false); @@ -213,6 +218,29 @@ class QueryStreamFlatMap extends QueryStreamBase { } +class QueryStreamMap extends QueryStreamBase { + s: QueryStreamBase; + mapFn: (v: T) => T[]; + + constructor(s: QueryStreamBase, mapFn: (v: T) => T[]) { + super(s.root); + this.s = s; + this.mapFn = mapFn; + } + + subscribe(f: SubscribeFn) { + this.s.subscribe((isDone, value, tx) => { + if (isDone) { + f(true, undefined, tx); + return; + } + let mappedValue = this.mapFn(value); + f(false, mappedValue, tx); + }); + } +} + + class QueryStreamIndexJoin extends QueryStreamBase> { s: QueryStreamBase; storeName: string; -- cgit v1.2.3