Skip to content

Commit f3240e2

Browse files
author
Veetaha
committed
vscode: move throtting of download progress to call site
1 parent a63659b commit f3240e2

File tree

2 files changed

+19
-16
lines changed

2 files changed

+19
-16
lines changed

editors/code/src/installation/download_file.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,18 @@
11
import fetch from "node-fetch";
2-
import { throttle } from "throttle-debounce";
32
import * as fs from "fs";
43
import { strict as assert } from "assert";
54

65
/**
76
* Downloads file from `url` and stores it at `destFilePath`.
8-
* `onProgress` callback is periodically called to track the progress of downloading,
9-
* it gets the already read and total amount of bytes to read as its parameters.
7+
* `onProgress` callback is called on recieveing each chunk of bytes
8+
* to track the progress of downloading, it gets the already read and total
9+
* amount of bytes to read as its parameters.
1010
*/
1111
export async function downloadFile(
1212
url: string,
1313
destFilePath: fs.PathLike,
1414
onProgress: (readBytes: number, totalBytes: number) => void
1515
): Promise<void> {
16-
onProgress = throttle(200, /* noTrailing: */ true, onProgress);
17-
1816
const response = await fetch(url);
1917

2018
const totalBytes = Number(response.headers.get('content-length'));

editors/code/src/installation/language_server.ts

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
import { spawnSync } from "child_process";
21
import * as vscode from "vscode";
32
import * as path from "path";
43
import { strict as assert } from "assert";
54
import { promises as fs } from "fs";
5+
import { spawnSync } from "child_process";
6+
import { throttle } from "throttle-debounce";
67

78
import { BinarySource } from "./interfaces";
89
import { fetchLatestArtifactMetadata } from "./fetch_latest_artifact_metadata";
@@ -28,19 +29,23 @@ export async function downloadLatestLanguageServer(
2829
{
2930
location: vscode.ProgressLocation.Notification,
3031
cancellable: false, // FIXME: add support for canceling download?
31-
title: `Downloading language server ${releaseName}`
32+
title: `Downloading language server (${releaseName})`
3233
},
3334
async (progress, _cancellationToken) => {
3435
let lastPrecentage = 0;
35-
await downloadFile(downloadUrl, installationPath, (readBytes, totalBytes) => {
36-
const newPercentage = (readBytes / totalBytes) * 100;
37-
progress.report({
38-
message: newPercentage.toFixed(0) + "%",
39-
increment: newPercentage - lastPrecentage
40-
});
41-
42-
lastPrecentage = newPercentage;
43-
});
36+
await downloadFile(downloadUrl, installationPath, throttle(
37+
200,
38+
/* noTrailing: */ true,
39+
(readBytes, totalBytes) => {
40+
const newPercentage = (readBytes / totalBytes) * 100;
41+
progress.report({
42+
message: newPercentage.toFixed(0) + "%",
43+
increment: newPercentage - lastPrecentage
44+
});
45+
46+
lastPrecentage = newPercentage;
47+
})
48+
);
4449
}
4550
);
4651

0 commit comments

Comments
 (0)