Skip to content

Commit 2a3a13e

Browse files
committed
[DURACOM-247] Refactored by providing the map of promises
1 parent aac6178 commit 2a3a13e

6 files changed

Lines changed: 27 additions & 23 deletions

File tree

src/app/core/cache/builders/link.service.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import {
22
Inject,
33
Injectable,
4-
InjectionToken,
54
Injector,
65
} from '@angular/core';
76
import {
@@ -25,7 +24,7 @@ import { FollowLinkConfig } from '../../../shared/utils/follow-link-config.model
2524
import { HALDataService } from '../../data/base/hal-data-service.interface';
2625
import { PaginatedList } from '../../data/paginated-list.model';
2726
import { RemoteData } from '../../data/remote-data';
28-
import { lazyService } from '../../lazy-service';
27+
import { lazyDataService } from '../../lazy-data-service';
2928
import { GenericConstructor } from '../../shared/generic-constructor';
3029
import { HALResource } from '../../shared/hal-resource.model';
3130
import {
@@ -43,7 +42,7 @@ export class LinkService {
4342

4443
constructor(
4544
protected injector: Injector,
46-
@Inject(APP_DATA_SERVICES_MAP) private map: InjectionToken<LazyDataServicesMap>,
45+
@Inject(APP_DATA_SERVICES_MAP) private map: LazyDataServicesMap,
4746
@Inject(LINK_DEFINITION_FACTORY) private getLinkDefinition: <T extends HALResource>(source: GenericConstructor<T>, linkName: keyof T['_links']) => LinkDefinition<T>,
4847
@Inject(LINK_DEFINITION_MAP_FACTORY) private getLinkDefinitions: <T extends HALResource>(source: GenericConstructor<T>) => Map<keyof T['_links'], LinkDefinition<T>>,
4948
) {
@@ -73,7 +72,7 @@ export class LinkService {
7372
public resolveLinkWithoutAttaching<T extends HALResource, U extends HALResource>(model, linkToFollow: FollowLinkConfig<T>): Observable<RemoteData<U | PaginatedList<U>>> {
7473
const matchingLinkDef = this.getLinkDefinition(model.constructor, linkToFollow.name);
7574
if (hasValue(matchingLinkDef)) {
76-
const lazyProvider$: Observable<HALDataService<any>> = lazyService(this.map[matchingLinkDef.resourceType.value], this.injector);
75+
const lazyProvider$: Observable<HALDataService<any>> = lazyDataService(this.map, matchingLinkDef.resourceType.value, this.injector);
7776
return lazyProvider$.pipe(
7877
switchMap((provider: HALDataService<any>) => {
7978
const link = model._links[matchingLinkDef.linkName];
Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,27 +7,32 @@ import {
77
Observable,
88
} from 'rxjs';
99

10+
import { LazyDataServicesMap } from '../../config/app-config.interface';
1011
import { isNotEmpty } from '../shared/empty.util';
12+
import { HALDataService } from './data/base/hal-data-service.interface';
1113

1214
/**
1315
* Loads a service lazily. The service is loaded when the observable is subscribed to.
1416
*
15-
* @param loader A function that returns a promise of the service to load.
17+
* @param map A map of promises returning the data services to load
18+
* @param key The key of the service
1619
* @param injector The injector to use to load the service. If not provided, the current injector is used.
1720
* @returns An observable of the service.
1821
*
1922
* @example
2023
* ```ts
21-
* const dataService$ = lazyService(() => import('./data-service').then((m) => m.MyService), this.injector);
24+
* const dataService$ = lazyDataService({ 'data-service': () => import('./data-service').then((m) => m.MyService)}, 'data-service', this.injector);
2225
* or
23-
* const dataService$ = lazyService(() => import('./data-service'), this.injector);
26+
* const dataService$ = lazyDataService({'data-service': () => import('./data-service')}, 'data-service', this.injector);
2427
* ```
2528
*/
26-
export function lazyService<T>(
27-
loader: () => Promise<Type<T>> | Promise<{ default: Type<T> }>,
29+
export function lazyDataService<T>(
30+
map: LazyDataServicesMap,
31+
key: string,
2832
injector: Injector,
2933
): Observable<T> {
3034
return defer(() => {
35+
const loader: () => Promise<Type<HALDataService<any>> | { default: HALDataService<any> }> = map[key];
3136
if (isNotEmpty(loader) && typeof loader === 'function') {
3237
return loader()
3338
.then((serviceOrDefault) => {

src/app/dso-shared/dso-edit-metadata/dso-edit-metadata.component.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import {
77
ChangeDetectorRef,
88
Component,
99
Inject,
10-
InjectionToken,
1110
Injector,
1211
Input,
1312
OnDestroy,
@@ -42,7 +41,7 @@ import {
4241
import { ArrayMoveChangeAnalyzer } from '../../core/data/array-move-change-analyzer.service';
4342
import { RemoteData } from '../../core/data/remote-data';
4443
import { UpdateDataService } from '../../core/data/update-data.service';
45-
import { lazyService } from '../../core/lazy-service';
44+
import { lazyDataService } from '../../core/lazy-data-service';
4645
import { DSpaceObject } from '../../core/shared/dspace-object.model';
4746
import { getFirstCompletedRemoteData } from '../../core/shared/operators';
4847
import { ResourceType } from '../../core/shared/resource-type';
@@ -152,7 +151,7 @@ export class DsoEditMetadataComponent implements OnInit, OnDestroy {
152151
protected parentInjector: Injector,
153152
protected arrayMoveChangeAnalyser: ArrayMoveChangeAnalyzer<number>,
154153
protected cdr: ChangeDetectorRef,
155-
@Inject(APP_DATA_SERVICES_MAP) private dataServiceMap: InjectionToken<LazyDataServicesMap>) {
154+
@Inject(APP_DATA_SERVICES_MAP) private dataServiceMap: LazyDataServicesMap) {
156155
}
157156

158157
/**
@@ -186,7 +185,7 @@ export class DsoEditMetadataComponent implements OnInit, OnDestroy {
186185
*/
187186
retrieveDataService(): Observable<UpdateDataService<DSpaceObject>> {
188187
if (hasNoValue(this.updateDataService)) {
189-
const lazyProvider$: Observable<UpdateDataService<DSpaceObject>> = lazyService(this.dataServiceMap[this.dsoType], this.parentInjector);
188+
const lazyProvider$: Observable<UpdateDataService<DSpaceObject>> = lazyDataService(this.dataServiceMap, this.dsoType, this.parentInjector);
190189
return lazyProvider$;
191190
} else {
192191
return EMPTY;

src/app/shared/eperson-group-list/eperson-group-list.component.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import {
77
Component,
88
EventEmitter,
99
Inject,
10-
InjectionToken,
1110
Injector,
1211
Input,
1312
OnDestroy,
@@ -35,7 +34,7 @@ import { EPersonDataService } from '../../core/eperson/eperson-data.service';
3534
import { GroupDataService } from '../../core/eperson/group-data.service';
3635
import { EPERSON } from '../../core/eperson/models/eperson.resource-type';
3736
import { GROUP } from '../../core/eperson/models/group.resource-type';
38-
import { lazyService } from '../../core/lazy-service';
37+
import { lazyDataService } from '../../core/lazy-data-service';
3938
import { PaginationService } from '../../core/pagination/pagination.service';
4039
import { DSpaceObject } from '../../core/shared/dspace-object.model';
4140
import { getFirstCompletedRemoteData } from '../../core/shared/operators';
@@ -133,15 +132,15 @@ export class EpersonGroupListComponent implements OnInit, OnDestroy {
133132
constructor(public dsoNameService: DSONameService,
134133
private parentInjector: Injector,
135134
private paginationService: PaginationService,
136-
@Inject(APP_DATA_SERVICES_MAP) private dataServiceMap: InjectionToken<LazyDataServicesMap>) {
135+
@Inject(APP_DATA_SERVICES_MAP) private dataServiceMap: LazyDataServicesMap) {
137136
}
138137

139138
/**
140139
* Initialize the component
141140
*/
142141
ngOnInit(): void {
143142
const resourceType: ResourceType = (this.isListOfEPerson) ? EPERSON : GROUP;
144-
const lazyProvider$: Observable<EPersonDataService | GroupDataService> = lazyService(this.dataServiceMap[resourceType.value], this.parentInjector);
143+
const lazyProvider$: Observable<EPersonDataService | GroupDataService> = lazyDataService(this.dataServiceMap, resourceType.value, this.parentInjector);
145144
lazyProvider$.subscribe((dataService: EPersonDataService | GroupDataService) => {
146145
this.dataService = dataService;
147146
console.log(dataService);

src/app/shared/resource-policies/resolvers/resource-policy-target.resolver.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import {
22
inject,
3-
InjectionToken,
43
Injector,
54
} from '@angular/core';
65
import {
@@ -12,10 +11,13 @@ import {
1211
import { Observable } from 'rxjs';
1312
import { switchMap } from 'rxjs/operators';
1413

15-
import { LazyDataServicesMap } from '../../../../config/app-config.interface';
14+
import {
15+
APP_DATA_SERVICES_MAP,
16+
LazyDataServicesMap,
17+
} from '../../../../config/app-config.interface';
1618
import { IdentifiableDataService } from '../../../core/data/base/identifiable-data.service';
1719
import { RemoteData } from '../../../core/data/remote-data';
18-
import { lazyService } from '../../../core/lazy-service';
20+
import { lazyDataService } from '../../../core/lazy-data-service';
1921
import { DSpaceObject } from '../../../core/shared/dspace-object.model';
2022
import { getFirstCompletedRemoteData } from '../../../core/shared/operators';
2123
import { ResourceType } from '../../../core/shared/resource-type';
@@ -34,7 +36,7 @@ import { isEmpty } from '../../empty.util';
3436
export const resourcePolicyTargetResolver: ResolveFn<RemoteData<DSpaceObject>> = (
3537
route: ActivatedRouteSnapshot,
3638
state: RouterStateSnapshot,
37-
dataServiceMap: InjectionToken<LazyDataServicesMap> = inject(InjectionToken<LazyDataServicesMap>),
39+
dataServiceMap: LazyDataServicesMap = inject(APP_DATA_SERVICES_MAP),
3840
parentInjector: Injector = inject(Injector),
3941
router: Router = inject(Router),
4042
): Observable<RemoteData<DSpaceObject>> => {
@@ -46,7 +48,7 @@ export const resourcePolicyTargetResolver: ResolveFn<RemoteData<DSpaceObject>> =
4648
}
4749

4850
const resourceType: ResourceType = new ResourceType(targetType);
49-
const lazyProvider$: Observable<IdentifiableDataService<DSpaceObject>> = lazyService(dataServiceMap[resourceType.value], parentInjector);
51+
const lazyProvider$: Observable<IdentifiableDataService<DSpaceObject>> = lazyDataService(dataServiceMap, resourceType.value, parentInjector);
5052

5153
return lazyProvider$.pipe(
5254
switchMap((dataService: IdentifiableDataService<DSpaceObject>) => {

src/config/app-config.interface.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ const APP_CONFIG = new InjectionToken<AppConfig>('APP_CONFIG');
7474
const APP_CONFIG_STATE = makeStateKey<AppConfig>('APP_CONFIG_STATE');
7575

7676
export interface LazyDataServicesMap {
77-
[type: string]: () => Promise<Type<HALDataService<any>>>
77+
[type: string]: () => Promise<Type<HALDataService<any>> | { default: HALDataService<any> }>
7878
}
7979

8080
export const APP_DATA_SERVICES_MAP: InjectionToken<LazyDataServicesMap> = new InjectionToken<LazyDataServicesMap>('APP_DATA_SERVICES_MAP');

0 commit comments

Comments
 (0)