/*
 * Copyright 2012-2015 JetBrains s.r.o
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package jetbrains.jetpad.base.edt;

import com.google.gwt.core.client.Scheduler;
import com.google.gwt.user.client.Timer;
import jetbrains.jetpad.base.Registration;

public final class JsEventDispatchThread implements EventDispatchThread {
  public static final JsEventDispatchThread INSTANCE = new JsEventDispatchThread();

  private JsEventDispatchThread() {
  }

  @Override
  public void schedule(final Runnable r) {
    Scheduler.get().scheduleFinally(new Scheduler.ScheduledCommand() {
      @Override
      public void execute() {
        r.run();
      }
    });
  }

  @Override
  public Registration schedule(int delay, final Runnable r) {
    final Timer timer = new Timer() {
      @Override
      public void run() {
        r.run();
      }
    };
    timer.schedule(delay);
    return timerReg(timer);
  }

  @Override
  public Registration scheduleRepeating(int period, final Runnable r) {
    final Timer timer = new Timer() {
      @Override
      public void run() {
        r.run();
      }
    };
    timer.scheduleRepeating(period);
    return timerReg(timer);
  }

  private Registration timerReg(final Timer timer) {
    return new Registration() {
      @Override
      protected void doRemove() {
        if (timer.isRunning()) {
          timer.cancel();
        }
      }
    };
  }
}
@@ -631,16 +631,55 @@
 e.edt;%0A%0A
+import com.google.gwt.core.client.GWT;%0A
 import c
@@ -800,16 +800,82 @@
 ation;%0A%0A
+import java.util.logging.Level;%0Aimport java.util.logging.Logger;%0A%0A
 public f
@@ -940,16 +940,109 @@
 hread %7B%0A
+  private static final Logger LOG = Logger.getLogger(JsEventDispatchThread.class.getName());%0A
   public
@@ -1343,464 +1343,1211 @@
     
-r.run();%0A      %7D%0A    %7D);%0A  %7D%0A%0A  @Override%0A  public Registration schedule(int delay, final Runnable r) %7B%0A    final Timer timer = new Timer() %7B%0A      @Override%0A      public void run() %7B%0A        r.run();%0A      %7D%0A    %7D;%0A    timer.schedule(delay);%0A    return timerReg(timer);%0A  %7D%0A%0A  @Override%0A  public Registration scheduleRepeating(int period, final Runnable r) %7B%0A    final Timer timer = new Timer() %7B%0A      @Override%0A      public void run() %7B%0A        r.run();
+try %7B%0A          r.run();%0A        %7D catch (Throwable t) %7B%0A          LOG.log(Level.SEVERE, %22Runnable submitted to JsEventDispatchThread failed%22, t);%0A          GWT.log(%22Runnable submitted to JsEventDispatchThread failed. Message: %22 + t.getMessage());%0A        %7D%0A      %7D%0A    %7D);%0A  %7D%0A%0A  @Override%0A  public Registration schedule(int delay, final Runnable r) %7B%0A    final Timer timer = new Timer() %7B%0A      @Override%0A      public void run() %7B%0A        try %7B%0A          r.run();%0A        %7D catch (Throwable t) %7B%0A          LOG.log(Level.SEVERE, %22Runnable submitted to JsEventDispatchThread failed%22, t);%0A          GWT.log(%22Runnable submitted to JsEventDispatchThread failed. Message: %22 + t.getMessage());%0A        %7D%0A      %7D%0A    %7D;%0A    timer.schedule(delay);%0A    return timerReg(timer);%0A  %7D%0A%0A  @Override%0A  public Registration scheduleRepeating(int period, final Runnable r) %7B%0A    final Timer timer = new Timer() %7B%0A      @Override%0A      public void run() %7B%0A        try %7B%0A          r.run();%0A        %7D catch (Throwable t) %7B%0A          LOG.log(Level.SEVERE, %22Runnable submitted to JsEventDispatchThread failed%22, t);%0A          GWT.log(%22Runnable submitted to JsEventDispatchThread failed. Message: %22 + t.getMessage());%0A        %7D
 %0A   
@@ -2852,8 +2852,9 @@
 %7D;%0A  %7D%0A%7D
+%0A

add exceptions logging to JsEventDispatchThread