Skip to content

Commit 9c0f0d1

Browse files
authored
feat: preload fuze and full files tree (conwnet#239)
1 parent daa6049 commit 9c0f0d1

4 files changed

Lines changed: 49 additions & 2 deletions

File tree

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/**
2+
* @file async related helpers
3+
*/
4+
5+
// below code is comes from:
6+
//https://github.com/microsoft/vscode/blob/a3415e669a8f3879c290af5616a8ed45dd0534af/src/vs/base/common/async.ts#L344
7+
export class Barrier {
8+
private _isOpen: boolean;
9+
private _promise: Promise<boolean>;
10+
private _completePromise!: (v: boolean) => void;
11+
12+
constructor() {
13+
this._isOpen = false;
14+
this._promise = new Promise<boolean>((c, e) => {
15+
this._completePromise = c;
16+
});
17+
}
18+
19+
isOpen(): boolean {
20+
return this._isOpen;
21+
}
22+
23+
open(): void {
24+
this._isOpen = true;
25+
this._completePromise(true);
26+
}
27+
28+
wait(): Promise<boolean> {
29+
return this._promise;
30+
}
31+
}

extensions/github1s/src/listeners/router/explorer.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import * as vscode from 'vscode';
77
import {
88
changedFileDecorationProvider,
99
submoduleDecorationProvider,
10+
fileSearchProvider,
1011
} from '@/providers';
1112
import { RouterState } from '@/router/types';
1213

@@ -28,5 +29,6 @@ export const explorerRouterListener = (
2829

2930
changedFileDecorationProvider.updateDecorations();
3031
submoduleDecorationProvider.updateDecorations();
32+
fileSearchProvider.loadFuzeForCurrentAuthority();
3133
}
3234
};

extensions/github1s/src/providers/fileSearchProvider.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,23 @@ export class GitHub1sFileSearchProvider
2626
private readonly disposable: Disposable;
2727
private fuseMap: Map<string, Fuse<GithubRESTEntry>> = new Map();
2828

29-
constructor(private fsProvider: GitHub1sFileSystemProvider) {}
29+
constructor(private fsProvider: GitHub1sFileSystemProvider) {
30+
// Preload the fuze for better `ctrl/command + p` experience.
31+
// Once we have loaded the fuze, it will also populate the files into
32+
// fileSystemProvider's cache. So after that, you don't have to send
33+
// a request when you open the new directory in explorer late
34+
this.loadFuzeForCurrentAuthority();
35+
}
3036

3137
dispose() {
3238
this.disposable?.dispose();
3339
}
3440

41+
// load the fuze for current authority
42+
async loadFuzeForCurrentAuthority() {
43+
return this.getFuse(await router.getAuthority());
44+
}
45+
3546
/**
3647
* getFuse for fuzzy file search,
3748
* it maybe take longer time, so we just run it in backend

extensions/github1s/src/router/index.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,15 @@
55

66
import * as vscode from 'vscode';
77
import { History, createMemoryHistory } from 'history';
8+
import { Barrier } from '@/helpers/async';
89
import { parseGitHubUrl } from './parser';
910
import { EventEmitter } from './events';
1011
import { RouterState } from './types';
1112

1213
export class Router extends EventEmitter<RouterState> {
1314
private static instance: Router;
1415

16+
private readyBarrier = new Barrier();
1517
private _previousStatePromise: Promise<RouterState>;
1618
private _currentStatePromise: Promise<RouterState>;
1719
public history: History = createMemoryHistory();
@@ -31,6 +33,7 @@ export class Router extends EventEmitter<RouterState> {
3133
this.history.replace(targetPath);
3234
this._currentStatePromise = parseGitHubUrl(targetPath);
3335
this._previousStatePromise = this._currentStatePromise;
36+
this.readyBarrier.open();
3437

3538
this.history.listen(async ({ location }) => {
3639
const targetPath = `${location.pathname}${location.search}${location.hash}`;
@@ -51,7 +54,7 @@ export class Router extends EventEmitter<RouterState> {
5154

5255
// get the routerState for current url
5356
public async getState(): Promise<RouterState> {
54-
return this._currentStatePromise;
57+
return this.readyBarrier.wait().then(() => this._currentStatePromise);
5558
}
5659

5760
// compute the file URI authority of current routerState

0 commit comments

Comments
 (0)