Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8303209

Make it easier to fire an event and find out whether it was consumed

    XMLWordPrintable

Details

    • Enhancement
    • Resolution: Unresolved
    • P4
    • tbd
    • None
    • javafx
    • None

    Description

      When creating your own events, you may want to know if the event was consumed or not. A simple use case is when there are multiple possible events you want to send (in response to some action), but if the first one was consumed, the others shouldn't be sent.

      Currently, Node#fireEvent has a void return - most internal event dispatching methods either return `null` or a new Event, allowing you to check for `null` to see if the event was consumed. Checking the event you passed in with `isConsumed` is pointless as the event is copied many times during dispatching.

      Proposal:

      Add a new default `dispatchEvent` on the `EventTarget` interface (named `dispatchEvent` to avoid conflict with `fireEvent` in `Node`) with the signature:

          /**
           * Dispatches the specified event to this event target.
           * The return value represents the event after processing completes.
           * If further processing is to be done after the call the event referenced
           * by the return value should be used instead of the original event. In the
           * case the event is fully handled / consumed in the chain the returned
           * value is {@code null} and no further processing should be done with that
           * event.
           *
           * @param event the event to dispatch
           * @return the processed event or {@code null} if the event had been fully
           * handled / consumed
           */
           Event dispatchEvent(Event);

      (documentation copied from `EventDispatchChain#dispatchEvent`)

      It's implementation would be moved from `EventUtil` to `EventTarget` (the implementation in `EventUtil` is fully static and can be moved to the `EventTarget` interface).

      Risks:

      This would provide a `dispatchEvent` method for every `EventTarget`. This is however not really an issue as `EventTarget` already exposes `buildEventDispatchChain`, which can be used to fire events any way (it requires an implementation of `EventDispatchChain` to be passed, which is also a public interface, for which the user must provide an implementation).

      There isn't really anything stopping users from doing this, and this proposal only simplifies the process while providing functionality to see the consumed status of your event.

      Alternatives:

      Provide a public implementation of `EventDispatchChain`. This would then allow users to call:

            eventTarget.buildEventDispatchChain(new PublicEventDispatchChain())
                .dispatchEvent(event);

      vs proposed:

            eventTarget.dispatchEvent(event);






      Attachments

        Activity

          People

            jhendrikx John Hendrikx
            jhendrikx John Hendrikx
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

              Created:
              Updated: