src/lib/service/inbox/inbox.service.ts
InboxService handles interaction with the inbox service on the backend side.
Properties |
|
Methods |
constructor(backend: BackendService)
|
||||||
Defined in src/lib/service/inbox/inbox.service.ts:26
|
||||||
Parameters :
|
getItems |
getItems()
|
Defined in src/lib/service/inbox/inbox.service.ts:36
|
Fetches inbox items from the backend.
Returns :
Observable<InboxItem[]>
Observable of inbox items. |
getState |
getState()
|
Defined in src/lib/service/inbox/inbox.service.ts:71
|
Fetches state informations for the users inbox
Returns :
InboxState
inbox state object |
markAsRead | ||||||||
markAsRead(item: InboxItem)
|
||||||||
Defined in src/lib/service/inbox/inbox.service.ts:128
|
||||||||
Marks an inbox item as read.
Parameters :
Returns :
Observable<any>
Observable |
refreshInboxState |
refreshInboxState()
|
Defined in src/lib/service/inbox/inbox.service.ts:78
|
Public access to fetch inbox
Returns :
void
|
removeItem | ||||||||
removeItem(item: InboxItem)
|
||||||||
Defined in src/lib/service/inbox/inbox.service.ts:101
|
||||||||
Removes an item from the inbox.
Parameters :
Returns :
Observable<any>
Observable |
Public updateInboxItems |
updateInboxItems(id?: string, item?: any, multi?: any[])
|
Defined in src/lib/service/inbox/inbox.service.ts:116
|
Soft update for data in the inbox.
Returns :
void
|
Public inboxItems$ |
inboxItems$:
|
Type : Observable<InboxItem[]>
|
Default value : this.inboxItemsGridData$.pipe(this.backend.gridDataFilter)
|
Defined in src/lib/service/inbox/inbox.service.ts:26
|
Public inboxItemsGridData$ |
inboxItemsGridData$:
|
Type : Observable<InboxItem[]>
|
Default value : this.inboxItemsSource.asObservable()
|
Defined in src/lib/service/inbox/inbox.service.ts:25
|
Public inboxState$ |
inboxState$:
|
Type : Observable<InboxState>
|
Default value : this.inboxStateSource.asObservable()
|
Defined in src/lib/service/inbox/inbox.service.ts:21
|
import {map, tap, catchError, expand, concatMap, skipWhile, filter} from 'rxjs/operators';
import {Injectable} from '@angular/core';
import {EMPTY, Observable, ReplaySubject} from 'rxjs';
import {BackendService} from '../backend/backend.service';
import {InboxItem, InboxState} from '../../model/inbox-item.model';
import {Utils} from '../../util/utils';
/**
* InboxService handles interaction with the inbox service on the backend side.
*/
@Injectable({
providedIn: 'root'
})
export class InboxService {
// backends maximum page size
private MAX_PAGE_SIZE = 1000;
private inboxState: InboxState;
private inboxStateSource = new ReplaySubject<InboxState>(1);
public inboxState$: Observable<InboxState> = this.inboxStateSource.asObservable();
private inboxItems: InboxItem[];
private inboxItemsSource = new ReplaySubject<InboxItem[]>(1);
public inboxItemsGridData$: Observable<InboxItem[]> = this.inboxItemsSource.asObservable();
public inboxItems$: Observable<InboxItem[]> = this.inboxItemsGridData$.pipe(this.backend.gridDataFilter);
constructor(private backend: BackendService) {
}
/**
* Fetches inbox items from the backend.
*
* @returns Observable of inbox items.
*/
getItems(): Observable<InboxItem[]> {
return this.getAllPages()
.pipe(
map(res => res.map(item => new InboxItem(item))),
tap(res => {
this.inboxItems = res;
this.inboxItemsSource.next(this.inboxItems);
}
),
catchError(Utils.throw(() => this.inboxItemsSource.next([]))));
}
private getAllPages(): Observable<any[]> {
let items = [];
let pageNumber = 1;
return this.getPage().pipe(
expand(res => {
return !res.last ? this.getPage(pageNumber++) : EMPTY;
}),
tap(res => items = [...items, ...res.content]),
skipWhile(res => !res.last),
map(_ => items)
);
}
private getPage(index?: number) {
return this.backend
.getJson(`/get?size=${this.MAX_PAGE_SIZE}&page=${index || 0}`, this.backend.getInboxBase());
}
/**
* Fetches state informations for the users inbox
*
* @returns inbox state object
*/
getState(): InboxState {
return this.inboxState;
}
/**
* Public access to fetch inbox
*/
refreshInboxState() {
this.fetchInboxState();
}
/**
* Retrieve inbox state
*/
private fetchInboxState() {
this.backend
.getJson('/state', this.backend.getInboxBase())
.pipe(map(res => res as InboxState))
.subscribe(res => {
this.inboxState = res;
this.inboxStateSource.next(this.inboxState);
}, Utils.logError(null, 'Failed to fetch inbox state!'));
}
/**
* Removes an item from the inbox.
*
* @param item inbox item to be removed
* @returns Observable<any>
*/
removeItem(item: InboxItem): Observable<any> {
return this.backend
.del('/' + item.id, this.backend.getInboxBase())
.pipe(
tap(() => this.updateInboxItems(item.id))
);
}
/**
* Soft update for data in the inbox.
*
* @param id
* @param item
* @param multi
*/
public updateInboxItems(id?: string, item?: any, multi?: any[]) {
if (this.backend.update(this.inboxItems, multi || [{id, item}])) {
this.inboxItemsSource.next(this.inboxItems);
}
}
/**
* Marks an inbox item as read.
*
* @param item the item to be marked as read
* @returns Observable<any>
*/
markAsRead(item: InboxItem): Observable<any> {
return this.backend.put('/read/' + item.id, this.backend.getInboxBase());
}
}