mirror of
https://github.com/DSpace/dspace-angular.git
synced 2025-10-07 10:04:11 +00:00
[DURACOM-247] Refactored by providing the map of promises
This commit is contained in:
7
src/app/core/cache/builders/link.service.ts
vendored
7
src/app/core/cache/builders/link.service.ts
vendored
@@ -1,7 +1,6 @@
|
||||
import {
|
||||
Inject,
|
||||
Injectable,
|
||||
InjectionToken,
|
||||
Injector,
|
||||
} from '@angular/core';
|
||||
import {
|
||||
@@ -25,7 +24,7 @@ import { FollowLinkConfig } from '../../../shared/utils/follow-link-config.model
|
||||
import { HALDataService } from '../../data/base/hal-data-service.interface';
|
||||
import { PaginatedList } from '../../data/paginated-list.model';
|
||||
import { RemoteData } from '../../data/remote-data';
|
||||
import { lazyService } from '../../lazy-service';
|
||||
import { lazyDataService } from '../../lazy-data-service';
|
||||
import { GenericConstructor } from '../../shared/generic-constructor';
|
||||
import { HALResource } from '../../shared/hal-resource.model';
|
||||
import {
|
||||
@@ -43,7 +42,7 @@ export class LinkService {
|
||||
|
||||
constructor(
|
||||
protected injector: Injector,
|
||||
@Inject(APP_DATA_SERVICES_MAP) private map: InjectionToken<LazyDataServicesMap>,
|
||||
@Inject(APP_DATA_SERVICES_MAP) private map: LazyDataServicesMap,
|
||||
@Inject(LINK_DEFINITION_FACTORY) private getLinkDefinition: <T extends HALResource>(source: GenericConstructor<T>, linkName: keyof T['_links']) => LinkDefinition<T>,
|
||||
@Inject(LINK_DEFINITION_MAP_FACTORY) private getLinkDefinitions: <T extends HALResource>(source: GenericConstructor<T>) => Map<keyof T['_links'], LinkDefinition<T>>,
|
||||
) {
|
||||
@@ -73,7 +72,7 @@ export class LinkService {
|
||||
public resolveLinkWithoutAttaching<T extends HALResource, U extends HALResource>(model, linkToFollow: FollowLinkConfig<T>): Observable<RemoteData<U | PaginatedList<U>>> {
|
||||
const matchingLinkDef = this.getLinkDefinition(model.constructor, linkToFollow.name);
|
||||
if (hasValue(matchingLinkDef)) {
|
||||
const lazyProvider$: Observable<HALDataService<any>> = lazyService(this.map[matchingLinkDef.resourceType.value], this.injector);
|
||||
const lazyProvider$: Observable<HALDataService<any>> = lazyDataService(this.map, matchingLinkDef.resourceType.value, this.injector);
|
||||
return lazyProvider$.pipe(
|
||||
switchMap((provider: HALDataService<any>) => {
|
||||
const link = model._links[matchingLinkDef.linkName];
|
||||
|
@@ -7,27 +7,32 @@ import {
|
||||
Observable,
|
||||
} from 'rxjs';
|
||||
|
||||
import { LazyDataServicesMap } from '../../config/app-config.interface';
|
||||
import { isNotEmpty } from '../shared/empty.util';
|
||||
import { HALDataService } from './data/base/hal-data-service.interface';
|
||||
|
||||
/**
|
||||
* Loads a service lazily. The service is loaded when the observable is subscribed to.
|
||||
*
|
||||
* @param loader A function that returns a promise of the service to load.
|
||||
* @param map A map of promises returning the data services to load
|
||||
* @param key The key of the service
|
||||
* @param injector The injector to use to load the service. If not provided, the current injector is used.
|
||||
* @returns An observable of the service.
|
||||
*
|
||||
* @example
|
||||
* ```ts
|
||||
* const dataService$ = lazyService(() => import('./data-service').then((m) => m.MyService), this.injector);
|
||||
* const dataService$ = lazyDataService({ 'data-service': () => import('./data-service').then((m) => m.MyService)}, 'data-service', this.injector);
|
||||
* or
|
||||
* const dataService$ = lazyService(() => import('./data-service'), this.injector);
|
||||
* const dataService$ = lazyDataService({'data-service': () => import('./data-service')}, 'data-service', this.injector);
|
||||
* ```
|
||||
*/
|
||||
export function lazyService<T>(
|
||||
loader: () => Promise<Type<T>> | Promise<{ default: Type<T> }>,
|
||||
export function lazyDataService<T>(
|
||||
map: LazyDataServicesMap,
|
||||
key: string,
|
||||
injector: Injector,
|
||||
): Observable<T> {
|
||||
return defer(() => {
|
||||
const loader: () => Promise<Type<HALDataService<any>> | { default: HALDataService<any> }> = map[key];
|
||||
if (isNotEmpty(loader) && typeof loader === 'function') {
|
||||
return loader()
|
||||
.then((serviceOrDefault) => {
|
@@ -7,7 +7,6 @@ import {
|
||||
ChangeDetectorRef,
|
||||
Component,
|
||||
Inject,
|
||||
InjectionToken,
|
||||
Injector,
|
||||
Input,
|
||||
OnDestroy,
|
||||
@@ -42,7 +41,7 @@ import {
|
||||
import { ArrayMoveChangeAnalyzer } from '../../core/data/array-move-change-analyzer.service';
|
||||
import { RemoteData } from '../../core/data/remote-data';
|
||||
import { UpdateDataService } from '../../core/data/update-data.service';
|
||||
import { lazyService } from '../../core/lazy-service';
|
||||
import { lazyDataService } from '../../core/lazy-data-service';
|
||||
import { DSpaceObject } from '../../core/shared/dspace-object.model';
|
||||
import { getFirstCompletedRemoteData } from '../../core/shared/operators';
|
||||
import { ResourceType } from '../../core/shared/resource-type';
|
||||
@@ -152,7 +151,7 @@ export class DsoEditMetadataComponent implements OnInit, OnDestroy {
|
||||
protected parentInjector: Injector,
|
||||
protected arrayMoveChangeAnalyser: ArrayMoveChangeAnalyzer<number>,
|
||||
protected cdr: ChangeDetectorRef,
|
||||
@Inject(APP_DATA_SERVICES_MAP) private dataServiceMap: InjectionToken<LazyDataServicesMap>) {
|
||||
@Inject(APP_DATA_SERVICES_MAP) private dataServiceMap: LazyDataServicesMap) {
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -186,7 +185,7 @@ export class DsoEditMetadataComponent implements OnInit, OnDestroy {
|
||||
*/
|
||||
retrieveDataService(): Observable<UpdateDataService<DSpaceObject>> {
|
||||
if (hasNoValue(this.updateDataService)) {
|
||||
const lazyProvider$: Observable<UpdateDataService<DSpaceObject>> = lazyService(this.dataServiceMap[this.dsoType], this.parentInjector);
|
||||
const lazyProvider$: Observable<UpdateDataService<DSpaceObject>> = lazyDataService(this.dataServiceMap, this.dsoType, this.parentInjector);
|
||||
return lazyProvider$;
|
||||
} else {
|
||||
return EMPTY;
|
||||
|
@@ -7,7 +7,6 @@ import {
|
||||
Component,
|
||||
EventEmitter,
|
||||
Inject,
|
||||
InjectionToken,
|
||||
Injector,
|
||||
Input,
|
||||
OnDestroy,
|
||||
@@ -35,7 +34,7 @@ import { EPersonDataService } from '../../core/eperson/eperson-data.service';
|
||||
import { GroupDataService } from '../../core/eperson/group-data.service';
|
||||
import { EPERSON } from '../../core/eperson/models/eperson.resource-type';
|
||||
import { GROUP } from '../../core/eperson/models/group.resource-type';
|
||||
import { lazyService } from '../../core/lazy-service';
|
||||
import { lazyDataService } from '../../core/lazy-data-service';
|
||||
import { PaginationService } from '../../core/pagination/pagination.service';
|
||||
import { DSpaceObject } from '../../core/shared/dspace-object.model';
|
||||
import { getFirstCompletedRemoteData } from '../../core/shared/operators';
|
||||
@@ -133,7 +132,7 @@ export class EpersonGroupListComponent implements OnInit, OnDestroy {
|
||||
constructor(public dsoNameService: DSONameService,
|
||||
private parentInjector: Injector,
|
||||
private paginationService: PaginationService,
|
||||
@Inject(APP_DATA_SERVICES_MAP) private dataServiceMap: InjectionToken<LazyDataServicesMap>) {
|
||||
@Inject(APP_DATA_SERVICES_MAP) private dataServiceMap: LazyDataServicesMap) {
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -141,7 +140,7 @@ export class EpersonGroupListComponent implements OnInit, OnDestroy {
|
||||
*/
|
||||
ngOnInit(): void {
|
||||
const resourceType: ResourceType = (this.isListOfEPerson) ? EPERSON : GROUP;
|
||||
const lazyProvider$: Observable<EPersonDataService | GroupDataService> = lazyService(this.dataServiceMap[resourceType.value], this.parentInjector);
|
||||
const lazyProvider$: Observable<EPersonDataService | GroupDataService> = lazyDataService(this.dataServiceMap, resourceType.value, this.parentInjector);
|
||||
lazyProvider$.subscribe((dataService: EPersonDataService | GroupDataService) => {
|
||||
this.dataService = dataService;
|
||||
console.log(dataService);
|
||||
|
@@ -1,6 +1,5 @@
|
||||
import {
|
||||
inject,
|
||||
InjectionToken,
|
||||
Injector,
|
||||
} from '@angular/core';
|
||||
import {
|
||||
@@ -12,10 +11,13 @@ import {
|
||||
import { Observable } from 'rxjs';
|
||||
import { switchMap } from 'rxjs/operators';
|
||||
|
||||
import { LazyDataServicesMap } from '../../../../config/app-config.interface';
|
||||
import {
|
||||
APP_DATA_SERVICES_MAP,
|
||||
LazyDataServicesMap,
|
||||
} from '../../../../config/app-config.interface';
|
||||
import { IdentifiableDataService } from '../../../core/data/base/identifiable-data.service';
|
||||
import { RemoteData } from '../../../core/data/remote-data';
|
||||
import { lazyService } from '../../../core/lazy-service';
|
||||
import { lazyDataService } from '../../../core/lazy-data-service';
|
||||
import { DSpaceObject } from '../../../core/shared/dspace-object.model';
|
||||
import { getFirstCompletedRemoteData } from '../../../core/shared/operators';
|
||||
import { ResourceType } from '../../../core/shared/resource-type';
|
||||
@@ -34,7 +36,7 @@ import { isEmpty } from '../../empty.util';
|
||||
export const resourcePolicyTargetResolver: ResolveFn<RemoteData<DSpaceObject>> = (
|
||||
route: ActivatedRouteSnapshot,
|
||||
state: RouterStateSnapshot,
|
||||
dataServiceMap: InjectionToken<LazyDataServicesMap> = inject(InjectionToken<LazyDataServicesMap>),
|
||||
dataServiceMap: LazyDataServicesMap = inject(APP_DATA_SERVICES_MAP),
|
||||
parentInjector: Injector = inject(Injector),
|
||||
router: Router = inject(Router),
|
||||
): Observable<RemoteData<DSpaceObject>> => {
|
||||
@@ -46,7 +48,7 @@ export const resourcePolicyTargetResolver: ResolveFn<RemoteData<DSpaceObject>> =
|
||||
}
|
||||
|
||||
const resourceType: ResourceType = new ResourceType(targetType);
|
||||
const lazyProvider$: Observable<IdentifiableDataService<DSpaceObject>> = lazyService(dataServiceMap[resourceType.value], parentInjector);
|
||||
const lazyProvider$: Observable<IdentifiableDataService<DSpaceObject>> = lazyDataService(dataServiceMap, resourceType.value, parentInjector);
|
||||
|
||||
return lazyProvider$.pipe(
|
||||
switchMap((dataService: IdentifiableDataService<DSpaceObject>) => {
|
||||
|
@@ -74,7 +74,7 @@ const APP_CONFIG = new InjectionToken<AppConfig>('APP_CONFIG');
|
||||
const APP_CONFIG_STATE = makeStateKey<AppConfig>('APP_CONFIG_STATE');
|
||||
|
||||
export interface LazyDataServicesMap {
|
||||
[type: string]: () => Promise<Type<HALDataService<any>>>
|
||||
[type: string]: () => Promise<Type<HALDataService<any>> | { default: HALDataService<any> }>
|
||||
}
|
||||
|
||||
export const APP_DATA_SERVICES_MAP: InjectionToken<LazyDataServicesMap> = new InjectionToken<LazyDataServicesMap>('APP_DATA_SERVICES_MAP');
|
||||
|
Reference in New Issue
Block a user