### File

`src/lib/application.ts`

### Index

###### Properties

- Public app
- Public config
- Public Readonly Optional configLoadOptions
- Public Readonly environment
- Public logger
- Public Readonly options

###### Methods

- Public after
- Public before
- Public Async bootstrap
- Protected Abstract create
- Protected Async handleAfter
- Protected Async handleBefore
- Protected Async loadConfig
- Protected Abstract prepareConfig
- Protected Async prepareEnvironment

### Constructor

`constructor(environment: Environment, options: Config, configLoadOptions?: ConfigLoadOptions)`
Defined in src/lib/application.ts:39

Parameters :

Name | Type | Optional
environment | `Environment` | No
options | `Config` | No
configLoadOptions | `ConfigLoadOptions` | Yes

### Properties

Public app
Type : `Ref | null`
Default value : `null`
Defined in src/lib/application.ts:35

Public config
Type : `ConfigService | null`
Default value : `null`
Defined in src/lib/application.ts:37

Public Readonly Optional configLoadOptions
Type : `ConfigLoadOptions`
Defined in src/lib/application.ts:44

Public Readonly environment
Type : `Environment`
Defined in src/lib/application.ts:42

Public logger
Type : `NGXLogger | null`
Default value : `null`
Defined in src/lib/application.ts:36

Public Readonly options
Type : `Config`
Default value : `{} as any`
Defined in src/lib/application.ts:43

### Methods

Public after
`after(fnc: ApplicationAfterFunction<Ref>)`
Defined in src/lib/application.ts:86

Parameters :

Name | Type | Optional
fnc | `ApplicationAfterFunction<Ref>` | No

Returns : `void`

Public before
`before(fnc: ApplicationBeforeFunction<Config>)`
Defined in src/lib/application.ts:82

Parameters :

Name | Type | Optional
fnc | `ApplicationBeforeFunction<Config>` | No

Returns : `void`

Public Async bootstrap
`bootstrap()`
Defined in src/lib/application.ts:47
Returns : `any`

Protected Abstract create
`create()`
Defined in src/lib/application.ts:112
Returns : `Promise<Ref>`

Protected Async handleAfter
`handleAfter(app: Ref, logger: NGXLogger, config: ConfigService)`
Defined in src/lib/application.ts:96

Parameters :

Name | Type | Optional
app | `Ref` | No
logger | `NGXLogger` | No
config | `ConfigService` | No

Returns : `any`

Protected Async handleBefore
`handleBefore(config: ConfigService, options: Config)`
Defined in src/lib/application.ts:90

Parameters :

Name | Type | Optional
config | `ConfigService` | No
options | `Config` | No

Returns : `any`

Protected Async loadConfig
`loadConfig(environment: Environment)`
Defined in src/lib/application.ts:102

Parameters :

Name | Type | Optional
environment | `Environment` | No

Returns : `any`

Protected Abstract prepareConfig
`prepareConfig(config: Config)`
Defined in src/lib/application.ts:110

Parameters :

Name | Type | Optional
config | `Config` | No

Returns : `void`

Protected Async prepareEnvironment
`prepareEnvironment(environment: Environment)`
Defined in src/lib/application.ts:106

Parameters :

Name | Type | Optional
environment | `Environment` | No

Returns : `any`

    import { EnvironmentInjector } from '@angular/core';
    import {
      ConfigLoadOptions,
      ConfigService,
    } from '@rxap/config';
    import {
      Environment,
      UpdateEnvironment,
    } from '@rxap/environment';
    import { NGXLogger } from 'ngx-logger';

    /**
     * @param ...args Arbitrary arguments
     */
    export type ApplicationBeforeFunction<Config extends object> = (
      config: ConfigService,
      options: Config,
    ) => any | Promise<any>;

    /**
     * The common interface of ApplicationRef and ModuleRef
     */
    export interface RefWithInjector {
      readonly injector: EnvironmentInjector;
    }

    export type ApplicationAfterFunction<Ref extends RefWithInjector> = (
      app: Ref,
      config: ConfigService,
      logger: NGXLogger,
    ) => any | Promise<any>;

    export abstract class Application<Config extends object, Ref extends RefWithInjector> {

      public app: Ref | null = null;
      public logger: NGXLogger | null = null;
      public config: ConfigService | null = null;
      private _beforeList: ApplicationBeforeFunction<Config>[] = [];
      private _afterList: ApplicationAfterFunction<Ref>[] = [];

      constructor(
        public readonly environment: Environment,
        public readonly options: Config = {} as any,
        public readonly configLoadOptions?: ConfigLoadOptions,
      ) {}

      public async bootstrap() {

        await this.prepareEnvironment(this.environment);

        await this.loadConfig(this.environment);

        this.prepareConfig(this.options);

        await this.handleBefore(new ConfigService(ConfigService.Config), this.options);

        try {
          this.app = await this.create();
        } catch (e: any) {
          throw new Error(`Angular app creation failed: ${ e.message }`);
        }

        if (!this.app) {
          throw new Error('Angular app creation failed');
        }

        this.logger = this.app.injector.get(NGXLogger);
        this.config = this.app.injector.get(ConfigService);

        if (!this.logger) {
          throw new Error('Could not inject a NGXLogger instance');
        }

        if (!this.config) {
          throw new Error('Could not inject a ConfigService instance');
        }

        await this.handleAfter(this.app, this.logger, this.config);

      }

      public before(fnc: ApplicationBeforeFunction<Config>) {
        this._beforeList.push(fnc);
      }

      public after(fnc: ApplicationAfterFunction<Ref>) {
        this._afterList.push(fnc);
      }

      protected async handleBefore(config: ConfigService, options: Config) {
        for (const before of this._beforeList) {
          await before(config, options);
        }
      }

      protected async handleAfter(app: Ref, logger: NGXLogger, config: ConfigService) {
        for (const after of this._afterList) {
          await after(app, config, logger);
        }
      }

      protected async loadConfig(environment: Environment) {
        await ConfigService.Load(this.configLoadOptions, environment);
      }

      protected async prepareEnvironment(environment: Environment) {
        await UpdateEnvironment(environment);
      }

      protected abstract prepareConfig(config: Config): void;

      protected abstract create(): Promise<Ref>;

    }

# results matching ""

# No results matching ""