11import { HttpClient } from '@angular/common/http' ;
2- import { Component , NgZone , OnInit } from '@angular/core' ;
2+ import { Component , Inject , NgZone , OnDestroy , OnInit , PLATFORM_ID } from '@angular/core' ;
33import { ActivatedRoute , Router } from '@angular/router' ;
4- import { BehaviorSubject , Observable } from 'rxjs' ;
4+ import { BehaviorSubject , interval , Observable , shareReplay , Subscription } from 'rxjs' ;
55import { finalize , map , switchMap , take , tap } from 'rxjs/operators' ;
66import { AuthService } from '../../core/auth/auth.service' ;
77import { DSONameService } from '../../core/breadcrumbs/dso-name.service' ;
@@ -26,6 +26,8 @@ import { NgbModal, NgbModalRef } from '@ng-bootstrap/ng-bootstrap';
2626import { getProcessListRoute } from '../process-page-routing.paths' ;
2727import { NotificationsService } from '../../shared/notifications/notifications.service' ;
2828import { TranslateService } from '@ngx-translate/core' ;
29+ import { followLink } from '../../shared/utils/follow-link-config.model' ;
30+ import { isPlatformBrowser } from '@angular/common' ;
2931
3032@Component ( {
3133 selector : 'ds-process-detail' ,
@@ -34,7 +36,7 @@ import { TranslateService } from '@ngx-translate/core';
3436/**
3537 * A component displaying detailed information about a DSpace Process
3638 */
37- export class ProcessDetailComponent implements OnInit {
39+ export class ProcessDetailComponent implements OnInit , OnDestroy {
3840
3941 /**
4042 * The AlertType enumeration
@@ -65,48 +67,58 @@ export class ProcessDetailComponent implements OnInit {
6567 /**
6668 * Boolean on whether or not to show the output logs
6769 */
68- showOutputLogs ;
70+ showOutputLogs = false ;
6971 /**
7072 * When it's retrieving the output logs from backend, to show loading component
7173 */
72- retrievingOutputLogs$ : BehaviorSubject < boolean > ;
74+ retrievingOutputLogs$ = new BehaviorSubject < boolean > ( false ) ;
7375
7476 /**
7577 * Date format to use for start and end time of processes
7678 */
7779 dateFormat = 'yyyy-MM-dd HH:mm:ss ZZZZ' ;
7880
81+ refreshCounter$ = new BehaviorSubject ( 0 ) ;
82+
7983 /**
8084 * Reference to NgbModal
8185 */
8286 protected modalRef : NgbModalRef ;
8387
84- constructor ( protected route : ActivatedRoute ,
85- protected router : Router ,
86- protected processService : ProcessDataService ,
87- protected bitstreamDataService : BitstreamDataService ,
88- protected nameService : DSONameService ,
89- private zone : NgZone ,
90- protected authService : AuthService ,
91- protected http : HttpClient ,
92- protected modalService : NgbModal ,
93- protected notificationsService : NotificationsService ,
94- protected translateService : TranslateService
95- ) {
96- }
88+ private refreshTimerSub ?: Subscription ;
89+
90+ constructor (
91+ @Inject ( PLATFORM_ID ) protected platformId : object ,
92+ protected route : ActivatedRoute ,
93+ protected router : Router ,
94+ protected processService : ProcessDataService ,
95+ protected bitstreamDataService : BitstreamDataService ,
96+ protected nameService : DSONameService ,
97+ private zone : NgZone ,
98+ protected authService : AuthService ,
99+ protected http : HttpClient ,
100+ protected modalService : NgbModal ,
101+ protected notificationsService : NotificationsService ,
102+ protected translateService : TranslateService
103+ ) { }
97104
98105 /**
99106 * Initialize component properties
100107 * Display a 404 if the process doesn't exist
101108 */
102109 ngOnInit ( ) : void {
103- this . showOutputLogs = false ;
104- this . retrievingOutputLogs$ = new BehaviorSubject < boolean > ( false ) ;
105110 this . processRD$ = this . route . data . pipe (
106111 map ( ( data ) => {
112+ if ( isPlatformBrowser ( this . platformId ) ) {
113+ if ( ! this . isProcessFinished ( data . process . payload ) ) {
114+ this . startRefreshTimer ( ) ;
115+ }
116+ }
117+
107118 return data . process as RemoteData < Process > ;
108119 } ) ,
109- redirectOn4xx ( this . router , this . authService )
120+ redirectOn4xx ( this . router , this . authService ) ,
121+ shareReplay ( 1 )
110122 ) ;
111123
112124 this . filesRD$ = this . processRD$ . pipe (
@@ -115,6 +127,53 @@ export class ProcessDetailComponent implements OnInit {
115127 ) ;
116128 }
117129
130+ refresh ( ) {
131+ this . processRD$ = this . processService . findById (
132+ this . route . snapshot . params . id ,
133+ false ,
134+ true ,
135+ followLink ( 'script' )
136+ ) . pipe (
137+ getFirstSucceededRemoteData ( ) ,
138+ redirectOn4xx ( this . router , this . authService ) ,
139+ tap ( ( processRemoteData : RemoteData < Process > ) => {
140+ if ( ! this . isProcessFinished ( processRemoteData . payload ) ) {
141+ this . startRefreshTimer ( ) ;
142+ }
143+ } ) ,
144+ shareReplay ( 1 )
145+ ) ;
146+
147+ this . filesRD$ = this . processRD$ . pipe (
148+ getFirstSucceededRemoteDataPayload ( ) ,
149+ switchMap ( ( process : Process ) => this . processService . getFiles ( process . processId ) )
150+ ) ;
151+ }
152+
153+ startRefreshTimer ( ) {
154+ this . refreshCounter$ . next ( 0 ) ;
155+
156+ this . refreshTimerSub = interval ( 1000 ) . subscribe (
157+ value => {
158+ if ( value > 5 ) {
159+ setTimeout ( ( ) => {
160+ this . refresh ( ) ;
161+ this . stopRefreshTimer ( ) ;
162+ this . refreshCounter$ . next ( 0 ) ;
163+ } , 1 ) ;
164+ } else {
165+ this . refreshCounter$ . next ( 5 - value ) ;
166+ }
167+ } ) ;
168+ }
169+
170+ stopRefreshTimer ( ) {
171+ if ( hasValue ( this . refreshTimerSub ) ) {
172+ this . refreshTimerSub . unsubscribe ( ) ;
173+ this . refreshTimerSub = undefined ;
174+ }
175+ }
176+
118177 /**
119178 * Get the name of a bitstream
120179 * @param bitstream
@@ -210,11 +269,15 @@ export class ProcessDetailComponent implements OnInit {
210269 openDeleteModal ( content ) {
211270 this . modalRef = this . modalService . open ( content ) ;
212271 }
272+
213273 /**
214274 * Close the modal.
215275 */
216276 closeModal ( ) {
217277 this . modalRef . close ( ) ;
218278 }
219279
280+ ngOnDestroy ( ) : void {
281+ this . stopRefreshTimer ( ) ;
282+ }
220283}
0 commit comments