From ccee60f591cdb00b04c4e5db0483420b9db7b7c8 Mon Sep 17 00:00:00 2001
From: bob <bcz@cs.brown.edu>
Date: Thu, 7 Feb 2019 16:04:32 -0500
Subject: simplified DB API and added a SocketStub class that should be
 replaced with an actual Socket-to-DB implementation.

---
 src/Server.tsx | 117 +++++++++++++++++++--------------------------------------
 1 file changed, 38 insertions(+), 79 deletions(-)

(limited to 'src/Server.tsx')

diff --git a/src/Server.tsx b/src/Server.tsx
index 04473424a..83f0d0f43 100644
--- a/src/Server.tsx
+++ b/src/Server.tsx
@@ -1,66 +1,59 @@
 import { Field, FieldWaiting, FIELD_ID, DOC_ID, FIELD_WAITING } from "./fields/Field"
 import { Key, KeyStore } from "./fields/Key"
-import { ObservableMap, computed, action, observable } from "mobx";
+import { ObservableMap, action } from "mobx";
 import { Document } from "./fields/Document"
+import { SocketStub } from "./SocketStub";
 
 export class Server {
-    static FieldStore: ObservableMap<FIELD_ID, Field> = new ObservableMap();
-    static DocumentStore: ObservableMap<DOC_ID, ObservableMap<Key, FIELD_ID>> = new ObservableMap();
-    public static ClientFieldsCached: ObservableMap<DOC_ID, Field | FIELD_WAITING> = new ObservableMap();
+    private static ClientFieldsCached: ObservableMap<DOC_ID, Field | FIELD_WAITING> = new ObservableMap();
 
-    // 'hack' is here temoporarily for simplicity when debugging things.
-    // normally, you can't assume this will return a document since the server responds asynchronously 
-    // and there might not actually be a matching document on the server.
-    // the right way to call this is from within a reaction where you test whether the return value is FieldWaiting.
-    public static GetDocument(docid: DOC_ID, hack: boolean = false) {
-        if (!this.ClientFieldsCached.has(docid)) {
-            this.SEND_DOCUMENT_REQUEST(docid, hack);
+    // Retrieves the cached value of the field and sends a request to the server for the real value (if it's not cached).
+    // Call this is from within a reaction and test whether the return value is FieldWaiting.
+    // 'hackTimeout' is here temporarily for simplicity when debugging things.
+    public static GetField(fieldid: FIELD_ID, callback: (field: Field) => void = (f) => { }, hackTimeout: number = -1) {
+        if (!this.ClientFieldsCached.has(fieldid)) {
+            this.ClientFieldsCached.set(fieldid, FieldWaiting);
+            //simulating a server call with a registered callback action
+            SocketStub.SEND_FIELD_REQUEST(fieldid,
+                action((field: Field) => {
+                    Server.cacheField(field);
+                    callback(field);
+                }), hackTimeout);
         }
-        return this.ClientFieldsCached.get(docid) as Document;
-    }
-    public static AddDocument(document: Document) {
-        // Replace with call to server
-        this.DocumentStore.set(document.Id, new ObservableMap());
-        document.fields.forEach((field, key) => {
-            this.FieldStore.set((field as Field).Id, (field as Field));
-            this.DocumentStore.get(document.Id)!.set(key, (field as Field).Id);
-        });
-    }
-    public static AddDocumentField(doc: Document, key: Key, value: Field) {
-        // Replace with call to server
-        if (this.DocumentStore.get(doc.Id))
-            this.DocumentStore.get(doc.Id)!.set(key, value.Id);
-    }
-    public static DeleteDocumentField(doc: Document, key: Key) {
-        // Replace with call to server
-        if (this.DocumentStore.get(doc.Id))
-            this.DocumentStore.get(doc.Id)!.delete(key);
-    }
-    public static SetFieldValue(field: Field, value: any) {
-        // Replace with call to server
-        if (this.FieldStore.get(field.Id))
-            this.FieldStore.get(field.Id)!.TrySetValue(value);
+        return this.ClientFieldsCached.get(fieldid);
     }
 
-
-    @action
+    static times = 0; // hack for testing
     public static GetDocumentField(doc: Document, key: Key) {
-        var fieldid = doc._proxies.get(key);
-        if (!this.ClientFieldsCached.has(fieldid)) {
-            this.ClientFieldsCached.set(fieldid, FieldWaiting);
-            this.SEND_DOCUMENT_FIELD_REQUEST(doc, key, fieldid);
-        }
+        var timeoutHack: number = key == KeyStore.Data ? (this.times++ == 0 ? 5000 : 1000) : key == KeyStore.X ? 2500 : 500;
 
-        var field = this.ClientFieldsCached.get(fieldid);
+        var field = this.GetField(doc._proxies.get(key),
+            action((fieldfromserver: Field) => {
+                doc._proxies.delete(key);
+                doc.fields.set(key, this.cacheField(fieldfromserver));
+            })
+            , timeoutHack);
         if (field != FieldWaiting) {
             doc._proxies.delete(key); // perhaps another document inquired the same field 
         }
         return field;
     }
-    static times = 0; // hack for testing
+
+    public static AddDocument(document: Document) {
+        SocketStub.SEND_ADD_DOCUMENT(document);
+    }
+    public static AddDocumentField(doc: Document, key: Key, value: Field) {
+        SocketStub.SEND_ADD_DOCUMENT_FIELD(doc, key, value);
+    }
+    public static DeleteDocumentField(doc: Document, key: Key) {
+        SocketStub.SEND_DELETE_DOCUMENT_FIELD(doc, key);
+    }
+    public static SetFieldValue(field: Field, value: any) {
+        SocketStub.SEND_SET_FIELD(field, value);
+    }
 
     @action
-    static cacheField(clientField: Field) {
+    private static cacheField(clientField: Field) {
         var cached = this.ClientFieldsCached.get(clientField.Id);
         if (!cached || cached == FieldWaiting) {
             this.ClientFieldsCached.set(clientField.Id, clientField);
@@ -69,38 +62,4 @@ export class Server {
         }
         return this.ClientFieldsCached.get(clientField.Id) as Field;
     }
-
-    public static SEND_DOCUMENT_FIELD_REQUEST(doc: Document, key: Key, fieldid: FIELD_ID) {
-        //simulating a server call with a registered callback action
-        setTimeout(() => this.receivedDocumentField(doc, key, fieldid, this.FieldStore.get(fieldid)),
-            key == KeyStore.Data ? (this.times++ == 0 ? 5000 : 1000) : key == KeyStore.X ? 2500 : 500
-        )
-    }
-
-    public static SEND_DOCUMENT_REQUEST(docid: DOC_ID, hack: boolean = false) {
-        if (hack) { // temporary for debugging
-            this.receivedDocument(docid, this.DocumentStore.get(docid)!)
-        } else {
-            //simulating a server call with a registered callback action
-            setTimeout(() => this.receivedDocument(docid, this.DocumentStore.get(docid)!), 1500);
-        }
-    }
-
-    @action
-    static receivedDocument(docid: DOC_ID, fieldlist: ObservableMap<Key, FIELD_ID>) {
-        var cachedDoc = this.cacheField(new Document(docid));
-        fieldlist!.forEach((field: FIELD_ID, key: Key) => (cachedDoc as Document)._proxies.set(key, field));
-    }
-
-    @action
-    static receivedDocumentField(doc: Document, key: Key, fieldid: FIELD_ID, fieldfromserver: Field | undefined) {
-        doc._proxies.delete(key);
-        var cachedField = this.cacheField(fieldfromserver!);
-
-        // if the field is a document and it wasn't already cached, then we need to inquire all of its fields from the server...
-        if (cachedField instanceof Document && fieldfromserver! == cachedField) {
-            this.SEND_DOCUMENT_REQUEST(cachedField.Id);
-        }
-        doc.fields.set(key, cachedField);
-    }
 }
-- 
cgit v1.2.3-70-g09d2

