import debugging from 'debug' ; import analytics from '../analytics' ; import UploadProvider from './uploadProviders/UploadProvider' ; import UploadService from './UploadService' ; import DownloadService from './DownloadService' ; import CompressionUploadProvider from './uploadProviders/CompressionUploadProvider' ; import Connection from './Connection' ; const debug = debugging ( 'blymp:transfer' ) ; /**
 * Transfer: Stores and manages all information about the current transfer
 */ export default class Transfer { type Boolean isSender : boolean = false ; type Number receiverId : number | null = null ; type Boolean isValidId : boolean = true ; type Function updateHandler : Function = ( ) => { } ; type Function openPage : Function = ( _ ) => { } ; type Integer totalFiles : number = 0 ; type Integer currentFile : number = 0 ; /**
     * Name of the file we are currently transferring
     */ currentFileName = '' ; type Boolean finishedTransfer : boolean = false ; type Integer estimate : number = - 1 ; type Integer progress : number = 0 ; /**
     * UploadProvider used for uploading data
     */ uploadProvider : UploadProvider | undefined ; /**
     * Text that should be shown on the "transfer" page
     */ transferStatusText : String = "Transferring files..." ; connection : Connection ; /**
     * Prepare the transfer
     */ constructor ( ) { this . connection = new Connection ( this ) ; } param { number } id useReceiver ( id ) { this . isValidId = true ; this . triggerUpdate ( ) ; if ( String ( id ) . length !== 4 ) return ; if ( Number ( id ) === this . receiverId ) { this . isValidId = false ; this . triggerUpdate ( ) ; analytics ( "connected-to-self" ) ; debug ( "Tried to connect to self - aborting" ) ; return ; } this . connection . useReceiverId ( id ) ; } /**
     * Trigger an update to the update handler to signal important changes
     * This will cause React to re-render changes
     */ triggerUpdate ( ) { if ( this . updateHandler ) { this . updateHandler ( ) ; } } /**
     * Set text for transfer status and inform partner to show the same text
     */ setTransferStatusText ( text ) { this . transferStatusText = text ; this . connection . sendTransferStatusText ( ) ; this . triggerUpdate ( ) ; } /**
     * Upload the currently selected files to our partner
     */ async uploadFiles ( ) { debug ( "Starting upload" ) ; if ( ! this . uploadProvider ) { throw new Error ( 'Internal error: Cannot upload data as no upload provider was set' ) ; } analytics ( "start-upload" ) ; this . connection . prepareUpload ( ) ; this . setTransferStatusText ( "Preparing transfer..." ) ; this . openPage ( '/transfer' ) ; if ( this . uploadProvider . getEstimatedTotalSize ( ) > ( 1024 * 10 ) && this . uploadProvider . getEstimatedTotalSize ( ) < ( 1024 * 1024 * 100 ) ) { debug ( "Adding Compression Provider as we are in the right range" ) ; this . uploadProvider = new CompressionUploadProvider ( this . uploadProvider , this ) ; } debug ( "Starting Upload Service" ) ; const service = new UploadService ( this , this . uploadProvider ) ; await service . startUpload ( ) ; this . connection . disconnectAll ( ) ; } /**
     * Handle downloading and saving the files
     */ async downloadFiles ( ) { debug ( "Starting download" ) ; analytics ( "start-download" ) ; debug ( "Starting download service" ) ; const service = new DownloadService ( this ) ; await service . startDownload ( ) ; this . connection . disconnectAll ( ) ; } }