Skip to content

[perf] stop blocking on WorkspaceCache.refresh #8097

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 23, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 16 additions & 12 deletions flutter-idea/src/io/flutter/bazel/WorkspaceCache.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,18 @@

import com.google.common.base.Objects;
import com.google.common.collect.ImmutableSet;
import com.intellij.openapi.application.ReadAction;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.registry.Registry;
import com.intellij.util.concurrency.AppExecutorUtil;
import io.flutter.FlutterUtils;
import io.flutter.dart.FlutterDartAnalysisServer;
import io.flutter.project.ProjectWatch;
import io.flutter.utils.FileWatch;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import javax.swing.*;
import java.util.LinkedHashSet;
import java.util.Set;
import java.util.concurrent.atomic.AtomicReference;
Expand All @@ -35,7 +37,7 @@ public class WorkspaceCache {

private boolean refreshScheduled = false;

private final Set<Runnable> subscribers = new LinkedHashSet<>();
private final @NotNull Set<@NotNull Runnable> subscribers = new LinkedHashSet<>();

private WorkspaceCache(@NotNull final Project project) {
this.project = project;
Expand All @@ -44,7 +46,7 @@ private WorkspaceCache(@NotNull final Project project) {
// dartProjectsWithoutPubspecRegistryKey registry key.
//
// https://github.com/flutter/flutter-intellij/issues/7333
if(Registry.is(dartProjectsWithoutPubspecRegistryKey, false)) {
if (Registry.is(dartProjectsWithoutPubspecRegistryKey, false)) {
this.cache = null;
return;
}
Expand Down Expand Up @@ -78,13 +80,15 @@ private void scheduleRefresh() {
return;
}
refreshScheduled = true;
SwingUtilities.invokeLater(() -> {
refreshScheduled = false;
if (project.isDisposed()) {
return;
}
refresh();
});
ReadAction.nonBlocking(() -> {
refreshScheduled = false;
if (!project.isDisposed()) {
refresh();
}
return null;
})
.expireWith(FlutterDartAnalysisServer.getInstance(project))
.submit(AppExecutorUtil.getAppExecutorService());
}

@NotNull
Expand Down Expand Up @@ -112,7 +116,7 @@ public boolean isBazel() {
/**
* Runs a callback each time the current Workspace changes.
*/
public void subscribe(Runnable callback) {
public void subscribe(@NotNull Runnable callback) {
synchronized (subscribers) {
subscribers.add(callback);
}
Expand Down Expand Up @@ -152,7 +156,7 @@ private void refresh() {
notifyListeners();
}

private Set<Runnable> getSubscribers() {
private Set<@NotNull Runnable> getSubscribers() {
synchronized (subscribers) {
return ImmutableSet.copyOf(subscribers);
}
Expand Down