blob: 66a404e62ecdd3bcba967e164e95ec359b8189ea [file] [log] [blame]
/*
* Copyright 2016 The Bazel Authors. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.idea.blaze.base.prefetch;
import com.google.idea.blaze.base.settings.Blaze;
import com.intellij.ProjectTopics;
import com.intellij.openapi.components.ProjectComponent;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.roots.ModuleRootAdapter;
import com.intellij.openapi.roots.ModuleRootEvent;
import com.intellij.util.messages.MessageBusConnection;
import org.jetbrains.annotations.NotNull;
/**
* Instructs prefetchers to prefetch all project files when they may have changed / be stale.
*/
public class PrefetchServiceProjectComponent implements ProjectComponent {
private final Project project;
public PrefetchServiceProjectComponent(Project project) {
this.project = project;
MessageBusConnection connection = project.getMessageBus().connect(project);
connection.subscribe(ProjectTopics.PROJECT_ROOTS, new ModuleRootAdapter() {
@Override
public void rootsChanged(ModuleRootEvent event) {
prefetch();
}
});
}
@Override
public void projectOpened() {
prefetch();
}
private void prefetch() {
if (!Blaze.isBlazeProject(project)) {
return;
}
PrefetchService.getInstance().prefetchProjectFiles(project);
}
@Override
public void projectClosed() {
}
@Override
public void initComponent() {
}
@Override
public void disposeComponent() {
}
@NotNull
@Override
public String getComponentName() {
return "PrefetchServiceProjectComponent";
}
}