-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDataService.ts
More file actions
53 lines (46 loc) · 1.53 KB
/
Copy pathDataService.ts
File metadata and controls
53 lines (46 loc) · 1.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import { Injectable, Inject } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { Observable, of, Subject, BehaviorSubject } from 'rxjs';
import { map, shareReplay, switchMap, takeUntil } from 'rxjs/operators';
@Injectable({
providedIn: 'root'
})
export class DataService {
private readonly apiUrl = 'https://codeholistic.com/api';
private readonly cacheSize = 1;
private destroy$ = new Subject<void>();
private _data: BehaviorSubject<Data[]> = new BehaviorSubject<Data[]>([]);
public data: Observable<Data[]> = this._data.asObservable();
constructor(private http: HttpClient, @Inject('ENVIRONMENT') private environment: any) { }
public getData(): Observable<Data[]> {
return this.http.get<Data[]>(`${this.apiUrl}/data`).pipe(
takeUntil(this.destroy$),
map(data => {
this._data.next(data);
return data;
}),
shareReplay(this.cacheSize) // cache previous emissions
);
}
public patchData(data: Data): void {
this.http.patch<Data>(`${this.apiUrl}/data`, data)
.pipe(
takeUntil(this.destroy$),
switchMap(updatedData => {
const currentValue = this._data.value;
const newValue = currentValue.map(d => {
if (d.id === updatedData.id) {
return updatedData;
}
return d;
});
this._data.next(newValue);
return of(updatedData);
})
).subscribe();
}
ngOnDestroy() {
this.destroy$.next();
this.destroy$.complete();
}
}