aboutsummaryrefslogtreecommitdiff
path: root/src/client/Server.ts
diff options
context:
space:
mode:
Diffstat (limited to 'src/client/Server.ts')
-rw-r--r--src/client/Server.ts106
1 files changed, 84 insertions, 22 deletions
diff --git a/src/client/Server.ts b/src/client/Server.ts
index a08fc2e87..2077c0c57 100644
--- a/src/client/Server.ts
+++ b/src/client/Server.ts
@@ -1,54 +1,104 @@
-import { Field, FieldWaiting, FIELD_ID, FIELD_WAITING, FieldValue } from "../fields/Field"
-import { Key, KeyStore } from "../fields/Key"
-import { ObservableMap, action } from "mobx";
+import { Field, FieldWaiting, FIELD_ID, FIELD_WAITING, FieldValue, Opt } from "../fields/Field"
+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 } from "./../server/Message";
+import { MessageStore, Types } from "./../server/Message";
export class Server {
- private static ClientFieldsCached: ObservableMap<FIELD_ID, Field | FIELD_WAITING> = new ObservableMap();
- static Socket: SocketIOClient.Socket = OpenSocket("http://localhost:1234")
+ public static ClientFieldsCached: ObservableMap<FIELD_ID, Field | FIELD_WAITING> = new ObservableMap();
+ static Socket: SocketIOClient.Socket = OpenSocket("http://localhost:1234");
static GUID: string = Utils.GenerateGuid()
+
// 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)) {
+ 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) => callback(Server.cacheField(field))));
- } else if (this.ClientFieldsCached.get(fieldid) != FieldWaiting) {
- callback(this.ClientFieldsCached.get(fieldid) as Field);
+ SocketStub.SEND_FIELD_REQUEST(fieldid, action((field: Field | undefined) => {
+ 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 (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)
+ });
}
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;
-
+ // let keyId: string = element[0]
+ // let valueId: string = element[1]
+ // Server.GetField(keyId, (key: Field) => {
+ // if (key instanceof Key) {
+ // Server.GetField(valueId, (field: Field) => {
+ // console.log(field)
+ // doc.Set(key as Key, field)
+ // })
+ // }
+ // else {
+ // console.log("how did you get a key that isnt a key wtf")
+ // }
+ // })
return this.GetField(doc._proxies.get(key.Id),
- action((fieldfromserver: Field) => {
- doc._proxies.delete(key.Id);
- doc.fields.set(key, fieldfromserver);
- })
- , hackTimeout);
+ action((fieldfromserver: Opt<Field>) => {
+ if (fieldfromserver) {
+ doc.fields.set(key.Id, { key, field: fieldfromserver });
+ }
+ }), doc, key);
}
public static AddDocument(document: Document) {
SocketStub.SEND_ADD_DOCUMENT(document);
}
public static AddDocumentField(doc: Document, key: Key, value: Field) {
+ console.log("Add doc field " + doc.Title + " " + key.Name + " fid " + value.Id + " " + value);
SocketStub.SEND_ADD_DOCUMENT_FIELD(doc, key, value);
}
public static DeleteDocumentField(doc: Document, key: Key) {
SocketStub.SEND_DELETE_DOCUMENT_FIELD(doc, key);
}
+
public static UpdateField(field: Field) {
+ if (!this.ClientFieldsCached.has(field.Id)) {
+ this.ClientFieldsCached.set(field.Id, field)
+ }
SocketStub.SEND_SET_FIELD(field);
}
@@ -66,6 +116,18 @@ export class Server {
}
return this.ClientFieldsCached.get(clientField.Id) as Field;
}
+
+ @action
+ static updateField(field: { _id: string, data: any, type: Types }) {
+ if (Server.ClientFieldsCached.has(field._id)) {
+ var f = Server.ClientFieldsCached.get(field._id);
+ if (f && f != FieldWaiting) {
+ f.UpdateFromServer(field.data);
+ f.init(() => { });
+ }
+ }
+ }
}
-Server.Socket.on(MessageStore.Foo.Message, Server.connected); \ No newline at end of file
+Server.Socket.on(MessageStore.Foo.Message, Server.connected);
+Server.Socket.on(MessageStore.SetField.Message, Server.updateField); \ No newline at end of file