diff options
author | Zachary Zhang <zacharyzhang7@gmail.com> | 2024-08-31 00:46:29 -0400 |
---|---|---|
committer | Zachary Zhang <zacharyzhang7@gmail.com> | 2024-08-31 00:46:29 -0400 |
commit | 196294f331496262bef256da8b8e9dbc80288bea (patch) | |
tree | 85ff27b7a8070585f9a5ef71dff63566e03232ba /src/client/util/SerializationHelper.ts | |
parent | 0cf61501ec9be34294935f01973c1bd9cad6d267 (diff) | |
parent | c36607691e0b7f5c04f3209a64958f5e51ddd785 (diff) |
Merge branch 'master' into zach-starter
Diffstat (limited to 'src/client/util/SerializationHelper.ts')
-rw-r--r-- | src/client/util/SerializationHelper.ts | 35 |
1 files changed, 19 insertions, 16 deletions
diff --git a/src/client/util/SerializationHelper.ts b/src/client/util/SerializationHelper.ts index d9d22437c..ccb02fb79 100644 --- a/src/client/util/SerializationHelper.ts +++ b/src/client/util/SerializationHelper.ts @@ -1,14 +1,15 @@ import { PropSchema, serialize, deserialize, custom, setDefaultModelSchema, getDefaultModelSchema } from 'serializr'; +import Context from 'serializr/lib/core/Context'; // import { Field } from '../../fields/Doc'; let serializing = 0; -export function afterDocDeserialize(cb: (err: any, val: any) => void, err: any, newValue: any) { +export function afterDocDeserialize(cb: (err: unknown, val: unknown) => void, err: unknown, newValue: unknown) { serializing++; cb(err, newValue); serializing--; } -const serializationTypes: { [name: string]: { ctor: { new (): any }; afterDeserialize?: (obj: any) => void | Promise<any> } } = {}; +const serializationTypes: { [name: string]: { ctor: { new (): unknown }; afterDeserialize?: (obj: unknown) => void | Promise<unknown> } } = {}; const reverseMap: { [ctor: string]: string } = {}; export namespace SerializationHelper { @@ -16,7 +17,7 @@ export namespace SerializationHelper { return serializing > 0; } - export function Serialize(obj: any /* Field */): any { + export function Serialize(obj: unknown /* Field */): unknown { if (obj === undefined || obj === null) { return null; } @@ -37,7 +38,7 @@ export namespace SerializationHelper { return json; } - export async function Deserialize(obj: any): Promise<any> { + export async function Deserialize(obj: unknown): Promise<unknown> { if (obj === undefined || obj === null) { return undefined; } @@ -46,16 +47,17 @@ export namespace SerializationHelper { return obj; } - if (!obj.__type) { - console.warn("No property 'type' found in JSON."); + const objtype = '__type' in obj ? (obj.__type as string) : undefined; + if (!objtype) { + console.warn(`No property ${objtype} found in JSON.`); return undefined; } - if (!(obj.__type in serializationTypes)) { - throw Error(`type '${obj.__type}' not registered. Make sure you register it using a @Deserializable decorator`); + if (!(objtype in serializationTypes)) { + throw Error(`type '${objtype}' not registered. Make sure you register it using a @Deserializable decorator`); } - const type = serializationTypes[obj.__type]; + const type = serializationTypes[objtype]; const value = await new Promise(res => { deserialize(type.ctor, obj, (err, result) => res(result)); }); @@ -65,11 +67,12 @@ export namespace SerializationHelper { } } -export function Deserializable(classNameForSerializer: string, afterDeserialize?: (obj: any) => void | Promise<any>, constructorArgs?: [string]): (constructor: { new (...args: any[]): any }) => void { - function addToMap(className: string, Ctor: { new (...args: any[]): any }) { - const schema = getDefaultModelSchema(Ctor) as any; - if (schema.targetClass !== Ctor || constructorArgs) { - setDefaultModelSchema(Ctor, { ...schema, factory: (context: any) => new Ctor(...(constructorArgs ?? []).map(arg => context.json[arg])) }); +// eslint-disable-next-line @typescript-eslint/no-explicit-any +export function Deserializable(classNameForSerializer: string, afterDeserialize?: (obj: unknown) => void | Promise<unknown>, constructorArgs?: [string]): (constructor: { new (...args: any[]): any }) => void { + function addToMap(className: string, Ctor: { new (...args: unknown[]): unknown }) { + const schema = getDefaultModelSchema(Ctor); + if (schema && (schema.targetClass !== Ctor || constructorArgs)) { + setDefaultModelSchema(Ctor, { ...schema, factory: (context: Context) => new Ctor(...(constructorArgs ?? []).map(arg => context.json[arg])) }); } if (!(className in serializationTypes)) { serializationTypes[className] = { ctor: Ctor, afterDeserialize }; @@ -78,12 +81,12 @@ export function Deserializable(classNameForSerializer: string, afterDeserialize? throw new Error(`Name ${className} has already been registered as deserializable`); } } - return (ctor: { new (...args: any[]): any }) => addToMap(classNameForSerializer, ctor); + return (ctor: { new (...args: unknown[]): unknown }) => addToMap(classNameForSerializer, ctor); } export function autoObject(): PropSchema { return custom( s => SerializationHelper.Serialize(s), - (json: any, context: any, oldValue: any, cb: (err: any, result: any) => void) => SerializationHelper.Deserialize(json).then(res => cb(null, res)) + (json: object, context: Context, oldValue: unknown, cb: (err: unknown, result: unknown) => void) => SerializationHelper.Deserialize(json).then(res => cb(null, res)) ); } |