adobe-photoshop / spaces-design

Adobe Photoshop Design Space
http://adobe-photoshop.github.io/
Other
853 stars 74 forks source link

Put a timer on adding event listeners to the window #3588

Closed DivyaPrabhakar closed 8 years ago

DivyaPrabhakar commented 8 years ago

References #3532

With the previous version of bluebird, the call to _addListeners() was working asynchronously behind the scenes. Therefore the click events on the document header we handled before an event listener was added on the window. With this recent change to bluebird, https://github.com/petkaantonov/bluebird/issues/915, this became synchronous and the event listener on the window was added before the click to open the document header buttons were interpreted, resulting in the automatic toggle. This PR makes the addition of event listeners in the componentDidUpdate asynchronous again.

iwehrman commented 8 years ago

In a little more detail, here's what we found was happening:

  1. A click event on the document header buttons was handled (in the bubble phase) by the button click handler. This click handler executed an action to toggle a dialog open.
  2. The handler is of course run synchronously as part of the browser's event propagation. The action execution was also being processed in the same frame (assuming there were no blocking tasks executing in the queue).
  3. The dialog action prompts the dialog store to emit a change event (in the same frame), which prompts the search or export dialog to re-render as "open" (in the same frame), which causes the dialog to attached click handlers to the window (in the bubble phase, and in the same frame), which will close the dialog when the window receives a click event.
  4. Event propagation continues, eventually bubbling back up to the window, which now has a click event handler which dismisses the dialog. :black_large_square:

Our hypothesis is that action execution was previously running asynchronously and in a different frame from the event propagation, but that a recent bluebird upgrade changed this so that it started running in the same frame. And, indeed, petkaantonov/bluebird#915 supports this hypothesis. The "fix" is to ensure that the important part of the dialog open work happens in a different frame to ensure that any event handler that prompted a dialog open is not processed by the event handlers attached by the dialog, which are all intended to only act upon later events.