forked from flutter/flutter-intellij
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWorkspaceCache.java
182 lines (156 loc) · 5.04 KB
/
WorkspaceCache.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/*
* Copyright 2017 The Chromium Authors. All rights reserved.
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
package io.flutter.bazel;
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 java.util.LinkedHashSet;
import java.util.Set;
import java.util.concurrent.atomic.AtomicReference;
import static java.util.Objects.requireNonNull;
/**
* Holds the current Bazel workspace for a Project.
* <p>
* <p>Automatically reloads the workspace when out of date.
*/
public class WorkspaceCache {
@NotNull private final Project project;
@Nullable private Workspace cache;
private boolean disconnected = false;
private boolean refreshScheduled = false;
private final @NotNull Set<@NotNull Runnable> subscribers = new LinkedHashSet<>();
private WorkspaceCache(@NotNull final Project project) {
this.project = project;
// Do not attempt to set the WorkspaceCache unless the user has configured the
// dartProjectsWithoutPubspecRegistryKey registry key.
//
// https://github.com/flutter/flutter-intellij/issues/7333
if (Registry.is(dartProjectsWithoutPubspecRegistryKey, false)) {
this.cache = null;
return;
}
// Trigger a reload when file dependencies change.
final AtomicReference<FileWatch> fileWatch = new AtomicReference<>();
subscribe(() -> {
if (project.isDisposed()) return;
final Workspace next = cache;
FileWatch nextWatch = null;
if (next != null) {
nextWatch = FileWatch.subscribe(next.getRoot(), next.getDependencies(), this::scheduleRefresh);
nextWatch.setDisposeParent(project);
}
final FileWatch prevWatch = fileWatch.getAndSet(nextWatch);
if (prevWatch != null) prevWatch.unsubscribe();
});
// Detect module root changes.
try (var projectWatch = ProjectWatch.subscribe(project, this::scheduleRefresh)) {
// Load initial value.
refresh();
}
}
private void scheduleRefresh() {
if (refreshScheduled) {
return;
}
refreshScheduled = true;
ReadAction.nonBlocking(() -> {
refreshScheduled = false;
if (!project.isDisposed()) {
refresh();
}
return null;
})
.expireWith(FlutterDartAnalysisServer.getInstance(project))
.submit(AppExecutorUtil.getAppExecutorService());
}
@NotNull
public static WorkspaceCache getInstance(@NotNull final Project project) {
return requireNonNull(project.getService(WorkspaceCache.class));
}
/**
* Returns the Workspace in the cache.
* <p>
* <p>Returning a null means there is no current workspace for this project.
*/
@Nullable
public Workspace get() {
return cache;
}
/**
* Returns whether the project is a bazel project.
*/
public boolean isBazel() {
return cache != null;
}
/**
* Runs a callback each time the current Workspace changes.
*/
public void subscribe(@NotNull Runnable callback) {
synchronized (subscribers) {
subscribers.add(callback);
}
}
/**
* Stops notifications to a callback passed to {@link #subscribe}.
*/
public void unsubscribe(Runnable callback) {
synchronized (subscribers) {
subscribers.remove(callback);
}
}
/**
* The Dart plugin uses this registry key to avoid bazel users getting their settings overridden on projects that include a
* pubspec.yaml.
* <p>
* In other words, this key tells the plugin to configure dart projects without pubspec.yaml.
*/
private static final String dartProjectsWithoutPubspecRegistryKey = "dart.projects.without.pubspec";
/**
* Executes a cache refresh.
*/
private void refresh() {
final Workspace workspace = Workspace.loadUncached(project);
if (workspace == cache && !disconnected) return;
if (cache != null && workspace == null) {
disconnected = true;
return;
}
disconnected = false;
cache = workspace;
notifyListeners();
}
private Set<@NotNull Runnable> getSubscribers() {
synchronized (subscribers) {
return ImmutableSet.copyOf(subscribers);
}
}
private void notifyListeners() {
if (project.isDisposed()) {
return;
}
for (Runnable sub : getSubscribers()) {
try {
sub.run();
}
catch (Exception e) {
if (!Objects.equal(e.getMessage(), "expected failure in test")) {
FlutterUtils.warn(LOG, "A subscriber to a WorkspaceCache threw an exception", e);
}
}
}
}
private static final Logger LOG = Logger.getInstance(WorkspaceCache.class);
}