From e5171e5fbc73c93487c1545871e52e6b8fa190a6 Mon Sep 17 00:00:00 2001 From: Tyler Schicke Date: Sun, 21 Apr 2019 18:15:24 -0400 Subject: Refactored schema to be faster hopefully --- src/new_fields/Schema.ts | 32 +++++++++++++++++++++++--------- 1 file changed, 23 insertions(+), 9 deletions(-) (limited to 'src') diff --git a/src/new_fields/Schema.ts b/src/new_fields/Schema.ts index 6022716be..696a5d2a8 100644 --- a/src/new_fields/Schema.ts +++ b/src/new_fields/Schema.ts @@ -16,6 +16,7 @@ export const emptySchema = createSchema({}); export const Document = makeInterface(emptySchema); export type Document = makeInterface<[typeof emptySchema]>; +const DocSymbol = Symbol("Doc"); export type makeInterface = Partial> & U; // export function makeInterface(schemas: T): (doc: U) => All; // export function makeInterface(schema: T): (doc: U) => makeInterface; @@ -26,16 +27,29 @@ export function makeInterface(...schemas: schema[key] = s[key]; } } - return function (doc: any) { - return new Proxy(doc, { - get(target, prop) { - const field = target[prop]; - if (prop in schema) { - return Cast(field, (schema as any)[prop]); - } - return field; + const proto = new Proxy({}, { + get(target: any, prop) { + if (prop === DocSymbol) { + return target[prop]; } - }); + const field = target[DocSymbol][prop]; + if (prop in schema) { + return Cast(field, (schema as any)[prop]); + } + return field; + }, + set(target: any, prop, value) { + if (prop === DocSymbol) { + target[prop] = value; + } + target[DocSymbol][prop] = value; + return true; + } + }); + return function (doc: any) { + const obj = Object.create(proto); + obj.__doc = doc; + return obj; }; } -- cgit v1.2.3-70-g09d2