gnunet-svn
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[GNUnet-SVN] [taler-wallet-webex] branch master updated: Add support for


From: gnunet
Subject: [GNUnet-SVN] [taler-wallet-webex] branch master updated: Add support for conditionals in DB queries
Date: Fri, 24 Mar 2017 16:21:45 +0100

This is an automated email from the git hooks/post-receive script.

dold pushed a commit to branch master
in repository wallet-webex.

The following commit(s) were added to refs/heads/master by this push:
     new 970230d  Add support for conditionals in DB queries
970230d is described below

commit 970230da068570385a03483dec0559049f3ac458
Author: Florian Dold <address@hidden>
AuthorDate: Fri Mar 24 16:20:31 2017 +0100

    Add support for conditionals in DB queries
---
 src/query.ts | 82 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 82 insertions(+)

diff --git a/src/query.ts b/src/query.ts
index 30633b0..89d4133 100644
--- a/src/query.ts
+++ b/src/query.ts
@@ -76,10 +76,87 @@ export interface QueryStream<T> {
   map<S>(f: (x:T) => S): QueryStream<S>;
   flatMap<S>(f: (x: T) => S[]): QueryStream<S>;
   toArray(): Promise<T[]>;
+  first(): QueryValue<T>;
 
   then(onfulfill: any, onreject: any): any;
 }
 
+
+/**
+ * Query result that consists of at most one value.
+ */
+export interface QueryValue<T> {
+  map<S>(f: (x: T) => S): QueryValue<S>;
+  cond<R>(f: (x: T) => boolean, onTrue: (r: QueryRoot) => R, onFalse: (r: 
QueryRoot) => R): Promise<void>;
+}
+
+
+abstract class BaseQueryValue<T> implements QueryValue<T> {
+  root: QueryRoot;
+
+  constructor(root: QueryRoot) {
+    this.root = root;
+  }
+
+  map<S>(f: (x: T) => S): QueryValue<S> {
+    return new MapQueryValue<T,S>(this, f);
+  }
+
+  cond<R>(f: (x: T) => boolean, onTrue: (r: QueryRoot) => R, onFalse: (r: 
QueryRoot) => R): Promise<void> {
+    return new Promise((resolve, reject) => {
+      this.subscribeOne((v, tx) => {
+        if (f(v)) {
+          onTrue(this.root);
+        } else {
+          onFalse(this.root);
+        }
+      });
+      resolve();
+    });
+  }
+
+  abstract subscribeOne(f: SubscribeOneFn): void;
+}
+
+class FirstQueryValue<T> extends BaseQueryValue<T> {
+  gotValue = false;
+  s: QueryStreamBase<T>;
+  constructor(stream: QueryStreamBase<T>) {
+    super(stream.root);
+    this.s = stream;
+  }
+
+  subscribeOne(f: SubscribeOneFn): void {
+    this.s.subscribe((isDone, value, tx) => {
+      if (this.gotValue) {
+        return;
+      }
+      if (isDone) {
+          f(undefined, tx);
+      } else {
+        f(value, tx);
+      }
+      this.gotValue = true;
+    });
+  }
+}
+
+class MapQueryValue<T,S> extends BaseQueryValue<S> {
+  mapFn: (x: T) => S;
+  v: BaseQueryValue<T>;
+
+  constructor(v: BaseQueryValue<T>, mapFn: (x: T) => S) {
+    super(v.root);
+    this.v = v;
+    this.mapFn = mapFn;
+  }
+
+  subscribeOne(f: SubscribeOneFn): void {
+    this.v.subscribeOne((v, tx) => this.mapFn(v));
+  }
+}
+
+
 export let AbortTransaction = Symbol("abort_transaction");
 
 /**
@@ -112,6 +189,10 @@ abstract class QueryStreamBase<T> implements 
QueryStream<T>, PromiseLike<void> {
     this.root = root;
   }
 
+  first(): QueryValue<T> {
+    return new FirstQueryValue(this);
+  }
+
   then<R>(onfulfilled: (value: void) => R | PromiseLike<R>, onrejected: 
(reason: any) => R | PromiseLike<R>): PromiseLike<R>  {
     return this.root.then(onfulfilled, onrejected);
   }
@@ -183,6 +264,7 @@ abstract class QueryStreamBase<T> implements 
QueryStream<T>, PromiseLike<void> {
 
 type FilterFn = (e: any) => boolean;
 type SubscribeFn = (done: boolean, value: any, tx: IDBTransaction) => void;
+type SubscribeOneFn = (value: any, tx: IDBTransaction) => void;
 
 interface FlatMapFn<T> {
   (v: T): T[];

-- 
To stop receiving notification emails like this one, please contact
address@hidden



reply via email to

[Prev in Thread] Current Thread [Next in Thread]