@@ -26,9 +26,13 @@ import { IProcessNotification } from '../models/process-notification.model';
2626import { ProcessDataService } from '../../../core/data/processes/process-data.service' ;
2727import { Process } from '../../../process-page/processes/process.model' ;
2828import { Bitstream } from '../../../core/shared/bitstream.model' ;
29- import { getFirstCompletedRemoteData , getFirstSucceededRemoteListPayload } from '../../../core/shared/operators' ;
29+ import {
30+ getAllCompletedRemoteData , getFirstCompletedRemoteData
31+ } from '../../../core/shared/operators' ;
3032import { DSONameService } from '../../../core/breadcrumbs/dso-name.service' ;
3133import { DSpaceObject } from '../../../core/shared/dspace-object.model' ;
34+ import { filter , map , switchMap , take } from 'rxjs/operators' ;
35+ import { RemoteData } from '../../../core/data/remote-data' ;
3236
3337@Component ( {
3438 selector : 'ds-process-notification' ,
@@ -84,7 +88,7 @@ export class ProcessNotificationComponent implements OnInit, OnDestroy {
8488 /**
8589 * The process that is being checked.
8690 */
87- public process : Process ;
91+ public processStatus$ : BehaviorSubject < string > = new BehaviorSubject ( '' ) ;
8892
8993 /**
9094 * If process checking is finished.
@@ -94,12 +98,12 @@ export class ProcessNotificationComponent implements OnInit, OnDestroy {
9498 /**
9599 * Files generated from process end.
96100 */
97- public files : Bitstream [ ] ;
101+ public files$ : BehaviorSubject < Bitstream [ ] > = new BehaviorSubject < Bitstream [ ] > ( [ ] ) ;
98102
99103 /**
100104 * Type of the notification visualisation.
101105 */
102- public notificationType = 'alert-info' ;
106+ public notificationType$ : BehaviorSubject < string > = new BehaviorSubject ( 'alert-info' ) ;
103107
104108 constructor ( private notificationService : NotificationsService ,
105109 private domSanitizer : DomSanitizer ,
@@ -114,55 +118,53 @@ export class ProcessNotificationComponent implements OnInit, OnDestroy {
114118 */
115119 ngOnInit ( ) : void {
116120 this . animate = this . notification . options . animate + NotificationAnimationsStatus . In ;
117- this . initCheckProcess ( ) ;
121+ this . pollUntilProcessFinished ( ) ;
118122 this . html = this . notification . html ;
119123 this . contentType ( this . notification . title , 'title' ) ;
120124 }
121125
122126 /**
123- * Initialization of timer .
127+ * Poll process endpoint until it's finished .
124128 */
125- initCheckProcess ( ) {
126- const source = timer ( 0 , this . notification . checkTime ) ;
127- this . sub = source . subscribe ( val => {
128- this . checkProcess ( ) ;
129+ pollUntilProcessFinished ( ) {
130+ timer ( 0 , 5000 ) . pipe (
131+ switchMap ( ( ) => this . processService . getProcess ( this . notification . processId ) ) ,
132+ getAllCompletedRemoteData ( ) ,
133+ filter ( ( res : RemoteData < Process > ) => res ?. payload ?. processStatus . toString ( ) === 'COMPLETED' || res ?. payload ?. processStatus . toString ( ) === 'FAILED' ) ,
134+ take ( 1 ) ,
135+ ) . subscribe ( ( res : RemoteData < Process > ) => {
136+ this . pollingFinishedFor ( res . payload ) ;
129137 } ) ;
130138 }
131139
132140 /**
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- } ) ;
141+ * Handle process results
142+ *
143+ * @param process The process finished
144+ */
145+ pollingFinishedFor ( process : Process ) {
146+ const processStatus = process . processStatus . toString ( ) ;
147+ if ( processStatus === 'COMPLETED' ) {
148+ this . notificationType$ . next ( 'alert-success' ) ;
149+ this . processStatus$ . next ( 'process.new.notification.process.status.completed' ) ;
150+ this . getFiles ( ) ;
151+ } else {
152+ this . processStatus$ . next ( 'process.new.notification.process.status.failed' ) ;
153+ this . notificationType$ . next ( 'alert-danger' ) ;
154+ }
155+ this . finished . next ( true ) ;
153156 }
154157
155158 /**
156159 * When the process is completed get the files output.
157160 */
158161 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 ( ) ) ;
162+ this . processService . getFiles ( this . notification . processId ) . pipe (
163+ getFirstCompletedRemoteData ( ) ,
164+ map ( ( response ) => response . hasSucceeded ? response . payload . page : [ ] )
165+ ) . subscribe ( ( files : Bitstream [ ] ) => {
166+ const logFiles = files . filter ( ( file ) => ! this . getFileName ( file ) . includes ( '.log' ) ) ;
167+ this . files$ . next ( logFiles ) ;
166168 } ) ;
167169 }
168170
0 commit comments