import { ActionType , createAction } from '@ulangi/ulangi-action' ; import { ErrorBag } from '@ulangi/ulangi-common/interfaces' ; import { EventBus , group , on , once } from '@ulangi/ulangi-event' ; export class SpeakDelegate { private eventBus : EventBus ; public constructor ( eventBus ) { this . eventBus = eventBus ; } public synthesize ( text , languageCode , callback ) { this . eventBus . pubsub ( createAction ( ActionType . AUDIO__SYNTHESIZE_SPEECH , { text , languageCode , } ) , group ( on ( ActionType . AUDIO__SYNTHESIZING_SPEECH , callback . onSynthesizing ) , once ( ActionType . AUDIO__SYNTHESIZE_SPEECH_SUCCEEDED , ( { filePath } ) : void => callback . onSynthesizeSucceeded ( filePath ) ) , once ( ActionType . AUDIO__SYNTHESIZE_SPEECH_FAILED , ( errorBag ) : void => callback . onSynthesizeFailed ( errorBag ) ) ) ) ; } public speak ( filePath , callback ) { this . eventBus . pubsub ( createAction ( ActionType . AUDIO__PLAY , { filePath , } ) , group ( on ( ActionType . AUDIO__PLAYING , callback . onSpeaking ) , once ( ActionType . AUDIO__PLAY_SUCCEEDED , ( ) : void => callback . onSpeakSucceeded ( ) ) , once ( ActionType . AUDIO__PLAY_FAILED , ( errorBag ) : void => callback . onSpeakFailed ( errorBag ) ) ) ) ; } }