Skip to content

Commit

Permalink
Merge branch 'zenozen-module-token-factory-replacer'
Browse files Browse the repository at this point in the history
  • Loading branch information
kamilmysliwiec committed Jun 28, 2019
2 parents dc219bf + 0cab179 commit 5fe51f7
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 6 deletions.
22 changes: 16 additions & 6 deletions packages/core/injector/module-token-factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,25 @@ export class ModuleTokenFactory {
scope: Type<any>[],
dynamicModuleMetadata?: Partial<DynamicModule> | undefined,
): string {
const reflectedScope = this.reflectScope(metatype);
const isSingleScoped = reflectedScope === true;
const moduleScope = this.reflectScope(metatype);
const isSingleScoped = moduleScope === true;
const opaqueToken = {
module: this.getModuleName(metatype),
dynamic: this.getDynamicMetadataToken(dynamicModuleMetadata),
scope: isSingleScoped ? this.getScopeStack(scope) : reflectedScope,
scope: isSingleScoped ? this.getScopeStack(scope) : moduleScope,
};
return hash(opaqueToken);
}

public getDynamicMetadataToken(
dynamicModuleMetadata: Partial<DynamicModule> | undefined,
): string {
// Uses safeStringify instead of JSON.stringify
// to support circular dynamic modules
return dynamicModuleMetadata ? stringify(dynamicModuleMetadata) : '';
// Uses safeStringify instead of JSON.stringify to support circular dynamic modules
// The replacer function is also required in order to obtain real class names
// instead of the unified "Function" key
return dynamicModuleMetadata
? stringify(dynamicModuleMetadata, this.replacer)
: '';
}

public getModuleName(metatype: Type<any>): string {
Expand All @@ -50,4 +53,11 @@ export class ModuleTokenFactory {
const scope = Reflect.getMetadata(SHARED_MODULE_METADATA, metatype);
return scope ? scope : 'global';
}

private replacer(key: string, value: any) {
if (typeof value === 'function') {
return value.name;
}
return value;
}
}
1 change: 1 addition & 0 deletions packages/core/scanner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,7 @@ export class DependenciesScanner {
(a: any[], b: any[]) => a.concat(b),
initialValue,
) as any[];

const combinedInjectables = [
...controllerInjectables,
...flattenMethodsInjectables,
Expand Down
7 changes: 7 additions & 0 deletions packages/core/test/injector/module-token-factory.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,13 @@ describe('ModuleTokenFactory', () => {
JSON.stringify(metadata),
);
});
it('should return hash with class', () => {
class Provider {}
const metadata = { providers: [Provider], exports: [Provider] };
expect(factory.getDynamicMetadataToken(metadata)).to.be.eql(
'{"providers":["Provider"],"exports":["Provider"]}'
);
});
});
describe('when metadata does not exist', () => {
it('should return empty string', () => {
Expand Down

0 comments on commit 5fe51f7

Please sign in to comment.