Labels

Milestone

Assignee

4 participants

Premise: Domains work best only in a synchronous flow. To cover async situations, though, timers bind current domain to their callbacks and event emitters store it in their constructor. (maybe other stuff as well...)

Problem: Async callbacks returned from low-level libraries, like in the case of net streams, are not covered. If the event emitter was not defined inside a certain domain, nor will listeners added to it get bound to any present domain.

Solution: Binding domains on adding event listeners and activating them on emit. (similar to how intervals handle domains)

Since the relevant domain becomes the one on the function rather than the EventEmitter, it's going to be confusing: I've added an EE to a given domain, but something else was active when the ee.on() was called, and now the ee's domain is not the one it's bound to all the time.

The idea is that a domain "owns" the emitter object itself. If a single EE object is switching domains for different handlers, then that's strange. Also, a single event handler could be applied at different times to different emitters (this is quite common, in fact), and you'll get really odd crosstalk, since the last one to attach it to any event is the one that's going to catch errors it throws.

How about if we inverted the order of loading the domains? That is, if the event emitter has already been added to a domain then that one will be activated, otherwise, fallback on the one present when the listener was defined (if any).

This way it won't change any present behavior, but instead cover more domain "holes."