|  | 
|  | 1 | +import { | 
|  | 2 | +  Controller, | 
|  | 3 | +  HttpCode, | 
|  | 4 | +  OnApplicationShutdown, | 
|  | 5 | +  Post, | 
|  | 6 | +} from '@nestjs/common'; | 
|  | 7 | +import { | 
|  | 8 | +  ClientProxy, | 
|  | 9 | +  EventPattern, | 
|  | 10 | +  MessagePattern, | 
|  | 11 | +} from '@nestjs/microservices'; | 
|  | 12 | +import { GCPubSubClient } from '../../lib'; | 
|  | 13 | +import { Observable } from 'rxjs'; | 
|  | 14 | + | 
|  | 15 | +@Controller() | 
|  | 16 | +export class GCPubSubScopedEnvController1 implements OnApplicationShutdown { | 
|  | 17 | +  static IS_NOTIFIED = false; | 
|  | 18 | + | 
|  | 19 | +  client: ClientProxy; | 
|  | 20 | + | 
|  | 21 | +  constructor() { | 
|  | 22 | +    this.client = new GCPubSubClient({ | 
|  | 23 | +      client: { | 
|  | 24 | +        apiEndpoint: 'localhost:8681', | 
|  | 25 | +        projectId: 'microservice', | 
|  | 26 | +      }, | 
|  | 27 | +      replyTopic: 'default_reply_topic', | 
|  | 28 | +      replySubscription: 'default_reply_subscription', | 
|  | 29 | +      scopedEnvKey: 'foobar', | 
|  | 30 | +    }); | 
|  | 31 | +  } | 
|  | 32 | + | 
|  | 33 | +  onApplicationShutdown(signal?: string) { | 
|  | 34 | +    return this.client.close(); | 
|  | 35 | +  } | 
|  | 36 | + | 
|  | 37 | +  @Post() | 
|  | 38 | +  @HttpCode(200) | 
|  | 39 | +  call() { | 
|  | 40 | +    return this.client.send({ cmd: 'rpc' }, {}); | 
|  | 41 | +  } | 
|  | 42 | + | 
|  | 43 | +  @Post('notify') | 
|  | 44 | +  async sendNotification(): Promise<any> { | 
|  | 45 | +    return this.client.emit<{ notification: boolean; id: string }>( | 
|  | 46 | +      'notification', | 
|  | 47 | +      { notification: true, id: 'id' }, | 
|  | 48 | +    ); | 
|  | 49 | +  } | 
|  | 50 | + | 
|  | 51 | +  @MessagePattern({ cmd: 'rpc' }) | 
|  | 52 | +  rpc(): string { | 
|  | 53 | +    return 'scoped RPC'; | 
|  | 54 | +  } | 
|  | 55 | + | 
|  | 56 | +  @EventPattern('notification') | 
|  | 57 | +  eventHandler(data: { notification: boolean; id: string }) { | 
|  | 58 | +    GCPubSubScopedEnvController1.IS_NOTIFIED = data.notification; | 
|  | 59 | +  } | 
|  | 60 | +} | 
|  | 61 | + | 
|  | 62 | +@Controller() | 
|  | 63 | +export class GCPubSubScopedEnvController2 implements OnApplicationShutdown { | 
|  | 64 | +  static IS_NOTIFIED = false; | 
|  | 65 | + | 
|  | 66 | +  client: ClientProxy; | 
|  | 67 | + | 
|  | 68 | +  constructor() { | 
|  | 69 | +    this.client = new GCPubSubClient({ | 
|  | 70 | +      client: { | 
|  | 71 | +        apiEndpoint: 'localhost:8681', | 
|  | 72 | +        projectId: 'microservice', | 
|  | 73 | +      }, | 
|  | 74 | +      replyTopic: 'default_reply_topic', | 
|  | 75 | +      replySubscription: 'default_reply_subscription', | 
|  | 76 | +    }); | 
|  | 77 | +  } | 
|  | 78 | + | 
|  | 79 | +  onApplicationShutdown(signal?: string) { | 
|  | 80 | +    return this.client.close(); | 
|  | 81 | +  } | 
|  | 82 | + | 
|  | 83 | +  @MessagePattern({ cmd: 'rpc' }) | 
|  | 84 | +  rpc(): string { | 
|  | 85 | +    return 'RPC'; | 
|  | 86 | +  } | 
|  | 87 | + | 
|  | 88 | +  @EventPattern('notification') | 
|  | 89 | +  eventHandler(data: { notification: boolean; id: string }) { | 
|  | 90 | +    GCPubSubScopedEnvController2.IS_NOTIFIED = data.notification; | 
|  | 91 | +  } | 
|  | 92 | +} | 
0 commit comments