aboutsummaryrefslogtreecommitdiff
path: root/src/new_fields/Doc.ts
diff options
context:
space:
mode:
authorTyler Schicke <tyler_schicke@brown.edu>2019-04-22 03:39:39 -0400
committerTyler Schicke <tyler_schicke@brown.edu>2019-04-22 03:39:39 -0400
commit5a9e437bbd175b36a161e1d96c8ae873dfe6d105 (patch)
tree0f54896387bb601aa18ee0be511a6059400d17eb /src/new_fields/Doc.ts
parent393d351420b3a0d28f4cd1ea8b674fa5d04bfcde (diff)
More
Diffstat (limited to 'src/new_fields/Doc.ts')
-rw-r--r--src/new_fields/Doc.ts13
1 files changed, 10 insertions, 3 deletions
diff --git a/src/new_fields/Doc.ts b/src/new_fields/Doc.ts
index 5d18cbb2e..60abccce6 100644
--- a/src/new_fields/Doc.ts
+++ b/src/new_fields/Doc.ts
@@ -4,7 +4,7 @@ import { autoObject, SerializationHelper, Deserializable } from "../client/util/
import { Utils } from "../Utils";
import { DocServer } from "../client/DocServer";
import { setter, getter, getField } from "./util";
-import { Cast, FieldCtor } from "./Types";
+import { Cast, ToConstructor } from "./Types";
export type FieldId = string;
export const HandleUpdate = Symbol("HandleUpdate");
@@ -28,6 +28,7 @@ export const Parent = Symbol("Parent");
export class ObjectField {
protected [OnUpdate]?: (diff?: any) => void;
private [Parent]?: Doc;
+ readonly [Id] = "";
}
export type Field = number | string | boolean | ObjectField | RefField;
@@ -71,7 +72,7 @@ export namespace Doc {
const self = doc[Self];
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> {
+ export function GetTAsync<T extends Field>(doc: Doc, key: string, ctor: ToConstructor<T>, ignoreProto: boolean = false): Promise<T | undefined> {
return new Promise(async res => {
const field = await GetAsync(doc, key, ignoreProto);
return Cast(field, ctor);
@@ -81,9 +82,15 @@ export namespace Doc {
const self = doc[Self];
return getField(self, key, ignoreProto);
}
- export function GetT<T extends Field>(doc: Doc, key: string, ctor: FieldCtor<T>, ignoreProto: boolean = false): T | null | undefined {
+ export function GetT<T extends Field>(doc: Doc, key: string, ctor: ToConstructor<T>, ignoreProto: boolean = false): T | null | undefined {
return Cast(Get(doc, key, ignoreProto), ctor) as T | null | undefined;
}
+ export async function SetOnPrototype(doc: Doc, key: string, value: Field) {
+ const proto = await Cast(doc.prototype, Doc);
+ if (proto) {
+ proto[key] = value;
+ }
+ }
export function MakeDelegate(doc: Opt<Doc>): Opt<Doc> {
if (!doc) {
return undefined;