Skip to content

Commit dde91ae

Browse files
rljoiaRafael Lima Joia
authored and
Rafael Lima Joia
committed
issue-1694: Implement countdown in boot screen
1 parent 4f55c03 commit dde91ae

File tree

1 file changed

+30
-9
lines changed

1 file changed

+30
-9
lines changed

src/app/modules/core/services/boot-progress.service.ts

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@ import {Inject, Injectable, OnDestroy} from '@angular/core';
22
import {Store} from '@ngxs/store';
33
import {DOCUMENT} from '@angular/common';
44
import * as dayjs from 'dayjs';
5-
import {BehaviorSubject, Observable, of, Subject, throwError} from 'rxjs';
5+
import {BehaviorSubject, Observable, of, Subject, throwError, timer} from 'rxjs';
66
import {HttpClient} from '@angular/common/http';
7-
import {catchError, takeUntil, tap} from 'rxjs/operators';
7+
import {catchError, delay, finalize, take, takeUntil, tap} from 'rxjs/operators';
88

99
import {environment} from '../../../../environments/environment';
1010
import fromEventSource, {EventSourceMessage} from '../rxjs/fromEventSource';
11-
import retryWithConsecutiveBreak from '../rxjs/retryWithConsecutiveBreak';
11+
import retryWithConsecutiveBreak, {RetryAttempt} from '../rxjs/retryWithConsecutiveBreak';
1212
import {CheckCredentialsAction} from '../auth/state/auth.actions';
1313

1414
@Injectable({
@@ -71,6 +71,28 @@ export class BootProgressService implements OnDestroy {
7171
}
7272
}
7373

74+
private propagateRetryMessage(attempt: RetryAttempt) {
75+
// set boot state to retry
76+
this.state$.next(BootState.RETRY);
77+
78+
let retryInSeconds = Math.round(attempt.delay / 1000) - 1;
79+
80+
timer(0, 1000)
81+
.pipe(
82+
take(retryInSeconds),
83+
finalize(() => {
84+
of(null)
85+
.pipe(delay(850), takeUntil(this.destroy$))
86+
.subscribe(() => this.message$.next('Connecting...'));
87+
}),
88+
takeUntil(this.destroy$)
89+
)
90+
.subscribe((number) => {
91+
const remainingTime = retryInSeconds - number;
92+
this.message$.next(`Backend is down - retrying in ${remainingTime}s`);
93+
});
94+
}
95+
7496
private connect() {
7597
if (this.stream$ === null) {
7698
this.state$.next(BootState.BOOTING);
@@ -79,9 +101,6 @@ export class BootProgressService implements OnDestroy {
79101
['booting', 'booted', 'ready'],
80102
{withCredentials: false}
81103
).pipe(
82-
// kill switch.
83-
takeUntil(this.destroy$),
84-
85104
tap((e: EventSourceMessage) => {
86105
if (e.type === 'ready') {
87106
this.handleReady(e);
@@ -96,17 +115,19 @@ export class BootProgressService implements OnDestroy {
96115
maxRetries: -1,
97116
exponentialDelay: true,
98117
incrementFraction: 1.45,
99-
maxDelay: 15000,
118+
maxDelay: 16000,
100119
destroy$: this.destroy$,
101120
logAttempt: true,
102121
onRetry: attempt => {
103122
if (this.state$.getValue() !== BootState.BOOTED) {
104-
this.state$.next(BootState.RETRY);
105-
this.message$.next(`Backend is down - retrying in ${Math.round(attempt.delay / 1000)}s`);
123+
this.propagateRetryMessage(attempt);
106124
}
107125
}
108126
}),
109127

128+
// kill switch.
129+
takeUntil(this.destroy$),
130+
110131
catchError((err) => {
111132
this.state$.next(BootState.FATAL);
112133
this.message$.next('Something unexpected happened - open dev console.');

0 commit comments

Comments
 (0)