Interface JsSnippets

All Superinterfaces:
Serializable
All Known Subinterfaces:
ClickNotifier.ClickFiltersJs, Clipboard.PasteJs, DownloadAction.DownloadJs, OpenInNewTabAction.OpenInNewTabJs, ReadFromClipboardAction.ReadFromClipboardJs, RequestFullscreenAction.RequestFullscreenJs, ShareAction.ShareJs, ShortcutRegistration.ShortcutFilterJs, TriggerInputJs, TriggerJs, UIInternals.ChunkJs, WriteToClipboardAction.WriteToClipboardJs

public interface JsSnippets extends Serializable
Marks an interface whose methods declare snippets: JavaScript that is not run when the method is called, but evaluated later on the client by what the returned value is passed to. An implementation is obtained from of(Class).

Like with JsInvokers, an interface that is passed to of(Class) must itself be annotated with JsDefinition, which is what makes it findable during the build. Annotations on interfaces are not inherited. An interface can not extend both marker interfaces. See JsDefinition for how a definition that extends another interface is declared.

A snippet is not bound to an element, which is why the implementation comes from here rather than from Element.executeJs(Class). It holds no state, so one implementation can be kept in a constant and shared.

A snippet declares a function that a call of another definition then gets as an argument:

 @JsDefinition
 public interface LoggerJs extends JsSnippets {

     @JsExpression("console.log($0, value);")
     @JsFunctionParameterNames("value")
     JsFunction createLogger(String prefix);
 }

 @JsDefinition
 public interface ClickJs extends JsInvokers {

     @JsExpression("this.addEventListener('click', () => $0('clicked'));")
     void onClick(JsFunction handler);
 }

 static final LoggerJs LOGGER = JsSnippets.of(LoggerJs.class);

 button.getElement().executeJs(ClickJs.class)
         .onClick(LOGGER.createLogger("Button:"));
 
The build collects both declarations into the bundle, roughly as:
 // LoggerJs.createLogger: a snippet, evaluated with the captured arguments
 jsSnippets["<id>"] = function ($0) {
     return function (value) {
         console.log($0, value);
     };
 };
 // ClickJs.onClick: a call, run on the element it was obtained from
 jsDefinitions["<id>"] = async function ($0) {
     this.addEventListener('click', () => $0('clicked'));
 };
 
The server sends the browser only the two identifiers and the argument "Button:". The client calls the snippet function with "Button:" to get the logger, and runs onClick on the button with that logger as $0, so a click logs Button: clicked. Nothing is compiled from a string, which is what keeps this working under a content security policy that does not allow unsafe-eval.
Since:
25.4
See Also:
  • Method Summary

    Static Methods
    Modifier and Type
    Method
    Description
    static <T extends JsSnippets>
    T
    of(Class<T> definitionType)
    Gets an implementation of the given interface, whose methods answer with the snippet they declare.
  • Method Details

    • of

      static <T extends JsSnippets> T of(Class<T> definitionType)
      Gets an implementation of the given interface, whose methods answer with the snippet they declare.
      Type Parameters:
      T - the type of the interface
      Parameters:
      definitionType - the interface that declares the snippets, annotated with JsDefinition, not null
      Returns:
      an implementation of the interface, not null
      Throws:
      IllegalArgumentException - if the type is not an interface, is not annotated with JsDefinition, also extends JsInvokers, or has a method that does not declare a snippet