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

Delay notification for slowTask for 2 seconds #505

Merged
merged 1 commit into from
Feb 22, 2021
Merged
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
75 changes: 44 additions & 31 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ import {
DecorationTypeDidChange,
DecorationsRangesDidChange,
} from "./decoration-protocol";
import { clearTimeout } from "timers";

const outputChannel = window.createOutputChannel("Metals");
const openSettingsAction = "Open settings";
Expand Down Expand Up @@ -784,41 +785,53 @@ function launchMetals(
client.onRequest(MetalsSlowTask.type, (params, requestToken) => {
return new Promise((requestResolve) => {
const showLogs = ` ([show logs](command:${ClientCommands.ToggleLogs} "Show Metals logs"))`;
window.withProgress(
{
location: ProgressLocation.Notification,
title: params.message + showLogs,
cancellable: true,
},
(progress, progressToken) => {
// Update total running time every second.
let seconds = params.secondsElapsed || 0;
const interval = setInterval(() => {
seconds += 1;
progress.report({ message: readableSeconds(seconds) });
}, 1000);

// Hide logs and clean up resources on completion.
function onComplete() {
clearInterval(interval);
}

// Client triggered cancelation from the progress notification.
progressToken.onCancellationRequested(() => {
onComplete();
requestResolve({ cancel: true });
});
// Wait a bit before showing the progress notification
const waitTime = 2;
gourlaysama marked this conversation as resolved.
Show resolved Hide resolved
let delay = Math.max(0, waitTime - (params.secondsElapsed || 0));
const timeout = setTimeout(() => {
window.withProgress(
{
location: ProgressLocation.Notification,
title: params.message + showLogs,
cancellable: true,
},
(progress, progressToken) => {
// Update total running time every second.
let seconds = params.secondsElapsed || waitTime;
progress.report({ message: readableSeconds(seconds) });
const interval = setInterval(() => {
seconds += 1;
progress.report({ message: readableSeconds(seconds) });
}, 1000);

// Hide logs and clean up resources on completion.
function onComplete() {
clearInterval(interval);
}

return new Promise((progressResolve) => {
// Server completed long running task.
requestToken.onCancellationRequested(() => {
// Client triggered cancelation from the progress notification.
progressToken.onCancellationRequested(() => {
onComplete();
progress.report({ increment: 100 });
setTimeout(() => progressResolve(undefined), 1000);
requestResolve({ cancel: true });
});
});
}
);

return new Promise((progressResolve) => {
// Server completed long running task.
requestToken.onCancellationRequested(() => {
onComplete();
progress.report({ increment: 100 });
setTimeout(() => progressResolve(undefined), 1000);
});
});
}
);
}, delay * 1000);

// do not show the notification at all if the task already completed
requestToken.onCancellationRequested(() => {
clearTimeout(timeout);
});
});
});
// NOTE(olafur): `require("./package.json")` should work in theory but it
Expand Down