Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature] connect to persistent subscription #98

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import {
DeleteResult as TCPDeleteResult,
LiveProcessingStartedCallback,
SubscriptionDroppedCallback,
ConnectionSettings
ConnectionSettings,
EventStorePersistentSubscription
} from "node-eventstore-client";

import {
Expand Down Expand Up @@ -218,6 +219,7 @@ export class TCPClient {
};
subscribeToStream(streamName: string, onEventAppeared?: MappedEventAppearedCallback<EventStoreSubscription>, onDropped?: SubscriptionDroppedCallback<EventStoreSubscription>, resolveLinkTos?: boolean): Promise<EventStoreSubscription>;
subscribeToStreamFrom(streamName: string, fromEventNumber?: number, onEventAppeared?: MappedEventAppearedCallback<EventStoreCatchUpSubscription>, onLiveProcessingStarted?: LiveProcessingStartedCallback, onDropped?: SubscriptionDroppedCallback<EventStoreCatchUpSubscription>, settings?: SubscribeToStreamFromSettings): Promise<EventStoreCatchUpSubscription>;
connectToPersistentSubscription(streamName: string, groupName: string, onEventAppeared?: MappedEventAppearedCallback<EventStoreCatchUpSubscription>, onDropped?: SubscriptionDroppedCallback<EventStoreSubscription>): Promise<EventStorePersistentSubscription>;
close(): Promise<void>;
getPool(): Promise<TCPPool<object>>;
closeAllPools(): Promise<void>;
Expand Down
43 changes: 43 additions & 0 deletions lib/tcpClient/connectToPersistentSubscription.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import connectionManager from './connectionManager';
import mapEvents from './utilities/mapEvents';
import client from 'node-eventstore-client';
import debugModule from 'debug';
import assert from 'assert';

const debug = debugModule('geteventstore:connectToPersistentSubscription');
const baseErr = 'Connect to Persistent Subscription - ';

export default (config) => (
streamName,
groupName,
onEventAppeared,
onDropped,
) => new Promise(async (resolve, reject) => {
assert(streamName, `${baseErr}Stream Name not provided`);
assert(groupName, `${baseErr}Group Name not provided`);

let connection;
const onEvent = (sub, ev) => {
const mappedEvent = mapEvents([ev])[0];
if (mappedEvent) onEventAppeared(sub, mappedEvent);
};

const onConnected = async () => {
try {
const subscription = await connection.connectToPersistentSubscription(
streamName, groupName, onEvent, onDropped, new client.UserCredentials(config.credentials.username, config.credentials.password), 10, true
);

debug('', 'Persistent Subscription: %j', subscription);
resolve(subscription);
} catch (ex) {
reject(ex);
}
};

try {
connection = await connectionManager.create(config, onConnected, true);
} catch (ex) {
reject(ex);
}
});
2 changes: 2 additions & 0 deletions lib/tcpClient/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import connectToPersistentSubscription from './connectToPersistentSubscription';
import subscribeToStreamFrom from './subscribeToStreamFrom';
import getAllStreamEvents from './getAllStreamEvents';
import checkStreamExists from './checkStreamExists';
Expand Down Expand Up @@ -58,6 +59,7 @@ export default class TCPClient {
this.eventEnumerator = eventEnumerator(_config);
this.subscribeToStream = subscribeToStream(_config);
this.subscribeToStreamFrom = subscribeToStreamFrom(_config);
this.connectToPersistentSubscription = connectToPersistentSubscription(_config);
this.close = connectionManager.close(_config);
this.getPool = connectionManager.getPool(_config);
this.closeAllPools = connectionManager.closeAllPools;
Expand Down
Loading