-
Notifications
You must be signed in to change notification settings - Fork 0
First version of event-dispatcher documentation. #372
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
EHandtkeBasis
wants to merge
11
commits into
main
Choose a base branch
from
event-dispatcher
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 3 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
25ae474
First version of event-dispatcher documentation.
EHandtkeBasis 93ce3a5
Rewrote event dispatcher doc and added more detail about custom event…
EHandtkeBasis ba04b61
Refactored according to feedback.
EHandtkeBasis 0c65c9f
Merge branch 'main' into event-dispatcher
EHandtkeBasis 2110052
Fixed linting issues.
EHandtkeBasis 51f5762
Merge branch 'main' into event-dispatcher
EHandtkeBasis 49a646a
Merge branch 'main' into event-dispatcher
EHandtkeBasis 6bbce20
Reworked event dispatcher doc.
EHandtkeBasis 8d8348b
Chore: Revision edits for PR #372
bbrennanbasis 51b5f9a
Merge branch 'main' into event-dispatcher
EHandtkeBasis 2e57228
Adjusted according to feedback.
EHandtkeBasis File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,81 @@ | ||
| --- | ||
| title: Event Dispatcher System | ||
| description: How the webforJ event dispatcher system works and how to use it for event-driven programming. | ||
| sidebar_position: 50 | ||
| --- | ||
|
|
||
| The webforJ event dispatcher system provides a flexible and type-safe way to handle events in your app. It allows you to register listeners for specific event types and dispatch events to those listeners, enabling event-driven programming across components. | ||
|
|
||
|
|
||
| ## `EventDispatcher` | ||
|
|
||
| [`EventDispatcher`](https://webforj.com/javadoc/com/webforj/dispatcher/EventDispatcher.html) is a minimalistic event manager for dispatching events to listeners. It's not tied to UI components or element events. | ||
|
|
||
| ### Available APIs | ||
|
|
||
| - `addListener(Class<T> eventClass, EventListener<T> listener)`: Registers a listener for a specific event type. Returns a `ListenerRegistration<T>` for later removal. | ||
| - `removeListener(Class<T> eventClass, EventListener<T> listener)`: Removes a specific listener for the given event type. | ||
| - `removeAllListeners(Class<T> eventClass)`: Removes all listeners for a given event type. | ||
| - `removeAllListeners()`: Removes all listeners from the dispatcher. | ||
| - `dispatchEvent(T event)`: Notifies all listeners of the given event. | ||
|
|
||
| <ComponentDemo | ||
| path='/webforj/eventdispatchercustomevent' | ||
| javaE='https://raw.githubusercontent.com/webforj/webforj-documentation/refs/heads/main/src/main/java/com/webforj/samples/views/advanced/EventDispatcherCustomEventView.java' | ||
| height = '300px' | ||
| /> | ||
|
|
||
| ### Creating and registering listeners | ||
|
|
||
| ```java | ||
| import com.webforj.dispatcher.EventDispatcher; | ||
| import com.webforj.dispatcher.EventListener; | ||
| import java.util.EventObject; | ||
|
|
||
| // Create the dispatcher | ||
| EventDispatcher dispatcher = new EventDispatcher(); | ||
|
|
||
| // Register a listener | ||
| ListenerRegistration<EventObject> reg = dispatcher.addListener(EventObject.class, event -> { | ||
| // handle event | ||
| System.out.println("Event received: " + event); | ||
| }); | ||
|
|
||
| // Remove a listener | ||
| reg.remove(); | ||
| // or | ||
| dispatcher.removeListener(EventObject.class, reg.getListener()); | ||
|
|
||
| // Dispatch an event | ||
| dispatcher.dispatchEvent(new EventObject(this)); | ||
| ``` | ||
|
|
||
| ## Advanced registration options | ||
|
|
||
| For advanced scenarios, you can register listeners with additional options using `ElementEventOptions`. These options allow you to: | ||
| - Add custom data to the event payload | ||
| - Debounce or throttle event firing | ||
| - Filter events based on conditions | ||
| - Execute JavaScript before the event is fired (for UI events) | ||
|
|
||
| Example: | ||
|
|
||
| ```java | ||
| ElementEventOptions options = new ElementEventOptions(); | ||
| options.addData("value", "event.target.value"); | ||
| options.setDebounce(300); // Debounce by 300ms | ||
| options.setFilter("event.target.value.length > 2"); | ||
|
|
||
| myComponent.addEventListener("input", event -> { | ||
| String value = (String) event.getData().get("value"); | ||
| System.out.println("Input value: " + value); | ||
| }, options); | ||
| ``` | ||
|
|
||
| ## Best practices | ||
|
|
||
| - Always remove listeners when they're no longer needed to avoid memory leaks. | ||
| - Use the provided event payload methods to access event data efficiently, avoiding unnecessary client-server round-trips. | ||
| - Prefer using component APIs for event handling unless you have advanced requirements. | ||
| - Use registration options (payload, debounce, filter) to optimize performance and event data handling. | ||
|
|
||
63 changes: 63 additions & 0 deletions
63
src/main/java/com/webforj/samples/views/advanced/EventDispatcherCustomEventView.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| package com.webforj.samples.views.advanced; | ||
|
|
||
| import com.webforj.dispatcher.EventDispatcher; | ||
| import com.webforj.router.annotation.FrameTitle; | ||
| import com.webforj.router.annotation.Route; | ||
| import com.webforj.component.button.Button; | ||
| import com.webforj.component.Composite; | ||
| import com.webforj.component.html.elements.Div; | ||
| import java.util.EventObject; | ||
|
|
||
| @Route | ||
| @FrameTitle("Event Dispatcher") | ||
| public class EventDispatcherCustomEventView extends Composite<Div> { | ||
| private final EventDispatcher dispatcher = new EventDispatcher(); | ||
|
|
||
| /** | ||
| * A custom event that carries a message string. | ||
| */ | ||
| public static class CustomMessageEvent extends EventObject { | ||
| private final String message; | ||
|
|
||
| public CustomMessageEvent(Object source, String message) { | ||
| super(source); | ||
| this.message = message; | ||
| } | ||
|
|
||
| public String getMessage() { | ||
| return message; | ||
| } | ||
| } | ||
|
|
||
| public EventDispatcherCustomEventView() { | ||
| Button button = new Button("Fire Custom Event"); | ||
|
|
||
| Div statusText = new Div("waiting for custom event"); | ||
| statusText.setStyle("border", "2px solid #333"); | ||
| statusText.setStyle("padding", "8px 24px"); | ||
| statusText.setStyle("margin-top", "24px"); | ||
| statusText.setStyle("border-radius", "6px"); | ||
| statusText.setStyle("font-size", "1.1em"); | ||
| statusText.setStyle("background", "#fafbfc"); | ||
|
|
||
| // Center the content using flexbox styles on the root Div | ||
| Div root = this.getBoundComponent(); | ||
| root.setStyle("display", "flex"); | ||
| root.setStyle("flex-direction", "column"); | ||
| root.setStyle("justify-content", "center"); | ||
| root.setStyle("align-items", "center"); | ||
| root.setStyle("height", "100vh"); | ||
|
|
||
| root.add(button); | ||
| root.add(statusText); | ||
|
|
||
| // Register a listener for the custom event | ||
| dispatcher.addListener(CustomMessageEvent.class, event -> { | ||
| statusText.setText("received custom event "); | ||
| button.setEnabled(false); | ||
| }); | ||
|
|
||
| // Fire the custom event with a message when the button is clicked | ||
| button.onClick(e -> dispatcher.dispatchEvent(new CustomMessageEvent(this, "Hello from custom event!"))); | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[webforJ.BeDirect] Avoid using 'optimize performance and event'. Focus more on explicitly giving details about the feature.