aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/client/Server.ts173
-rw-r--r--src/client/SocketStub.ts69
2 files changed, 0 insertions, 242 deletions
diff --git a/src/client/Server.ts b/src/client/Server.ts
deleted file mode 100644
index 66e9878d9..000000000
--- a/src/client/Server.ts
+++ /dev/null
@@ -1,173 +0,0 @@
-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, 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();
-
- // 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>>;
- public static GetField(fieldid: FieldId, callback: (field: Opt<Field>) => void): void;
- public static GetField(fieldid: FieldId, callback?: (field: Opt<Field>) => void): Promise<Opt<Field>> | void {
- let fn = (cb: (field: Opt<Field>) => void) => {
-
- let cached = this.ClientFieldsCached.get(fieldid);
- 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) {
- cb(cached);
- }
- else {
- if (field) {
- this.ClientFieldsCached.set(fieldid, field);
- } else {
- this.ClientFieldsCached.delete(fieldid);
- }
- cb(field);
- }
- }));
- } else if (cached !== FieldWaiting) {
- setTimeout(() => cb(cached as Field), 0);
- } else {
- reaction(() => this.ClientFieldsCached.get(fieldid),
- (field, reaction) => {
- if (field !== FieldWaiting) {
- reaction.dispose();
- cb(field);
- }
- });
- }
- };
- if (callback) {
- fn(callback);
- } else {
- 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 = action((cb: (fields: FieldMap) => void) => {
-
- let neededFieldIds: FieldId[] = [];
- let waitingFieldIds: FieldId[] = [];
- let existingFields: FieldMap = {};
- for (let id of fieldIds) {
- let field = this.ClientFieldsCached.get(id);
- if (field === undefined) {
- neededFieldIds.push(id);
- this.ClientFieldsCached.set(id, FieldWaiting);
- } else if (field === FieldWaiting) {
- waitingFieldIds.push(id);
- } else {
- existingFields[id] = field;
- }
- }
- SocketStub.SEND_FIELDS_REQUEST(neededFieldIds, action((fields: FieldMap) => {
- for (let id of neededFieldIds) {
- let field = fields[id];
- if (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");
- }
- } 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");
- }
- }
- }
- 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 });
- }
- }, { fireImmediately: true });
- }));
- });
- if (callback) {
- fn(callback);
- } else {
- return new Promise(fn);
- }
- }
-
- public static GetDocumentField(doc: Document, key: Key, callback?: (field: Field) => void) {
- let field = doc._proxies.get(key.Id);
- if (field) {
- this.GetField(field,
- action((fieldfromserver: Opt<Field>) => {
- if (fieldfromserver) {
- doc.fields.set(key.Id, { key, field: fieldfromserver });
- if (callback) {
- callback(fieldfromserver);
- }
- }
- }));
- }
- }
-
- 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);
- }
-
- static connected(message: string) {
- Server.Socket.emit(MessageStore.Bar.Message, Server.GUID);
- }
-
- @action
- private static cacheField(clientField: Field) {
- var cached = this.ClientFieldsCached.get(clientField.Id);
- 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);
- }
-
- @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) {
- // console.log("Applying : " + field.id);
- f.UpdateFromServer(field.data);
- f.init(emptyFunction);
- } else {
- // console.log("Not applying wa : " + field.id);
- }
- } else {
- // console.log("Not applying mi : " + field.id);
- }
- }
-}
-
-Utils.AddServerHandler(Server.Socket, MessageStore.Foo, Server.connected);
-Utils.AddServerHandler(Server.Socket, MessageStore.SetField, Server.updateField);
diff --git a/src/client/SocketStub.ts b/src/client/SocketStub.ts
deleted file mode 100644
index 382a81f66..000000000
--- a/src/client/SocketStub.ts
+++ /dev/null
@@ -1,69 +0,0 @@
-import { Key } from "../fields/Key";
-import { Field, FieldId, Opt } from "../fields/Field";
-import { ObservableMap } from "mobx";
-import { Document } from "../fields/Document";
-import { MessageStore, Transferable } from "../server/Message";
-import { Utils } from "../Utils";
-import { Server } from "./Server";
-import { ServerUtils } from "../server/ServerUtil";
-
-
-export interface FieldMap {
- [id: string]: Opt<Field>;
-}
-
-//TODO tfs: I think it might be cleaner to not have SocketStub deal with turning what the server gives it into Fields (in other words not call ServerUtils.FromJson), and leave that for the Server class.
-export class SocketStub {
-
- static FieldStore: ObservableMap<FieldId, Field> = new ObservableMap();
-
- public static SEND_FIELD_REQUEST(fieldid: FieldId): Promise<Opt<Field>>;
- public static SEND_FIELD_REQUEST(fieldid: FieldId, callback: (field: Opt<Field>) => void): void;
- public static SEND_FIELD_REQUEST(fieldid: FieldId, callback?: (field: Opt<Field>) => void): Promise<Opt<Field>> | void {
- let fn = function (cb: (field: Opt<Field>) => void) {
- Utils.EmitCallback(Server.Socket, MessageStore.GetField, fieldid, (field: Transferable) => {
- if (field) {
- ServerUtils.FromJson(field).init(cb);
- } else {
- cb(undefined);
- }
- });
- };
- if (callback) {
- fn(callback);
- } else {
- return new Promise(fn);
- }
- }
-
- public static SEND_FIELDS_REQUEST(fieldIds: FieldId[], callback: (fields: FieldMap) => any) {
- Utils.EmitCallback(Server.Socket, MessageStore.GetFields, fieldIds, (fields: Transferable[]) => {
- let fieldMap: FieldMap = {};
- fields.map(field => fieldMap[field.id] = ServerUtils.FromJson(field));
- let proms = Object.values(fieldMap).map(val =>
- new Promise(resolve => val!.init(resolve)));
- Promise.all(proms).then(() => callback(fieldMap));
- });
- }
-
- public static SEND_DELETE_DOCUMENT_FIELD(doc: Document, key: Key) {
- // Send a request to delete the field stored under the specified key from the document
-
- // ...SOCKET(DELETE_DOCUMENT_FIELD, document id, key id)
-
- // Server removes the field id from the document's list of field proxies
- var document = this.FieldStore.get(doc.Id) as Document;
- if (document) {
- document._proxies.delete(key.Id);
- }
- }
-
- public static SEND_SET_FIELD(field: Field) {
- // Send a request to set the value of a field
-
- // ...SOCKET(SET_FIELD, field id, serialized field value)
-
- // Server updates the value of the field in its fieldstore
- Utils.Emit(Server.Socket, MessageStore.SetField, field.ToJson());
- }
-}