diff options
Diffstat (limited to 'src/client/Server.ts')
-rw-r--r-- | src/client/Server.ts | 83 |
1 files changed, 29 insertions, 54 deletions
diff --git a/src/client/Server.ts b/src/client/Server.ts index ed69e70b7..2077c0c57 100644 --- a/src/client/Server.ts +++ b/src/client/Server.ts @@ -1,12 +1,12 @@ import { Field, FieldWaiting, FIELD_ID, FIELD_WAITING, FieldValue, Opt } from "../fields/Field" -import { Key, KeyStore } from "../fields/Key" -import { ObservableMap, action } from "mobx"; +import { Key } from "../fields/Key" +import { KeyStore } from "../fields/KeyStore" +import { ObservableMap, action, reaction, when } from "mobx"; import { Document } from "../fields/Document" import { SocketStub } from "./SocketStub"; import * as OpenSocket from 'socket.io-client'; import { Utils } from "./../Utils"; import { MessageStore, Types } from "./../server/Message"; -import { ListField } from "../fields/ListField"; export class Server { public static ClientFieldsCached: ObservableMap<FIELD_ID, Field | FIELD_WAITING> = new ObservableMap(); @@ -17,36 +17,45 @@ export class Server { // 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: Opt<Field>) => void = (f) => { }, doc: Document, key: Key, hackTimeout: number = -1) { - if (!this.ClientFieldsCached.get(fieldid)) { - var ft = this.times++; - var title = (!doc.fields.has(KeyStore.Title.Id) ? "???" : doc.Title) + "(" + doc.Id + ")"; - var mesg = " Query> field(" + ft + ") " + title + " " + key.Name; - console.log(mesg); + public static GetField(fieldid: FIELD_ID, callback: (field: Opt<Field>) => void = (f) => { }, doc?: Document, key?: Key, hackTimeout: number = -1) { + let cached = this.ClientFieldsCached.get(fieldid); + if (!cached) { this.ClientFieldsCached.set(fieldid, FieldWaiting); - //simulating a server call with a registered callback action SocketStub.SEND_FIELD_REQUEST(fieldid, action((field: Field | undefined) => { - console.log(" Reply> field(" + ft + ") " + title + " " + key.Name + " = " + (field ? field.GetValue() : "<undefined>")); - - if (this.ClientFieldsCached.has(fieldid) && this.ClientFieldsCached.get(fieldid) != FieldWaiting) - callback(this.ClientFieldsCached.get(fieldid) as Field); + let cached = this.ClientFieldsCached.get(fieldid); + if (cached != FieldWaiting) + callback(cached); else { if (field) { this.ClientFieldsCached.set(fieldid, field); + } else { + this.ClientFieldsCached.delete(fieldid) } callback(field) } })); - } else if (this.ClientFieldsCached.get(fieldid) != FieldWaiting) { - callback(this.ClientFieldsCached.get(fieldid) as Field); + } else if (cached != FieldWaiting) { + callback(cached); + } else { + reaction(() => { + return this.ClientFieldsCached.get(fieldid); + }, (field, reaction) => { + if (field !== "<Waiting>") { + callback(field) + reaction.dispose() + } + }) } - return this.ClientFieldsCached.get(fieldid); + return cached; } public static GetFields(fieldIds: FIELD_ID[], callback: (fields: { [id: string]: Field }) => any) { SocketStub.SEND_FIELDS_REQUEST(fieldIds, (fields) => { for (let key in fields) { let field = fields[key]; + if (!this.ClientFieldsCached.has(field.Id)) { + this.ClientFieldsCached.set(field.Id, field) + } } callback(fields) }); @@ -86,44 +95,11 @@ export class Server { SocketStub.SEND_DELETE_DOCUMENT_FIELD(doc, key); } - private static lock: boolean = false; - - static printfield(field: Field) { - if (field instanceof Key) { - return field.Name; - } - else if (field instanceof Document) { - var title = (field._proxies.has(KeyStore.Title.Id) ? field.Title : "???") - return title; - } else if (field instanceof ListField) { - var str = "["; - (field as ListField<Field>).Data.map(d => str += this.printfield(d)); - str += "]"; - return str; - } - return field.GetValue() - } - public static UpdateField(field: Field) { - if (this.lock) { - // setTimeout(this.UpdateField, 1000, field) - } - this.lock = true - var type = "field" - if (field instanceof Key) { - type = "Key"; + if (!this.ClientFieldsCached.has(field.Id)) { + this.ClientFieldsCached.set(field.Id, field) } - else if (field instanceof Document) { - type = "Doc"; - } else if (field instanceof ListField) { - type = "List" - } - console.log("Set: " + type + "(" + field.Id + ") =" + this.printfield(field)); - SocketStub.SEND_SET_FIELD(field, (args: any) => { - if (this.lock) { - this.lock = false - } - }); + SocketStub.SEND_SET_FIELD(field); } static connected(message: string) { @@ -146,7 +122,6 @@ export class Server { if (Server.ClientFieldsCached.has(field._id)) { var f = Server.ClientFieldsCached.get(field._id); if (f && f != FieldWaiting) { - console.log("Update from server:" + Server.printfield(f)); f.UpdateFromServer(field.data); f.init(() => { }); } |