From 27b514747188f12e0b0eeeb124576c556316923d Mon Sep 17 00:00:00 2001 From: Tyler Schicke Date: Mon, 22 Apr 2019 22:57:18 -0400 Subject: Changes --- src/new_fields/Doc.ts | 11 ++++++----- src/new_fields/Schema.ts | 13 ++++++------- src/new_fields/Types.ts | 12 ++++++------ 3 files changed, 18 insertions(+), 18 deletions(-) (limited to 'src/new_fields') diff --git a/src/new_fields/Doc.ts b/src/new_fields/Doc.ts index 60abccce6..5ae095e68 100644 --- a/src/new_fields/Doc.ts +++ b/src/new_fields/Doc.ts @@ -33,8 +33,8 @@ export class ObjectField { export type Field = number | string | boolean | ObjectField | RefField; export type Opt = T | undefined; -export type FieldWaiting = Promise; -export type FieldResult = Opt | FieldWaiting; +export type FieldWaiting = T extends undefined ? never : Promise; +export type FieldResult = Opt | FieldWaiting>; export const Self = Symbol("Self"); @@ -58,7 +58,8 @@ export class Doc extends RefField { @serializable(alias("fields", map(autoObject()))) @observable - private __fields: { [key: string]: Field | FieldWaiting | undefined } = {}; + //{ [key: string]: Field | FieldWaiting | undefined } + private __fields: any = {}; private [Update] = (diff: any) => { DocServer.UpdateField(this[Id], diff); @@ -86,7 +87,7 @@ export namespace Doc { 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); + const proto = await Cast(doc.proto, Doc); if (proto) { proto[key] = value; } @@ -97,7 +98,7 @@ export namespace Doc { } const delegate = new Doc(); //TODO Does this need to be doc[Self]? - delegate.prototype = doc; + delegate.proto = doc; return delegate; } export const Prototype = Symbol("Prototype"); diff --git a/src/new_fields/Schema.ts b/src/new_fields/Schema.ts index 59c6db0bd..5081521c7 100644 --- a/src/new_fields/Schema.ts +++ b/src/new_fields/Schema.ts @@ -1,7 +1,5 @@ import { Interface, ToInterface, Cast, ToConstructor, HasTail, Head, Tail, ListSpec, ToType } from "./Types"; -import { Doc, Field, ObjectField } from "./Doc"; -import { URLField } from "./URLField"; -import { List } from "./List"; +import { Doc, Field } from "./Doc"; type AllToInterface = { 1: ToInterface> & AllToInterface>, @@ -15,7 +13,7 @@ export type Document = makeInterface<[typeof emptySchema]>; export type makeInterface = Partial> & Doc; // export function makeInterface(schemas: T): (doc: U) => All; // export function makeInterface(schema: T): (doc: U) => makeInterface; -export function makeInterface(...schemas: T): (doc: Doc) => makeInterface { +export function makeInterface(...schemas: T): (doc?: Doc) => makeInterface { let schema: Interface = {}; for (const s of schemas) { for (const key in s) { @@ -35,7 +33,8 @@ export function makeInterface(...schemas: T): (doc: Doc) return true; } }); - return function (doc: Doc) { + return function (doc?: Doc) { + doc = doc || new Doc; if (!(doc instanceof Doc)) { throw new Error("Currently wrapping a schema in another schema isn't supported"); } @@ -73,8 +72,8 @@ export function makeStrictInterface(schema: T): (doc: Doc) }; } -export function createSchema(schema: T): T & { prototype: ToConstructor } { - schema.prototype = Doc; +export function createSchema(schema: T): T & { proto: ToConstructor } { + schema.proto = Doc; return schema as any; } diff --git a/src/new_fields/Types.ts b/src/new_fields/Types.ts index fbf002c84..246b0624e 100644 --- a/src/new_fields/Types.ts +++ b/src/new_fields/Types.ts @@ -1,4 +1,4 @@ -import { Field, Opt, FieldWaiting, FieldResult } from "./Doc"; +import { Field, Opt, FieldWaiting, FieldResult, RefField } from "./Doc"; import { List } from "./List"; export type ToType | ListSpec> = @@ -18,7 +18,7 @@ export type ToConstructor = new (...args: any[]) => T; export type ToInterface = { - [P in keyof T]: ToType; + [P in keyof T]: FieldResult>; }; // type ListSpec = { List: ToContructor> | ListSpec> }; @@ -37,11 +37,11 @@ export interface Interface { // [key: string]: ToConstructor | ListSpec; } -export function Cast | ListSpec>(field: Field | FieldWaiting | undefined, ctor: T): FieldResult>; -export function Cast | ListSpec>(field: Field | FieldWaiting | undefined, ctor: T, defaultVal: ToType): ToType; -export function Cast | ListSpec>(field: Field | FieldWaiting | undefined, ctor: T, defaultVal?: ToType): FieldResult> | undefined { +export function Cast | ListSpec>(field: FieldResult, ctor: T): FieldResult>; +export function Cast | ListSpec>(field: FieldResult, ctor: T, defaultVal: ToType): ToType; +export function Cast | ListSpec>(field: FieldResult, ctor: T, defaultVal?: ToType): FieldResult> | undefined { if (field instanceof Promise) { - return defaultVal === undefined ? field.then(f => Cast(f, ctor) as any) : defaultVal; + return defaultVal === undefined ? field.then(f => Cast(f, ctor) as any) as any : defaultVal; } if (field !== undefined && !(field instanceof Promise)) { if (typeof ctor === "string") { -- cgit v1.2.3-70-g09d2