import { BrowserWindow , ipcMain } from "electron" ; import AWS = require ( "aws-sdk" ) ; export class JobManager { private jobs : { [ key ] : AWS . Request < any , any > | AWS . S3 . ManagedUpload ; } = { } ; private jobsProgress : { [ key ] : number ; } = { } ; private window : BrowserWindow ; constructor ( ) { if ( ipcMain ) { ipcMain . on ( 'Jobs-Abort' , ( event , arg ) => { this . stopJob ( arg . id ) ; } ) ; } } public initialize ( w ) { this . window = w ; } public addJob ( id , type , request , from , to , localFile ) { this . jobs [ id ] = request ; this . window . webContents . send ( 'Jobs-Created' , { id , type , from , to , localFile } ) ; if ( request ) { request . on ( 'success' , ( ) => { this . completeJob ( id ) ; } ) ; request . on ( 'error' , ( resp ) => { this . failJob ( id , resp ) ; } ) ; if ( type === 'download' ) { request . on ( 'httpDownloadProgress' , ( p ) => { this . reportProgress ( id , p . loaded , p . total ) ; } ) ; } else { request . on ( 'httpUploadProgress' , ( p ) => { this . reportProgress ( id , p . loaded , p . total ) ; } ) ; } } } public addManagedUploadJob ( id , type , request , from , to , localFile ) { this . jobs [ id ] = request ; this . window . webContents . send ( 'Jobs-Created' , { id , type , from , to , localFile } ) ; if ( request ) { request . on ( 'httpUploadProgress' , ( p ) => { this . reportProgress ( id , p . loaded , p . total ) ; } ) ; return request . promise ( ) . then ( ( data ) => { this . completeJob ( id ) ; return Promise . resolve ( data ) ; } , ( err ) => { this . failJob ( id , err ) ; return Promise . reject ( err ) ; } ) ; } return Promise . reject ( "Attempt to add job with no request" ) ; } public completeJob ( id ) { if ( this . jobs [ id ] !== undefined ) { this . window . webContents . send ( 'Jobs-Completed' , { id } ) ; delete this . jobs [ id ] ; } } public failJob ( id , error ) { if ( this . jobs [ id ] !== undefined ) { this . window . webContents . send ( 'Jobs-Failed' , { id , error : error . message } ) ; delete this . jobs [ id ] ; delete this . jobsProgress [ id ] ; } } public reportProgress ( id , loaded , total ) { if ( this . jobs [ id ] !== undefined ) { let percentage = Math . round ( ( loaded / total ) * 100 ) ; if ( ! this . jobsProgress [ id ] || this . jobsProgress [ id ] < percentage ) { this . jobsProgress [ id ] = percentage ; this . window . webContents . send ( 'Jobs-Progress' , { id , percentage } ) ; } } } public stopJob ( id ) { if ( this . jobs [ id ] !== undefined ) { this . jobs [ id ] . abort ( ) ; } } }