diff --git a/src-ui/src/app/services/rest/abstract-paperless-service.ts b/src-ui/src/app/services/rest/abstract-paperless-service.ts index 16064c702..3feed320e 100644 --- a/src-ui/src/app/services/rest/abstract-paperless-service.ts +++ b/src-ui/src/app/services/rest/abstract-paperless-service.ts @@ -1,5 +1,6 @@ import { HttpClient, HttpParams } from '@angular/common/http' -import { Observable } from 'rxjs' +import { Observable, of, Subject } from 'rxjs' +import { map, publishReplay, refCount } from 'rxjs/operators' import { ObjectWithId } from 'src/app/data/object-with-id' import { Results } from 'src/app/data/results' import { environment } from 'src/environments/environment' @@ -51,8 +52,28 @@ export abstract class AbstractPaperlessService { return this.http.get>(this.getResourceUrl(), {params: httpParams}) } + private _listAll: Observable> + listAll(ordering?: string, extraParams?): Observable> { - return this.list(1, 100000, ordering, extraParams) + if (!this._listAll) { + this._listAll = this.list(1, 100000, ordering, extraParams).pipe( + publishReplay(1), + refCount() + ) + } + return this._listAll + } + + getCached(id: number): Observable { + return this.listAll().pipe( + map(list => list.results.find(o => o.id == id)) + ) + } + + getCachedMany(ids: number[]): Observable { + return this.listAll().pipe( + map(list => ids.map(id => list.results.find(o => o.id == id))) + ) } get(id: number): Observable { @@ -60,14 +81,17 @@ export abstract class AbstractPaperlessService { } create(o: T): Observable { + this._listAll = null return this.http.post(this.getResourceUrl(), o) } delete(o: T): Observable { + this._listAll = null return this.http.delete(this.getResourceUrl(o.id)) } update(o: T): Observable { + this._listAll = null return this.http.put(this.getResourceUrl(o.id), o) } } \ No newline at end of file