All files / services/resolver resolver.service.ts

51.52% Statements 17/33
22.22% Branches 4/18
33.33% Functions 2/6
48.39% Lines 15/31

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 531x 1x 1x 1x 1x 1x 1x     1x   1x 1x     1x 1x 1x 1x                           1x                                        
import { Service, Container, Inject } from '../../container';
import { CacheService } from '../cache/cache-layer.service';
import { InternalLayers, InternalEvents } from '../../helpers/events';
import { switchMap, filter, map } from 'rxjs/operators';
import { of, Observable } from 'rxjs';
import { BootstrapLogger } from '../bootstrap-logger/bootstrap-logger';
import { Injector } from '../../decorators/injector/injector.decorator';
 
@Service()
export class ResolverService {
 
    @Injector(BootstrapLogger) private bootstrapLogger: BootstrapLogger;
    @Injector(CacheService) private cacheService: CacheService;
 
    resolveDependencies(hash, target, moduleName): Observable<any[]> {
        this.cacheService.getLayer(InternalLayers.modules).putItem({ key: hash, data: target });
        const currentModule = this.cacheService.getLayer(hash);
        currentModule.putItem({ key: InternalEvents.config, data: { moduleName, moduleHash: hash } });
        return currentModule.getItemObservable(InternalEvents.load)
            .pipe(
                switchMap((config) => {
                    if (!config.data) {
                        return of(null);
                    }
                    return currentModule.items.asObservable();
                }),
                filter((res) => res && res.length),
                map(this.resolveContainerDependencies(target, moduleName))
            );
    }
 
    private resolveContainerDependencies(target, moduleName: string) {
        return (res) => {
            res.forEach((i) => {
                if (i.key === InternalEvents.load || i.key === InternalEvents.config) {
                    return;
                }
                const found = this.cacheService.searchForItem(i.data);
                if (found) {
                    if (found.provide) {
                        return found;
                    }
                    const moduleType = found.metadata.type.charAt(0).toUpperCase() +  found.metadata.type.slice(1);
                    this.bootstrapLogger.log(`Start -> @Module('${moduleName}')${this.bootstrapLogger.logHashes(`(${target.name})`)}: @${moduleType}('${found.originalName}')${this.bootstrapLogger.logHashes(`(${found.name})`)}` + ' initialized!');
                    return Container.get(found);
                } else {
                    throw new Error('not found');
                }
            });
            return res;
        };
    }
}