import { CoatyObject , CommunicationEventType , ContextFilter , isContextFilterValid , isPlainObject , ObjectMatcher } from ".." ; import { CommunicationEvent , CommunicationEventData } from "./communication-event" ; /**
 * Call event for invoking a remote operation call.
 */ export class CallEvent extends CommunicationEvent < CallEventData > { get eventType ( ) { return CommunicationEventType . Call ; } get eventTypeFilter ( ) { return this . _operation ; } get responseEventType ( ) { return CommunicationEventType . Return ; } /**
     * A non-empty string containing the name of the operation to be invoked.
     */ get operation ( ) { return this . _operation ; } param event returnEvent : ( event : ReturnEvent ) => void ; private _operation : string ; internal param operation param eventData throws constructor ( operation , eventData ) { super ( eventData ) ; if ( ! CommunicationEvent . isValidEventFilter ( operation ) ) { throw new TypeError ( "in CallEvent: argument 'operation' is not a valid operation name" ) ; } this . _operation = operation ; } param operation param parameters param filter throws static with ( operation , parameters ? , filter ? ) { return new CallEvent ( operation , new CallEventData ( parameters , filter ) ) ; } internal param eventData throws ensureValidResponseParameters ( eventData ) { } } /**
 * Defines event data format for invoking a remote operation call.
 */ export class CallEventData extends CommunicationEventData { /**
     * Holds the parameter values to be used during the remote invocation of the operation (optional).
     * Parameters must be either by-position through a JSON array or by-name
     * through a JSON object. If no parameters have been specified the value returned is `undefined`.
     */ get parameters ( ) { return this . _parameters ; } /**
     * A context filter whose conditions must match a context object at the remote end
     * in order to allow execution of the remote call (optional).
     * If no filter has been specified the value returned is `undefined`.
     */ get filter ( ) { return this . _filter ; } private _parameters : any [ ] | { [ key ] : any ; } ; private _filter : ContextFilter ; param parameters param filter constructor ( parameters ? , filter ? ) { super ( ) ; this . _parameters = parameters ; this . _filter = filter ; if ( ! this . _hasValidParameters ( ) ) { throw new TypeError ( "in CallEventData: arguments not valid" ) ; } } internal static createFrom ( eventData ) { return new CallEventData ( eventData . parameters , eventData . filter ) ; } param index getParameterByIndex ( index ) { if ( ! Array . isArray ( this . _parameters ) ) { return undefined ; } return this . _parameters [ index ] ; } param name getParameterByName ( name ) { if ( ! isPlainObject ( this . _parameters ) ) { return undefined ; } return this . parameters [ name ] ; } internal param context matchesFilter ( context ? ) { if ( this . _filter !== undefined && context !== undefined ) { return ObjectMatcher . matchesFilter ( context , this . _filter ) ; } if ( this . _filter === undefined && context !== undefined ) { return false ; } return true ; } toJsonObject ( ) { return { parameters : this . _parameters , filter : this . _filter , } ; } private _hasValidParameters ( ) { return ( this . _parameters === undefined || Array . isArray ( this . _parameters ) || isPlainObject ( this . _parameters ) ) && isContextFilterValid ( this . _filter ) ; } } /**
 * Defines error codes for pre-defined remote call errors.
 *
 * The integer error codes from and including -32768 to -32000 are reserved for pre-defined errors
 * encountered while executing a remote call. Any code within this range, but not defined explicitly
 * below is reserved for future use. The remaining integers are available for application defined errors.
 *
 * The predefined error messages corresponding to these predefined error codes are defined by enum
 * `RemoteCallErrorMessage`.
 */ export enum RemoteCallErrorCode { InvalidParameters = - 32602 } /**
 * Defines error messages for pre-defined remote call errors.
 *
 * The predefined error codes corresponding to these predefined error messages are defined by enum
 * `RemoteCallErrorCode`.
 */ export enum RemoteCallErrorMessage { InvalidParameters = "Invalid params" } /**
 * Return event for providing the response of executing a remote operation call.
 */ export class ReturnEvent extends CommunicationEvent < ReturnEventData > { get eventType ( ) { return CommunicationEventType . Return ; } internal eventRequest : CallEvent ; param result param executionInfo static withResult ( result , executionInfo ? ) { return new ReturnEvent ( new ReturnEventData ( result , undefined , executionInfo ) ) ; } param code param message param executionInfo static withError ( code , message , executionInfo ? ) { return new ReturnEvent ( new ReturnEventData ( undefined , { code , message } , executionInfo ) ) ; } } /**
 * Defines event data format for returning a result or error from executing a remote operation call.
 */ export class ReturnEventData extends CommunicationEventData { /**
     * The result value to be returned (any JSON data type). The value is `undefined`,
     * if operation execution yielded an error.
     */ get result ( ) { return this . _result ; } /**
     * The error object to be returned in case the operation call yielded an error (optional).
     * The value is `undefined` if the operation executed successfully.
     *
     * The error object consists of two properties: `code`, `message`.
     *
     * The error code given is an integer that indicates the error type
     * that occurred, either a predefined error or an application defined one. Predefined error
     * codes are defined by the `RemoteCallErrorCode` enum. Predefined error
     * codes are within the range -32768 to -32000. Application defined error codes must be
     * defined outside this range.
     *
     * The error message provides a short description of the error. Predefined error messages
     * exist for all predefined error codes (see enum `RemoteCallErrorMessage`).
     */ get error ( ) { return this . _error ; } /**
     * Determines whether an error object has been returned.
     *
     * Returns true, if remote operation failed with an error; false otherwise.
     */ get isError ( ) { return this . _error !== undefined ; } /**
     * Defines additional information about the execution environment (ayn JSON value)
     * such as the execution time of the operation or the ID of the operated control unit
     * (optional).
     *
     * The value is `undefined`, if no execution info has been specified.
     */ get executionInfo ( ) { return this . _executionInfo ; } private _result : any ; private _error : { code : number ; message : string ; } ; private _executionInfo : any ; param result param error param executionInfo constructor ( result ? , error ? , executionInfo ? ) { super ( ) ; this . _result = result ; this . _error = error ; this . _executionInfo = executionInfo ; if ( ! this . _hasValidParameters ( ) ) { throw new TypeError ( "in ReturnEventData: arguments not valid" ) ; } } internal static createFrom ( eventData ) { return new ReturnEventData ( eventData . result , eventData . error , eventData . executionInfo ) ; } toJsonObject ( ) { return { result : this . _result , error : this . _error , executionInfo : this . _executionInfo , } ; } private _hasValidParameters ( ) { if ( this . _result === undefined && this . _error === undefined ) { return false ; } if ( this . _result !== undefined && this . _error !== undefined ) { return false ; } if ( this . _error !== undefined && ( ! isPlainObject ( this . _error ) || this . _error . code === undefined || this . _error . message === undefined ) ) { return false ; } return true ; } }