import { Statistic } from '../../../protocol/statistic/statistic' ; import { StatisticType } from '../../../protocol/statistic/statistictype' ; import { Debouncer } from '../common/debouncer' ; import { PickerController } from '../picker/pickercontroller' ; import { ServerWrapper } from '../server/serverwrapper' ; import { StatisticRecorder } from './statisticrecorder' ; export class DebouncingStatisticRecorder implements StatisticRecorder { private pickerController_ : PickerController ; private server_ : ServerWrapper ; private debouncer_ : Debouncer ; private statisticsToSend_ : Statistic [ ] ; constructor ( pickerController , server , debounceIntervalMs ) { this . pickerController_ = pickerController ; this . server_ = server ; this . debouncer_ = new Debouncer ( ( ) => this . sendToServer_ ( ) , debounceIntervalMs ) ; this . statisticsToSend_ = [ ] ; } recordRightMove ( pgn ) { this . recordStatistic_ ( pgn , StatisticType . RIGHT_MOVE ) ; } recordWrongMove ( pgn ) { this . recordStatistic_ ( pgn , StatisticType . WRONG_MOVE ) ; } recordFinishLine ( pgn ) { this . recordStatistic_ ( pgn , StatisticType . FINISH_LINE ) ; } private recordStatistic_ ( pgn , statisticType ) { const repertoireId = this . pickerController_ . getSelectedMetadataId ( ) ; this . statisticsToSend_ . push ( { repertoireId , pgn , statisticType } ) ; this . debouncer_ . fire ( ) ; } private sendToServer_ ( ) { this . server_ . recordStatistics ( this . statisticsToSend_ ) ; this . statisticsToSend_ = [ ] ; } }