import { Injectable } from '@angular/core' ; import { progress } from '@progress/jsdo-core' ; import { map } from 'rxjs/operators' ; import { DataProviderService } from './service-config' ; import { Observable , throwError , from , of } from 'rxjs' ; @ Injectable ( ) export class ProgressSessionService { public createdSessions : { [ key ] : progress . data . JSDOSession ; } = { } ; constructor ( protected dataProviderService ) { } public tryCreateSession ( providerName ) { return this . createSession ( providerName , '' , '' ) ; } public signIn ( providerName , userName , password ) { return this . createSession ( providerName , userName , password ) ; } public signOut ( providerName ) { const session = this . createdSessions [ providerName ] ; const result = session ? session . invalidate ( ) : null ; delete this . createdSessions [ providerName ] ; return of ( result ) ; } protected createSession ( providerName , userName , password ) { const dataProvider = this . dataProviderService . get ( providerName ) ; if ( ! dataProvider ) { return throwError ( new Error ( `Error creating JSDO session. Unknown data provider: ${ providerName }` ) ) ; } if ( this . createdSessions [ providerName ] ) { return of ( this . createdSessions [ providerName ] ) ; } return from ( progress . data . getSession ( { name : providerName , authenticationModel : dataProvider . authenticationModel , serviceURI : dataProvider . serviceUri , catalogURI : dataProvider . catalogUris [ 0 ] , username : userName , password } ) ) . pipe ( map ( ( sessionResponse ) => { this . createdSessions [ providerName ] = sessionResponse . jsdosession ; return this . createdSessions [ providerName ] ; } ) ) ; } }