-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: fixed topic actors to pass tests
- Loading branch information
1 parent
b1a9568
commit 3c8653f
Showing
2 changed files
with
111 additions
and
96 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,45 +1,53 @@ | ||
import {connect, Connection, Channel, Options} from 'amqplib'; | ||
|
||
import {Channel, connect, Connection, Options} from 'amqplib'; | ||
|
||
class TopicProducer { | ||
private connection: Connection; | ||
private channel: Channel; | ||
private exchange: string; | ||
private exchangeOptions: Options.AssertExchange; | ||
public static EXCHANGE_TYPE = 'topic'; | ||
|
||
static create(connection) { | ||
return new TopicProducer(connection); | ||
} | ||
public static create(exchange?: string) { | ||
return new TopicProducer(exchange); | ||
} | ||
|
||
constructor(connection: Connection) { | ||
this.connection = connection; | ||
} | ||
public exchange: string; | ||
public exchangeOptions: Options.AssertExchange; | ||
private connection: Connection; | ||
private channel: Channel; | ||
|
||
constructor(exchange?: string) { | ||
this.exchange = exchange; | ||
} | ||
|
||
setExchange(name: string, options: Options.AssertExchange) { | ||
this.exchange = name; | ||
this.exchangeOptions = options; | ||
public setExchange(name: string, options?: Options.AssertExchange) { | ||
this.exchange = name; | ||
this.exchangeOptions = options; | ||
} | ||
|
||
public async start(url: string) { | ||
if (!this.connection) { | ||
this.connection = await connect(url); | ||
} | ||
|
||
async start(amqpUrl) { | ||
if (!this.connection) { | ||
this.connection = await connect(amqpUrl); | ||
} | ||
this.channel = await this.connection.createChannel(); | ||
|
||
this.channel = await this.connection.createChannel(); | ||
await this.channel.assertExchange('events', 'topic', {durable: true}); | ||
await this.channel.assertExchange( | ||
this.exchange, | ||
TopicProducer.EXCHANGE_TYPE, | ||
{ | ||
durable: true, | ||
}, | ||
); | ||
|
||
return this; | ||
} | ||
return this; | ||
} | ||
|
||
publish(topic, data) { | ||
const serialised = JSON.stringify(data); | ||
this.channel.publish(this.exchange, topic, new Buffer(serialised)); | ||
} | ||
public publish(topic, data) { | ||
const content = new Buffer(JSON.stringify(data)); | ||
this.channel.publish(this.exchange, topic, content); | ||
} | ||
|
||
stop() { | ||
this.connection.close(); | ||
} | ||
public async stop() { | ||
return await this.connection.close(); | ||
} | ||
|
||
} | ||
|
||
export default TopicProducer; | ||
export default TopicProducer; |