import { Utils } from "../Utils"; import { Types } from "../server/Message"; import { computed } from "mobx"; export function Cast(field: FieldValue, ctor: { new(): T }): Opt { if (field) { if (ctor && field instanceof ctor) { return field; } } return undefined; } export const FieldWaiting: FIELD_WAITING = ""; export type FIELD_WAITING = ""; export type FieldId = string; export type Opt = T | undefined; export type FieldValue = Opt | FIELD_WAITING; export abstract class Field { //FieldUpdated: TypedEvent> = new TypedEvent>(); init(callback: (res: Field) => any) { callback(this); } private id: FieldId; @computed get Id(): FieldId { return this.id; } constructor(id: Opt = undefined) { this.id = id || Utils.GenerateGuid(); } Dereference(): FieldValue { return this; } DereferenceToRoot(): FieldValue { return this; } DereferenceT(ctor: { new(): T }): FieldValue { return Cast(this.Dereference(), ctor); } DereferenceToRootT(ctor: { new(): T }): FieldValue { return Cast(this.DereferenceToRoot(), ctor); } Equals(other: Field): boolean { return this.id === other.id; } abstract UpdateFromServer(serverData: any): void; abstract ToScriptString(): string; abstract TrySetValue(value: any): boolean; abstract GetValue(): any; abstract Copy(): Field; abstract ToJson(): { _id: string, type: Types, data: any } }