import DataMessage from '../datamessage/DataMessage' ; import RealtimeController from '../realtimecontroller/RealtimeController' ; import TranscriptEvent , { TranscriptEventConverter } from './TranscriptEvent' ; import TranscriptionController from './TranscriptionController' ; export const TRANSCRIPTION_DATA_MESSAGE_TOPIC = 'aws:chime:transcription' ; export default class DefaultTranscriptionController implements TranscriptionController { private readonly transcriptEventCallbacks : Set < ( transcriptEvent : TranscriptEvent ) => void > ; constructor ( private realtimeController ) { this . transcriptEventCallbacks = new Set < ( transcriptEvent : TranscriptEvent ) => void > ( ) ; } subscribeToTranscriptEvent ( callback ) { if ( this . transcriptEventCallbacks . size === 0 ) { this . realtimeController . realtimeSubscribeToReceiveDataMessage ( TRANSCRIPTION_DATA_MESSAGE_TOPIC , ( dataMessage ) => { for ( const transcriptEvent of TranscriptEventConverter . from ( dataMessage ) ) { for ( const transcriptEventCallback of this . transcriptEventCallbacks ) { transcriptEventCallback ( transcriptEvent ) ; } } } ) ; } this . transcriptEventCallbacks . add ( callback ) ; } unsubscribeFromTranscriptEvent ( callback ) { this . transcriptEventCallbacks . delete ( callback ) ; if ( this . transcriptEventCallbacks . size === 0 ) { this . realtimeController . realtimeUnsubscribeFromReceiveDataMessage ( TRANSCRIPTION_DATA_MESSAGE_TOPIC ) ; } } }