import { Observable , ReplaySubject } from 'rxjs' ; import { assert , bitInBitfield } from './auxiliaries' ; import { EyeGazeDataStream , EyeGazeDataStreams } from './eyegazedatastream' ; import { EyeGazeEvent } from './eyegazeevent' ; export class EyeGazeEventProvider { /**
     * Time frame for events to be buffered (windowTime in rxjs per ReplaySubject).
     */ protected _timeframe : number | undefined ; /**
     * This mask saves for which types of events, event.preventDefault should be called. This is useful to disallow
     * some kinds of standard events like successful connected and handshake success.
     */ protected _preventDefaultMask : EyeGazeEventProvider . Type ; protected _eyeGazeDataListener : { ( event ) : void ; } ; protected _eyeGazeDataSubject : ReplaySubject < EyeGazeEvent > ; protected _newServerMessageListener : { ( event ) : void ; } ; protected _newServerMessageSubject : ReplaySubject < EyeGazeEvent > ; protected _connectionStatusListener : { ( event ) : void ; } ; protected _connectionStatusSubject : ReplaySubject < EyeGazeEvent > ; protected _binaryMessageParsingErrorListener : { ( event ) : void ; } ; protected _binaryMessageParsingErrorSubject : ReplaySubject < EyeGazeEvent > ; protected _eyeGazeDataStream : EyeGazeDataStream ; constructor ( eyeGazeDataStreams , serverAddress ) { assert ( eyeGazeDataStreams !== undefined , `expected a valid eye gaze data streams object on initialization, given ${ eyeGazeDataStreams }.` ) ; this . _eyeGazeDataStream = new EyeGazeDataStream ( ) ; this . _eyeGazeDataStream . eyeGazeDataStreams = eyeGazeDataStreams ; this . _eyeGazeDataStream . connect ( serverAddress ) ; } /**
     * Checks whether or not to prevent the default handling of the given event. This depends on the internal
     * `preventDefaultMask` which can be modified using `preventDefault` function @see preventDefault .
     *  param type param event protected preventDefaultOnEvent ( type , event ) { if ( bitInBitfield ( this . _preventDefaultMask , type ) ) { event . preventDefault ( ) ; } } param types allowDefault ( ... types ) { for ( const type of types ) { if ( bitInBitfield ( this . _preventDefaultMask , type ) ) { this . _preventDefaultMask &= ~ type ; } } } param types preventDefault ( ... types ) { for ( const type of types ) { if ( ! bitInBitfield ( this . _preventDefaultMask , type ) ) { this . _preventDefaultMask |= type ; } } } observable ( type ) { switch ( type ) { case EyeGazeEventProvider . Type . EyeGazeData : return this . EyeGazeData$ ; case EyeGazeEventProvider . Type . NewServerMessage : return this . NewServerMessage$ ; case EyeGazeEventProvider . Type . ConnectionStatus : return this . ConnectionStatus$ ; case EyeGazeEventProvider . Type . BinaryMessageParsingError : return this . BinaryMessageParsingError$ ; default : return undefined ; } } get EyeGazeData$ ( ) : Observable < EyeGazeEvent > { if ( this . _eyeGazeDataSubject === undefined ) { this . _eyeGazeDataSubject = new ReplaySubject < EyeGazeEvent > ( undefined , this . _timeframe ) ; this . _eyeGazeDataListener = ( event ) => { this . preventDefaultOnEvent ( EyeGazeEventProvider . Type . EyeGazeData , event ) ; this . _eyeGazeDataSubject . next ( event ) ; } ; this . _eyeGazeDataStream . addEventListener ( EyeGazeDataStream . EYE_GAZE_DATA , this . _eyeGazeDataListener ) ; } return this . _eyeGazeDataSubject . asObservable ( ) ; } get NewServerMessage$ ( ) : Observable < EyeGazeEvent > { if ( this . _newServerMessageSubject === undefined ) { this . _newServerMessageSubject = new ReplaySubject < EyeGazeEvent > ( undefined , this . _timeframe ) ; this . _newServerMessageListener = ( event ) => { this . preventDefaultOnEvent ( EyeGazeEventProvider . Type . NewServerMessage , event ) ; this . _newServerMessageSubject . next ( event ) ; } ; this . _eyeGazeDataStream . addEventListener ( EyeGazeDataStream . NEW_SERVER_MESSAGE , this . _newServerMessageListener ) ; } return this . _newServerMessageSubject . asObservable ( ) ; } get ConnectionStatus$ ( ) : Observable < EyeGazeEvent > { if ( this . _connectionStatusSubject === undefined ) { this . _connectionStatusSubject = new ReplaySubject < EyeGazeEvent > ( undefined , this . _timeframe ) ; this . _connectionStatusListener = ( event ) => { this . preventDefaultOnEvent ( EyeGazeEventProvider . Type . ConnectionStatus , event ) ; this . _connectionStatusSubject . next ( event ) ; } ; this . _eyeGazeDataStream . addEventListener ( EyeGazeDataStream . CONNECTION_STATUS , this . _connectionStatusListener ) ; } return this . _connectionStatusSubject . asObservable ( ) ; } get BinaryMessageParsingError$ ( ) : Observable < EyeGazeEvent > { if ( this . _binaryMessageParsingErrorSubject === undefined ) { this . _binaryMessageParsingErrorSubject = new ReplaySubject < EyeGazeEvent > ( undefined , this . _timeframe ) ; this . _binaryMessageParsingErrorListener = ( event ) => { this . preventDefaultOnEvent ( EyeGazeEventProvider . Type . BinaryMessageParsingError , event ) ; this . _binaryMessageParsingErrorSubject . next ( event ) ; } ; this . _eyeGazeDataStream . addEventListener ( EyeGazeDataStream . BINARY_MESSAGE_PARSING_ERROR , this . _binaryMessageParsingErrorListener ) ; } return this . _eyeGazeDataSubject . asObservable ( ) ; } } export namespace EyeGazeEventProvider { export enum Type { EyeGazeData = 1 << 0 , NewServerMessage = 1 << 1 , ConnectionStatus = 1 << 2 , BinaryMessageParsingError = 1 << 3 } }