11import { ConnectionEvents , create_container , Connection as RheaConnection } from "rhea"
22import { AmqpManagement , Management } from "./management.js"
33import { EnvironmentParams } from "./environment.js"
4+ import { AmqpPublisher , Publisher } from "./publisher.js"
5+ import { DestinationOptions } from "./message.js"
46
57export interface Connection {
68 close ( ) : Promise < boolean >
79 isOpen ( ) : boolean
810 management ( ) : Management
11+ createPublisher ( options ?: DestinationOptions ) : Promise < Publisher >
12+ get publishers ( ) : Map < string , Publisher >
913}
1014
1115export class AmqpConnection implements Connection {
16+ private _publishers : Map < string , Publisher > = new Map < string , Publisher > ( )
17+
1218 static async create ( params : EnvironmentParams ) {
1319 const connection = await AmqpConnection . open ( params )
1420 const topologyManagement = await AmqpManagement . create ( connection )
@@ -43,6 +49,7 @@ export class AmqpConnection implements Connection {
4349 return rej ( new Error ( "Connection error: " + context . connection . error ) )
4450 } )
4551
52+ this . _publishers . forEach ( ( p ) => p . close ( ) )
4653 this . connection . close ( )
4754 } )
4855 }
@@ -51,6 +58,16 @@ export class AmqpConnection implements Connection {
5158 return this . topologyManagement
5259 }
5360
61+ async createPublisher ( options ?: DestinationOptions ) : Promise < Publisher > {
62+ const publisher = await AmqpPublisher . createFrom ( this . connection , this . _publishers , options )
63+ this . _publishers . set ( publisher . id , publisher )
64+ return publisher
65+ }
66+
67+ public get publishers ( ) : Map < string , Publisher > {
68+ return this . _publishers
69+ }
70+
5471 public isOpen ( ) : boolean {
5572 return this . connection ? this . connection . is_open ( ) : false
5673 }
0 commit comments