import { IDataOperator , ReceiveDataEvent , ShowStatusCodeEvent , FailedConnectEvent } from "src/interfaces/IDataOperator" ; import { UUID } from "src/shared/ExtensionMethods" ; import { Connection } from "./Connection" ; import { Port } from "./Port" ; import { EventDispatcher , Handler } from "./Shared/EventDispatcher" ; export class LogicComponent { originID : string = UUID ( ) ; color : string = "#6059DF" ; fillColor : boolean = true ; isFlowSimulationOn : boolean = false ; connectTo ( operator , connectingWithOutput ) { let otherPort = operator . getPort ( ! connectingWithOutput ) ; if ( ! operator . canConnectTo ( this . getPort ( connectingWithOutput ) , ! connectingWithOutput ) || ! this . canConnectTo ( otherPort , connectingWithOutput ) ) return null ; return this . getPort ( connectingWithOutput ) . connectTo ( otherPort ) ; } canConnectTo ( port , connectingWithOutput ) { if ( connectingWithOutput ) { let outputPort = this [ "outputPort" ] ; if ( outputPort != null ) { if ( ! port . isOutput ) return true ; } } else { let inputPort = this [ "inputPort" ] ; if ( inputPort != null ) { if ( port . isOutput ) return true ; } } this . fireFailedConnect ( { message : "You can only connect input to output." } ) ; return false ; } getPort ( outputPort = false ) { if ( outputPort ) return this [ "outputPort" ] ; return this [ "inputPort" ] ; } destroy ( ) { if ( this [ "outputPort" ] ) this [ "outputPort" ] . removeConnections ( ) ; if ( this [ "inputPort" ] ) this [ "inputPort" ] . removeConnections ( ) ; } onConnectionUpdate ( wasOutput = false ) { } protected receiveDataDispatcher = new EventDispatcher < ReceiveDataEvent > ( ) ; public onReceiveData ( handler ) { this . receiveDataDispatcher . register ( handler ) ; } protected fireReceiveData ( event ) { this . receiveDataDispatcher . fire ( event ) ; } protected showStatusCodeDispatcher = new EventDispatcher < ShowStatusCodeEvent > ( ) ; public onShowStatusCode ( handler ) { this . showStatusCodeDispatcher . register ( handler ) ; } protected fireShowStatusCode ( event ) { this . showStatusCodeDispatcher . fire ( event ) ; } protected failedConnectDispatcher = new EventDispatcher < FailedConnectEvent > ( ) ; public onFailedConnect ( handler ) { this . failedConnectDispatcher . register ( handler ) ; } protected fireFailedConnect ( event ) { this . failedConnectDispatcher . fire ( event ) ; } }