import { LogImpressionsRequest } from '../../../protocol/actions' ; import { ExtraData } from '../../../protocol/impression/extradata' ; import { Impression } from '../../../protocol/impression/impression' ; import { ImpressionCode } from '../../../protocol/impression/impressioncode' ; import { AuthManager } from '../auth/authmanager' ; import { Debouncer } from '../common/debouncer' ; import { ImpressionSender } from './impressionsender' ; export class DebouncingImpressionSender implements ImpressionSender { private impressionSessionId_ : string ; private authManager_ : AuthManager ; private impressionsToSend_ : Impression [ ] ; private debouncer_ : Debouncer ; constructor ( impressionSessionId , authManager , debounceIntervalMs ) { this . impressionSessionId_ = impressionSessionId ; this . authManager_ = authManager ; this . impressionsToSend_ = [ ] ; this . debouncer_ = new Debouncer ( ( ) => this . sendImpressions_ ( ) , debounceIntervalMs ) ; } sendImpression ( impressionCode , extraData ? ) { const sessionInfo = this . authManager_ . getSessionInfo ( ) ; const user = sessionInfo ? sessionInfo . userId : '(anonymous)' ; const impression = { impressionCode : impressionCode , user : user , timestampMs : Date . now ( ) , sessionId : this . impressionSessionId_ , userAgent : navigator . userAgent , extraData : extraData || { } } ; this . impressionsToSend_ . push ( impression ) ; this . debouncer_ . fire ( ) ; } private sendImpressions_ ( ) { const options = { method : 'POST' , headers : { 'Content-Type' : 'application/json' } , body : JSON . stringify ( this . makeRequest_ ( ) ) } ; fetch ( '/impressions' , options ) . catch ( ( ) => { } ) ; this . impressionsToSend_ = [ ] ; } private makeRequest_ ( ) { return { impressions : this . impressionsToSend_ } ; } }