diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/Server.tsx | 90 | ||||
-rw-r--r-- | src/documents/Documents.ts | 4 |
2 files changed, 59 insertions, 35 deletions
diff --git a/src/Server.tsx b/src/Server.tsx index 2f8135ed0..11a40a825 100644 --- a/src/Server.tsx +++ b/src/Server.tsx @@ -2,7 +2,6 @@ import { Field, FieldWaiting, FIELD_ID, DOC_ID, FIELD_WAITING } from "./fields/F import { Key, KeyStore } from "./fields/Key" import { ObservableMap, computed, action, observable } from "mobx"; import { Document } from "./fields/Document" -import { TextField } from "./fields/TextField"; export class Server { static FieldStore: ObservableMap<FIELD_ID, Field> = new ObservableMap(); @@ -10,18 +9,18 @@ export class Server { public static ClientFieldsWaiting: ObservableMap<FIELD_ID, boolean> = new ObservableMap(); public static ClientFieldsCached: ObservableMap<DOC_ID, Field | FIELD_WAITING> = new ObservableMap(); - public static GetDocument(docid: DOC_ID) { - if (this.ClientFieldsCached.has(docid)) - return this.ClientFieldsCached.get(docid) as Document; - - if (this.DocumentStore.has(docid)) { - var clientDoc = new Document(docid); - this.cacheFieldInstance(clientDoc); - return clientDoc; // bcz: careful ... this assumes the document is on the server. if it's not, the client will have a document with no backing store. + // '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); } + 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)); @@ -29,56 +28,81 @@ export class Server { }); } 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); } + @action 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); + } - // replace this block with appropriate callback-style fetch code from actual server - setTimeout(action(() => { - var fieldfromserver = this.FieldStore.get(fieldid); - this.ClientFieldsWaiting.delete(fieldid); - doc._proxies.delete(key); - fieldfromserver = this.cacheFieldInstance(fieldfromserver!); - doc.fields.set(key, fieldfromserver); - }), - key == KeyStore.Data ? (this.times++ == 0 ? 5000 : 1000) : key == KeyStore.X ? 2500 : 500 - ) + var field = this.ClientFieldsCached.get(fieldid); + if (field != FieldWaiting) { + doc._proxies.delete(key); // perhaps another document inquired the same field } - return this.ClientFieldsCached.get(fieldid); + return field; } static times = 0; // hack for testing @action - static cacheFieldInstance(clientField: Field) { + static cacheField(clientField: Field) { var cached = this.ClientFieldsCached.get(clientField.Id); if (!cached || cached == FieldWaiting) { this.ClientFieldsCached.set(clientField.Id, clientField); - - // if the field is a document, then we need to inquire all of its fields from the server... - if (clientField instanceof Document) { - clientField.Set(KeyStore.Title, new TextField(clientField.Title)); - setTimeout(action(() => { - var clientDocFields = this.DocumentStore.get(clientField.Id); - clientDocFields!.forEach((field: FIELD_ID, key: Key) => clientField._proxies.set(key, field)); - } - ), - 1500); - } + } else { + // probably should overwrite the values within any field that was already here... } 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) { + this.ClientFieldsWaiting.delete(fieldid); + 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/documents/Documents.ts b/src/documents/Documents.ts index 2ad6b64c3..90124d36c 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)!; + return Server.GetDocument(imageProtoId, true)!; } export function ImageDocument(url: string, options: DocumentOptions = {}): Document { @@ -131,7 +131,7 @@ export namespace Documents { setupOptions(doc, options); doc.Set(KeyStore.Data, new ImageField(new URL(url))); Server.AddDocument(doc); - return Server.GetDocument(doc.Id)!; + return Server.GetDocument(doc.Id, true)!; } let collectionProto: Document; |