aboutsummaryrefslogtreecommitdiff
path: root/src/new_fields
diff options
context:
space:
mode:
authorTyler Schicke <tyler_schicke@brown.edu>2019-04-20 19:06:28 -0400
committerTyler Schicke <tyler_schicke@brown.edu>2019-04-20 19:06:28 -0400
commit8ddec1c70c01b3d7d919908299e1168b75698dc7 (patch)
tree653353473d8865d8b738dfcdc02f56dda77f9132 /src/new_fields
parent1eb965a5d9c8aaebf1970bc645edecfb7017b601 (diff)
More refactoring
Diffstat (limited to 'src/new_fields')
-rw-r--r--src/new_fields/Doc.ts13
-rw-r--r--src/new_fields/List.ts2
-rw-r--r--src/new_fields/Proxy.ts20
-rw-r--r--src/new_fields/Types.ts2
-rw-r--r--src/new_fields/URLField.ts12
-rw-r--r--src/new_fields/util.ts4
6 files changed, 26 insertions, 27 deletions
diff --git a/src/new_fields/Doc.ts b/src/new_fields/Doc.ts
index e0eb44ee9..8cbd8cf38 100644
--- a/src/new_fields/Doc.ts
+++ b/src/new_fields/Doc.ts
@@ -6,14 +6,15 @@ import { DocServer } from "../client/DocServer";
import { setter, getter, getField } from "./util";
import { Cast, FieldCtor } from "./Types";
+export type FieldId = string;
export const HandleUpdate = Symbol("HandleUpdate");
export const Id = Symbol("Id");
export abstract class RefField {
@serializable(alias("id", primitive()))
- private __id: string;
- readonly [Id]: string;
+ private __id: FieldId;
+ readonly [Id]: FieldId;
- constructor(id?: string) {
+ constructor(id?: FieldId) {
this.__id = id || Utils.GenerateGuid();
this[Id] = this.__id;
}
@@ -39,7 +40,7 @@ export const Self = Symbol("Self");
@Deserializable("doc").withFields(["id"])
export class Doc extends RefField {
- constructor(id?: string, forceSave?: boolean) {
+ constructor(id?: FieldId, forceSave?: boolean) {
super(id);
const doc = new Proxy<this>(this, {
set: setter,
@@ -53,7 +54,7 @@ export class Doc extends RefField {
return doc;
}
- [key: string]: Field | null | undefined;
+ [key: string]: Field | FieldWaiting | undefined;
@serializable(alias("fields", map(autoObject())))
@observable
@@ -72,7 +73,6 @@ export namespace Doc {
return new Promise(res => getField(self, key, ignoreProto, res));
}
export function GetTAsync<T extends Field>(doc: Doc, key: string, ctor: FieldCtor<T>, ignoreProto: boolean = false): Promise<T | undefined> {
- const self = doc[Self];
return new Promise(async res => {
const field = await GetAsync(doc, key, ignoreProto);
return Cast(field, ctor);
@@ -90,6 +90,7 @@ export namespace Doc {
return undefined;
}
const delegate = new Doc();
+ //TODO Does this need to be doc[Self]?
delegate.prototype = doc;
return delegate;
}
diff --git a/src/new_fields/List.ts b/src/new_fields/List.ts
index a1a623f83..58b252f7b 100644
--- a/src/new_fields/List.ts
+++ b/src/new_fields/List.ts
@@ -9,7 +9,7 @@ class ListImpl<T extends Field> extends ObjectField {
constructor() {
super();
const list = new Proxy<this>(this, {
- set: function (a, b, c, d) { return setter(a, b, c, d); },
+ set: setter,
get: getter,
deleteProperty: () => { throw new Error("Currently properties can't be deleted from documents, assign to undefined instead"); },
defineProperty: () => { throw new Error("Currently properties can't be defined on documents using Object.defineProperty"); },
diff --git a/src/new_fields/Proxy.ts b/src/new_fields/Proxy.ts
index 3b4b2e452..6a78388c1 100644
--- a/src/new_fields/Proxy.ts
+++ b/src/new_fields/Proxy.ts
@@ -1,7 +1,8 @@
import { Deserializable } from "../client/util/SerializationHelper";
-import { RefField, Id, ObjectField } from "./Doc";
+import { RefField, Id, ObjectField, FieldWaiting } from "./Doc";
import { primitive, serializable } from "serializr";
-import { observable } from "mobx";
+import { observable, action } from "mobx";
+import { DocServer } from "../client/DocServer";
@Deserializable("proxy")
export class ProxyField<T extends RefField> extends ObjectField {
@@ -32,7 +33,7 @@ export class ProxyField<T extends RefField> extends ObjectField {
private failed = false;
private promise?: Promise<any>;
- value(callback?: ((field: T | undefined) => void)): T | undefined | null {
+ value(callback?: ((field: T | undefined) => void)): T | undefined | FieldWaiting {
if (this.cache) {
callback && callback(this.cache);
return this.cache;
@@ -41,13 +42,12 @@ export class ProxyField<T extends RefField> extends ObjectField {
return undefined;
}
if (!this.promise) {
- // this.promise = Server.GetField(this.fieldId).then(action((field: any) => {
- // this.promise = undefined;
- // this.cache = field;
- // if (field === undefined) this.failed = true;
- // return field;
- // }));
- this.promise = new Promise(r => r());
+ this.promise = DocServer.GetRefField(this.fieldId).then(action((field: any) => {
+ this.promise = undefined;
+ this.cache = field;
+ if (field === undefined) this.failed = true;
+ return field;
+ }));
}
callback && this.promise.then(callback);
return null;
diff --git a/src/new_fields/Types.ts b/src/new_fields/Types.ts
index 6ffb3e02f..0fbd8984c 100644
--- a/src/new_fields/Types.ts
+++ b/src/new_fields/Types.ts
@@ -50,8 +50,6 @@ export function Cast<T extends FieldCtor<Field>>(field: Field | null | undefined
} else if (field instanceof (ctor as any)) {
return field as ToType<T>;
}
- } else {
- return defaultVal;
}
return defaultVal;
}
diff --git a/src/new_fields/URLField.ts b/src/new_fields/URLField.ts
index d27a2b692..e456a7d16 100644
--- a/src/new_fields/URLField.ts
+++ b/src/new_fields/URLField.ts
@@ -1,16 +1,16 @@
import { Deserializable } from "../client/util/SerializationHelper";
-import { serializable } from "serializr";
+import { serializable, custom } from "serializr";
import { ObjectField } from "./Doc";
function url() {
- return {
- serializer: function (value: URL) {
+ return custom(
+ function (value: URL) {
return value.href;
},
- deserializer: function (jsonValue: string, done: (err: any, val: any) => void) {
- done(undefined, new URL(jsonValue));
+ function (jsonValue: string) {
+ return new URL(jsonValue);
}
- };
+ );
}
@Deserializable("url")
diff --git a/src/new_fields/util.ts b/src/new_fields/util.ts
index 0f08ecf03..3806044bd 100644
--- a/src/new_fields/util.ts
+++ b/src/new_fields/util.ts
@@ -22,6 +22,7 @@ export function setter(target: any, prop: string | symbol | number, value: any,
value = new ProxyField(value);
}
if (value instanceof ObjectField) {
+ //TODO Instead of target, maybe use target[Self]
if (value[Parent] && value[Parent] !== target) {
throw new Error("Can't put the same object in multiple documents at the same time");
}
@@ -51,7 +52,7 @@ export function getter(target: any, prop: string | symbol | number, receiver: an
if (SerializationHelper.IsSerializing()) {
return target[prop];
}
- return getField(target, prop, receiver);
+ return getField(target, prop);
}
export function getField(target: any, prop: string | number, ignoreProto: boolean = false, callback?: (field: Field | undefined) => void): any {
@@ -69,5 +70,4 @@ export function getField(target: any, prop: string | number, ignoreProto: boolea
}
callback && callback(field);
return field;
-
}