/*
 * PromiseWithProgress.java
 *
 * Copyright (C) 2009-20 by RStudio, Inc.
 *
 * Unless you have received this program directly from RStudio pursuant
 * to the terms of a commercial license agreement with RStudio, then
 * this program is licensed to you under the terms of version 3 of the
 * GNU Affero General Public License. This program is distributed WITHOUT
 * ANY EXPRESS OR IMPLIED WARRANTY, INCLUDING THOSE OF NON-INFRINGEMENT,
 * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. Please refer to the
 * AGPL (http://www.gnu.org/licenses/agpl-3.0.txt) for more details.
 *
 */

package org.rstudio.core.client.promise;

import org.rstudio.core.client.CommandWithArg;
import org.rstudio.core.client.widget.ProgressIndicator;
import org.rstudio.studio.client.RStudioGinjector;
import org.rstudio.studio.client.common.GlobalDisplay;
import org.rstudio.studio.client.common.GlobalProgressDelayer;

import elemental2.promise.IThenable;
import elemental2.promise.Promise;
import elemental2.promise.IThenable.ThenOnFulfilledCallbackFn;
import elemental2.promise.IThenable.ThenOnRejectedCallbackFn;


public class PromiseWithProgress<V>
{ 
   public PromiseWithProgress(Promise<V> promise, V errorVal, CommandWithArg<V> completed)
   {
      this(promise, "Working...", errorVal, completed);
   }
   
   public PromiseWithProgress(Promise<V> promise, String progress, V errorVal, CommandWithArg<V> completed)
   {
      // setup progress
      GlobalDisplay globalDisplay = RStudioGinjector.INSTANCE.getGlobalDisplay();
      ProgressIndicator indicator = new GlobalProgressDelayer(globalDisplay, 500, progress).getIndicator();
      
      // execute the promise
      promise.then(new ThenOnFulfilledCallbackFn<V,V>() {
         @Override
         public IThenable<V> onInvoke(V v)
         {
            indicator.onCompleted();
            completed.execute(v);
            return null;
           
         }
      },new ThenOnRejectedCallbackFn<V>() {
      
         @Override
         public IThenable<V> onInvoke(Object error)
         {
            indicator.onError(error.toString());
            completed.execute(errorVal);
            return null;
         }
      });
   }
   
}

@@ -1230,24 +1230,189 @@
 leted)%0A   %7B%0A
+      this(promise, errorVal, 500, completed);%0A   %7D%0A   %0A   public PromiseWithProgress(Promise%3CV%3E promise, V errorVal, int delayMs, CommandWithArg%3CV%3E completed)%0A   %7B%0A
       this(p
@@ -1442,16 +1442,25 @@
 rrorVal,
+ delayMs,
  complet
@@ -1543,32 +1543,45 @@
 ess, V errorVal,
+ int delayMs,
  CommandWithArg%3C
@@ -1782,19 +1782,23 @@
 isplay, 
-500
+delayMs
 , progre

change timing for promise progress