Skip to content

Commit 519a5a8

Browse files
author
Benjamin Pasero
committed
debt - more async/await
1 parent f555434 commit 519a5a8

12 files changed

Lines changed: 163 additions & 114 deletions

File tree

src/vs/base/browser/ui/keybindingLabel/keybindingLabel.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ export interface KeybindingLabelOptions {
3333
export class KeybindingLabel {
3434

3535
private domNode: HTMLElement;
36-
private keybinding: ResolvedKeybinding | null | undefined;
36+
private keybinding: ResolvedKeybinding | undefined;
3737
private matches: Matches | undefined;
3838
private didEverRender: boolean;
3939

@@ -47,7 +47,7 @@ export class KeybindingLabel {
4747
return this.domNode;
4848
}
4949

50-
set(keybinding: ResolvedKeybinding | null | undefined, matches?: Matches) {
50+
set(keybinding: ResolvedKeybinding | undefined, matches?: Matches) {
5151
if (this.didEverRender && this.keybinding === keybinding && KeybindingLabel.areSame(this.matches, matches)) {
5252
return;
5353
}

src/vs/base/node/config.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -61,15 +61,15 @@ export class ConfigWatcher<T> implements IConfigWatcher<T>, IDisposable {
6161
this.initAsync();
6262
}
6363

64-
public get path(): string {
64+
get path(): string {
6565
return this._path;
6666
}
6767

68-
public get hasParseErrors(): boolean {
68+
get hasParseErrors(): boolean {
6969
return this.parseErrors && this.parseErrors.length > 0;
7070
}
7171

72-
public get onDidUpdateConfiguration(): Event<IConfigurationChangeEvent<T>> {
72+
get onDidUpdateConfiguration(): Event<IConfigurationChangeEvent<T>> {
7373
return this._onDidUpdateConfiguration.event;
7474
}
7575

@@ -179,7 +179,7 @@ export class ConfigWatcher<T> implements IConfigWatcher<T>, IDisposable {
179179
this.timeoutHandle = global.setTimeout(() => this.reload(), this.options.changeBufferDelay || 0);
180180
}
181181

182-
public reload(callback?: (config: T) => void): void {
182+
reload(callback?: (config: T) => void): void {
183183
this.loadAsync(currentConfig => {
184184
if (!objects.equals(currentConfig, this.cache)) {
185185
this.updateCache(currentConfig);
@@ -193,7 +193,7 @@ export class ConfigWatcher<T> implements IConfigWatcher<T>, IDisposable {
193193
});
194194
}
195195

196-
public getConfig(): T {
196+
getConfig(): T {
197197
this.ensureLoaded();
198198

199199
return this.cache;
@@ -205,7 +205,7 @@ export class ConfigWatcher<T> implements IConfigWatcher<T>, IDisposable {
205205
}
206206
}
207207

208-
public dispose(): void {
208+
dispose(): void {
209209
this.disposed = true;
210210
this.disposables = dispose(this.disposables);
211211
}

src/vs/base/node/decoder.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ export class LineDecoder {
2323
this.remaining = null;
2424
}
2525

26-
public write(buffer: Buffer): string[] {
26+
write(buffer: Buffer): string[] {
2727
const result: string[] = [];
2828
const value = this.remaining
2929
? this.remaining + this.stringDecoder.write(buffer)
@@ -56,7 +56,7 @@ export class LineDecoder {
5656
return result;
5757
}
5858

59-
public end(): string | null {
59+
end(): string | null {
6060
return this.remaining;
6161
}
6262
}

src/vs/base/node/extfs.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ interface IEnsuredWriteFileOptions extends IWriteFileOptions {
195195
}
196196

197197
let canFlush = true;
198-
export function writeFileAndFlush(path: string, data: string | Buffer | NodeJS.ReadableStream | Uint8Array, options: IWriteFileOptions, callback: (error?: Error) => void): void {
198+
export function writeFileAndFlush(path: string, data: string | Buffer | NodeJS.ReadableStream | Uint8Array, options: IWriteFileOptions | undefined, callback: (error?: Error) => void): void {
199199
const ensuredOptions = ensureWriteOptions(options);
200200

201201
if (typeof data === 'string' || Buffer.isBuffer(data) || data instanceof Uint8Array) {

src/vs/base/node/pfs.ts

Lines changed: 75 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
import * as extfs from 'vs/base/node/extfs';
77
import { join, dirname } from 'vs/base/common/path';
8-
import { nfcall, Queue } from 'vs/base/common/async';
8+
import { Queue } from 'vs/base/common/async';
99
import * as fs from 'fs';
1010
import * as os from 'os';
1111
import * as platform from 'vs/base/common/platform';
@@ -15,53 +15,61 @@ import { promisify } from 'util';
1515
import { CancellationToken } from 'vs/base/common/cancellation';
1616

1717
export function readdir(path: string): Promise<string[]> {
18-
return nfcall(extfs.readdir, path);
18+
return promisify(extfs.readdir)(path);
1919
}
2020

2121
export function exists(path: string): Promise<boolean> {
22-
return new Promise(c => fs.exists(path, c));
22+
return promisify(fs.exists)(path);
2323
}
2424

25-
export function chmod(path: string, mode: number): Promise<boolean> {
26-
return nfcall(fs.chmod, path, mode);
25+
export function chmod(path: string, mode: number): Promise<void> {
26+
return promisify(fs.chmod)(path, mode);
2727
}
2828

29-
export function rimraf(path: string): Promise<void> {
30-
return lstat(path).then(stat => {
29+
export async function rimraf(path: string): Promise<void> {
30+
try {
31+
const stat = await lstat(path);
32+
33+
// Folder delete (recursive) - NOT for symbolic links though!
3134
if (stat.isDirectory() && !stat.isSymbolicLink()) {
32-
return readdir(path)
33-
.then(children => Promise.all(children.map(child => rimraf(join(path, child)))))
34-
.then(() => rmdir(path));
35-
} else {
35+
const children = await readdir(path);
36+
37+
await Promise.all(children.map(child => rimraf(join(path, child))));
38+
39+
await promisify(fs.rmdir)(path);
40+
}
41+
42+
// Single file delete
43+
else {
3644
return unlink(path);
3745
}
38-
}, (err: NodeJS.ErrnoException) => {
39-
if (err.code === 'ENOENT') {
40-
return undefined;
46+
} catch (error) {
47+
if (error.code !== 'ENOENT') {
48+
throw error;
4149
}
42-
43-
return Promise.reject(err);
44-
});
50+
}
4551
}
4652

4753
export function realpath(path: string): Promise<string> {
48-
return nfcall(extfs.realpath, path);
54+
return promisify(extfs.realpath)(path);
4955
}
5056

5157
export function stat(path: string): Promise<fs.Stats> {
52-
return nfcall(fs.stat, path);
58+
return promisify(fs.stat)(path);
5359
}
5460

5561
export function statLink(path: string): Promise<{ stat: fs.Stats, isSymbolicLink: boolean }> {
56-
return nfcall(extfs.statLink, path);
62+
return new Promise((resolve, reject) => {
63+
extfs.statLink(path, (error, result) => error ? reject(error) : resolve(result!));
64+
});
5765
}
5866

5967
export function lstat(path: string): Promise<fs.Stats> {
60-
return nfcall(fs.lstat, path);
68+
return promisify(fs.lstat)(path);
6169
}
6270

6371
export function rename(oldPath: string, newPath: string): Promise<void> {
64-
return nfcall(fs.rename, oldPath, newPath);
72+
return promisify(fs.rename)(oldPath, newPath);
6573
}
6674

6775
export function renameIgnoreError(oldPath: string, newPath: string): Promise<void> {
@@ -70,30 +78,26 @@ export function renameIgnoreError(oldPath: string, newPath: string): Promise<voi
7078
});
7179
}
7280

73-
export function rmdir(path: string): Promise<void> {
74-
return nfcall(fs.rmdir, path);
75-
}
76-
7781
export function unlink(path: string): Promise<void> {
78-
return nfcall(fs.unlink, path);
82+
return promisify(fs.unlink)(path);
7983
}
8084

8185
export function symlink(target: string, path: string, type?: string): Promise<void> {
82-
return nfcall<void>(fs.symlink, target, path, type);
86+
return promisify(fs.symlink)(target, path, type);
8387
}
8488

8589
export function readlink(path: string): Promise<string> {
86-
return nfcall<string>(fs.readlink, path);
90+
return promisify(fs.readlink)(path);
8791
}
8892

8993
export function truncate(path: string, len: number): Promise<void> {
90-
return nfcall(fs.truncate, path, len);
94+
return promisify(fs.truncate)(path, len);
9195
}
9296

9397
export function readFile(path: string): Promise<Buffer>;
9498
export function readFile(path: string, encoding: string): Promise<string>;
9599
export function readFile(path: string, encoding?: string): Promise<Buffer | string> {
96-
return nfcall(fs.readFile, path, encoding);
100+
return promisify(fs.readFile)(path, encoding);
97101
}
98102

99103
// According to node.js docs (https://nodejs.org/docs/v6.5.0/api/fs.html#fs_fs_writefile_file_data_options_callback)
@@ -109,7 +113,11 @@ export function writeFile(path: string, data: string | Buffer | NodeJS.ReadableS
109113
export function writeFile(path: string, data: string | Buffer | NodeJS.ReadableStream | Uint8Array, options?: extfs.IWriteFileOptions): Promise<void> {
110114
const queueKey = toQueueKey(path);
111115

112-
return ensureWriteFileQueue(queueKey).queue(() => nfcall(extfs.writeFileAndFlush, path, data, options));
116+
return ensureWriteFileQueue(queueKey).queue(() => {
117+
return new Promise((resolve, reject) => {
118+
extfs.writeFileAndFlush(path, data, options, error => error ? reject(error) : resolve());
119+
});
120+
});
113121
}
114122

115123
function toQueueKey(path: string): string {
@@ -137,43 +145,51 @@ function ensureWriteFileQueue(queueKey: string): Queue<void> {
137145
return writeFileQueue;
138146
}
139147

140-
/**
141-
* Read a dir and return only subfolders
142-
*/
143-
export function readDirsInDir(dirPath: string): Promise<string[]> {
144-
return readdir(dirPath).then(children => {
145-
return Promise.all(children.map(c => dirExists(join(dirPath, c)))).then(exists => {
146-
return children.filter((_, i) => exists[i]);
147-
});
148-
});
148+
export async function readDirsInDir(dirPath: string): Promise<string[]> {
149+
const children = await readdir(dirPath);
150+
const directories: string[] = [];
151+
152+
for (const child of children) {
153+
if (await dirExists(join(dirPath, child))) {
154+
directories.push(child);
155+
}
156+
}
157+
158+
return directories;
149159
}
150160

151-
/**
152-
* `path` exists and is a directory
153-
*/
154-
export function dirExists(path: string): Promise<boolean> {
155-
return stat(path).then(stat => stat.isDirectory(), () => false);
161+
export async function dirExists(path: string): Promise<boolean> {
162+
try {
163+
const fileStat = await stat(path);
164+
165+
return fileStat.isDirectory();
166+
} catch (error) {
167+
return false;
168+
}
156169
}
157170

158-
/**
159-
* `path` exists and is a file.
160-
*/
161-
export function fileExists(path: string): Promise<boolean> {
162-
return stat(path).then(stat => stat.isFile(), () => false);
171+
export async function fileExists(path: string): Promise<boolean> {
172+
try {
173+
const fileStat = await stat(path);
174+
175+
return fileStat.isFile();
176+
} catch (error) {
177+
return false;
178+
}
163179
}
164180

165-
/**
166-
* Deletes a path from disk.
167-
*/
168-
let _tmpDir: string | null = null;
181+
let tmpDir: string | null = null;
169182
function getTmpDir(): string {
170-
if (!_tmpDir) {
171-
_tmpDir = os.tmpdir();
183+
if (!tmpDir) {
184+
tmpDir = os.tmpdir();
172185
}
173-
return _tmpDir;
186+
return tmpDir;
174187
}
188+
175189
export function del(path: string, tmp = getTmpDir()): Promise<void> {
176-
return nfcall(extfs.del, path, tmp);
190+
return new Promise((resolve, reject) => {
191+
extfs.del(path, tmp, error => error ? reject(error) : resolve());
192+
});
177193
}
178194

179195
export function whenDeleted(path: string): Promise<void> {

0 commit comments

Comments
 (0)