// Copyright 2000-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package com.intellij.openapi.vcs.impl.projectlevelman;

import com.intellij.openapi.application.Application;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.RequestsMerger;
import com.intellij.openapi.vfs.LocalFileSystem;
import org.jetbrains.annotations.NotNull;

public class FileWatchRequestsManager {
  private final RequestsMerger myMerger;
  private final Project myProject;

  public FileWatchRequestsManager(@NotNull Project project, @NotNull NewMappings newMappings) {
    this(project, newMappings, LocalFileSystem.getInstance());
  }

  public FileWatchRequestsManager(@NotNull Project project, @NotNull NewMappings newMappings, @NotNull LocalFileSystem localFileSystem) {
    myProject = project;
    myMerger = new RequestsMerger(new FileWatchRequestModifier(project, newMappings, localFileSystem), runnable -> {
      if (!myProject.isInitialized() || myProject.isDisposed()) {
        return;
      }

      Application application = ApplicationManager.getApplication();
      if (application.isUnitTestMode()) {
        runnable.run();
      }
      else {
        application.executeOnPooledThread(runnable);
      }
    });
  }

  public void ping() {
    myMerger.request();
  }
}

@@ -194,61 +194,8 @@
 n;%0A%0A
-import com.intellij.openapi.application.Application;%0A
 impo
@@ -299,57 +299,8 @@
 ct;%0A
-import com.intellij.openapi.util.RequestsMerger;%0A
 impo
@@ -344,16 +344,48 @@
 System;%0A
+import com.intellij.util.Alarm;%0A
 import o
@@ -479,29 +479,41 @@
 nal 
+FileWatch
 Request
-sMerg
+Modifi
 er myM
-erg
+odifi
 er;%0A
@@ -532,25 +532,21 @@
 nal 
-Project myProject
+Alarm myAlarm
 ;%0A%0A 
@@ -856,61 +856,19 @@
   my
-Project = project;%0A    myMerger = new RequestsMerger(
+Modifier = 
 new 
@@ -934,146 +934,148 @@
 tem)
-, runnable -%3E %7B%0A      if (!myProject.isInitialized() %7C%7C myProject.isDisposed()) %7B%0A        return;%0A      %7D%0A%0A      Application application =
+;%0A    myAlarm = new Alarm(Alarm.ThreadToUse.POOLED_THREAD, newMappings);%0A  %7D%0A%0A  public void ping() %7B%0A    if (myAlarm.isDisposed() %7C%7C%0A       
  App
@@ -1110,31 +1110,8 @@
 on()
-;%0A      if (application
 .isU
@@ -1137,18 +1137,18 @@
     
-  runnable
+myModifier
 .run
@@ -1159,18 +1159,14 @@
     
-  
 %7D%0A    
- 
- 
 else
@@ -1178,122 +1178,83 @@
     
-  application.executeOnPooledThread(runnable);%0A      %7D%0A    %7D);%0A  %7D%0A%0A  public void ping() %7B%0A    myMerger.request();
+myAlarm.cancelAllRequests();%0A      myAlarm.addRequest(myModifier, 0);%0A    %7D
 %0A  %7D

simplify - use Alarm instead of RequestsMerger