Skip to content

Commit ef694ca

Browse files
committed
Made consumer options really optional when creating it from event stores
By accident, there was a leftover forcing user to still provide even empty object, which doesn't make sense as it should be passed.
1 parent 909b36c commit ef694ca

File tree

5 files changed

+34
-34
lines changed

5 files changed

+34
-34
lines changed

src/packages/emmett-esdb/src/eventStore/consumers/eventStoreDBEventStoreConsumer.ts

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ import {
2020
export type EventStoreDBEventStoreConsumerConfig<
2121
ConsumerEventType extends Event = Event,
2222
> = {
23-
connectionString: string;
2423
from?: EventStoreDBEventStoreConsumerType;
2524
processors?: EventStoreDBEventStoreProcessor<ConsumerEventType>[];
2625
pulling?: {
@@ -35,7 +34,7 @@ export type EventStoreDBEventStoreConsumerOptions<
3534
| {
3635
connectionString: string;
3736
}
38-
| { eventStoreDBClient: EventStoreDBClient }
37+
| { client: EventStoreDBClient }
3938
);
4039

4140
export type $all = '$all';
@@ -54,7 +53,6 @@ export type EventStoreDBEventStoreConsumerType =
5453
export type EventStoreDBEventStoreConsumer<
5554
ConsumerEventType extends Event = Event,
5655
> = Readonly<{
57-
connectionString: string;
5856
isRunning: boolean;
5957
processors: EventStoreDBEventStoreProcessor<ConsumerEventType>[];
6058
processor: <EventType extends ConsumerEventType = ConsumerEventType>(
@@ -71,17 +69,17 @@ export const eventStoreDBEventStoreConsumer = <
7169
options: EventStoreDBEventStoreConsumerOptions<ConsumerEventType>,
7270
): EventStoreDBEventStoreConsumer<ConsumerEventType> => {
7371
let isRunning = false;
74-
const { connectionString, pulling } = options;
72+
const { pulling } = options;
7573
const processors = options.processors ?? [];
7674

7775
let start: Promise<void>;
7876

79-
let currentMessagePooler:
80-
| EventStoreDBEventStoreMessageBatchPuller
81-
| undefined;
77+
let currentSubscription: EventStoreDBEventStoreMessageBatchPuller | undefined;
8278

83-
const eventStoreDBClient =
84-
EventStoreDBClient.connectionString(connectionString);
79+
const client =
80+
'client' in options
81+
? options.client
82+
: EventStoreDBClient.connectionString(options.connectionString);
8583

8684
const eachBatch: EventStoreDBEventStoreMessagesBatchHandler<
8785
ConsumerEventType
@@ -97,7 +95,7 @@ export const eventStoreDBEventStoreConsumer = <
9795
const result = await Promise.allSettled(
9896
activeProcessors.map((s) => {
9997
// TODO: Add here filtering to only pass messages that can be handled by processor
100-
return s.handle(messagesBatch, { eventStoreDBClient });
98+
return s.handle(messagesBatch, { client });
10199
}),
102100
);
103101

@@ -110,8 +108,8 @@ export const eventStoreDBEventStoreConsumer = <
110108
};
111109
};
112110

113-
const messagePuller = (currentMessagePooler = eventStoreDBSubscription({
114-
eventStoreDBClient,
111+
const subscription = (currentSubscription = eventStoreDBSubscription({
112+
client,
115113
eachBatch,
116114
batchSize:
117115
pulling?.batchSize ?? DefaultEventStoreDBEventStoreProcessorBatchSize,
@@ -120,15 +118,14 @@ export const eventStoreDBEventStoreConsumer = <
120118
const stop = async () => {
121119
if (!isRunning) return;
122120
isRunning = false;
123-
if (currentMessagePooler) {
124-
await currentMessagePooler.stop();
125-
currentMessagePooler = undefined;
121+
if (currentSubscription) {
122+
await currentSubscription.stop();
123+
currentSubscription = undefined;
126124
}
127125
await start;
128126
};
129127

130128
return {
131-
connectionString,
132129
processors,
133130
get isRunning() {
134131
return isRunning;
@@ -156,10 +153,10 @@ export const eventStoreDBEventStoreConsumer = <
156153
isRunning = true;
157154

158155
const startFrom = zipEventStoreDBEventStoreMessageBatchPullerStartFrom(
159-
await Promise.all(processors.map((o) => o.start(eventStoreDBClient))),
156+
await Promise.all(processors.map((o) => o.start(client))),
160157
);
161158

162-
return messagePuller.start({ startFrom });
159+
return subscription.start({ startFrom });
163160
})();
164161

165162
return start;

src/packages/emmett-esdb/src/eventStore/consumers/eventStoreDBEventStoreProcessor.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,12 @@ export type EventStoreDBEventStoreProcessorEventsBatch<
1717
export type EventStoreDBEventStoreProcessor<EventType extends Event = Event> = {
1818
id: string;
1919
start: (
20-
eventStoreDBClient: EventStoreDBClient,
20+
client: EventStoreDBClient,
2121
) => Promise<EventStoreDBSubscriptionStartFrom | undefined>;
2222
isActive: boolean;
2323
handle: (
2424
messagesBatch: EventStoreDBEventStoreProcessorEventsBatch<EventType>,
25-
context: { eventStoreDBClient: EventStoreDBClient },
25+
context: { client: EventStoreDBClient },
2626
) => Promise<EventStoreDBEventStoreProcessorMessageHandlerResult>;
2727
};
2828

@@ -88,7 +88,7 @@ export const eventStoreDBEventStoreProcessor = <
8888
return {
8989
id: options.processorId,
9090
start: (
91-
_eventStoreDBClient: EventStoreDBClient,
91+
_client: EventStoreDBClient,
9292
): Promise<EventStoreDBSubscriptionStartFrom | undefined> => {
9393
isActive = true;
9494
if (options.startFrom !== 'CURRENT')

src/packages/emmett-esdb/src/eventStore/consumers/subscriptions/index.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ export type EventStoreDBEventStoreMessagesBatchHandler<
4444

4545
export type EventStoreDBSubscriptionOptions<EventType extends Event = Event> = {
4646
from?: EventStoreDBEventStoreConsumerType;
47-
eventStoreDBClient: EventStoreDBClient;
47+
client: EventStoreDBClient;
4848
batchSize: number;
4949
eachBatch: EventStoreDBEventStoreMessagesBatchHandler<EventType>;
5050
};
@@ -82,23 +82,23 @@ const toStreamPosition = (startFrom: EventStoreDBSubscriptionStartFrom) =>
8282
: startFrom.position;
8383

8484
const subscribe = (
85-
eventStoreDBClient: EventStoreDBClient,
85+
client: EventStoreDBClient,
8686
from: EventStoreDBEventStoreConsumerType | undefined,
8787
options: EventStoreDBSubscriptionStartOptions,
8888
) =>
8989
from == undefined || from.stream == $all
90-
? eventStoreDBClient.subscribeToAll({
90+
? client.subscribeToAll({
9191
fromPosition: toGlobalPosition(options.startFrom),
9292
filter: excludeSystemEvents(),
9393
...(from?.options ?? {}),
9494
})
95-
: eventStoreDBClient.subscribeToStream(from.stream, {
95+
: client.subscribeToStream(from.stream, {
9696
fromRevision: toStreamPosition(options.startFrom),
9797
...(from.options ?? {}),
9898
});
9999

100100
export const eventStoreDBSubscription = <EventType extends Event = Event>({
101-
eventStoreDBClient,
101+
client,
102102
from,
103103
//batchSize,
104104
eachBatch,
@@ -112,7 +112,7 @@ export const eventStoreDBSubscription = <EventType extends Event = Event>({
112112
const pullMessages = async (
113113
options: EventStoreDBSubscriptionStartOptions,
114114
) => {
115-
subscription = subscribe(eventStoreDBClient, from, options);
115+
subscription = subscribe(client, from, options);
116116

117117
return new Promise<void>((resolve, reject) => {
118118
finished(

src/packages/emmett-esdb/src/eventStore/eventstoreDBEventStore.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ export interface EventStoreDBEventStore
6767
options?: AppendToStreamOptions,
6868
): Promise<AppendToStreamResultWithGlobalPosition>;
6969
consumer<ConsumerEventType extends Event = Event>(
70-
options: EventStoreDBEventStoreConsumerConfig<ConsumerEventType>,
70+
options?: EventStoreDBEventStoreConsumerConfig<ConsumerEventType>,
7171
): EventStoreDBEventStoreConsumer<ConsumerEventType>;
7272
}
7373

@@ -211,11 +211,11 @@ export const getEventStoreDBEventStore = (
211211
},
212212

213213
consumer: <ConsumerEventType extends Event = Event>(
214-
options: EventStoreDBEventStoreConsumerConfig<ConsumerEventType>,
214+
options?: EventStoreDBEventStoreConsumerConfig<ConsumerEventType>,
215215
): EventStoreDBEventStoreConsumer<ConsumerEventType> =>
216216
eventStoreDBEventStoreConsumer<ConsumerEventType>({
217-
...options,
218-
eventStoreDBClient: eventStore,
217+
...(options ?? {}),
218+
client: eventStore,
219219
}),
220220

221221
//streamEvents: streamEvents(eventStore),

src/packages/emmett-postgresql/src/eventStore/postgreSQLEventStore.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ export interface PostgresEventStore
5151
options?: AppendToStreamOptions,
5252
): Promise<AppendToStreamResultWithGlobalPosition>;
5353
consumer<ConsumerEventType extends Event = Event>(
54-
options: PostgreSQLEventStoreConsumerConfig<ConsumerEventType>,
54+
options?: PostgreSQLEventStoreConsumerConfig<ConsumerEventType>,
5555
): PostgreSQLEventStoreConsumer<ConsumerEventType>;
5656
close(): Promise<void>;
5757
schema: {
@@ -284,9 +284,12 @@ export const getPostgreSQLEventStore = (
284284
};
285285
},
286286
consumer: <ConsumerEventType extends Event = Event>(
287-
options: PostgreSQLEventStoreConsumerConfig<ConsumerEventType>,
287+
options?: PostgreSQLEventStoreConsumerConfig<ConsumerEventType>,
288288
): PostgreSQLEventStoreConsumer<ConsumerEventType> =>
289-
postgreSQLEventStoreConsumer<ConsumerEventType>({ ...options, pool }),
289+
postgreSQLEventStoreConsumer<ConsumerEventType>({
290+
...(options ?? {}),
291+
pool,
292+
}),
290293
close: () => pool.close(),
291294

292295
async withSession<T = unknown>(

0 commit comments

Comments
 (0)