com.vaadin.signals.

Class SignalEnvironment

java.lang.Object
com.vaadin.signals.SignalEnvironment
public class SignalEnvironment extends Object

Global configuration required by all kinds of signals. There is an object mapper that is used for converting signal values to and from their underlying JSON representation. There's an asynchronous dispatcher used for running effect callbacks. The dispatcher does not have to ensure tasks are executed in the order they were submitted. There can optionally be dispatcher overrides that customize how tasks are dispatched in specific cases.

  • Method Details

    • tryInitialize

      public static boolean tryInitialize(com.fasterxml.jackson.databind.ObjectMapper objectMapper, Executor dispatcher)

      Initializes the global signal environment if not already initialized. The provided values are ignored if the environment is already initialized. The environment must be initialized before signals are used.

      Parameters:

      objectMapper - the object mapper to use, not null

      dispatcher - the asynchronous dispatcher to use, not null

      Returns:

      true if the provided values were used to initialize the environment, false if the environment was already initialized

    • initialized

      public static boolean initialized()

      Checks whether the environment is already initialized.

      Returns:

      true if initialized, false if not initialized

    • addDispatcherOverride

      public static Runnable addDispatcherOverride(Supplier<Executor> dispatcherOverride)

      Adds a new supplier for override dispatchers. A supplier can inspect the circumstances under in which it's run and optionally provide a dispatcher to use for tasks initiated under those circumstances. The supplier is expected to use its own thread local variables to understand the context.

      The dispatcher is used both for asynchronously dispatched effect callbacks and for resolving signal operation results. The dispatcher needs to preserve ordering within the context that it belongs to (which is up to the implementation to define) so that operation results are published in order of confirmation.

      Parameters:

      dispatcherOverride - a supplier that can return a dispatcher to use or null to not provide any dispatcher under those circumstances. Not null.

      Returns:

      a callback that can be used to unregister the supplier, not null

    • objectMapper

      public static com.fasterxml.jackson.databind.ObjectMapper objectMapper()

      Gets the object mapper to use for converting signal values to and from their underlying JSON representation

      Returns:

      the object mapper, not null

      Throws:

      IllegalStateException - if the environment has not yet been initialized

    • defaultDispatcher

      public static Executor defaultDispatcher()

      Gets the dispatcher that was set when the environment was initialized without looking up overrides.

      Returns:

      the configured dispatcher, not null

      Throws:

      IllegalStateException - if the environment has not yet been initialized

    • asynchronousDispatcher

      public static Executor asynchronousDispatcher()

      Gets a dispatcher to use for asynchronous tasks. This is used e.g. for running effect callbacks. The dispatcher does not guarantee that tasks are run in the order they have been submitted. This method uses a dispatcher override if any supplier matches and otherwise uses the dispatcher provided when the environment was initialized.

      Returns:

      the dispatcher to use, not null

      Throws:

      IllegalStateException - if the environment has not yet been initialized

    • synchronousDispatcher

      public static Executor synchronousDispatcher()

      Gets a dispatcher to use for synchronous tasks. This is used e.g. for resolving operation results. The dispatcher guarantees that tasks for the same context are run in the order they have been submitted. This method uses a dispatcher override if any supplier matches and otherwise uses a dispatcher that runs tasks on the invoking thread.

      Returns:

      the dispatcher to use, not null

      Throws:

      IllegalStateException - if the environment has not yet been initialized