Skip to content

Commit

Permalink
Wait for task provider registration
Browse files Browse the repository at this point in the history
  • Loading branch information
Piotr Puszkiewicz committed Nov 1, 2017
1 parent 1c1e70b commit 272863d
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 6 deletions.
30 changes: 26 additions & 4 deletions src/vscodeAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ export interface IRegisteredTaskProvider {
provider: vscode.TaskProvider;
}

let registeredTaskProvider: IRegisteredTaskProvider;

class workspaceAdapter {
get workspaceFolders(): vscode.WorkspaceFolder[] {
return vscode.workspace.workspaceFolders;
Expand All @@ -34,7 +32,31 @@ class workspaceAdapter {

export const workspace = new workspaceAdapter();

export function getRegisteredTaskProvider() {
return registeredTaskProvider;
let registeredTaskProvider: IRegisteredTaskProvider;


function setRegisteredTaskProvider(taskProvider: IRegisteredTaskProvider): void {
registeredTaskProvider = taskProvider;
}

function sleep(ms = 0) {
return new Promise(r => setTimeout(r, ms));
}

export async function getRegisteredTaskProvider(timeout: number = 1000): Promise<IRegisteredTaskProvider> {
return poll(() => !!registeredTaskProvider, () => registeredTaskProvider, timeout, 100);
}

export async function poll<T>(condition: () => boolean, value: () => T, duration: number, step: number): Promise<T> {
while (duration > 0) {
if (condition()) {
return value();
}

await sleep(step);

duration -= step;
}

throw "Polling did not succeed within the alotted duration.";
}
6 changes: 4 additions & 2 deletions test/tasks.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,11 @@ suite(`Tasks generation: ${workspaceData.name}`, () => {
await csharpExtension.activate();
}

await vscode.commands.executeCommand('workbench.action.tasks.runTask');
await vscode.commands.executeCommand('workbench.action.tasks.runTask');

tasks = await getRegisteredTaskProvider()
let registeredTaskProvider = await getRegisteredTaskProvider(30000);

tasks = await registeredTaskProvider
.provider
.provideTasks();

Expand Down

0 comments on commit 272863d

Please sign in to comment.