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 {
|
import {
|
||||||
Inject,
|
Inject,
|
||||||
Injectable,
|
Injectable,
|
||||||
InjectionToken,
|
|
||||||
Injector,
|
Injector,
|
||||||
} from '@angular/core';
|
} from '@angular/core';
|
||||||
import {
|
import {
|
||||||
@@ -25,7 +24,7 @@ import { FollowLinkConfig } from '../../../shared/utils/follow-link-config.model
|
|||||||
import { HALDataService } from '../../data/base/hal-data-service.interface';
|
import { HALDataService } from '../../data/base/hal-data-service.interface';
|
||||||
import { PaginatedList } from '../../data/paginated-list.model';
|
import { PaginatedList } from '../../data/paginated-list.model';
|
||||||
import { RemoteData } from '../../data/remote-data';
|
import { RemoteData } from '../../data/remote-data';
|
||||||
import { lazyService } from '../../lazy-service';
|
import { lazyDataService } from '../../lazy-data-service';
|
||||||
import { GenericConstructor } from '../../shared/generic-constructor';
|
import { GenericConstructor } from '../../shared/generic-constructor';
|
||||||
import { HALResource } from '../../shared/hal-resource.model';
|
import { HALResource } from '../../shared/hal-resource.model';
|
||||||
import {
|
import {
|
||||||
@@ -43,7 +42,7 @@ export class LinkService {
|
|||||||
|
|
||||||
constructor(
|
constructor(
|
||||||
protected injector: Injector,
|
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_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>>,
|
@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>>> {
|
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);
|
const matchingLinkDef = this.getLinkDefinition(model.constructor, linkToFollow.name);
|
||||||
if (hasValue(matchingLinkDef)) {
|
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(
|
return lazyProvider$.pipe(
|
||||||
switchMap((provider: HALDataService<any>) => {
|
switchMap((provider: HALDataService<any>) => {
|
||||||
const link = model._links[matchingLinkDef.linkName];
|
const link = model._links[matchingLinkDef.linkName];
|
||||||
|
@@ -7,27 +7,32 @@ import {
|
|||||||
Observable,
|
Observable,
|
||||||
} from 'rxjs';
|
} from 'rxjs';
|
||||||
|
|
||||||
|
import { LazyDataServicesMap } from '../../config/app-config.interface';
|
||||||
import { isNotEmpty } from '../shared/empty.util';
|
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.
|
* 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.
|
* @param injector The injector to use to load the service. If not provided, the current injector is used.
|
||||||
* @returns An observable of the service.
|
* @returns An observable of the service.
|
||||||
*
|
*
|
||||||
* @example
|
* @example
|
||||||
* ```ts
|
* ```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
|
* 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>(
|
export function lazyDataService<T>(
|
||||||
loader: () => Promise<Type<T>> | Promise<{ default: Type<T> }>,
|
map: LazyDataServicesMap,
|
||||||
|
key: string,
|
||||||
injector: Injector,
|
injector: Injector,
|
||||||
): Observable<T> {
|
): Observable<T> {
|
||||||
return defer(() => {
|
return defer(() => {
|
||||||
|
const loader: () => Promise<Type<HALDataService<any>> | { default: HALDataService<any> }> = map[key];
|
||||||
if (isNotEmpty(loader) && typeof loader === 'function') {
|
if (isNotEmpty(loader) && typeof loader === 'function') {
|
||||||
return loader()
|
return loader()
|
||||||
.then((serviceOrDefault) => {
|
.then((serviceOrDefault) => {
|
@@ -7,7 +7,6 @@ import {
|
|||||||
ChangeDetectorRef,
|
ChangeDetectorRef,
|
||||||
Component,
|
Component,
|
||||||
Inject,
|
Inject,
|
||||||
InjectionToken,
|
|
||||||
Injector,
|
Injector,
|
||||||
Input,
|
Input,
|
||||||
OnDestroy,
|
OnDestroy,
|
||||||
@@ -42,7 +41,7 @@ import {
|
|||||||
import { ArrayMoveChangeAnalyzer } from '../../core/data/array-move-change-analyzer.service';
|
import { ArrayMoveChangeAnalyzer } from '../../core/data/array-move-change-analyzer.service';
|
||||||
import { RemoteData } from '../../core/data/remote-data';
|
import { RemoteData } from '../../core/data/remote-data';
|
||||||
import { UpdateDataService } from '../../core/data/update-data.service';
|
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 { DSpaceObject } from '../../core/shared/dspace-object.model';
|
||||||
import { getFirstCompletedRemoteData } from '../../core/shared/operators';
|
import { getFirstCompletedRemoteData } from '../../core/shared/operators';
|
||||||
import { ResourceType } from '../../core/shared/resource-type';
|
import { ResourceType } from '../../core/shared/resource-type';
|
||||||
@@ -152,7 +151,7 @@ export class DsoEditMetadataComponent implements OnInit, OnDestroy {
|
|||||||
protected parentInjector: Injector,
|
protected parentInjector: Injector,
|
||||||
protected arrayMoveChangeAnalyser: ArrayMoveChangeAnalyzer<number>,
|
protected arrayMoveChangeAnalyser: ArrayMoveChangeAnalyzer<number>,
|
||||||
protected cdr: ChangeDetectorRef,
|
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>> {
|
retrieveDataService(): Observable<UpdateDataService<DSpaceObject>> {
|
||||||
if (hasNoValue(this.updateDataService)) {
|
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$;
|
return lazyProvider$;
|
||||||
} else {
|
} else {
|
||||||
return EMPTY;
|
return EMPTY;
|
||||||
|
@@ -7,7 +7,6 @@ import {
|
|||||||
Component,
|
Component,
|
||||||
EventEmitter,
|
EventEmitter,
|
||||||
Inject,
|
Inject,
|
||||||
InjectionToken,
|
|
||||||
Injector,
|
Injector,
|
||||||
Input,
|
Input,
|
||||||
OnDestroy,
|
OnDestroy,
|
||||||
@@ -35,7 +34,7 @@ import { EPersonDataService } from '../../core/eperson/eperson-data.service';
|
|||||||
import { GroupDataService } from '../../core/eperson/group-data.service';
|
import { GroupDataService } from '../../core/eperson/group-data.service';
|
||||||
import { EPERSON } from '../../core/eperson/models/eperson.resource-type';
|
import { EPERSON } from '../../core/eperson/models/eperson.resource-type';
|
||||||
import { GROUP } from '../../core/eperson/models/group.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 { PaginationService } from '../../core/pagination/pagination.service';
|
||||||
import { DSpaceObject } from '../../core/shared/dspace-object.model';
|
import { DSpaceObject } from '../../core/shared/dspace-object.model';
|
||||||
import { getFirstCompletedRemoteData } from '../../core/shared/operators';
|
import { getFirstCompletedRemoteData } from '../../core/shared/operators';
|
||||||
@@ -133,7 +132,7 @@ export class EpersonGroupListComponent implements OnInit, OnDestroy {
|
|||||||
constructor(public dsoNameService: DSONameService,
|
constructor(public dsoNameService: DSONameService,
|
||||||
private parentInjector: Injector,
|
private parentInjector: Injector,
|
||||||
private paginationService: PaginationService,
|
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 {
|
ngOnInit(): void {
|
||||||
const resourceType: ResourceType = (this.isListOfEPerson) ? EPERSON : GROUP;
|
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) => {
|
lazyProvider$.subscribe((dataService: EPersonDataService | GroupDataService) => {
|
||||||
this.dataService = dataService;
|
this.dataService = dataService;
|
||||||
console.log(dataService);
|
console.log(dataService);
|
||||||
|
@@ -1,6 +1,5 @@
|
|||||||
import {
|
import {
|
||||||
inject,
|
inject,
|
||||||
InjectionToken,
|
|
||||||
Injector,
|
Injector,
|
||||||
} from '@angular/core';
|
} from '@angular/core';
|
||||||
import {
|
import {
|
||||||
@@ -12,10 +11,13 @@ import {
|
|||||||
import { Observable } from 'rxjs';
|
import { Observable } from 'rxjs';
|
||||||
import { switchMap } from 'rxjs/operators';
|
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 { IdentifiableDataService } from '../../../core/data/base/identifiable-data.service';
|
||||||
import { RemoteData } from '../../../core/data/remote-data';
|
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 { DSpaceObject } from '../../../core/shared/dspace-object.model';
|
||||||
import { getFirstCompletedRemoteData } from '../../../core/shared/operators';
|
import { getFirstCompletedRemoteData } from '../../../core/shared/operators';
|
||||||
import { ResourceType } from '../../../core/shared/resource-type';
|
import { ResourceType } from '../../../core/shared/resource-type';
|
||||||
@@ -34,7 +36,7 @@ import { isEmpty } from '../../empty.util';
|
|||||||
export const resourcePolicyTargetResolver: ResolveFn<RemoteData<DSpaceObject>> = (
|
export const resourcePolicyTargetResolver: ResolveFn<RemoteData<DSpaceObject>> = (
|
||||||
route: ActivatedRouteSnapshot,
|
route: ActivatedRouteSnapshot,
|
||||||
state: RouterStateSnapshot,
|
state: RouterStateSnapshot,
|
||||||
dataServiceMap: InjectionToken<LazyDataServicesMap> = inject(InjectionToken<LazyDataServicesMap>),
|
dataServiceMap: LazyDataServicesMap = inject(APP_DATA_SERVICES_MAP),
|
||||||
parentInjector: Injector = inject(Injector),
|
parentInjector: Injector = inject(Injector),
|
||||||
router: Router = inject(Router),
|
router: Router = inject(Router),
|
||||||
): Observable<RemoteData<DSpaceObject>> => {
|
): Observable<RemoteData<DSpaceObject>> => {
|
||||||
@@ -46,7 +48,7 @@ export const resourcePolicyTargetResolver: ResolveFn<RemoteData<DSpaceObject>> =
|
|||||||
}
|
}
|
||||||
|
|
||||||
const resourceType: ResourceType = new ResourceType(targetType);
|
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(
|
return lazyProvider$.pipe(
|
||||||
switchMap((dataService: IdentifiableDataService<DSpaceObject>) => {
|
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');
|
const APP_CONFIG_STATE = makeStateKey<AppConfig>('APP_CONFIG_STATE');
|
||||||
|
|
||||||
export interface LazyDataServicesMap {
|
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');
|
export const APP_DATA_SERVICES_MAP: InjectionToken<LazyDataServicesMap> = new InjectionToken<LazyDataServicesMap>('APP_DATA_SERVICES_MAP');
|
||||||
|
Reference in New Issue
Block a user