aboutsummaryrefslogtreecommitdiff
path: root/src/client
diff options
context:
space:
mode:
Diffstat (limited to 'src/client')
-rw-r--r--src/client/DocServer.ts97
-rw-r--r--src/client/views/Main.tsx5
-rw-r--r--src/client/views/MetadataEntryMenu.tsx1
3 files changed, 63 insertions, 40 deletions
diff --git a/src/client/DocServer.ts b/src/client/DocServer.ts
index d05793ea2..6737657c8 100644
--- a/src/client/DocServer.ts
+++ b/src/client/DocServer.ts
@@ -5,6 +5,7 @@ import { Utils, emptyFunction } from '../Utils';
import { SerializationHelper } from './util/SerializationHelper';
import { RefField } from '../new_fields/RefField';
import { Id, HandleUpdate } from '../new_fields/FieldSymbols';
+import { CurrentUserUtils } from '../server/authentication/models/current_user_utils';
/**
* This class encapsulates the transfer and cross-client synchronization of
@@ -21,12 +22,31 @@ import { Id, HandleUpdate } from '../new_fields/FieldSymbols';
*/
export namespace DocServer {
let _cache: { [id: string]: RefField | Promise<Opt<RefField>> } = {};
- const _socket = OpenSocket(`${window.location.protocol}//${window.location.hostname}:4321`);
+ let _socket: SocketIOClient.Socket;
// this client's distinct GUID created at initialization
- const GUID: string = Utils.GenerateGuid();
+ let GUID: string;
// indicates whether or not a document is currently being udpated, and, if so, its id
let updatingId: string | undefined;
+ export function init(protocol: string, hostname: string, port: number, identifier: string) {
+ _cache = {};
+ GUID = identifier;
+ _socket = OpenSocket(`${protocol}//${hostname}:${port}`);
+
+ _GetRefField = _GetRefFieldImpl;
+ _GetRefFields = _GetRefFieldsImpl;
+ _CreateField = _CreateFieldImpl;
+ _UpdateField = _UpdateFieldImpl;
+
+ /**
+ * Whenever the server sends us its handshake message on our
+ * websocket, we use the above function to return the handshake.
+ */
+ Utils.AddServerHandler(_socket, MessageStore.Foo, onConnection);
+ Utils.AddServerHandler(_socket, MessageStore.UpdateField, respondToUpdate);
+ Utils.AddServerHandler(_socket, MessageStore.DeleteField, respondToDelete);
+ Utils.AddServerHandler(_socket, MessageStore.DeleteFields, respondToDelete);
+ }
/**
* A convenience method. Prepends the full path (i.e. http://localhost:1050) to the
* requested extension
@@ -36,6 +56,10 @@ export namespace DocServer {
return window.location.origin + extension;
}
+ function errorFunc(): never {
+ throw new Error("Can't use DocServer without calling init first");
+ }
+
export namespace Control {
let _isReadOnly = false;
@@ -63,22 +87,16 @@ export namespace DocServer {
}
- export namespace Util {
-
- /**
- * Whenever the server sends us its handshake message on our
- * websocket, we use the above function to return the handshake.
- */
- Utils.AddServerHandler(_socket, MessageStore.Foo, onConnection);
+ /**
+ * This function emits a message (with this client's
+ * unique GUID) to the server
+ * indicating that this client has connected
+ */
+ function onConnection() {
+ _socket.emit(MessageStore.Bar.Message, GUID);
+ }
- /**
- * This function emits a message (with this client's
- * unique GUID) to the server
- * indicating that this client has connected
- */
- function onConnection() {
- _socket.emit(MessageStore.Bar.Message, GUID);
- }
+ export namespace Util {
/**
* Emits a message to the server that wipes
@@ -98,7 +116,7 @@ export namespace DocServer {
* the server if the document has not been cached.
* @param id the id of the requested document
*/
- export async function GetRefField(id: string): Promise<Opt<RefField>> {
+ const _GetRefFieldImpl = (id: string): Promise<Opt<RefField>> => {
// an initial pass through the cache to determine whether the document needs to be fetched,
// is already in the process of being fetched or already exists in the
// cache
@@ -139,8 +157,14 @@ export namespace DocServer {
return cached;
} else {
// CACHED => great, let's just return the cached field we have
- return cached;
+ return Promise.resolve(cached);
}
+ };
+
+ let _GetRefField: (id: string) => Promise<Opt<RefField>> = errorFunc;
+
+ export function GetRefField(id: string): Promise<Opt<RefField>> {
+ return _GetRefField(id);
}
/**
@@ -149,7 +173,7 @@ export namespace DocServer {
* the server if the document has not been cached.
* @param ids the ids that map to the reqested documents
*/
- export async function GetRefFields(ids: string[]): Promise<{ [id: string]: Opt<RefField> }> {
+ const _GetRefFieldsImpl = async (ids: string[]): Promise<{ [id: string]: Opt<RefField> }> => {
const requestedIds: string[] = [];
const waitingIds: string[] = [];
const promises: Promise<Opt<RefField>>[] = [];
@@ -245,16 +269,13 @@ export namespace DocServer {
// argument to the caller's promise (i.e. GetRefFields(["_id1_", "_id2_", "_id3_"]).then(map => //do something with map...))
// or it is the direct return result if the promise is awaited (i.e. let fields = await GetRefFields(["_id1_", "_id2_", "_id3_"])).
return map;
- }
+ };
- function _UpdateFieldImpl(id: string, diff: any) {
- if (id === updatingId) {
- return;
- }
- Utils.Emit(_socket, MessageStore.UpdateField, { id, diff });
- }
+ let _GetRefFields: (ids: string[]) => Promise<{ [id: string]: Opt<RefField> }> = errorFunc;
- let _UpdateField = _UpdateFieldImpl;
+ export function GetRefFields(ids: string[]) {
+ return _GetRefFields(ids);
+ }
// WRITE A NEW DOCUMENT TO THE SERVER
@@ -274,7 +295,7 @@ export namespace DocServer {
Utils.Emit(_socket, MessageStore.CreateField, initialState);
}
- let _CreateField = _CreateFieldImpl;
+ let _CreateField: (field: RefField) => void = errorFunc;
// NOTIFY THE SERVER OF AN UPDATE TO A DOC'S STATE
@@ -290,6 +311,15 @@ export namespace DocServer {
_UpdateField(id, updatedState);
}
+ function _UpdateFieldImpl(id: string, diff: any) {
+ if (id === updatingId) {
+ return;
+ }
+ Utils.Emit(_socket, MessageStore.UpdateField, { id, diff });
+ }
+
+ let _UpdateField: (id: string, diff: any) => void = errorFunc;
+
function _respondToUpdateImpl(diff: any) {
const id = diff.id;
// to be valid, the Diff object must reference
@@ -355,13 +385,4 @@ export namespace DocServer {
function respondToDelete(ids: string | string[]) {
_respondToDelete(ids);
}
-
- function connected() {
- _socket.emit(MessageStore.Bar.Message, GUID);
- }
-
- Utils.AddServerHandler(_socket, MessageStore.Foo, connected);
- Utils.AddServerHandler(_socket, MessageStore.UpdateField, respondToUpdate);
- Utils.AddServerHandler(_socket, MessageStore.DeleteField, respondToDelete);
- Utils.AddServerHandler(_socket, MessageStore.DeleteFields, respondToDelete);
} \ No newline at end of file
diff --git a/src/client/views/Main.tsx b/src/client/views/Main.tsx
index 589542806..80399e24b 100644
--- a/src/client/views/Main.tsx
+++ b/src/client/views/Main.tsx
@@ -6,6 +6,7 @@ import * as React from 'react';
import { Cast } from "../../new_fields/Types";
import { Doc, DocListCastAsync } from "../../new_fields/Doc";
import { List } from "../../new_fields/List";
+import { DocServer } from "../DocServer";
let swapDocs = async () => {
let oldDoc = await Cast(CurrentUserUtils.UserDocument.linkManagerDoc, Doc);
@@ -28,8 +29,10 @@ let swapDocs = async () => {
}
(async () => {
+ const info = await CurrentUserUtils.loadCurrentUser();
+ DocServer.init(window.location.protocol, window.location.hostname, 4321, info.email);
await Docs.Prototypes.initialize();
- await CurrentUserUtils.loadCurrentUser();
+ await CurrentUserUtils.loadUserDocument(info);
await swapDocs();
ReactDOM.render(<MainView />, document.getElementById('root'));
})(); \ No newline at end of file
diff --git a/src/client/views/MetadataEntryMenu.tsx b/src/client/views/MetadataEntryMenu.tsx
index 08abb9887..5ee661944 100644
--- a/src/client/views/MetadataEntryMenu.tsx
+++ b/src/client/views/MetadataEntryMenu.tsx
@@ -152,7 +152,6 @@ export class MetadataEntryMenu extends React.Component<MetadataEntryProps>{
}
render() {
- trace();
return (
<div className="metadataEntry-outerDiv">
Key: