Skip to content

Make event dispatch synchronous#40

Open
pavelhoral wants to merge 1 commit intoWrenSecurity:mainfrom
pavelhoral:fix-event-dispatch
Open

Make event dispatch synchronous#40
pavelhoral wants to merge 1 commit intoWrenSecurity:mainfrom
pavelhoral:fix-event-dispatch

Conversation

@pavelhoral
Copy link
Member

Change event dispatch process from calling event handlers in an async (setTimeout) fashion to sync calls. This fixes potential race conditions when a specific order of event processing is expected (e.g. showing and hiding loading progress spinner).

Why I see this as a meaningful change? The decision to process any event in async fashion shuold be done by the event subscriber, not the event dispatcher / manager. Or potentially the triggering component might want to send event asynchronously... again, that decision should not be made by the dispatcher / manager.

Change event dispatch process from calling event handlers in an
async (setTimeout) fashion to sync calls. This fixes potential
race conditions when a specific order of event processing is
expected (e.g. showing and hiding loading progress spinner).
@pavelhoral
Copy link
Member Author

This needs to have proper error handling (at least by ignoring or simply logging errors).

Also we might want to support both sync and async EventManagers (e.g. like EventEmitter from Angular. So that in case this breaks something in our project we might have opt-out of the sync behavior.

@pavelhoral
Copy link
Member Author

Another thing to consider when changing the current behaviour is NodeJS's EventEmitter and DOM's EventTarget. The main topics here is error propagation (i.e. does the caller get a chance to catch the error like with EventEmitter) and whether the error stops event propagation.

And I think that the simplest approach will be to have dispatch and error propagation to work as follows:

  • default event dispatch will be changed to synchronous (similar behaviour as NodeJS and Angular)
  • any error stops the propagation and the error is thrown in a standard way up the call-stack (similar behaviour as NodeJS and Angular)
  • the behaviour can be altered to asynchronous dispatch (e.g. via async dispatch event option)
  • errors during asynchronous dispatch halt the distribution and are propagated through failed promise (current behaviour)

The same applies to subscriptions, which I would personally mark as deprecated. But I guess there are use-cases for those... that is a topic for another day and PR :).

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

Successfully merging this pull request may close these issues.

1 participant