Skip to content

Commit

Permalink
Added connection handlers for disconnected and error (#113)
Browse files Browse the repository at this point in the history
  • Loading branch information
Rzial authored Apr 19, 2022
1 parent 3c882d1 commit 6813d2d
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
23 changes: 19 additions & 4 deletions src/event-store/event-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,23 @@ export class EventStore {
this.connection.on('closed', () => {
this.isConnected = false;
this.logger.error('Connection to EventStore closed!');
this.config.onTcpDisconnected(this);

typeof this.config.onTcpDisconnected === 'function' &&
this.config.onTcpDisconnected(this);
});
this.connection.on('disconnected', () => {
this.isConnected = false;
this.logger.error('EventStore disconnected!');

typeof this.config.onTcpDisconnected === 'function' &&
this.config.onTcpDisconnected(this);
});
this.connection.on('error', (err: Error) => {
this.isConnected = false;
this.logger.error(`EventStore errored: ${err.message}!`);

typeof this.config.onTcpErrored === 'function' &&
this.config.onTcpErrored(this, err);
});
}

Expand Down Expand Up @@ -165,9 +181,8 @@ export class EventStore {
this.logger.warn(
`Connected to persistent subscription ${group} on stream ${stream} dropped ${reason} : ${error}`,
);
this.persistentSubscriptions[
`${stream}-${group}`
].isConnected = false;
this.persistentSubscriptions[`${stream}-${group}`].isConnected =
false;
this.persistentSubscriptions[`${stream}-${group}`].status =
reason + ' ' + error;
if (onSubscriptionDropped) {
Expand Down
1 change: 1 addition & 0 deletions src/interfaces/config/event-store-config.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@ export interface IEventStoreConfig {
options?: ConnectionSettings;
onTcpConnected?: (eventStore: EventStore) => void;
onTcpDisconnected?: (eventStore: EventStore) => void;
onTcpErrored?: (eventStore: EventStore, error: Error) => void;
}

0 comments on commit 6813d2d

Please sign in to comment.