mirror of
				https://github.com/paperless-ngx/paperless-ngx.git
				synced 2025-10-30 03:56:23 -05:00 
			
		
		
		
	caching for listAll methods
This commit is contained in:
		| @@ -1,5 +1,6 @@ | |||||||
| import { HttpClient, HttpParams } from '@angular/common/http' | 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 { ObjectWithId } from 'src/app/data/object-with-id' | ||||||
| import { Results } from 'src/app/data/results' | import { Results } from 'src/app/data/results' | ||||||
| import { environment } from 'src/environments/environment' | import { environment } from 'src/environments/environment' | ||||||
| @@ -51,8 +52,28 @@ export abstract class AbstractPaperlessService<T extends ObjectWithId> { | |||||||
|     return this.http.get<Results<T>>(this.getResourceUrl(), {params: httpParams}) |     return this.http.get<Results<T>>(this.getResourceUrl(), {params: httpParams}) | ||||||
|   } |   } | ||||||
|  |  | ||||||
|  |   private _listAll: Observable<Results<T>> | ||||||
|  |  | ||||||
|   listAll(ordering?: string, extraParams?): Observable<Results<T>> { |   listAll(ordering?: string, extraParams?): Observable<Results<T>> { | ||||||
|     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<T> { | ||||||
|  |     return this.listAll().pipe( | ||||||
|  |       map(list => list.results.find(o => o.id == id)) | ||||||
|  |     ) | ||||||
|  |   } | ||||||
|  |  | ||||||
|  |   getCachedMany(ids: number[]): Observable<T[]> { | ||||||
|  |     return this.listAll().pipe( | ||||||
|  |       map(list => ids.map(id => list.results.find(o => o.id == id))) | ||||||
|  |     ) | ||||||
|   } |   } | ||||||
|  |  | ||||||
|   get(id: number): Observable<T> { |   get(id: number): Observable<T> { | ||||||
| @@ -60,14 +81,17 @@ export abstract class AbstractPaperlessService<T extends ObjectWithId> { | |||||||
|   } |   } | ||||||
|  |  | ||||||
|   create(o: T): Observable<T> { |   create(o: T): Observable<T> { | ||||||
|  |     this._listAll = null | ||||||
|     return this.http.post<T>(this.getResourceUrl(), o) |     return this.http.post<T>(this.getResourceUrl(), o) | ||||||
|   } |   } | ||||||
|  |  | ||||||
|   delete(o: T): Observable<any> { |   delete(o: T): Observable<any> { | ||||||
|  |     this._listAll = null | ||||||
|     return this.http.delete(this.getResourceUrl(o.id)) |     return this.http.delete(this.getResourceUrl(o.id)) | ||||||
|   } |   } | ||||||
|  |  | ||||||
|   update(o: T): Observable<T> { |   update(o: T): Observable<T> { | ||||||
|  |     this._listAll = null | ||||||
|     return this.http.put<T>(this.getResourceUrl(o.id), o) |     return this.http.put<T>(this.getResourceUrl(o.id), o) | ||||||
|   } |   } | ||||||
| } | } | ||||||
		Reference in New Issue
	
	Block a user
	 jonaswinkler
					jonaswinkler