Skip to content

Commit 2d0b8ad

Browse files
committed
Ensured that only one GraphQL configuration scan background task is queued at a time (#230, #164)
1 parent 9ff824b commit 2d0b8ad

File tree

1 file changed

+29
-3
lines changed

1 file changed

+29
-3
lines changed

src/main/com/intellij/lang/jsgraphql/ide/project/graphqlconfig/GraphQLConfigManager.java

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,8 @@ public class GraphQLConfigManager {
139139
private final Lock writeLock = cacheLock.writeLock();
140140
private final Lock readLock = cacheLock.readLock();
141141

142+
private final Ref<Runnable> buildConfigurationModelCallable = Ref.create();
143+
142144
public GraphQLConfigManager(Project myProject) {
143145
this.myProject = myProject;
144146
this.projectScope = GlobalSearchScope.projectScope(myProject);
@@ -487,17 +489,41 @@ private boolean hasGraphQLConfigComment(PsiElement psiElement) {
487489
*/
488490
public void buildConfigurationModel(@Nullable List<VirtualFile> changedConfigurationFiles, @Nullable Runnable onCompleted) {
489491
ApplicationManager.getApplication().invokeLater(() -> {
492+
493+
// runs on the UI thread so task scheduling can be considered atomic
494+
final boolean hasExistingTask = buildConfigurationModelCallable.get() != null;
495+
496+
// set the runnable that the task uses to the latest refresh info
497+
buildConfigurationModelCallable.set(() -> {
498+
doBuildConfigurationModel(changedConfigurationFiles);
499+
if (onCompleted != null) {
500+
onCompleted.run();
501+
}
502+
});
503+
504+
if (hasExistingTask) {
505+
// already queued up the task
506+
return;
507+
}
508+
490509
final Task.Backgroundable task = new Task.Backgroundable(myProject, "GraphQL Configuration Scan", false) {
491510
@Override
492511
public void run(@NotNull ProgressIndicator indicator) {
493512
indicator.setIndeterminate(true);
494513
DumbService.getInstance(myProject).runReadActionInSmartMode(() -> {
495-
doBuildConfigurationModel(changedConfigurationFiles);
496-
if (onCompleted != null) {
497-
onCompleted.run();
514+
final Runnable runnable = buildConfigurationModelCallable.get();
515+
if (runnable != null) {
516+
runnable.run();
498517
}
499518
});
500519
}
520+
521+
@Override
522+
public void onFinished() {
523+
// also called on UI thread
524+
buildConfigurationModelCallable.set(null);
525+
}
526+
501527
};
502528
if (!myProject.isDisposed()) {
503529
ProgressManager.getInstance().run(task);

0 commit comments

Comments
 (0)