package replicant;

import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;

/**
 * A thin abstraction for scheduling callbacks that works in JVM and GWT environments.
 */
final class Scheduler
{
  private static final SchedulerSupport c_support = new SchedulerSupport();

  public static void schedule( @Nonnull final SafeFunction<Boolean> command )
  {
    c_support.schedule( command );
  }

  /**
   * JVM Compatible variant which will have fields and methods stripped out during GWT compile and thus fallback to GWT variant.
   */
  private static final class SchedulerSupport
    extends AbstractSchedulerSupport
  {
    @GwtIncompatible
    private final ScheduledExecutorService _executorService = Executors.newScheduledThreadPool( 1 );
    @GwtIncompatible
    @Nullable
    private ScheduledFuture<?> _future;

    @GwtIncompatible
    @Override
    void schedule( @Nonnull final SafeFunction<Boolean> command )
    {
      final Runnable action = () -> {
        if ( !command.call() && null != _future )
        {
          _future.cancel( false );
          _future = null;
        }
      };
      _future = _executorService.scheduleAtFixedRate( action, 0, 1, TimeUnit.MILLISECONDS );
    }
  }

  private static abstract class AbstractSchedulerSupport
  {
    void schedule( @Nonnull final SafeFunction<Boolean> command )
    {
      com.google.gwt.core.client.Scheduler.get().scheduleIncremental( command::call );
    }
  }
}

@@ -155,46 +155,8 @@
 re;%0A
-import java.util.concurrent.TimeUnit;%0A
 impo
@@ -1080,54 +1080,60 @@
     
-final Runnable action = () -%3E %7B
+//noinspection StatementWithEmptyBody
 %0A      
-  if
+while
  ( 
-!
 comm
@@ -1146,27 +1146,8 @@
 ll()
- && null != _future
  )%0A 
@@ -1151,26 +1151,24 @@
 )%0A      
-  
 %7B%0A      
     _fut
@@ -1163,174 +1163,9 @@
     
-    _future.cancel( false );%0A          _future = null;%0A        %7D%0A      %7D;%0A      _future = _executorService.scheduleAtFixedRate( action, 0, 1, TimeUnit.MILLISECONDS );
+%7D
 %0A   

Update scheduler to execute all the steps in success in the JVM