aboutsummaryrefslogtreecommitdiff
path: root/src/client/util/SerializationHelper.ts
diff options
context:
space:
mode:
authorMohammad Amoush <mohammad_amoush@brown.edu>2019-07-24 11:00:09 -0400
committerMohammad Amoush <mohammad_amoush@brown.edu>2019-07-24 11:00:09 -0400
commit5591060e868053c8839fcc1de1ae77d4dac361ac (patch)
treecd2ec5a046686e5cf481a157fbebbcc479368282 /src/client/util/SerializationHelper.ts
parent2edcfb8a755b7fec7f937f135c794cbadbe0c94e (diff)
parent7fce10e6d687364feac6fd93a362634018a9f3ee (diff)
Merge branch 'master' of https://github.com/browngraphicslab/Dash-Web into youtube-api-muhammed
Diffstat (limited to 'src/client/util/SerializationHelper.ts')
-rw-r--r--src/client/util/SerializationHelper.ts27
1 files changed, 15 insertions, 12 deletions
diff --git a/src/client/util/SerializationHelper.ts b/src/client/util/SerializationHelper.ts
index dca539f3b..94b640afa 100644
--- a/src/client/util/SerializationHelper.ts
+++ b/src/client/util/SerializationHelper.ts
@@ -1,9 +1,14 @@
import { PropSchema, serialize, deserialize, custom, setDefaultModelSchema, getDefaultModelSchema, primitive, SKIP } from "serializr";
-import { Field } from "../../new_fields/Doc";
+import { Field, Doc } from "../../new_fields/Doc";
import { ClientUtils } from "./ClientUtils";
+let serializing = 0;
+export function afterDocDeserialize(cb: (err: any, val: any) => void, err: any, newValue: any) {
+ serializing++;
+ cb(err, newValue);
+ serializing--;
+}
export namespace SerializationHelper {
- let serializing: number = 0;
export function IsSerializing() {
return serializing > 0;
}
@@ -17,18 +22,18 @@ export namespace SerializationHelper {
return obj;
}
- serializing += 1;
+ serializing++;
if (!(obj.constructor.name in reverseMap)) {
throw Error(`type '${obj.constructor.name}' not registered. Make sure you register it using a @Deserializable decorator`);
}
const json = serialize(obj);
json.__type = reverseMap[obj.constructor.name];
- serializing -= 1;
+ serializing--;
return json;
}
- export function Deserialize(obj: any): any {
+ export async function Deserialize(obj: any): Promise<any> {
if (obj === undefined || obj === null) {
return undefined;
}
@@ -37,7 +42,6 @@ export namespace SerializationHelper {
return obj;
}
- serializing += 1;
if (!obj.__type) {
if (ClientUtils.RELEASE) {
console.warn("No property 'type' found in JSON.");
@@ -52,16 +56,15 @@ export namespace SerializationHelper {
}
const type = serializationTypes[obj.__type];
- const value = deserialize(type.ctor, obj);
+ const value = await new Promise(res => deserialize(type.ctor, obj, (err, result) => res(result)));
if (type.afterDeserialize) {
- type.afterDeserialize(value);
+ await type.afterDeserialize(value);
}
- serializing -= 1;
return value;
}
}
-let serializationTypes: { [name: string]: { ctor: { new(): any }, afterDeserialize?: (obj: any) => void } } = {};
+let serializationTypes: { [name: string]: { ctor: { new(): any }, afterDeserialize?: (obj: any) => void | Promise<any> } } = {};
let reverseMap: { [ctor: string]: string } = {};
export interface DeserializableOpts {
@@ -69,7 +72,7 @@ export interface DeserializableOpts {
withFields(fields: string[]): Function;
}
-export function Deserializable(name: string, afterDeserialize?: (obj: any) => void): DeserializableOpts;
+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 }) {
@@ -135,6 +138,6 @@ export namespace Deserializable {
export function autoObject(): PropSchema {
return custom(
(s) => SerializationHelper.Serialize(s),
- (s) => SerializationHelper.Deserialize(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