Skip to content

Commit

Permalink
issue #4 - fix function calls in decorator issue
Browse files Browse the repository at this point in the history
  • Loading branch information
kmanaseryan committed Jan 25, 2019
1 parent eea7c5c commit 9ea5ada
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 12 deletions.
19 changes: 9 additions & 10 deletions src/config.module.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,16 @@
import { Injectable, NgModule, ModuleWithProviders, APP_INITIALIZER, SkipSelf, Optional } from '@angular/core';
import { NgModule, ModuleWithProviders, APP_INITIALIZER, SkipSelf, Optional } from '@angular/core';
import { CommonModule } from '@angular/common';
import { HttpClientModule } from '@angular/common/http';

import { ConfigService } from './config.service';
import { EnvConfig } from './env-config';

let _env: EnvConfig;

export function ConfigFactory(config: ConfigService) {
const res = () => config.load(_env);
export function ConfigFactory(config: ConfigService, env: EnvConfig) {
const res = () => config.load(env);
return res;
}



@NgModule({
imports: [
CommonModule,
Expand All @@ -24,25 +21,27 @@ export function ConfigFactory(config: ConfigService) {
{
provide: APP_INITIALIZER,
useFactory: ConfigFactory,
deps: [ConfigService],
deps: [ConfigService, EnvConfig],
multi: true
}
],
declarations: []
})

export class ConfigModule {
constructor( @Optional() @SkipSelf() parentModule: ConfigModule) {
constructor(@Optional() @SkipSelf() parentModule: ConfigModule) {
if (parentModule) {
throw new Error(
'ConfigModule is already loaded. Import it in the AppModule only');
}
}

static forRoot(env: EnvConfig): ModuleWithProviders {
_env = env;
return {
ngModule: ConfigModule
ngModule: ConfigModule,
providers: [
{ provide: EnvConfig, useValue: env }
]
};
}
}
Expand Down
1 change: 0 additions & 1 deletion src/config.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ export class ConfigService {

load(env: EnvConfig) {
return new Promise((resolve, reject) => {
console.log(this._env)
this._env = env.state;
this._fallbackDev = env.fallbackDev || false;

Expand Down
2 changes: 1 addition & 1 deletion src/env-config.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

export interface EnvConfig {
export class EnvConfig {
state: string;
fallbackDev?: boolean;
}

0 comments on commit 9ea5ada

Please sign in to comment.