Skip to content

Commit ac6d461

Browse files
committed
make some cases of opening files dramatically faster, especially when not in home directory
1 parent 2f02de6 commit ac6d461

File tree

5 files changed

+40
-24
lines changed

5 files changed

+40
-24
lines changed

src/packages/frontend/client/project.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -467,7 +467,24 @@ export class ProjectClient {
467467
project_id: string;
468468
path: string;
469469
}): Promise<string> {
470-
return (await this.api(opts.project_id)).realpath(opts.path);
470+
const real = (await this.api(opts.project_id)).realpath(opts.path);
471+
return real;
472+
}
473+
474+
async isdir({
475+
project_id,
476+
path,
477+
}: {
478+
project_id: string;
479+
path: string;
480+
}): Promise<boolean> {
481+
const { stdout, exit_code } = await this.exec({
482+
project_id,
483+
command: "file",
484+
args: ["-Eb", path],
485+
err_on_exit: false,
486+
});
487+
return !exit_code && stdout.trim() == "directory";
471488
}
472489

473490
// Add and remove a license from a project. Note that these

src/packages/frontend/components/smart-anchor-tag.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ function CoCalcURL({ href, title, children, project_id }) {
110110
project_id,
111111
decodeURI(target),
112112
!((e as any)?.which === 2 || e?.ctrlKey || e?.metaKey),
113-
fragmentId
113+
fragmentId,
114114
);
115115
} catch (err) {
116116
// loadTarget could fail, e.g., if the project_id is mangled.
@@ -358,15 +358,15 @@ function InternalRelativeLink({ project_id, path, href, title, children }) {
358358
target = join(
359359
"files",
360360
path ? path_split(path).head : "",
361-
decodeURI(hrefPlain)
361+
decodeURI(hrefPlain),
362362
);
363363
}
364364
loadTarget(
365365
"projects",
366366
project_id,
367367
target,
368368
!((e as any).which === 2 || e.ctrlKey || e.metaKey),
369-
fragmentId
369+
fragmentId,
370370
);
371371
}}
372372
title={title}
@@ -381,7 +381,7 @@ function loadTarget(
381381
project_id: string,
382382
target: string,
383383
switchTo: boolean,
384-
fragmentId?: FragmentId
384+
fragmentId?: FragmentId,
385385
): void {
386386
if (!is_valid_uuid_string(project_id)) {
387387
throw Error(`invalid project id ${project_id}`);

src/packages/frontend/project/open-file.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ export async function open_file(
5050
actions: ProjectActions,
5151
opts: OpenFileOpts,
5252
): Promise<void> {
53-
// console.log(opts);
53+
// console.log("open_file: ", opts);
5454

5555
if (opts.path.endsWith("/")) {
5656
actions.open_directory(opts.path);

src/packages/frontend/project/utils.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -239,11 +239,15 @@ export function editor_id(project_id: string, path: string): string {
239239
// Also, if ~/ is somewhere in the path, start over at home.
240240
export function normalize(path: string): string {
241241
while (true) {
242-
const i = path.indexOf("~/");
242+
const pattern = "/~/";
243+
const i = path.indexOf(pattern);
243244
if (i == -1) {
244245
break;
245246
}
246-
path = path.slice(i + 2);
247+
path = path.slice(i + pattern.length);
248+
}
249+
if (path.startsWith("~/")) {
250+
path = path.slice(2);
247251
}
248252

249253
path = os_path.normalize(path);

src/packages/frontend/project_actions.ts

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3210,34 +3210,29 @@ export class ProjectActions extends Actions<ProjectStoreState> {
32103210
if (store == null) {
32113211
return; // project closed already
32123212
}
3213+
// We check whether the path is a directory or not, first by checking if
3214+
// we have a recent directory listing in our cache, and if not, by calling
3215+
// isdir, which is a single exec.
3216+
let isdir;
32133217
let { item, err } = store.get_item_in_path(last, parent_path);
32143218
if (item == null || err) {
3215-
// Fetch again if error or nothing found
32163219
try {
3217-
await this.fetch_directory_listing({
3218-
path: parent_path,
3220+
isdir = await webapp_client.project_client.isdir({
3221+
project_id: this.project_id,
3222+
path: normalize(full_path),
32193223
});
3220-
const store = this.get_store();
3221-
if (store == null) {
3222-
// project closed
3223-
return;
3224-
}
3225-
const x = store.get_item_in_path(last, parent_path);
3226-
if (x.err) throw Error(x.err);
3227-
if (x.item == null) {
3228-
item = Map(); // creating file
3229-
} else {
3230-
item = x.item;
3231-
}
32323224
} catch (err) {
3225+
// e.g., project is not running...
32333226
alert_message({
32343227
type: "error",
32353228
message: `Error opening '${target}': ${err}`,
32363229
});
32373230
return;
32383231
}
3232+
} else {
3233+
isdir = item.get("isdir");
32393234
}
3240-
if (item.get("isdir")) {
3235+
if (isdir) {
32413236
this.open_directory(full_path, change_history);
32423237
} else {
32433238
this.open_file({

0 commit comments

Comments
 (0)