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

Catch-all events #19

Closed
oswdr opened this issue Sep 12, 2016 · 7 comments
Closed

Catch-all events #19

oswdr opened this issue Sep 12, 2016 · 7 comments

Comments

@oswdr
Copy link

oswdr commented Sep 12, 2016

Hi Matt,

I was trying out your library, works great. I like the simplicity!

However I wan't to catch all events using one single event handler, but it seems it currently only supports catching events on instances of Entities. Is this by design?

Many thanks,

Joey

@eddieajau
Copy link

Do you mean to, for example, be able to log all events?

@mateodelnorte
Copy link
Owner

mateodelnorte commented Sep 13, 2016

It's by design in that Entities publish events by inheriting the behavior of Node's standard EventEmitter class.

If I were to do this, I would just .enqueue('eventName', this.toJson()); both an event which describes exactly what action was just performed on an entity, as well as a more generic .enqueue('updated', this.toJson()); event. Then, you can listen for both the specific event or all. It would look something like:

Invoice.prototype.approved = function (param) {
  this.digest('approved', param);
  this.approved = true;
  this.approvedAt = param.approvedAt;
  this.approvedBy = param.approvedBy;
  this.enqueue('approved', this.toJson());
  this.enqueue('updated', this.toJson());
};

If that's not fancy enough for you (I tend to prefer code that is less fancy, and more obvious) you could combine the top two answers from here and monkey patch your entities to use EventEmitter2, which would allow you to use wildcards to determine which events fire your listeners. You could create a little factory function which instantiates your entity, monkey patches it, and gives it back to you - ready to roll.

@oswdr
Copy link
Author

oswdr commented Sep 20, 2016

Thanks for you reply.

What I meant is if it is possible to catch all events on for example on the repository. This makes it possible to have my read services listen to all events from all entities. Please, if this is bad design - let me know.

More specifically, current situation:

var repository = ...
var card = new Card();
card.initialize(cmd);
card.on('initialize', () => ...);
repository.commit(card);

Preferred situation:

// read side
repository.on('initialize', () => {
  // update read side
});


// write side
var repository = ...
var card = new Card();
card.initialize(cmd);
repository.commit(card);

@mateodelnorte
Copy link
Owner

The top scenario you have is preferred, as it's the actual entity being
initialized, not the repo. Note the difference between an Entity's .emit
and .enqueue. enqueue will do exactly as emit, but only after the entity
has been committed to the repo. That may be the missing part you're looking
for.

On Sep 20, 2016 2:44 PM, "Joey Osseman" notifications@github.com wrote:

Thanks for you reply.

What I meant is if it is possible to catch all events on for example on
the repository. This makes it possible to have my read services listen to
all events from all entities. Please, if this is bad design - let me know.

More specifically, current situation:

var repository = ...
var card = new Card();
card.initialize(cmd);
card.on('initialize', () => ...);
repository.commit(card, () => res.send());

Preferred situation:

var repository = ...
var card = new Card();
card.initialize(cmd);
repository.commit(card, () => res.send());
repository.on('initialize', () => ...);


You are receiving this because you commented.
Reply to this email directly, view it on GitHub
#19 (comment),
or mute the thread
https://github.com/notifications/unsubscribe-auth/AAh3X2wPS9zXVq4gO6qYNwsUWFs7Uhbuks5qsCmAgaJpZM4J68Ea
.

@oswdr
Copy link
Author

oswdr commented Sep 20, 2016

Thanks for the quick reply, one last question :-). How would my read side respond on these events? In other words, how would they know an event was thrown?

@mateodelnorte
Copy link
Owner

You'll publish a message to a read-side process, which will instruct it to
update your read data store. Look at servicebus for that.

On Sep 20, 2016 3:14 PM, "Joey Osseman" notifications@github.com wrote:

Thanks for the quick reply, one last question :-). How would my read side
respond on these events? In other words, how would they know an event was
thrown?


You are receiving this because you commented.
Reply to this email directly, view it on GitHub
#19 (comment),
or mute the thread
https://github.com/notifications/unsubscribe-auth/AAh3Xz3DaXUA_iX7R-ZTp0H04QXtJkXXks5qsDCigaJpZM4J68Ea
.

@oswdr oswdr closed this as completed Sep 20, 2016
@oswdr
Copy link
Author

oswdr commented Sep 20, 2016

I will look into it, thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants