eventuate-clients / eventuate-client-nodejs

Other
4 stars 3 forks source link

Subscribing to events using Subscriber should be a one-liner #34

Open cer opened 8 years ago

cer commented 8 years ago

The developer should not need to write this.

const subscriber = new Subscriber({ subscriptions, logger });

subscriber.subscribe().forEach(subscription => {
  //Create EventDispatcher instance
  const dispatcher = new EventDispatcher({ getEventHandler, subscription, logger });
  dispatcher.run(subscription);

});
cer commented 8 years ago

One problem with this design is that the events being handled have no relationship with the events being subscribed to

const subscriptions = [
  {
    subscriberId: 'todo-list-todoAggregate',
    entityTypesAndEvents: todoEntityTypesAndEvents <<<===
  },
  {

vs.

Whatever getEventHandler knows how to process.

getEventHandler should tell Subscriber what to subscribe to.

cer commented 8 years ago

@dartvandru @dartandrevinsky

One possibility is an event handler looks like this:

export const eventHandlers = {
 [TodoAggregate] : {
   [todoCreatedEvent] : handleTodoCreatedEvent,
   [todoDeletedEvent] : handleTodoDeletedEvent,
   [todoUpdatedEvent] : handleTodoUpdatedEvent,
   [todoDeletionRequestedEvent]: handleTodoDeletionRequestedEvent
 }
};

In other words its a nested map: aggregate -> aggregateEvent -> eventHandler

The high-level subscription API looks something like this:


const subscriber = new Subscriber(eventuateClient);  // Rename -> SubscriptionManager?

// Do this one or more times:

subscriber.subscribe(eventHandlers)
cer commented 7 years ago

@dartvandru I think the subscription API is still not quite right

  const dispatcher = new EventDispatcher({ eventHandlers: todoBulkDeleteAggregateEventHandlers });
  const subscriber = new SubscriptionManager({ eventuateClient, dispatcher });
  subscriber.subscribe({ subscriberId: 'todo-list-todoBulkDeleteAggregate', eventHandlers: todoBulkDeleteAggregateEventHandlers });

It should really be something like this. First, create a global SubscriptionManager:

const subscriptionManager = new EventuateSubscriptionManager({ eventuateClient });

And then call subscribe() one or more times.

subscriptionManager.subscribe({ subscriberId: 'todo-list-todoBulkDeleteAggregate', 
                                   eventHandlers: todoBulkDeleteAggregateEventHandlers });

Internally, subscribe() can create a EventDispatcher etc but that should not be part of the API.