Class Trigger
- All Implemented Interfaces:
Serializable
- Direct Known Subclasses:
DomEventTrigger
BroadcastChannel message, a media-query match, … — and, when it does,
runs one or more actions.
Actions run synchronously inside the browser's handler when the trigger
fires; for triggers that originate from a user gesture (click, keypress, …)
this preserves the gesture context for downstream actions, letting them
invoke browser APIs that require it (clipboard, fullscreen, file picker,
share, …). Note that most such APIs are themselves asynchronous: the action
is dispatched synchronously, but any server-observable effect — for example,
a callback reporting whether navigator.clipboard.writeText resolved —
may reach the server arbitrarily later than the gesture itself, after one or
more event-loop turns.
Each call to triggers(Action...) produces one
addJsInitializer registration on the host
element; remove() removes all such registrations. Subclasses provide
the JS that installs and tears down the listener by overriding
installJs(); the action handler is exposed to that JS as a
JsFunction captured at $0.
For internal use only. May be renamed or removed in a future release.
- See Also:
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionfinal ElementgetHost()The host element this trigger fires on.protected abstract StringBuilds the JS expression that installs this trigger's listener and returns a cleanup function that removes it.final voidremove()Removes this trigger and all wirings created from it.final voidWires the given actions to this trigger.
-
Constructor Details
-
Trigger
Creates a new trigger bound to the given host component's root element.- Parameters:
host- the component whose root element the trigger fires on, notnull
-
-
Method Details
-
getHost
The host element this trigger fires on.- Returns:
- the host element, never
null
-
triggers
Wires the given actions to this trigger. They run in the order given the next time this trigger fires. Each call adds another wiring; the existing ones are kept.- Parameters:
actions- the actions to run, notnullor empty
-
installJs
Builds the JS expression that installs this trigger's listener and returns a cleanup function that removes it.The expression runs with
thisbound to the host element. The handlerJsFunctionis available as$0; subclasses pass it to whatever client API the trigger wraps (e.g.this.addEventListener(name, $0)) and reference the same$0in the cleanup callback to detach it.- Returns:
- the JS install expression, not
null
-
remove
public final void remove()Removes this trigger and all wirings created from it. The corresponding client-side listeners are detached as part of the next synchronisation.
-