diff options
-rw-r--r-- | editors/code/src/installation/download_file.ts | 8 | ||||
-rw-r--r-- | editors/code/src/installation/language_server.ts | 27 |
2 files changed, 19 insertions, 16 deletions
diff --git a/editors/code/src/installation/download_file.ts b/editors/code/src/installation/download_file.ts index ec16dcd66..53bf46d78 100644 --- a/editors/code/src/installation/download_file.ts +++ b/editors/code/src/installation/download_file.ts | |||
@@ -1,20 +1,18 @@ | |||
1 | import fetch from "node-fetch"; | 1 | import fetch from "node-fetch"; |
2 | import { throttle } from "throttle-debounce"; | ||
3 | import * as fs from "fs"; | 2 | import * as fs from "fs"; |
4 | import { strict as assert } from "assert"; | 3 | import { strict as assert } from "assert"; |
5 | 4 | ||
6 | /** | 5 | /** |
7 | * Downloads file from `url` and stores it at `destFilePath`. | 6 | * Downloads file from `url` and stores it at `destFilePath`. |
8 | * `onProgress` callback is periodically called to track the progress of downloading, | 7 | * `onProgress` callback is called on recieveing each chunk of bytes |
9 | * it gets the already read and total amount of bytes to read as its parameters. | 8 | * to track the progress of downloading, it gets the already read and total |
9 | * amount of bytes to read as its parameters. | ||
10 | */ | 10 | */ |
11 | export async function downloadFile( | 11 | export async function downloadFile( |
12 | url: string, | 12 | url: string, |
13 | destFilePath: fs.PathLike, | 13 | destFilePath: fs.PathLike, |
14 | onProgress: (readBytes: number, totalBytes: number) => void | 14 | onProgress: (readBytes: number, totalBytes: number) => void |
15 | ): Promise<void> { | 15 | ): Promise<void> { |
16 | onProgress = throttle(200, /* noTrailing: */ true, onProgress); | ||
17 | |||
18 | const response = await fetch(url); | 16 | const response = await fetch(url); |
19 | 17 | ||
20 | const totalBytes = Number(response.headers.get('content-length')); | 18 | const totalBytes = Number(response.headers.get('content-length')); |
diff --git a/editors/code/src/installation/language_server.ts b/editors/code/src/installation/language_server.ts index a169eae47..c1f37f978 100644 --- a/editors/code/src/installation/language_server.ts +++ b/editors/code/src/installation/language_server.ts | |||
@@ -1,8 +1,9 @@ | |||
1 | import { spawnSync } from "child_process"; | ||
2 | import * as vscode from "vscode"; | 1 | import * as vscode from "vscode"; |
3 | import * as path from "path"; | 2 | import * as path from "path"; |
4 | import { strict as assert } from "assert"; | 3 | import { strict as assert } from "assert"; |
5 | import { promises as fs } from "fs"; | 4 | import { promises as fs } from "fs"; |
5 | import { spawnSync } from "child_process"; | ||
6 | import { throttle } from "throttle-debounce"; | ||
6 | 7 | ||
7 | import { BinarySource } from "./interfaces"; | 8 | import { BinarySource } from "./interfaces"; |
8 | import { fetchLatestArtifactMetadata } from "./fetch_latest_artifact_metadata"; | 9 | import { fetchLatestArtifactMetadata } from "./fetch_latest_artifact_metadata"; |
@@ -28,19 +29,23 @@ export async function downloadLatestLanguageServer( | |||
28 | { | 29 | { |
29 | location: vscode.ProgressLocation.Notification, | 30 | location: vscode.ProgressLocation.Notification, |
30 | cancellable: false, // FIXME: add support for canceling download? | 31 | cancellable: false, // FIXME: add support for canceling download? |
31 | title: `Downloading language server ${releaseName}` | 32 | title: `Downloading language server (${releaseName})` |
32 | }, | 33 | }, |
33 | async (progress, _cancellationToken) => { | 34 | async (progress, _cancellationToken) => { |
34 | let lastPrecentage = 0; | 35 | let lastPrecentage = 0; |
35 | await downloadFile(downloadUrl, installationPath, (readBytes, totalBytes) => { | 36 | await downloadFile(downloadUrl, installationPath, throttle( |
36 | const newPercentage = (readBytes / totalBytes) * 100; | 37 | 200, |
37 | progress.report({ | 38 | /* noTrailing: */ true, |
38 | message: newPercentage.toFixed(0) + "%", | 39 | (readBytes, totalBytes) => { |
39 | increment: newPercentage - lastPrecentage | 40 | const newPercentage = (readBytes / totalBytes) * 100; |
40 | }); | 41 | progress.report({ |
41 | 42 | message: newPercentage.toFixed(0) + "%", | |
42 | lastPrecentage = newPercentage; | 43 | increment: newPercentage - lastPrecentage |
43 | }); | 44 | }); |
45 | |||
46 | lastPrecentage = newPercentage; | ||
47 | }) | ||
48 | ); | ||
44 | } | 49 | } |
45 | ); | 50 | ); |
46 | 51 | ||