Skip to content

Commit

Permalink
bugfix(@nestjs/core) multi global providers issue #812
Browse files Browse the repository at this point in the history
  • Loading branch information
kamilmysliwiec committed Jul 4, 2018
1 parent a9204cb commit 3d2d838
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 27 deletions.
10 changes: 4 additions & 6 deletions packages/core/injector/instance-loader.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
import iterate from 'iterare';
import { Logger } from '@nestjs/common';
import { Controller } from '@nestjs/common/interfaces/controllers/controller.interface';
import { Injectable } from '@nestjs/common/interfaces/injectable.interface';
import { moduleInitMessage } from '../helpers/messages';
import { NestContainer } from './container';
import { Injector } from './injector';
import { Injectable } from '@nestjs/common/interfaces/injectable.interface';
import { Controller } from '@nestjs/common/interfaces/controllers/controller.interface';
import { Module } from './module';
import { Logger, OnModuleInit } from '@nestjs/common';
import { moduleInitMessage } from '../helpers/messages';
import { isUndefined, isNil } from '@nestjs/common/utils/shared.utils';

export class InstanceLoader {
private readonly injector = new Injector();
Expand Down
34 changes: 23 additions & 11 deletions packages/core/scanner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,9 @@ import { CircularDependencyException } from './errors/exceptions/circular-depend
import { NestContainer } from './injector/container';

interface ApplicationProviderWrapper {
moduleToken: string;
providerToken: string;
moduleKey: string;
providerKey: string;
type: string;
}

export class DependenciesScanner {
Expand Down Expand Up @@ -237,15 +238,26 @@ export class DependenciesScanner {
}
const applyProvidersMap = this.getApplyProvidersMap();
const providersKeys = Object.keys(applyProvidersMap);
const providerToken = component.provide;
if (providersKeys.indexOf(providerToken) < 0) {
const type = component.provide;
if (providersKeys.indexOf(type) < 0) {
return this.container.addComponent(component, token);
}
const providerToken = Math.random()
.toString(36)
.substring(2, 32);

this.applicationProvidersApplyMap.push({
moduleToken: token,
providerToken,
type,
moduleKey: token,
providerKey: providerToken,
});
this.container.addComponent(component, token);
this.container.addComponent(
{
...component,
provide: providerToken,
},
token,
);
}

public storeInjectable(component: Type<Injectable>, token: string) {
Expand All @@ -270,12 +282,12 @@ export class DependenciesScanner {
public applyApplicationProviders() {
const applyProvidersMap = this.getApplyProvidersMap();
this.applicationProvidersApplyMap.forEach(
({ moduleToken, providerToken }) => {
({ moduleKey, providerKey, type }) => {
const modules = this.container.getModules();
const { components } = modules.get(moduleToken);
const { instance } = components.get(providerToken);
const { components } = modules.get(moduleKey);
const { instance } = components.get(providerKey);

applyProvidersMap[providerToken](instance);
applyProvidersMap[type](instance);
},
);
}
Expand Down
17 changes: 8 additions & 9 deletions packages/core/test/scanner.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ describe('DependenciesScanner', () => {
mockContainer.restore();
});

it('should "storeModule" call twice (2 modules) container method "addModule"', async () => {
it('should "storeModule" call twice (2 modules) container method "addModule"', async () => {
const expectation = mockContainer.expects('addModule').twice();
await scanner.scan(TestModule as any);
expectation.verify();
Expand Down Expand Up @@ -155,7 +155,7 @@ describe('DependenciesScanner', () => {
});

describe('storeRelatedModule', () => {
it('should call forwardRef() when forwardRef property exists', async () => {
it('should call forwardRef() when forwardRef property exists', async () => {
const module = { forwardRef: sinon.stub().returns({}) };

sinon.stub(container, 'addRelatedModule').returns({});
Expand Down Expand Up @@ -195,9 +195,7 @@ describe('DependenciesScanner', () => {
};

it('should call container "addComponent" with expected args', () => {
const expectation = mockContainer
.expects('addComponent')
.withArgs(component, token);
const expectation = mockContainer.expects('addComponent').atLeast(1);

mockContainer.expects('addComponent').callsFake(() => false);
scanner.storeComponent(component, token);
Expand All @@ -210,7 +208,7 @@ describe('DependenciesScanner', () => {
const applyMap = (scanner as any).applicationProvidersApplyMap;

expect(applyMap).to.have.length(1);
expect(applyMap[0].moduleToken).to.be.eql(token);
expect(applyMap[0].moduleKey).to.be.eql(token);
});
});
describe('and is not global', () => {
Expand Down Expand Up @@ -245,8 +243,9 @@ describe('DependenciesScanner', () => {
describe('applyApplicationProviders', () => {
it('should apply each provider', () => {
const provider = {
moduleToken: 'moduleToken',
providerToken: 'providerToken',
moduleKey: 'moduleToken',
providerKey: 'providerToken',
type: APP_GUARD,
};
(scanner as any).applicationProvidersApplyMap = [provider];

Expand All @@ -258,7 +257,7 @@ describe('DependenciesScanner', () => {
}));
const applySpy = sinon.spy();
sinon.stub(scanner, 'getApplyProvidersMap').callsFake(() => ({
[provider.providerToken]: applySpy,
[provider.type]: applySpy,
}));
scanner.applyApplicationProviders();
expect(applySpy.called).to.be.true;
Expand Down
2 changes: 1 addition & 1 deletion packages/websockets/adapters/ws-adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export class WsAdapter implements WebSocketAdapter {
}

public bindMessageHandlers(
client: WebSocket,
client: any,
handlers: MessageMappingProperties[],
transform: (data: any) => Observable<any>,
) {
Expand Down

0 comments on commit 3d2d838

Please sign in to comment.