package org.realityforge.arez.api2;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;

public class Reaction
  extends Observer
{
  /**
   * Flag indicating whether this reaction has been scheduled. i.e. has a pending reaction.
   */
  private boolean _scheduled;

  Reaction( @Nonnull final ArezContext context, @Nullable final String name )
  {
    super( context, name );
    setOnStale( this::schedule );
  }

  final boolean isScheduled()
  {
    return _scheduled;
  }

  final void schedule()
  {
    Guards.invariant( this::isActive,
                      () -> String.format(
                        "Observer named '%s' is not active but an attempt has been made to schedule observer.",
                        getName() ) );
    if ( !_scheduled )
    {
      _scheduled = true;
      getContext().scheduleReaction( this );
    }
  }
}

@@ -30,16 +30,42 @@
 .api2;%0A%0A
+import java.util.Objects;%0A
 import j
@@ -300,16 +300,134 @@
 heduled;
+%0A  /**%0A   * The transaction mode in which the action executes.%0A   */%0A  @Nonnull%0A  private final TransactionMode _mode;
 %0A%0A  Reac
@@ -495,16 +495,53 @@
 ing name
+, @Nonnull final TransactionMode mode
  )%0A  %7B%0A 
@@ -601,16 +601,276 @@
 dule );%0A
+    _mode = Objects.requireNonNull( mode );%0A  %7D%0A%0A  /**%0A   * Return the transaction mode in which the action executes.%0A   *%0A   * @return the transaction mode in which the action executes.%0A   */%0A  @Nonnull%0A  final TransactionMode getMode()%0A  %7B%0A    return _mode;%0A
   %7D%0A%0A  f

Add the transaction mode to the reaction