aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/client/Server.ts18
-rw-r--r--src/client/SocketStub.ts7
2 files changed, 20 insertions, 5 deletions
diff --git a/src/client/Server.ts b/src/client/Server.ts
index 6a47076fb..e2c352b14 100644
--- a/src/client/Server.ts
+++ b/src/client/Server.ts
@@ -2,7 +2,7 @@ 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 { SocketStub } from "./SocketStub";
+import { SocketStub, FieldMap } from "./SocketStub";
import * as OpenSocket from 'socket.io-client';
import { Utils } from "./../Utils";
import { MessageStore, Types } from "./../server/Message";
@@ -52,11 +52,15 @@ export class Server {
if (callback) {
fn(callback);
} else {
- return new Promise<Opt<Field>>(res => fn(res));
+ return new Promise(res => fn(res));
}
}
- public static GetFields(fieldIds: FieldId[], callback: (fields: { [id: string]: Field }) => any) {
+ 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 neededFieldIds: FieldId[] = [];
let waitingFieldIds: FieldId[] = [];
let existingFields: { [id: string]: Field } = {};
@@ -87,10 +91,16 @@ export class Server {
let realField = field as Field;
existingFields[realField.Id] = realField;
}
- callback({ ...fields, ...existingFields })
+ cb({ ...fields, ...existingFields })
}
}, { fireImmediately: true })
});
+ };
+ if (callback) {
+ fn(callback);
+ } else {
+ return new Promise(res => fn(res));
+ }
}
public static GetDocumentField(doc: Document, key: Key, callback?: (field: Field) => void) {
diff --git a/src/client/SocketStub.ts b/src/client/SocketStub.ts
index 27528c4c3..5045037c5 100644
--- a/src/client/SocketStub.ts
+++ b/src/client/SocketStub.ts
@@ -7,6 +7,11 @@ 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 {
@@ -54,7 +59,7 @@ export class SocketStub {
}
}
- public static SEND_FIELDS_REQUEST(fieldIds: FieldId[], callback: (fields: { [key: string]: Field }) => any) {
+ public static SEND_FIELDS_REQUEST(fieldIds: FieldId[], callback: (fields: FieldMap) => any) {
Utils.EmitCallback(Server.Socket, MessageStore.GetFields, fieldIds, (fields: any[]) => {
let fieldMap: any = {};
for (let field of fields) {