import { Injectable , RendererType2 } from '@angular/core' ; import { isPrimitiveArg , isStoreObjectArg , isRendererType2Arg , SerializerTypes , isDomEventTypeArg , DOM_EVENT_KEYS } from './api' ; import { ObjectStore } from './object-store' ; @ Injectable ( { providedIn : 'root' } ) export class Serializer { constructor ( private store ) { } serialize ( value , type = SerializerTypes . PRIMITIVE ) { if ( value === null || isPrimitiveArg ( type ) ) { return value ; } if ( isDomEventTypeArg ( type ) ) { return this . serializeDomEvent ( value ) ; } if ( Array . isArray ( value ) ) { return value . map ( _ => this . serialize ( _ , type ) ) ; } if ( isStoreObjectArg ( type ) ) { return this . store . serialize ( value ) ; } if ( isRendererType2Arg ( type ) ) { return this . serializeRendererType2 ( value ) ; } throw new Error ( `No found serializer for ${ SerializerTypes [ type ] }` ) ; } deserialize ( value , type = SerializerTypes . PRIMITIVE ) { if ( value === null || isPrimitiveArg ( type ) ) { return value ; } if ( isDomEventTypeArg ( type ) ) { return this . deserializeDomEvent ( value ) ; } if ( Array . isArray ( value ) ) { return value . map ( _ => this . deserialize ( _ , type ) ) ; } if ( isStoreObjectArg ( type ) ) { return this . store . deserialize ( value ) ; } if ( isRendererType2Arg ( type ) ) { return this . deserializeRendererType2 ( value ) ; } throw new Error ( `No found deserializer for ${ SerializerTypes [ type ] }` ) ; } private serializeRendererType2 ( type ) { return { id : type . id , encapsulation : this . serialize ( type . encapsulation ) , styles : this . serialize ( type . styles ) , data : this . serialize ( type . data ) } ; } private deserializeRendererType2 ( type ) { return { id : type . id , encapsulation : this . deserialize ( type . encapsulation ) , styles : this . deserialize ( type . styles ) , data : this . deserialize ( type . data ) } ; } private serializeDomEvent ( event ) { let result = null ; if ( event ) { result = { } ; for ( const key of DOM_EVENT_KEYS ) { result [ key ] = event [ key ] ; } if ( event . target ) { result . target = { value : event . target . value } ; } } return result ; } private deserializeDomEvent ( event ) { return event ; } }