Skip to content

Commit f2c29c5

Browse files
authored
[perf] stop blocking on WorkspaceCache.refresh (#8097)
Fixes: #8083 See: #8089 Tested w/ https://github.com/scalio/bazel-flutter ## Pre-launch Checklist - [x] I read the [Contributor Guide] and followed the process outlined there for submitting PRs. - [x] I read the [Tree Hygiene] wiki page, which explains my responsibilities. - [x] I read the [Flutter Style Guide] _recently_, and have followed its advice. - [x] I signed the [CLA]. - [x] I listed at least one issue that this PR fixes in the description above. - [x] I updated/added relevant documentation (doc comments with `///`). - [x] I added new tests to check the change I am making, or this PR is [test-exempt]. - [x] All existing and new tests are passing. If you need help, consider asking for advice on the #hackers-new channel on [Discord]. <!-- Links --> [Contributor Guide]: https://github.com/flutter/flutter/blob/master/docs/contributing/Tree-hygiene.md#overview [Tree Hygiene]: https://github.com/flutter/flutter/blob/master/docs/contributing/Tree-hygiene.md [test-exempt]: https://github.com/flutter/flutter/blob/master/docs/contributing/Tree-hygiene.md#tests [Flutter Style Guide]: https://github.com/flutter/flutter/blob/master/docs/contributing/Style-guide-for-Flutter-repo.md [CLA]: https://cla.developers.google.com/ [flutter/tests]: https://github.com/flutter/tests [breaking change policy]: https://github.com/flutter/flutter/blob/master/docs/contributing/Tree-hygiene.md#handling-breaking-changes [Discord]: https://github.com/flutter/flutter/blob/master/docs/contributing/Chat.md
1 parent 673095e commit f2c29c5

File tree

1 file changed

+16
-12
lines changed

1 file changed

+16
-12
lines changed

flutter-idea/src/io/flutter/bazel/WorkspaceCache.java

+16-12
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,18 @@
77

88
import com.google.common.base.Objects;
99
import com.google.common.collect.ImmutableSet;
10+
import com.intellij.openapi.application.ReadAction;
1011
import com.intellij.openapi.diagnostic.Logger;
1112
import com.intellij.openapi.project.Project;
1213
import com.intellij.openapi.util.registry.Registry;
14+
import com.intellij.util.concurrency.AppExecutorUtil;
1315
import io.flutter.FlutterUtils;
16+
import io.flutter.dart.FlutterDartAnalysisServer;
1417
import io.flutter.project.ProjectWatch;
1518
import io.flutter.utils.FileWatch;
1619
import org.jetbrains.annotations.NotNull;
1720
import org.jetbrains.annotations.Nullable;
1821

19-
import javax.swing.*;
2022
import java.util.LinkedHashSet;
2123
import java.util.Set;
2224
import java.util.concurrent.atomic.AtomicReference;
@@ -35,7 +37,7 @@ public class WorkspaceCache {
3537

3638
private boolean refreshScheduled = false;
3739

38-
private final Set<Runnable> subscribers = new LinkedHashSet<>();
40+
private final @NotNull Set<@NotNull Runnable> subscribers = new LinkedHashSet<>();
3941

4042
private WorkspaceCache(@NotNull final Project project) {
4143
this.project = project;
@@ -44,7 +46,7 @@ private WorkspaceCache(@NotNull final Project project) {
4446
// dartProjectsWithoutPubspecRegistryKey registry key.
4547
//
4648
// https://github.com/flutter/flutter-intellij/issues/7333
47-
if(Registry.is(dartProjectsWithoutPubspecRegistryKey, false)) {
49+
if (Registry.is(dartProjectsWithoutPubspecRegistryKey, false)) {
4850
this.cache = null;
4951
return;
5052
}
@@ -78,13 +80,15 @@ private void scheduleRefresh() {
7880
return;
7981
}
8082
refreshScheduled = true;
81-
SwingUtilities.invokeLater(() -> {
82-
refreshScheduled = false;
83-
if (project.isDisposed()) {
84-
return;
85-
}
86-
refresh();
87-
});
83+
ReadAction.nonBlocking(() -> {
84+
refreshScheduled = false;
85+
if (!project.isDisposed()) {
86+
refresh();
87+
}
88+
return null;
89+
})
90+
.expireWith(FlutterDartAnalysisServer.getInstance(project))
91+
.submit(AppExecutorUtil.getAppExecutorService());
8892
}
8993

9094
@NotNull
@@ -112,7 +116,7 @@ public boolean isBazel() {
112116
/**
113117
* Runs a callback each time the current Workspace changes.
114118
*/
115-
public void subscribe(Runnable callback) {
119+
public void subscribe(@NotNull Runnable callback) {
116120
synchronized (subscribers) {
117121
subscribers.add(callback);
118122
}
@@ -152,7 +156,7 @@ private void refresh() {
152156
notifyListeners();
153157
}
154158

155-
private Set<Runnable> getSubscribers() {
159+
private Set<@NotNull Runnable> getSubscribers() {
156160
synchronized (subscribers) {
157161
return ImmutableSet.copyOf(subscribers);
158162
}

0 commit comments

Comments
 (0)