import { Message , MessageResponse , Serialize , messageSource } from './message' ; import { SerializeableError } from '../utils/error-handling' ; import { Route } from '../backend/utils' ; import { MessageType } from './message-type' ; import { ApplicationError , ApplicationErrorType } from './application-error' ; import { getRandomHash } from './hash' ; import { Path , deserializePath } from '../tree/path' ; import { MutableTree } from '../tree/mutable-tree' ; import { Change } from '../tree/change' ; import { Node } from '../tree/node' ; import { serialize } from '../utils' ; import { SimpleOptions } from '../options' ; export const create = < T > ( properties ) => Object . assign ( { messageSource , messageId : getRandomHash ( ) , serialize : Serialize . None } , properties ) ; export abstract class MessageFactory { static initialize ( options ? ) { return create ( { messageType : MessageType . Initialize , content : options } ) ; } static frameworkLoaded ( ) { return create ( { messageType : MessageType . FrameworkLoaded } ) ; } static ping ( ) { return create ( { messageType : MessageType . Ping } ) ; } static angularDetected ( ) { return create ( { messageType : MessageType . NgApp } ) ; } static notNgApp ( ) { return create ( { messageType : MessageType . NotNgApp } ) ; } static push ( ) { return create ( { messageType : MessageType . Push } ) ; } static ngVersion ( ngVersion ) { return create ( { messageType : MessageType . NgVersion , content : ngVersion } ) ; } static treeUnchanged ( ) { return create ( { messageType : MessageType . TreeUnchanged , content : null , serialize : Serialize . None } ) ; } static completeTree ( tree ) { return create ( { messageType : MessageType . CompleteTree , content : tree . roots , serialize : Serialize . None } ) ; } static treeDiff ( changes ) { return create ( { messageType : MessageType . TreeDiff , content : changes , serialize : Serialize . None } ) ; } static selectComponent ( node , requestInstance ? ) { return create ( { messageType : MessageType . SelectComponent , content : { path : deserializePath ( node . id ) , requestInstance } } ) ; } static updateProperty ( path , newValue ) { return create ( { messageType : MessageType . UpdateProperty , content : { path , newValue } } ) ; } static updateProviderProperty ( path , token , propertyPath , newValue ) { return create ( { messageType : MessageType . UpdateProviderProperty , content : { path , token , propertyPath , newValue } } ) ; } static emitValue ( path , value ) { return create ( { messageType : MessageType . EmitValue , content : { path , value } } ) ; } static ngModules ( content ) { return create ( { messageType : MessageType . NgModules , content : content } ) ; } static routerTree ( content ) { return create ( { messageType : MessageType . RouterTree , content : content } ) ; } static highlight ( nodes ) { return create ( { messageType : MessageType . Highlight , content : { nodes : nodes . map ( n => n . id ) } } ) ; } static findDOMElement ( ) { return create ( { messageType : MessageType . FindElement , content : { start : true } } ) ; } static foundDOMElement ( node ) { return create ( { messageType : MessageType . FindElement , content : { node , stop : true } } ) ; } static analyticsEvent ( event , desc ) { return create ( { messageType : MessageType . GoogleTagManagerSend , content : { event , desc } } ) ; } static applicationError ( error ) { return create ( { messageType : MessageType . ApplicationError , content : error } ) ; } static errorCleared ( errorTypes ) { return create ( { messageType : MessageType . ErrorCleared , content : { errorTypes } } ) ; } static uncaughtApplicationError ( error ) { return create ( { messageType : MessageType . ApplicationError , content : new ApplicationError ( ApplicationErrorType . UncaughtException , error ) } ) ; } static sendUncaughtError ( error , ngVersion ) { return create ( { messageType : MessageType . SendUncaughtError , content : { error , ngVersion } } ) ; } static dispatchWrapper < T > ( message ) { return create ( { messageType : MessageType . DispatchWrapper , content : message } ) ; } static refresh ( ) { return create ( { messageType : MessageType . Refresh , content : null , serialize : Serialize . None } ) ; } static response < Response , T > ( message , response , serializeResponse ) { const prepare = ( ) : any => { if ( serializeResponse ) { return serialize ( response ) ; } return response ; } ; const prepareError = r => ( { name : r . name , message : r . message , stack : r . stack } ) ; const serialization = serializeResponse ? Serialize . Recreator : Serialize . None ; return create ( { messageType : MessageType . Response , messageId : null , messageSource : message . messageSource , messageResponseId : message . messageId , serialize : serialization , content : response instanceof Error ? null : prepare ( ) , error : response instanceof Error ? prepareError ( response ) : null } ) ; } }