1- import { BehaviorSubject , Observable , of as observableOf , Subscription , timer } from 'rxjs' ;
21import {
32 ChangeDetectionStrategy ,
43 ChangeDetectorRef ,
54 Component ,
65 Input ,
7- NgZone ,
86 OnDestroy ,
97 OnInit ,
108 TemplateRef ,
119 ViewEncapsulation
1210} from '@angular/core' ;
1311import { trigger } from '@angular/animations' ;
1412import { DomSanitizer } from '@angular/platform-browser' ;
13+
14+ import { BehaviorSubject , Observable , of as observableOf , Subscription , timer } from 'rxjs' ;
15+ import { filter , map , switchMap , take , tap } from 'rxjs/operators' ;
16+
1517import { NotificationsService } from '../notifications.service' ;
1618import { scaleEnter , scaleInState , scaleLeave , scaleOutState } from '../../animations/scale' ;
1719import { rotateEnter , rotateInState , rotateLeave , rotateOutState } from '../../animations/rotate' ;
@@ -26,9 +28,10 @@ import { IProcessNotification } from '../models/process-notification.model';
2628import { ProcessDataService } from '../../../core/data/processes/process-data.service' ;
2729import { Process } from '../../../process-page/processes/process.model' ;
2830import { Bitstream } from '../../../core/shared/bitstream.model' ;
29- import { getFirstCompletedRemoteData , getFirstSucceededRemoteListPayload } from '../../../core/shared/operators' ;
31+ import { getAllCompletedRemoteData , getFirstCompletedRemoteData } from '../../../core/shared/operators' ;
3032import { DSONameService } from '../../../core/breadcrumbs/dso-name.service' ;
3133import { DSpaceObject } from '../../../core/shared/dspace-object.model' ;
34+ import { RemoteData } from '../../../core/data/remote-data' ;
3235
3336@Component ( {
3437 selector : 'ds-process-notification' ,
@@ -84,7 +87,7 @@ export class ProcessNotificationComponent implements OnInit, OnDestroy {
8487 /**
8588 * The process that is being checked.
8689 */
87- public process : Process ;
90+ public processStatus$ : BehaviorSubject < string > = new BehaviorSubject ( '' ) ;
8891
8992 /**
9093 * If process checking is finished.
@@ -94,76 +97,76 @@ export class ProcessNotificationComponent implements OnInit, OnDestroy {
9497 /**
9598 * Files generated from process end.
9699 */
97- public files : Bitstream [ ] ;
100+ public files$ : BehaviorSubject < Bitstream [ ] > = new BehaviorSubject < Bitstream [ ] > ( [ ] ) ;
98101
99102 /**
100103 * Type of the notification visualisation.
101104 */
102- public notificationType = 'alert-info' ;
105+ public notificationType$ : BehaviorSubject < string > = new BehaviorSubject ( 'alert-info' ) ;
103106
104107 constructor ( private notificationService : NotificationsService ,
105108 private domSanitizer : DomSanitizer ,
106109 protected processService : ProcessDataService ,
107110 protected nameService : DSONameService ,
108111 private cdr : ChangeDetectorRef ,
109- private zone : NgZone ) {
112+ ) {
110113 }
111114
112115 /**
113116 * On init, start check process, and insert notifications information.
114117 */
115118 ngOnInit ( ) : void {
116119 this . animate = this . notification . options . animate + NotificationAnimationsStatus . In ;
117- this . initCheckProcess ( ) ;
120+ this . pollUntilProcessFinished ( ) ;
118121 this . html = this . notification . html ;
119122 this . contentType ( this . notification . title , 'title' ) ;
120123 }
121124
122125 /**
123- * Initialization of timer.
124- */
125- initCheckProcess ( ) {
126- const source = timer ( 0 , this . notification . checkTime ) ;
127- this . sub = source . subscribe ( val => {
128- this . checkProcess ( ) ;
126+ * Poll process endpoint until it's finished.
127+ */
128+ pollUntilProcessFinished ( ) {
129+ timer ( 0 , this . notification . checkTime ) . pipe (
130+ switchMap ( ( ) => this . processService . getProcess ( this . notification . processId ) ) ,
131+ getAllCompletedRemoteData ( ) ,
132+ filter ( ( res : RemoteData < Process > ) => res . hasFailed || res ?. payload ?. processStatus . toString ( ) === 'COMPLETED' || res ?. payload ?. processStatus . toString ( ) === 'FAILED' ) ,
133+ take ( 1 ) ,
134+ tap ( ( res : RemoteData < Process > ) => this . pollingFinishedFor ( res ) ) ,
135+ switchMap ( ( res : RemoteData < Process > ) => this . getFiles ( res ) ) ,
136+ ) . subscribe ( ( files : Bitstream [ ] ) => {
137+ const logFiles = files . filter ( ( file ) => ! this . getFileName ( file ) . includes ( '.log' ) ) ;
138+ this . files$ . next ( logFiles ) ;
139+ this . finished . next ( true ) ;
129140 } ) ;
130141 }
131142
132143 /**
133- * Send request to get the updated process information.
134- */
135- checkProcess ( ) {
136- this . processService . getProcess ( this . notification . processId )
137- . pipe ( getFirstCompletedRemoteData ( ) )
138- . subscribe ( ( res ) => {
139- this . process = res . payload ;
140- this . zone . run ( ( ) => this . cdr . detectChanges ( ) ) ;
141- if ( this . process . processStatus . toString ( ) === 'COMPLETED' || this . process . processStatus . toString ( ) === 'FAILED' ) {
142- this . sub . unsubscribe ( ) ;
143- if ( this . process . processStatus . toString ( ) === 'COMPLETED' ) {
144- this . notificationType = 'alert-success' ;
145- this . getFiles ( ) ;
146- } else {
147- this . notificationType = 'alert-danger' ;
148- this . zone . run ( ( ) => this . cdr . detectChanges ( ) ) ;
149- }
150- this . finished . next ( true ) ;
151- }
152- } ) ;
144+ * Handle process results
145+ *
146+ * @param processRD The RemoteData object for finished process
147+ */
148+ pollingFinishedFor ( processRD : RemoteData < Process > ) {
149+ if ( processRD . hasSucceeded && processRD . payload . processStatus . toString ( ) === 'COMPLETED' ) {
150+ this . notificationType$ . next ( 'alert-success' ) ;
151+ this . processStatus$ . next ( 'process.new.notification.process.status.completed' ) ;
152+ } else {
153+ this . processStatus$ . next ( 'process.new.notification.process.status.failed' ) ;
154+ this . notificationType$ . next ( 'alert-danger' ) ;
155+ }
153156 }
154157
155158 /**
156159 * When the process is completed get the files output.
157160 */
158- getFiles ( ) {
159- this . processService . getFiles ( this . notification . processId )
160- . pipe (
161- getFirstSucceededRemoteListPayload ( ) ,
162- )
163- . subscribe ( ( files : Bitstream [ ] ) => {
164- this . files = files . filter ( ( file ) => ! this . getFileName ( file ) . includes ( '.log' ) ) ;
165- this . zone . run ( ( ) => this . cdr . detectChanges ( ) ) ;
166- } ) ;
161+ getFiles ( processRD : RemoteData < Process > ) : Observable < Bitstream [ ] > {
162+ if ( processRD . hasSucceeded && processRD . payload . processStatus . toString ( ) === 'COMPLETED' ) {
163+ return this . processService . getFiles ( processRD . payload . processId ) . pipe (
164+ getFirstCompletedRemoteData ( ) ,
165+ map ( ( response ) => response . hasSucceeded ? response . payload . page : [ ] )
166+ ) ;
167+ } else {
168+ return observableOf ( [ ] ) ;
169+ }
167170 }
168171
169172 /**
0 commit comments