Skip to content

Commit

Permalink
Merge pull request #104 from Tolowe/#58-provider-refactor
Browse files Browse the repository at this point in the history
feat: changed provider interface to merged interfaces. fixed mispell …
  • Loading branch information
basedalexander authored Feb 25, 2018
2 parents 327e534 + c39a6d5 commit ac23a57
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 22 deletions.
27 changes: 8 additions & 19 deletions src/lib/container.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,8 @@ export class Container implements IContainer {
}

public register(provider: RegistrationProvider|RegistrationProvider[]): void {
provider = this.nornalizeProvider(provider);

if (Array.isArray(provider)) {
this.registerAll(<RegistrationProvider[]> provider);
} else {
provider = this.nornalizeProvider(provider);
this.registerOne(<IProvider> provider);
}
const normalizedProvider = this.normalizeProvider(provider);
this.registerAll(normalizedProvider);
}

public resolve(token: ProviderToken): IInjectionInstance {
Expand Down Expand Up @@ -77,11 +71,11 @@ export class Container implements IContainer {
return instance;
}

private registerAll(providers: RegistrationProvider[]): void {
private registerAll(providers: IProvider[]): void {
providers.forEach((p: IProvider) => this.registerOne(p));
}

private registerOne(provider: IProvider) {
private registerOne(provider: IProvider): void {
const registryData: IRegistryData = new RegistryData();

if (provider.useValue) {
Expand Down Expand Up @@ -147,15 +141,10 @@ export class Container implements IContainer {
}
}

private nornalizeProvider(provider: RegistrationProvider|RegistrationProvider[]): IProvider {
let normalizedProvider: any;

if (Array.isArray(provider)) {
normalizedProvider = provider.map<IProvider>((p: IProvider|IConstructor) => this.normalizeSingleProvider(p));
} else {
normalizedProvider = this.normalizeSingleProvider(provider);
}
return normalizedProvider;
private normalizeProvider(provider: RegistrationProvider|RegistrationProvider[]): IProvider[] {
return Array.isArray(provider)
? provider.map(p => this.normalizeSingleProvider(p))
: [ this.normalizeSingleProvider(provider) ];
}

private normalizeSingleProvider(provider: RegistrationProvider): IProvider {
Expand Down
15 changes: 12 additions & 3 deletions src/lib/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,20 @@ export enum LifeTime {

export interface IProvider {
token: ProviderToken;
useClass?: IConstructor;
lifeTime?: LifeTime;
}

export interface IProvider {
useValue?: any;
}

export interface IProvider {
useClass?: IConstructor;
}

export interface IProvider {
useFactory?: any;
inject?: ProviderToken[];
lifeTime?: LifeTime;
inject?: ProviderToken[];
}

export interface IInjectionMd {
Expand Down

0 comments on commit ac23a57

Please sign in to comment.