UNPKG

rage-framework

Version:

## How to Install *server-side* ```bash npm install @rage-framework/server ```

123 lines 6.31 kB
"use strict"; var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; return c > 3 && r && Object.defineProperty(target, key, r), r; }; var __metadata = (this && this.__metadata) || function (k, v) { if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v); }; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.ApplicationService = void 0; const rxjs_1 = require("rxjs"); const container_1 = require("../container"); const logger_service_1 = require("./utils/logger.service"); const cache_layer_service_1 = require("./cache/cache-layer.service"); const events_1 = require("../helpers/events"); const lazy_factory_service_1 = require("./utils/lazy-factory.service"); const config_service_1 = require("./config/config.service"); const operators_1 = require("rxjs/operators"); const behaviors_service_1 = require("./core/behaviors.service"); const services_service_1 = require("./core/services.service"); const service_1 = require("../decorators/service"); let ApplicationService = class ApplicationService { constructor(logger, cacheService, lazyFactoriesService, configService, behaviorsService, servicesService) { this.logger = logger; this.cacheService = cacheService; this.lazyFactoriesService = lazyFactoriesService; this.configService = configService; this.behaviorsService = behaviorsService; this.servicesService = servicesService; this.globalConfig = this.cacheService.createLayer({ name: events_1.InternalLayers.globalConfig }); } start(app, config) { this.configService.setConfig(config); this.globalConfig.putItem({ key: events_1.InternalEvents.config, data: config }); container_1.Container.get(app); const lazyFactoryKeys = Array.from(this.lazyFactoriesService.lazyFactories.keys()); return rxjs_1.of(lazyFactoryKeys).pipe(operators_1.map(factories => this.prepareAsyncChainables(factories)), operators_1.switchMap(res => rxjs_1.combineLatest(res).pipe(operators_1.take(1), operators_1.map(c => this.attachLazyLoadedChainables(lazyFactoryKeys, c)), operators_1.map(() => this.validateSystem()), operators_1.switchMap(() => rxjs_1.combineLatest(this.asyncChainableServices())), operators_1.switchMap(() => rxjs_1.combineLatest(this.asyncChainableBehaviors())), operators_1.map(() => this.loadApplication()), operators_1.map(() => this.final())))); } final() { return container_1.Container; } asyncChainableBehaviors() { return [ rxjs_1.of(true), ...this.behaviorsService .getComponents() .map((c) => __awaiter(this, void 0, void 0, function* () { return yield container_1.Container.get(c); })) ]; } asyncChainableServices() { return [ rxjs_1.of(true), ...this.servicesService .getServices() .map((c) => __awaiter(this, void 0, void 0, function* () { return yield container_1.Container.get(c); })) ]; } prepareAsyncChainables(injectables) { const asynChainables = [rxjs_1.of(true)]; const injectableLog = {}; const getName = n => n.name || n; injectables.map(i => { const date = Date.now(); injectableLog[getName(i)] = { started: date, end: null }; this.logger.log(`Bootstrap -> @Service('${getName(i)}'): loading...`); const somethingAsync = rxjs_1.from((this.lazyFactoriesService.getLazyFactory(i))).pipe(operators_1.shareReplay(1)); asynChainables.push(somethingAsync); somethingAsync.subscribe(() => { this.logger.log(`Bootstrap -> @Service('${getName(i)}'): loading finished after ${Date.now() - injectableLog[getName(i)].started}ms !`); delete injectableLog[getName(i)]; }); }); return asynChainables; } validateSystem() { if (this.configService.config.strict) { this.cacheService.searchForDuplicateDependenciesInsideApp(); } } attachLazyLoadedChainables(res, chainables) { chainables.splice(0, 1); let count = 0; res.map(name => { container_1.Container.set(name, chainables[count++]); }); return true; } loadApplication() { Array.from(this.cacheService.getLayer(events_1.InternalLayers.modules).map.keys()).forEach(m => this.cacheService.getLayer(m).putItem({ key: events_1.InternalEvents.load, data: true })); return true; } }; ApplicationService = __decorate([ service_1.Service(), __metadata("design:paramtypes", [logger_service_1.LoggerService, cache_layer_service_1.CacheService, lazy_factory_service_1.LazyFactory, config_service_1.ConfigService, behaviors_service_1.BehaviorsService, services_service_1.ServicesService]) ], ApplicationService); exports.ApplicationService = ApplicationService; //# sourceMappingURL=application.service.js.map