Class TriggerSupport

All Implemented Interfaces:
ConfigContext, Serializable

public class TriggerSupport extends ServerSideFeature implements ConfigContext
Per-element store of triggers, actions, outputs and bindings for the trigger API. Lazily instantiated by on(Element). Emits client snapshots via Element.executeJs(String, Object...) on every binding change and on each (re-)attach.

For internal use only.

See Also:
  • Constructor Details

    • TriggerSupport

      public TriggerSupport(StateNode node)
      Creates a TriggerSupport feature for the given state node.
      Parameters:
      node - the node
  • Method Details

    • on

      public static TriggerSupport on(Element host)
      Gets or creates the TriggerSupport for the given element.
      Parameters:
      host - the element, not null
      Returns:
      the TriggerSupport instance, never null
    • on

      public static TriggerSupport on(Component host)
      Gets or creates the TriggerSupport for the given component's root element.
      Parameters:
      host - the component, not null
      Returns:
      the TriggerSupport instance, never null
    • registerTrigger

      public int registerTrigger(AbstractTrigger trigger)
      Registers a trigger with this support, assigning it an id.
      Parameters:
      trigger - the trigger, not null
      Returns:
      the assigned id
    • registerAction

      public int registerAction(AbstractAction action)
      Registers an action with this support, assigning it an id, deduping by identity.
      Parameters:
      action - the action, not null
      Returns:
      the assigned id
    • registerOutput

      public int registerOutput(Output<?> output)
      Registers an output with this support, assigning it an id, deduping by identity.
      Specified by:
      registerOutput in interface ConfigContext
      Parameters:
      output - the output, not null
      Returns:
      the assigned id
    • referenceElement

      public int referenceElement(Element element)
      Returns a parameter index for the given element to be passed alongside the snapshot. The host's own element is at index 0 (the this of the executeJs invocation); other elements get sequential indices starting at 1.
      Specified by:
      referenceElement in interface ConfigContext
      Parameters:
      element - the element to reference, not null
      Returns:
      the parameter index
    • bind

      public void bind(AbstractTrigger trigger, Action[] actions)
      Adds a binding from a trigger to a sequence of actions.
      Parameters:
      trigger - the trigger, not null
      actions - the actions, not null or empty
    • removeTrigger

      public void removeTrigger(AbstractTrigger trigger)
      Removes a trigger and all bindings created from it.
      Parameters:
      trigger - the trigger, not null
    • getAction

      public @Nullable AbstractAction getAction(int id)
      Looks up an action by id. Used when the client posts a server-side mirror back over the applyServerSideEffect channel.
      Parameters:
      id - the action id
      Returns:
      the action, or null if unknown
    • getHost

      public Element getHost()
      Description copied from interface: ConfigContext
      The host element this snapshot belongs to. Useful for outputs that install element-scoped subscriptions (e.g. SignalOutput via ElementEffect.effect).
      Specified by:
      getHost in interface ConfigContext
      Returns:
      the host element
    • scheduleSync

      public void scheduleSync()
      Description copied from interface: ConfigContext
      Schedules a fresh client snapshot for the host to be emitted on the next beforeClientResponse flush. Used by outputs whose value may change between trigger fires (e.g. a SignalOutput). Idempotent within a request.
      Specified by:
      scheduleSync in interface ConfigContext
    • snapshotForTest

      public tools.jackson.databind.node.ObjectNode snapshotForTest()
      Builds the snapshot for testing.
      Returns:
      the snapshot
    • elementParamsForTest

      public Element[] elementParamsForTest()
      Parameter array (excluding the host at index 0) for testing.
      Returns:
      the secondary elements
    • dispatchMirrorForTest

      public void dispatchMirrorForTest(int actionId)
      Invokes the mirror dispatch for the given action id, as if the client had reported the action as fired. For testing only.
      Parameters:
      actionId - the action id