Fixed an issue where deadlocks could occur when loading the relationship decorators

This commit is contained in:
Art Lowel
2017-05-05 15:37:43 +02:00
parent b9de162e00
commit f56eefab59
8 changed files with 47 additions and 29 deletions

View File

@@ -4,6 +4,7 @@ import { ObjectCacheState, ObjectCacheEntry, CacheableObject } from "./object-ca
import { AddToObjectCacheAction, RemoveFromObjectCacheAction } from "./object-cache.actions";
import { Observable } from "rxjs";
import { hasNoValue } from "../../shared/empty.util";
import { GenericConstructor } from "../shared/generic-constructor";
/**
* A service to interact with the object cache
@@ -39,34 +40,53 @@ export class ObjectCacheService {
/**
* Get an observable of the object with the specified UUID
*
* The type needs to be specified as well, in order to turn
* the cached plain javascript object in to an instance of
* a class.
*
* e.g. get('c96588c6-72d3-425d-9d47-fa896255a695', Item)
*
* @param uuid
* The UUID of the object to get
* @param type
* The type of the object to get
* @return Observable<T>
* An observable of the requested object
*/
get<T extends CacheableObject>(uuid: string): Observable<T> {
get<T extends CacheableObject>(uuid: string, type: GenericConstructor<T>): Observable<T> {
return this.store.select<ObjectCacheEntry>('core', 'cache', 'object', uuid)
.filter(entry => this.isValid(entry))
.distinctUntilChanged()
.map((entry: ObjectCacheEntry) => <T> entry.data);
.map((entry: ObjectCacheEntry) => <T> Object.assign(new type(), entry.data));
}
getBySelfLink<T extends CacheableObject>(href: string): Observable<T> {
getBySelfLink<T extends CacheableObject>(href: string, type: GenericConstructor<T>): Observable<T> {
return this.store.select<string>('core', 'index', 'href', href)
.flatMap((uuid: string) => this.get<T>(uuid))
.flatMap((uuid: string) => this.get(uuid, type))
}
/**
* Get an observable for an array of objects
* Get an observable for an array of objects of the same type
* with the specified UUIDs
*
* The type needs to be specified as well, in order to turn
* the cached plain javascript object in to an instance of
* a class.
*
* e.g. getList([
* 'c96588c6-72d3-425d-9d47-fa896255a695',
* 'cff860da-cf5f-4fda-b8c9-afb7ec0b2d9e'
* ], Collection)
*
* @param uuids
* An array of UUIDs of the objects to get
* @param type
* The type of the objects to get
* @return Observable<Array<T>>
*/
getList<T extends CacheableObject>(uuids: Array<string>): Observable<Array<T>> {
getList<T extends CacheableObject>(uuids: Array<string>, type: GenericConstructor<T>): Observable<Array<T>> {
return Observable.combineLatest(
uuids.map((id: string) => this.get<T>(id))
uuids.map((id: string) => this.get<T>(id, type))
);
}