我正在尝试创建一个需要依赖注入的装饰器。例如:
@Injectable()
class UserService{
@TimeoutAndCache(1000)
async getUser(id:string):Promise<User>{
// Make a call to db to get all Users
}
}
Run Code Online (Sandbox Code Playgroud)
@TimeoutAndCache 返回一个新的承诺,它执行以下操作:
export const TimeoutAndCache = function timeoutCache(ts: number, namespace) {
return function log(
target: object,
propertyKey: string,
descriptor: TypedPropertyDescriptor<any>,
) {
const originalMethod = descriptor.value; // save a reference to the original method
descriptor.value = function(...args: any[]) {
// pre
let timedOut = false;
// run and store result
const result: Promise<object> = …Run Code Online (Sandbox Code Playgroud) 我正在两个 NestJS 实例之间设置基本的 EventPattern。但是,其他微服务并未发出/接收该事件。
我试图在文档中找到一些具体的例子,但是看起来设置与调用 clinet.emit/client.call 来调用其他微服务不同。
微服务 1.
import { Controller, Get } from '@nestjs/common';
import { AppService } from './app.service';
import { Client, Transport, ClientProxy, ClientsModule, EventPattern } from '@nestjs/microservices';
import { Observable } from 'rxjs';
@Controller()
export class AppController {
constructor(private readonly appService: AppService) {}
@Client({ transport: Transport.REDIS })
client: ClientProxy;
@Get()
async call(): Promise<number> {
const pattern = { cmd: 'sum' };
const payload = [1, 2, 3];
const result = await this.client.emit('user_created', {age: 5});
return …Run Code Online (Sandbox Code Playgroud)