Skip to content
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

manage cache lifetime explicitly #7633

Closed
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions packages/debug/src/browser/debug-session-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -439,6 +439,7 @@ export class DebugSessionManager {
return true;
}

this.taskService.clearCache();
const taskInfo = await this.taskService.runWorkspaceTask(workspaceFolderUri, taskName);
if (!checkErrors) {
return true;
Expand Down
1 change: 1 addition & 0 deletions packages/plugin-ext/src/main/browser/tasks-main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ export class TasksMainImpl implements TasksMain, Disposable {
}

let found: TaskConfiguration[] = [];
this.taskService.clearCache();
const tasks = [...(await this.taskService.getConfiguredTasks()), ...(await this.taskService.getProvidedTasks())];
if (taskType) {
found = tasks.filter(t => {
Expand Down
66 changes: 45 additions & 21 deletions packages/task/src/browser/provided-task-configurations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import { TaskProviderRegistry } from './task-contribution';
import { TaskDefinitionRegistry } from './task-definition-registry';
import { TaskConfiguration, TaskCustomization, TaskOutputPresentation } from '../common';
import URI from '@theia/core/lib/common/uri';
import { Deferred } from '@theia/core/lib/common/promise-util';

@injectable()
export class ProvidedTaskConfigurations {
Expand All @@ -28,16 +29,46 @@ export class ProvidedTaskConfigurations {
* For the second level of inner map, the key is task label.
* For the third level of inner map, the key is the task scope and value TaskConfiguration.
*/
protected tasksMap = new Map<string, Map<string, Map<string | undefined, TaskConfiguration>>>();
protected tasksMap: Map<string, Map<string, Map<string | undefined, TaskConfiguration>>> | undefined;

@inject(TaskProviderRegistry)
protected readonly taskProviderRegistry: TaskProviderRegistry;

@inject(TaskDefinitionRegistry)
protected readonly taskDefinitionRegistry: TaskDefinitionRegistry;

protected loading: Deferred<void> | undefined;

/** returns a list of provided tasks */
async getTasks(): Promise<TaskConfiguration[]> {
await this.ensureCachePopulated();
const tasks: TaskConfiguration[] = [];
for (const taskLabelMap of this.tasksMap!.values()) {
for (const taskScopeMap of taskLabelMap.values()) {
for (const task of taskScopeMap.values()) {
tasks.push(task);
}
}
}
return tasks;
}

clearCache(): void {
this.tasksMap = undefined;
}

async ensureCachePopulated(): Promise<void> {
if (!this.tasksMap) {
if (this.loading) {
await this.loading.promise;
} else {
await this.populateCache();
}
}
}

async populateCache(): Promise<void> {
this.loading = new Deferred();
const providers = await this.taskProviderRegistry.getProviders();
const providedTasks: TaskConfiguration[] = (await Promise.all(providers.map(p => p.provideTasks())))
.reduce((acc, taskArray) => acc.concat(taskArray), [])
Expand All @@ -52,17 +83,22 @@ export class ProvidedTaskConfigurations {
};
});
this.cacheTasks(providedTasks);
return providedTasks;
this.loading.resolve();
this.loading = undefined;
}

/** returns the task configuration for a given source and label or undefined if none */
async getTask(source: string, taskLabel: string, scope?: string): Promise<TaskConfiguration | undefined> {
const task = this.getCachedTask(source, taskLabel, scope);
if (task) {
return task;
} else {
await this.getTasks();
return this.getCachedTask(source, taskLabel, scope);
await this.ensureCachePopulated();
const labelConfigMap = this.tasksMap!.get(source);
if (labelConfigMap) {
const scopeConfigMap = labelConfigMap.get(taskLabel);
if (scopeConfigMap) {
if (scope) {
return scopeConfigMap.get(scope);
}
return Array.from(scopeConfigMap.values())[0];
}
}
}

Expand Down Expand Up @@ -111,20 +147,8 @@ export class ProvidedTaskConfigurations {
return matchedTask;
}

protected getCachedTask(source: string, taskLabel: string, scope?: string): TaskConfiguration | undefined {
const labelConfigMap = this.tasksMap.get(source);
if (labelConfigMap) {
const scopeConfigMap = labelConfigMap.get(taskLabel);
if (scopeConfigMap) {
if (scope) {
return scopeConfigMap.get(scope);
}
return Array.from(scopeConfigMap.values())[0];
}
}
}

protected cacheTasks(tasks: TaskConfiguration[]): void {
this.tasksMap = new Map();
for (const task of tasks) {
const label = task.label;
const source = task._source;
Expand Down
29 changes: 22 additions & 7 deletions packages/task/src/browser/task-frontend-contribution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,7 @@ export class TaskFrontendContribution implements CommandContribution, MenuContri
{
isEnabled: () => true,
execute: async (label: string) => {
this.taskService.clearCache();
const didExecute = await this.taskService.runTaskByLabel(label);
if (!didExecute) {
this.quickOpenTask.open();
Expand All @@ -228,6 +229,7 @@ export class TaskFrontendContribution implements CommandContribution, MenuContri
execute: (...args: any[]) => {
const [source, label, scope] = args;
if (source && label) {
this.taskService.clearCache();
return this.taskService.run(source, label, scope);
}
return this.quickOpenTask.open();
Expand All @@ -239,17 +241,21 @@ export class TaskFrontendContribution implements CommandContribution, MenuContri
{
isEnabled: () => this.workspaceService.opened,
// eslint-disable-next-line @typescript-eslint/no-explicit-any
execute: (...args: any[]) =>
this.quickOpenTask.runBuildOrTestTask('build')
execute: (...args: any[]) => {
this.taskService.clearCache();
this.quickOpenTask.runBuildOrTestTask('build');
}
}
);
registry.registerCommand(
TaskCommands.TASK_RUN_TEST,
{
isEnabled: () => this.workspaceService.opened,
// eslint-disable-next-line @typescript-eslint/no-explicit-any
execute: (...args: any[]) =>
this.quickOpenTask.runBuildOrTestTask('test')
execute: (...args: any[]) => {
this.taskService.clearCache();
this.quickOpenTask.runBuildOrTestTask('test');
}
}
);
registry.registerCommand(
Expand All @@ -263,7 +269,10 @@ export class TaskFrontendContribution implements CommandContribution, MenuContri
TaskCommands.TASK_RUN_LAST,
{
isEnabled: () => !!this.taskService.getLastTask(),
execute: () => this.taskService.runLastTask()
execute: () => {
this.taskService.clearCache();
this.taskService.runLastTask();
}
}
);
registry.registerCommand(
Expand All @@ -278,7 +287,10 @@ export class TaskFrontendContribution implements CommandContribution, MenuContri
registry.registerCommand(
TaskCommands.TASK_CONFIGURE,
{
execute: () => this.quickOpenTask.configure()
execute: () => {
this.taskService.clearCache();
this.quickOpenTask.configure();
}
}
);

Expand Down Expand Up @@ -306,7 +318,10 @@ export class TaskFrontendContribution implements CommandContribution, MenuContri
registry.registerCommand(
TaskCommands.TASK_RESTART_RUNNING,
{
execute: () => this.taskRestartRunningQuickOpen.open()
execute: () => {
this.taskService.clearCache();
this.taskRestartRunningQuickOpen.open();
}
}
);
}
Expand Down
4 changes: 4 additions & 0 deletions packages/task/src/browser/task-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,10 @@ export class TaskService implements TaskConfigurationClient {
return validTaskConfigs;
}

clearCache(): void {
this.providedTaskConfigurations.clearCache();
}

/** Returns an array of the task configurations which are provided by the extensions. */
getProvidedTasks(): Promise<TaskConfiguration[]> {
return this.providedTaskConfigurations.getTasks();
Expand Down