Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Overlay index fetch issue #82

Closed
21 changes: 6 additions & 15 deletions src/backends/index/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,20 +55,6 @@ export class Index extends Map<string, Stats> {
return JSON.stringify(this.toJSON());
}

/**
* Returns the files in the directory `dir`.
* This is expensive so it is only called once per directory.
*/
protected dirEntries(dir: string): string[] {
const entries = [];
for (const entry of this.keys()) {
if (dirname(entry) == dir) {
entries.push(basename(entry));
}
}
return entries;
}

/**
* Loads the index from JSON data
*/
Expand All @@ -82,7 +68,12 @@ export class Index extends Map<string, Stats> {
for (const [path, data] of Object.entries(json.entries)) {
const stats = new Stats(data);
if (stats.isDirectory()) {
stats.fileData = encode(JSON.stringify(this.dirEntries(path)));
const reImmediateSubdirs = new RegExp(`^${path}/[^/]+$`);
const immediateSubdirs = Object.keys(json.entries)
.filter(item => reImmediateSubdirs.test(item))
.map(item => basename(item));
immediateSubdirs.splice(immediateSubdirs.indexOf(path), 1);
stats.fileData = encode(JSON.stringify(immediateSubdirs));
}
this.set(path, stats);
}
Expand Down
17 changes: 11 additions & 6 deletions src/backends/locked.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
/**
* The current locks
*/
private locks: Map<string, MutexLock> = new Map();
private locks: Map<string, [MutexLock]> = new Map();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a tuple with a single MutexLock. Do you mean an array, MutexLock[]?


protected addLock(path: string): MutexLock {
const lock: MutexLock = {
Expand All @@ -36,7 +36,11 @@
this.unlock(path);
},
};
this.locks.set(path, lock);
if (this.locks.has(path)) {
this.locks.get(path)!.push(lock);
} else {
this.locks.set(path, [lock]);
}
return lock;
}

Expand All @@ -48,7 +52,8 @@
public async lock(path: string): Promise<MutexLock> {
if (this.locks.has(path)) {
// Non-null assertion: we already checked locks has path
await this.locks.get(path)!.promise;
james-pre marked this conversation as resolved.
Show resolved Hide resolved
const promise = this.locks.get(path)!.at(-1);
await promise;

Check failure on line 56 in src/backends/locked.ts

View workflow job for this annotation

GitHub Actions / Ubuntu / CI

Unexpected `await` of a non-Promise (non-"Thenable") value

Check failure on line 56 in src/backends/locked.ts

View workflow job for this annotation

GitHub Actions / MacOS / CI

Unexpected `await` of a non-Promise (non-"Thenable") value

Check failure on line 56 in src/backends/locked.ts

View workflow job for this annotation

GitHub Actions / Windows / CI

Unexpected `await` of a non-Promise (non-"Thenable") value
}

return this.addLock(path);
Expand Down Expand Up @@ -84,8 +89,8 @@
}

// Non-null assertion: we already checked locks has path
this.locks.get(path)!.resolve();
this.locks.delete(path);
const res = this.locks.get(path)!.shift();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Again, please inline: this.locks.get(path)!.shift().resolve();

res!.resolve();
return true;
}

Expand All @@ -94,7 +99,7 @@
* @internal
*/
public isLocked(path: string): boolean {
return this.locks.has(path);
return this.locks.has(path) && this.locks.get(path)!.length > 0;
james-pre marked this conversation as resolved.
Show resolved Hide resolved
}

public async ready(): Promise<void> {
Expand Down
Loading