import { IMineMeldAPIService } from './minemeldapi' ; import { IMinemeldAAAService , IMinemeldCurrentUser } from './aaa' ; export interface IMineMeldCurrentUserService { getReadWrite ( ) : angular . IPromise < boolean > ; } export class MineMeldCurrentUserService implements IMineMeldCurrentUserService { MineMeldAPIService : IMineMeldAPIService ; MinemeldAAAService : IMinemeldAAAService ; $q : angular . IQService ; toastr : any ; readWriteDeferred : ng . IDeferred < boolean > ; currentUser : IMinemeldCurrentUser ; ngInject constructor ( MinemeldAAAService , MineMeldAPIService , $q , toastr ) { this . MinemeldAAAService = MinemeldAAAService ; this . MineMeldAPIService = MineMeldAPIService ; this . toastr = toastr ; this . $q = $q ; this . readWriteDeferred = $q . defer < boolean > ( ) ; this . currentUser = { id : undefined , read_write : false } ; this . MineMeldAPIService . onLogin ( this . initCurrentUser . bind ( this ) ) ; this . MineMeldAPIService . onLogout ( this . destroyCurrentUser . bind ( this ) ) ; if ( this . MineMeldAPIService . isLoggedIn ( ) ) { this . initCurrentUser ( ) ; } } getReadWrite ( ) { return this . readWriteDeferred . promise ; } private initCurrentUser ( ) { this . MinemeldAAAService . getCurrentUser ( ) . then ( ( result ) => { this . currentUser = result ; this . readWriteDeferred . resolve ( this . currentUser . read_write ) ; } , ( error ) => { if ( error . status == 401 ) { return ; } this . toastr . error ( 'ERROR RETRIEVING CURRENT USER: ' + error . statusText ) ; } ) ; } private destroyCurrentUser ( ) { this . currentUser = { id : undefined , read_write : false } ; this . readWriteDeferred = this . $q . defer < boolean > ( ) ; } }