Prism: Support for async subscribers in the EventAggregator.

Created on 2 Nov 2016  路  6Comments  路  Source: PrismLibrary/Prism

Hi,

One of the main issues we found with the EventAggregator implementation is that it does not supports async subscribers. We have added support for that so one can now do:

void Subscribe () {
  EventAggregator.GetEvent<PubSubEvent<SaveProjectEvent>> ().SubscribeAsync (HandleSave);
}

async Task HandleSave (SaveProjectEvent saveEvent){
  saveEvent.ReturnValue = await AsyncTask();
}

void Save () {
  var saveEvent = new SaveProjectEvent (project;
  await  EventAggregator.GetEvent<PubSubEvent<SaveProjectEvent>> ().Publish (saveEvent);
  if (saveEvent.ReturnValue) {
     InfoMessage ("Success");
  }
}

This changes slightly the API, with Publish returning a Task and adding a new SubscribeAsync function, but I think the change makes the events aggregator much more useful.
Would you consider a change like that? In that case we can work on the PR.

Most helpful comment

You can find the implementation here with the commit adding support for Async on top of Prism

All 6 comments

Thanks for the idea, but this is not something we will be adding to Prism. The even aggregator is fire and forget.

@ylatuya would you mind sharing your implementation for PublishAsync?

You can find the implementation here with the commit adding support for Async on top of Prism

Thanks ! I took a different approach in our project, which doesn't involve touching Prism classes, but being on top of them. If I find time I can try to share it.

I too am interested in such functionality. @molinch, might you be able to share your solution?

I've successfully used the Deferral solution provided by Stephen Cleary. It requires adding a Deferral to the PubSubEvent payload that can be awaited on. It works great, but the syntax is clunky for users and implementers.

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

brianlagunas picture brianlagunas  路  4Comments

bartlannoeye picture bartlannoeye  路  3Comments

weitzhandler picture weitzhandler  路  6Comments

michaelmairegger picture michaelmairegger  路  3Comments

znakeeye picture znakeeye  路  7Comments