aboutsummaryrefslogtreecommitdiff
path: root/src/new_fields/Doc.ts
diff options
context:
space:
mode:
authorTyler Schicke <tyler_schicke@brown.edu>2019-04-19 23:13:17 -0400
committerTyler Schicke <tyler_schicke@brown.edu>2019-04-19 23:13:17 -0400
commite678ac7f21e0c44eaa8ad88577093cdb313e21bb (patch)
tree9fb0f73365ae427ebbc5c48ca1887394e16fe5bd /src/new_fields/Doc.ts
parent3e96161afe4f48afa6abd1b2158b5a1c4fe85a32 (diff)
Deleted more old fields and split new stuff into multiple files
Diffstat (limited to 'src/new_fields/Doc.ts')
-rw-r--r--src/new_fields/Doc.ts90
1 files changed, 90 insertions, 0 deletions
diff --git a/src/new_fields/Doc.ts b/src/new_fields/Doc.ts
new file mode 100644
index 000000000..c67170573
--- /dev/null
+++ b/src/new_fields/Doc.ts
@@ -0,0 +1,90 @@
+import { observable, action } from "mobx";
+import { serializable, primitive, map, alias, list } from "serializr";
+import { autoObject, SerializationHelper, Deserializable } from "../client/util/SerializationHelper";
+import { Utils } from "../Utils";
+import { DocServer } from "../client/DocServer";
+import { setter, getter, getField } from "./util";
+import { Cast, FieldCtor } from "./Types";
+
+export const HandleUpdate = Symbol("HandleUpdate");
+export const Id = Symbol("Id");
+export abstract class RefField {
+ @serializable(alias("id", primitive()))
+ private __id: string;
+ readonly [Id]: string;
+
+ constructor(id?: string) {
+ this.__id = id || Utils.GenerateGuid();
+ this[Id] = this.__id;
+ }
+
+ protected [HandleUpdate]?(diff: any): void;
+}
+
+export const Update = Symbol("Update");
+export const OnUpdate = Symbol("OnUpdate");
+export const Parent = Symbol("Parent");
+export class ObjectField {
+ protected [OnUpdate]?: (diff?: any) => void;
+ private [Parent]?: Doc;
+}
+
+export type Field = number | string | boolean | ObjectField | RefField;
+export type Opt<T> = T | undefined;
+export type FieldWaiting = null;
+export const FieldWaiting: FieldWaiting = null;
+
+export const Self = Symbol("Self");
+
+@Deserializable("doc").withFields(["id"])
+export class Doc extends RefField {
+ constructor(id?: string, forceSave?: boolean) {
+ super(id);
+ const doc = new Proxy<this>(this, {
+ 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"); },
+ });
+ if (!id || forceSave) {
+ DocServer.CreateField(SerializationHelper.Serialize(doc));
+ }
+ return doc;
+ }
+
+ [key: string]: Field | null | undefined;
+
+ @serializable(alias("fields", map(autoObject())))
+ @observable
+ private __fields: { [key: string]: Field | null | undefined } = {};
+
+ private [Update] = (diff: any) => {
+ DocServer.UpdateField(this[Id], diff);
+ }
+
+ private [Self] = this;
+}
+
+export namespace Doc {
+ export function GetAsync(doc: Doc, key: string, ignoreProto: boolean = false): Promise<Field | undefined> {
+ 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> {
+ const self = doc[Self];
+ return new Promise(async res => {
+ const field = await GetAsync(doc, key, ignoreProto);
+ return Cast(field, ctor);
+ });
+ }
+ export function Get(doc: Doc, key: string, ignoreProto: boolean = false): Field | null | undefined {
+ 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): Field | null | undefined {
+ return Cast(Get(doc, key, ignoreProto), ctor);
+ }
+ export const Prototype = Symbol("Prototype");
+}
+
+export const GetAsync = Doc.GetAsync; \ No newline at end of file