Skip to content

Commit

Permalink
fix: try service resolution with class reference (fixes #35)
Browse files Browse the repository at this point in the history
  • Loading branch information
tahubu committed Aug 2, 2021
1 parent 7cb10de commit 0cc7d34
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/decorator/listen.decorator.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ describe('@Listen', () => {
it('should set correctly the queue name and target data', () => {
const metadata = Reflect.getMetadata(QUEUE_LISTEN_METADATA_KEY, instance.method1);

expect(metadata).toEqual(expect.objectContaining({ source: queueName, targetName: Test.name, callbackName: 'method1' }));
expect(metadata).toEqual(expect.objectContaining({ source: queueName, targetName: Test.name, target: Test, callbackName: 'method1' }));
});

it('should set the queue options', () => {
Expand Down
1 change: 1 addition & 0 deletions src/decorator/listen.decorator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export const Listen = <T>(source: string, options?: ListenOptions<T>) => {
metadata.options = options;

metadata.targetName = target.constructor.name;
metadata.target = target.constructor;

metadata.callback = descriptor.value;
metadata.callbackName = propertyKey;
Expand Down
9 changes: 8 additions & 1 deletion src/domain/listener-metadata.domain.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { ListenOptions } from '../interface';

/* eslint-disable @typescript-eslint/ban-types */

/**
* Metadata added by the `@Listener` decorator
*/
Expand All @@ -25,7 +27,12 @@ export class ListenerMetadata<T> {
public options: ListenOptions<T>;

/**
* The Class the method belongs to
* The name of Class the method belongs to
*/
public targetName: string;

/**
* The Class the method belongs to
*/
public target: object;
}
12 changes: 11 additions & 1 deletion src/queue.module.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { DynamicModule, Inject, Module, OnModuleDestroy, OnModuleInit, Provider, Type } from '@nestjs/common';
import { Connection } from 'rhea-promise';
import { MetadataScanner, ModuleRef } from '@nestjs/core';
import { UnknownElementException } from '@nestjs/core/errors/exceptions/unknown-element.exception';

import { QueueModuleOptions, QueueModuleAsyncOptions, QueueModuleOptionsFactory } from './interface';
import { AMQPService, ObjectValidatorService, QueueService } from './service';
Expand Down Expand Up @@ -145,7 +146,16 @@ export class QueueModule implements OnModuleInit, OnModuleDestroy {
logger.debug(`attaching listener for @Listen: ${JSON.stringify(listener)}`);

// fetch instance from DI framework
const target = this.moduleRef.get(listener.targetName, { strict: false });
let target: any;
try {
target = this.moduleRef.get(listener.target as any, { strict: false });
} catch (err) {
if (err instanceof UnknownElementException) {
target = this.moduleRef.get(listener.targetName, { strict: false });
} else {
throw err;
}
}

await this.queueService.listen(listener.source, listener.callback.bind(target), listener.options);
}
Expand Down

0 comments on commit 0cc7d34

Please sign in to comment.