11import { HttpClient , HttpHeaders } from '@angular/common/http'
22import { Injectable } from '@angular/core'
3- import { Observable , ReplaySubject } from 'rxjs'
3+ import { BehaviorSubject , Observable , ReplaySubject } from 'rxjs'
44import { catchError , map , switchMap , tap , retry } from 'rxjs/operators'
55import { AMOUNT_OF_RETRIEVE_NOTIFICATIONS_PER_CALL } from 'src/app/constants'
66import { ERROR_REPORT } from 'src/app/errors'
@@ -22,6 +22,10 @@ export class InboxService {
2222 private currentlyIncludingArchive : boolean
2323 private currentLevel = 0
2424 private headers : HttpHeaders
25+ private _unreadCountSubject = new BehaviorSubject < number | null > ( null )
26+ public readonly unreadCount$ : Observable < number | null > =
27+ this . _unreadCountSubject . asObservable ( )
28+
2529 private inboxSubject = new ReplaySubject <
2630 (
2731 | InboxNotificationAmended
@@ -218,13 +222,39 @@ export class InboxService {
218222 )
219223 }
220224
221- retrieveUnreadCount ( ) : any {
222- return this . _http . get (
223- runtimeEnvironment . BASE_URL + 'inbox/unreadCount.json'
224- )
225+ private _fetchAndUpdateUnreadCount ( ) : Observable < number > {
226+ return this . _http
227+ . get < number > ( runtimeEnvironment . BASE_URL + 'inbox/unreadCount.json' , {
228+ headers : this . headers ,
229+ } )
230+ . pipe (
231+ retry ( 3 ) ,
232+ tap ( ( count : number ) => {
233+ this . _unreadCountSubject . next ( count )
234+ } ) ,
235+ catchError ( ( error ) =>
236+ this . _errorHandler . handleError ( error , ERROR_REPORT . STANDARD_VERBOSE )
237+ )
238+ )
225239 }
226240
227- totalNumber ( ) {
241+ /**
242+
243+ * Calling retrieveUnreadCount() will:
244+ * 1) Kick off a fresh HTTP GET to inbox/unreadCount.json
245+ * 2) Push that result into _unreadCountSubject
246+ * 3) Return the “hot” Observable from _unreadCountSubject, so that ANY future changes are pass through
247+ */
248+ retrieveUnreadCount ( ) : Observable < number | null > {
249+ this . _fetchAndUpdateUnreadCount ( ) . subscribe ( )
250+ return this . unreadCount$
251+ }
252+
253+ /**
254+ * Returns Observable<TotalNotificationCount>
255+ * and triggers a fresh fetch of unreadCount.json so that unreadCount$ subscribers are updated.
256+ */
257+ totalNumber ( ) : Observable < TotalNotificationCount > {
228258 return this . _http
229259 . get < TotalNotificationCount > (
230260 runtimeEnvironment . BASE_URL + `inbox/totalCount.json` ,
@@ -238,6 +268,10 @@ export class InboxService {
238268 value . archived = value . all - value . nonArchived
239269 return value
240270 } ) ,
271+ // As a side‐effect, refresh the unread count whenever totalNumber() is called:
272+ tap ( ( ) => {
273+ this . _fetchAndUpdateUnreadCount ( ) . subscribe ( )
274+ } ) ,
241275 catchError ( ( error ) => this . _errorHandler . handleError ( error ) )
242276 )
243277 }
0 commit comments