diff options
-rw-r--r-- | src/Server.tsx | 115 | ||||
-rw-r--r-- | src/SocketStub.tsx | 78 | ||||
-rw-r--r-- | src/documents/Documents.ts | 5 | ||||
-rw-r--r-- | src/fields/Document.ts | 2 | ||||
-rw-r--r-- | src/fields/Field.ts | 1 |
5 files changed, 117 insertions, 84 deletions
diff --git a/src/Server.tsx b/src/Server.tsx index 08e400619..645420771 100644 --- a/src/Server.tsx +++ b/src/Server.tsx @@ -1,66 +1,55 @@ -import { Field, FieldWaiting, FIELD_ID, DOC_ID, FIELD_WAITING, FieldValue } from "./fields/Field" +import { Field, FieldWaiting, FIELD_ID, FIELD_WAITING, FieldValue } 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<FIELD_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.get(fieldid)) { + this.ClientFieldsCached.set(fieldid, FieldWaiting); + //simulating a server call with a registered callback action + SocketStub.SEND_FIELD_REQUEST(fieldid, + action((field: Field) => callback(Server.cacheField(field))), + hackTimeout); + } else if (this.ClientFieldsCached.get(fieldid) != FieldWaiting) { + callback(this.ClientFieldsCached.get(fieldid) as Field); } - return this.ClientFieldsCached.get(docid) as Document; + return this.ClientFieldsCached.get(fieldid); + } + + static times = 0; // hack for testing + public static GetDocumentField(doc: Document, key: Key) { + var hackTimeout: number = key == KeyStore.Data ? (this.times++ == 0 ? 5000 : 1000) : key == KeyStore.X ? 2500 : 500; + + return this.GetField(doc._proxies.get(key), + action((fieldfromserver: Field) => { + doc._proxies.delete(key); + doc.fields.set(key, fieldfromserver); + }) + , hackTimeout); } + 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); - }); + SocketStub.SEND_ADD_DOCUMENT(document); } 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); + SocketStub.SEND_ADD_DOCUMENT_FIELD(doc, key, value); } 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); + SocketStub.SEND_DELETE_DOCUMENT_FIELD(doc, 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); - } - - - @action - public static GetDocumentField(doc: Document, key: Key): FieldValue<Field> { - 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 field = this.ClientFieldsCached.get(fieldid); - if (field != FieldWaiting) { - doc._proxies.delete(key); // perhaps another document inquired the same field - } - return field; + SocketStub.SEND_SET_FIELD(field, value); } - static times = 0; // hack for testing @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 +58,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)), 50 - // 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)!), 50); - } - } - - @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); - } } diff --git a/src/SocketStub.tsx b/src/SocketStub.tsx new file mode 100644 index 000000000..f9d48c067 --- /dev/null +++ b/src/SocketStub.tsx @@ -0,0 +1,78 @@ +import { Field, FIELD_ID } from "./fields/Field" +import { Key, KeyStore } from "./fields/Key" +import { ObservableMap, action } from "mobx"; +import { Document } from "./fields/Document" + +export class SocketStub { + + static FieldStore: ObservableMap<FIELD_ID, Field> = new ObservableMap(); + public static SEND_ADD_DOCUMENT(document: Document) { + + // Send a serialized version of the document to the server + // ...SOCKET(ADD_DOCUMENT, serialied document) + + // server stores each document field in its repository of stored fields + document.fields.forEach((f, key) => this.FieldStore.set((f as Field).Id, f as Field)); + + // server stores stripped down document (w/ only field id proxies) in the field store + this.FieldStore.set(document.Id, new Document(document.Id)); + document.fields.forEach((f, key) => (this.FieldStore.get(document.Id) as Document)._proxies.set(key, (f as Field).Id)); + } + + public static SEND_FIELD_REQUEST(fieldid: FIELD_ID, callback: (field: Field) => void, timeout: number) { + + if (timeout < 0)// this is a hack to make things easier to setup until we have a server... won't be neededa fter that. + callback(this.FieldStore.get(fieldid) as Field); + else { // actual logic here... + + // Send a request for fieldid to the server + // ...SOCKET(RETRIEVE_FIELD, fieldid) + + // server responds (simulated with a timeout) and the callback is invoked + setTimeout(() => + + // when the field data comes back, call the callback() function + callback(this.FieldStore.get(fieldid) as Field), + + + timeout); + } + } + + public static SEND_ADD_DOCUMENT_FIELD(doc: Document, key: Key, value: Field) { + + // Send a serialized version of the field to the server along with the + // associated info of the document id and key where it is used. + + // ...SOCKET(ADD_DOCUMENT_FIELD, document id, key id, serialized field) + + // server updates its document to hold a proxy mapping from key => fieldId + var document = this.FieldStore.get(doc.Id) as Document; + if (document) + document._proxies.set(key, value.Id); + + // server adds the field to its repository of fields + this.FieldStore.set(value.Id, value); + } + + public static SEND_DELETE_DOCUMENT_FIELD(doc: Document, key: Key) { + // Send a request to delete the field stored under the specified key from the document + + // ...SOCKET(DELETE_DOCUMENT_FIELD, document id, key id) + + // Server removes the field id from the document's list of field proxies + var document = this.FieldStore.get(doc.Id) as Document; + if (document) + document._proxies.delete(key); + } + + public static SEND_SET_FIELD(field: Field, value: any) { + // Send a request to set the value of a field + + // ...SOCKET(SET_FIELD, field id, serialized field value) + + // Server updates the value of the field in its fieldstore + if (this.FieldStore.get(field.Id)) + this.FieldStore.get(field.Id)!.TrySetValue(value); + } +} diff --git a/src/documents/Documents.ts b/src/documents/Documents.ts index 90124d36c..ce9a1529d 100644 --- a/src/documents/Documents.ts +++ b/src/documents/Documents.ts @@ -123,7 +123,7 @@ export namespace Documents { Server.AddDocument(imageProto); return imageProto; } - return Server.GetDocument(imageProtoId, true)!; + return Server.GetField(imageProtoId) as Document; } export function ImageDocument(url: string, options: DocumentOptions = {}): Document { @@ -131,7 +131,8 @@ export namespace Documents { setupOptions(doc, options); doc.Set(KeyStore.Data, new ImageField(new URL(url))); Server.AddDocument(doc); - return Server.GetDocument(doc.Id, true)!; + var sdoc = Server.GetField(doc.Id) as Document; + return sdoc; } let collectionProto: Document; diff --git a/src/fields/Document.ts b/src/fields/Document.ts index e482df622..546c89e04 100644 --- a/src/fields/Document.ts +++ b/src/fields/Document.ts @@ -1,4 +1,4 @@ -import { Field, Cast, Opt, FieldWaiting, FIELD_ID, DOC_ID, FieldValue } from "./Field" +import { Field, Cast, Opt, FieldWaiting, FIELD_ID, FieldValue } from "./Field" import { Key, KeyStore } from "./Key" import { NumberField } from "./NumberField"; import { ObservableMap, computed, action, observable } from "mobx"; diff --git a/src/fields/Field.ts b/src/fields/Field.ts index 2bfc9d1da..6adee9b61 100644 --- a/src/fields/Field.ts +++ b/src/fields/Field.ts @@ -13,7 +13,6 @@ export function Cast<T extends Field>(field: FieldValue<Field>, ctor: { new(): T export let FieldWaiting: FIELD_WAITING = "<Waiting>"; export type FIELD_WAITING = "<Waiting>"; export type FIELD_ID = string | undefined; -export type DOC_ID = FIELD_ID; export type Opt<T> = T | undefined; export type FieldValue<T> = Opt<T> | FIELD_WAITING; |