aboutsummaryrefslogtreecommitdiff
path: root/src/client/Server.ts
diff options
context:
space:
mode:
authorlaurawilsonri <laura_wilson@brown.edu>2019-04-11 14:12:49 -0400
committerlaurawilsonri <laura_wilson@brown.edu>2019-04-11 14:12:49 -0400
commitc392a9322c1df269cfd823dd82d07d991fe065c0 (patch)
treefdd44c511bd179984dc3dc18b92745751c86bfc5 /src/client/Server.ts
parent15514b0f3d685764d1bd7ebeac9cdee1f778e184 (diff)
parent50be8cb7a93110821c972c679567ddb6aae8bc6f (diff)
Merge branch 'master' of https://github.com/browngraphicslab/Dash-Web into richTextEditor
Diffstat (limited to 'src/client/Server.ts')
-rw-r--r--src/client/Server.ts91
1 files changed, 45 insertions, 46 deletions
diff --git a/src/client/Server.ts b/src/client/Server.ts
index 37e3c2c0d..3bbbebe72 100644
--- a/src/client/Server.ts
+++ b/src/client/Server.ts
@@ -1,16 +1,16 @@
-import { Key } from "../fields/Key"
-import { ObservableMap, action, reaction } from "mobx";
-import { Field, FieldWaiting, FIELD_WAITING, Opt, FieldId } from "../fields/Field"
-import { Document } from "../fields/Document"
+import { Key } from "../fields/Key";
+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 {
public static ClientFieldsCached: ObservableMap<FieldId, Field | FIELD_WAITING> = new ObservableMap();
static Socket: SocketIOClient.Socket = OpenSocket(`${window.location.protocol}//${window.location.hostname}:4321`);
- static GUID: string = Utils.GenerateGuid()
+ 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).
@@ -21,52 +21,52 @@ export class Server {
let fn = (cb: (field: Opt<Field>) => void) => {
let cached = this.ClientFieldsCached.get(fieldid);
- if (!cached) {
+ if (cached === undefined) {
this.ClientFieldsCached.set(fieldid, FieldWaiting);
SocketStub.SEND_FIELD_REQUEST(fieldid, action((field: Field | undefined) => {
let cached = this.ClientFieldsCached.get(fieldid);
- if (cached != FieldWaiting)
+ if (cached !== FieldWaiting) {
cb(cached);
+ }
else {
if (field) {
this.ClientFieldsCached.set(fieldid, field);
} else {
- this.ClientFieldsCached.delete(fieldid)
+ this.ClientFieldsCached.delete(fieldid);
}
- cb(field)
+ cb(field);
}
}));
- } else if (cached != FieldWaiting) {
+ } else if (cached !== FieldWaiting) {
setTimeout(() => cb(cached as Field), 0);
} else {
- reaction(() => {
- return this.ClientFieldsCached.get(fieldid);
- }, (field, reaction) => {
- if (field !== "<Waiting>") {
- reaction.dispose()
- cb(field)
- }
- })
+ reaction(() => this.ClientFieldsCached.get(fieldid),
+ (field, reaction) => {
+ if (field !== FieldWaiting) {
+ reaction.dispose();
+ cb(field);
+ }
+ });
}
- }
+ };
if (callback) {
fn(callback);
} else {
- return new Promise(res => fn(res));
+ return new Promise(fn);
}
}
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,37 +79,36 @@ export class Server {
for (let id of neededFieldIds) {
let field = fields[id];
if (field) {
- if (!(this.ClientFieldsCached.get(field.Id) instanceof Field)) {
- this.ClientFieldsCached.set(field.Id, 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")
+ throw new Error("we shouldn't be trying to replace things that are already in the cache");
}
} else {
if (this.ClientFieldsCached.get(id) === FieldWaiting) {
this.ClientFieldsCached.delete(id);
} else {
- throw new Error("we shouldn't be trying to replace things that are already in the cache")
+ throw new Error("we shouldn't be trying to replace things that are already in the cache");
}
}
}
- reaction(() => {
- return waitingFieldIds.map(id => this.ClientFieldsCached.get(id));
- }, (cachedFields, reaction) => {
- if (!cachedFields.some(field => !field || field === FieldWaiting)) {
- reaction.dispose();
- for (let field of cachedFields) {
- let realField = field as Field;
- existingFields[realField.Id] = realField;
+ reaction(() => waitingFieldIds.map(id => this.ClientFieldsCached.get(id)),
+ (cachedFields, reaction) => {
+ if (!cachedFields.some(field => field === FieldWaiting)) {
+ const realFields = cachedFields as Opt<Field>[];
+ reaction.dispose();
+ waitingFieldIds.forEach((id, index) => {
+ existingFields[id] = realFields[index];
+ });
+ cb({ ...fields, ...existingFields });
}
- cb({ ...fields, ...existingFields })
- }
- }, { fireImmediately: true })
+ }, { fireImmediately: true });
}));
- };
+ });
if (callback) {
fn(callback);
} else {
- return new Promise(res => fn(res));
+ return new Promise(fn);
}
}
@@ -141,7 +140,7 @@ export class Server {
public static UpdateField(field: Field) {
if (!this.ClientFieldsCached.has(field.Id)) {
- this.ClientFieldsCached.set(field.Id, field)
+ this.ClientFieldsCached.set(field.Id, field);
}
SocketStub.SEND_SET_FIELD(field);
}
@@ -153,22 +152,22 @@ export class Server {
@action
private static cacheField(clientField: Field) {
var cached = this.ClientFieldsCached.get(clientField.Id);
- if (!cached || cached == FieldWaiting) {
+ if (!cached) {
this.ClientFieldsCached.set(clientField.Id, clientField);
} else {
// probably should overwrite the values within any field that was already here...
}
- return this.ClientFieldsCached.get(clientField.Id) as Field;
+ return this.ClientFieldsCached.get(clientField.Id);
}
@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) {
+ if (f) {
// console.log("Applying : " + field._id);
f.UpdateFromServer(field.data);
- f.init(() => { });
+ f.init(emptyFunction);
} else {
// console.log("Not applying wa : " + field._id);
}