diff options
author | bobzel <zzzman@gmail.com> | 2022-12-21 19:38:29 -0500 |
---|---|---|
committer | bobzel <zzzman@gmail.com> | 2022-12-21 19:38:29 -0500 |
commit | 631826f13c67aef760bc7e76665e152f1f63bf5b (patch) | |
tree | 8733aac0da4fcdf12277bf204a63c68ac7ac20aa /src/client/util/SerializationHelper.ts | |
parent | e373e66f8ed06f4501e00af8348f15ad113c7424 (diff) | |
parent | 4c0de84cf9a3d5be2f5058d514c8ca58e2004a4b (diff) |
cleaning up proxys and getFieldRef hopefully to be more understandable and efficient
Diffstat (limited to 'src/client/util/SerializationHelper.ts')
-rw-r--r-- | src/client/util/SerializationHelper.ts | 39 |
1 files changed, 21 insertions, 18 deletions
diff --git a/src/client/util/SerializationHelper.ts b/src/client/util/SerializationHelper.ts index 2d598c1ac..2d1f61cfb 100644 --- a/src/client/util/SerializationHelper.ts +++ b/src/client/util/SerializationHelper.ts @@ -1,6 +1,6 @@ -import { PropSchema, serialize, deserialize, custom, setDefaultModelSchema, getDefaultModelSchema } from "serializr"; -import { Field } from "../../fields/Doc"; -import { ClientUtils } from "./ClientUtils"; +import { PropSchema, serialize, deserialize, custom, setDefaultModelSchema, getDefaultModelSchema } from 'serializr'; +import { Field } from '../../fields/Doc'; +import { ClientUtils } from './ClientUtils'; let serializing = 0; export function afterDocDeserialize(cb: (err: any, val: any) => void, err: any, newValue: any) { @@ -25,7 +25,7 @@ export namespace SerializationHelper { serializing++; if (!(obj.constructor.name in reverseMap)) { serializing--; - throw Error("Error: " + `type '${obj.constructor.name}' not registered. Make sure you register it using a @Deserializable decorator`); + throw Error('Error: ' + `type '${obj.constructor.name}' not registered. Make sure you register it using a @Deserializable decorator`); } const json = serialize(obj); @@ -59,24 +59,24 @@ export namespace SerializationHelper { const type = serializationTypes[obj.__type]; const value = await new Promise(res => deserialize(type.ctor, obj, (err, result) => res(result))); if (type.afterDeserialize) { - await type.afterDeserialize(value); + type.afterDeserialize(value); } return value; } } -const serializationTypes: { [name: string]: { ctor: { new(): any }, afterDeserialize?: (obj: any) => void | Promise<any> } } = {}; +const serializationTypes: { [name: string]: { ctor: { new (): any }; afterDeserialize?: (obj: any) => void | Promise<any> } } = {}; const reverseMap: { [ctor: string]: string } = {}; export interface DeserializableOpts { - (constructor: { new(...args: any[]): any }): void; + (constructor: { new (...args: any[]): any }): void; withFields(fields: string[]): Function; } export function Deserializable(name: string, afterDeserialize?: (obj: any) => void | Promise<any>): DeserializableOpts; -export function Deserializable(constructor: { new(...args: any[]): any }): void; -export function Deserializable(constructor: { new(...args: any[]): any } | string, afterDeserialize?: (obj: any) => void): DeserializableOpts | void { - function addToMap(name: string, ctor: { new(...args: any[]): any }) { +export function Deserializable(constructor: { new (...args: any[]): any }): void; +export function Deserializable(constructor: { new (...args: any[]): any } | string, afterDeserialize?: (obj: any) => void): DeserializableOpts | void { + function addToMap(name: string, ctor: { new (...args: any[]): any }) { const schema = getDefaultModelSchema(ctor) as any; if (schema.targetClass !== ctor) { const newSchema = { ...schema, factory: () => new ctor() }; @@ -89,17 +89,20 @@ export function Deserializable(constructor: { new(...args: any[]): any } | strin throw new Error(`Name ${name} has already been registered as deserializable`); } } - if (typeof constructor === "string") { - return Object.assign((ctor: { new(...args: any[]): any }) => { - addToMap(constructor, ctor); - }, { withFields: (fields: string[]) => Deserializable.withFields(fields, constructor, afterDeserialize) }); + if (typeof constructor === 'string') { + return Object.assign( + (ctor: { new (...args: any[]): any }) => { + addToMap(constructor, ctor); + }, + { withFields: (fields: string[]) => Deserializable.withFields(fields, constructor, afterDeserialize) } + ); } addToMap(constructor.name, constructor); } export namespace Deserializable { export function withFields(fields: string[], name?: string, afterDeserialize?: (obj: any) => void | Promise<any>) { - return function (constructor: { new(...fields: any[]): any }) { + return function (constructor: { new (...fields: any[]): any }) { Deserializable(name || constructor.name, afterDeserialize)(constructor); let schema = getDefaultModelSchema(constructor); if (schema) { @@ -128,7 +131,7 @@ export namespace Deserializable { factory: context => { const args = fields.map(key => context.json[key]); return new constructor(...args); - } + }, }; setDefaultModelSchema(constructor, schema); } @@ -138,7 +141,7 @@ export namespace Deserializable { export function autoObject(): PropSchema { return custom( - (s) => SerializationHelper.Serialize(s), + s => SerializationHelper.Serialize(s), (json: any, context: any, oldValue: any, cb: (err: any, result: any) => void) => SerializationHelper.Deserialize(json).then(res => cb(null, res)) ); -}
\ No newline at end of file +} |