aboutsummaryrefslogtreecommitdiff
path: root/src/client/DocServer.ts
diff options
context:
space:
mode:
authorTyler Schicke <tyler_schicke@brown.edu>2019-05-07 16:29:02 -0400
committerTyler Schicke <tyler_schicke@brown.edu>2019-05-07 16:29:02 -0400
commit14c776b6d30e0bc0d5b3712f28e4b9f1170eae3b (patch)
tree5255d8cce8a72a5b09cc1ad58661e2176295467a /src/client/DocServer.ts
parente19fdbba4cf672aee5bfb59b91b6162431d146d3 (diff)
parent26141a697ae52a7edf3cc6845ce2153111f8860e (diff)
Merge branch 'master' of github-tsch-brown:browngraphicslab/Dash-Web into new_search
Diffstat (limited to 'src/client/DocServer.ts')
-rw-r--r--src/client/DocServer.ts78
1 files changed, 68 insertions, 10 deletions
diff --git a/src/client/DocServer.ts b/src/client/DocServer.ts
index 9a3e122e8..a288d394a 100644
--- a/src/client/DocServer.ts
+++ b/src/client/DocServer.ts
@@ -1,20 +1,32 @@
import * as OpenSocket from 'socket.io-client';
-import { MessageStore, Types } from "./../server/Message";
-import { Opt, FieldWaiting, RefField, HandleUpdate } from '../fields/NewDoc';
+import { MessageStore } from "./../server/Message";
+import { Opt } from '../new_fields/Doc';
import { Utils } from '../Utils';
import { SerializationHelper } from './util/SerializationHelper';
+import { RefField, HandleUpdate, Id } from '../new_fields/RefField';
export namespace DocServer {
const _cache: { [id: string]: RefField | Promise<Opt<RefField>> } = {};
const _socket = OpenSocket(`${window.location.protocol}//${window.location.hostname}:4321`);
const GUID: string = Utils.GenerateGuid();
+ export function prepend(extension: string): string {
+ return window.location.origin + extension;
+ }
+
+ export function DeleteDatabase() {
+ Utils.Emit(_socket, MessageStore.DeleteAll, {});
+ }
+
export async function GetRefField(id: string): Promise<Opt<RefField>> {
let cached = _cache[id];
if (cached === undefined) {
const prom = Utils.EmitCallback(_socket, MessageStore.GetRefField, id).then(fieldJson => {
- const field = fieldJson === undefined ? fieldJson : SerializationHelper.Deserialize(fieldJson);
- if (field) {
+ const field = SerializationHelper.Deserialize(fieldJson);
+ if (_cache[id] !== undefined && !(_cache[id] instanceof Promise)) {
+ id;
+ }
+ if (field !== undefined) {
_cache[id] = field;
} else {
delete _cache[id];
@@ -30,17 +42,61 @@ export namespace DocServer {
}
}
+ export async function GetRefFields(ids: string[]): Promise<{ [id: string]: Opt<RefField> }> {
+ const requestedIds: string[] = [];
+ const waitingIds: string[] = [];
+ const promises: Promise<Opt<RefField>>[] = [];
+ const map: { [id: string]: Opt<RefField> } = {};
+ for (const id of ids) {
+ const cached = _cache[id];
+ if (cached === undefined) {
+ requestedIds.push(id);
+ } else if (cached instanceof Promise) {
+ promises.push(cached);
+ waitingIds.push(id);
+ } else {
+ map[id] = cached;
+ }
+ }
+ const prom = Utils.EmitCallback(_socket, MessageStore.GetRefFields, requestedIds).then(fields => {
+ const fieldMap: { [id: string]: RefField } = {};
+ for (const field of fields) {
+ if (field !== undefined) {
+ fieldMap[field.id] = SerializationHelper.Deserialize(field);
+ }
+ }
+ return fieldMap;
+ });
+ requestedIds.forEach(id => _cache[id] = prom.then(fields => fields[id]));
+ const fields = await prom;
+ requestedIds.forEach(id => {
+ const field = fields[id];
+ if (field !== undefined) {
+ _cache[id] = field;
+ } else {
+ delete _cache[id];
+ }
+ map[id] = field;
+ });
+ const otherFields = await Promise.all(promises);
+ waitingIds.forEach((id, index) => map[id] = otherFields[index]);
+ return map;
+ }
+
export function UpdateField(id: string, diff: any) {
+ if (id === updatingId) {
+ return;
+ }
Utils.Emit(_socket, MessageStore.UpdateField, { id, diff });
}
- export function CreateField(initialState: any) {
- if (!("id" in initialState)) {
- throw new Error("Can't create a field on the server without an id");
- }
+ export function CreateField(field: RefField) {
+ _cache[field[Id]] = field;
+ const initialState = SerializationHelper.Serialize(field);
Utils.Emit(_socket, MessageStore.CreateField, initialState);
}
+ let updatingId: string | undefined;
function respondToUpdate(diff: any) {
const id = diff.id;
if (id === undefined) {
@@ -53,7 +109,9 @@ export namespace DocServer {
}
const handler = f[HandleUpdate];
if (handler) {
- handler(diff);
+ updatingId = id;
+ handler.call(f, diff.diff);
+ updatingId = undefined;
}
};
if (field instanceof Promise) {
@@ -63,7 +121,7 @@ export namespace DocServer {
}
}
- function connected(message: string) {
+ function connected() {
_socket.emit(MessageStore.Bar.Message, GUID);
}