aboutsummaryrefslogtreecommitdiff
path: root/src/client/Server.ts
diff options
context:
space:
mode:
authorAndrew Kim <andrewdkim@users.noreply.github.com>2019-04-20 18:42:01 -0400
committerAndrew Kim <andrewdkim@users.noreply.github.com>2019-04-20 18:42:01 -0400
commit840de58f003d0962ef7d3a0ad6ea284d1f4870db (patch)
tree32377ecaedceb24e54b6645ae76f3a5e0171fd54 /src/client/Server.ts
parent604d20941837fa90c06a7da7e77a27c262cd4648 (diff)
parente47656cdc18aa1fd801a3853fa0f819140a68646 (diff)
update
Diffstat (limited to 'src/client/Server.ts')
-rw-r--r--src/client/Server.ts46
1 files changed, 19 insertions, 27 deletions
diff --git a/src/client/Server.ts b/src/client/Server.ts
index 857101a33..66e9878d9 100644
--- a/src/client/Server.ts
+++ b/src/client/Server.ts
@@ -1,10 +1,10 @@
import { Key } from "../fields/Key";
-import { ObservableMap, action, reaction } from "mobx";
+import { ObservableMap, action, reaction, runInAction } from "mobx";
import { Field, FieldWaiting, FIELD_WAITING, Opt, FieldId } from "../fields/Field";
import { Document } from "../fields/Document";
import { SocketStub, FieldMap } from "./SocketStub";
import * as OpenSocket from 'socket.io-client';
-import { Utils } from "./../Utils";
+import { Utils, emptyFunction } from "./../Utils";
import { MessageStore, Types } from "./../server/Message";
export class Server {
@@ -12,7 +12,6 @@ export class Server {
static Socket: SocketIOClient.Socket = OpenSocket(`${window.location.protocol}//${window.location.hostname}:4321`);
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.
public static GetField(fieldid: FieldId): Promise<Opt<Field>>;
@@ -59,14 +58,14 @@ export class Server {
public static GetFields(fieldIds: FieldId[]): Promise<{ [id: string]: Field }>;
public static GetFields(fieldIds: FieldId[], callback: (fields: FieldMap) => any): void;
public static GetFields(fieldIds: FieldId[], callback?: (fields: FieldMap) => any): Promise<FieldMap> | void {
- let fn = (cb: (fields: FieldMap) => void) => {
+ let fn = action((cb: (fields: FieldMap) => void) => {
let neededFieldIds: FieldId[] = [];
let waitingFieldIds: FieldId[] = [];
- let existingFields: { [id: string]: Field } = {};
+ let existingFields: FieldMap = {};
for (let id of fieldIds) {
let field = this.ClientFieldsCached.get(id);
- if (!field) {
+ if (field === undefined) {
neededFieldIds.push(id);
this.ClientFieldsCached.set(id, FieldWaiting);
} else if (field === FieldWaiting) {
@@ -79,7 +78,7 @@ export class Server {
for (let id of neededFieldIds) {
let field = fields[id];
if (field) {
- if (!(this.ClientFieldsCached.get(field.Id) instanceof Field)) {
+ if (this.ClientFieldsCached.get(field.Id) === FieldWaiting) {
this.ClientFieldsCached.set(field.Id, field);
} else {
throw new Error("we shouldn't be trying to replace things that are already in the cache");
@@ -94,17 +93,17 @@ export class Server {
}
reaction(() => waitingFieldIds.map(id => this.ClientFieldsCached.get(id)),
(cachedFields, reaction) => {
- if (!cachedFields.some(field => !field)) {
+ if (!cachedFields.some(field => field === FieldWaiting)) {
+ const realFields = cachedFields as Opt<Field>[];
reaction.dispose();
- for (let field of cachedFields) {
- let realField = field as Field;
- existingFields[realField.Id] = realField;
- }
+ waitingFieldIds.forEach((id, index) => {
+ existingFields[id] = realFields[index];
+ });
cb({ ...fields, ...existingFields });
}
}, { fireImmediately: true });
}));
- };
+ });
if (callback) {
fn(callback);
} else {
@@ -127,13 +126,6 @@ export class Server {
}
}
- 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);
}
@@ -161,18 +153,18 @@ export class Server {
}
@action
- static updateField(field: { _id: string, data: any, type: Types }) {
- if (Server.ClientFieldsCached.has(field._id)) {
- var f = Server.ClientFieldsCached.get(field._id);
+ static updateField(field: { id: string, data: any, type: Types }) {
+ if (Server.ClientFieldsCached.has(field.id)) {
+ var f = Server.ClientFieldsCached.get(field.id);
if (f) {
- // console.log("Applying : " + field._id);
+ // console.log("Applying : " + field.id);
f.UpdateFromServer(field.data);
- f.init(() => { });
+ f.init(emptyFunction);
} else {
- // console.log("Not applying wa : " + field._id);
+ // console.log("Not applying wa : " + field.id);
}
} else {
- // console.log("Not applying mi : " + field._id);
+ // console.log("Not applying mi : " + field.id);
}
}
}