Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions packages/typescript/src/api/async/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1816,6 +1816,7 @@ export class Program<Id extends ProjectId = ProjectId> implements FormatDiagnost
private readonly toPath: (fileName: string, basePath?: string) => PathKey;
private readonly decoder = new Wtf8Decoder();
private readonly sourceFileMetadataCache = new Map<PathKey, Promise<SourceFileMetadata | undefined>>();
private readonly pendingSourceFiles = new Map<PathKey, Promise<SourceFile | undefined>>(); // @sync-skip
private ownedSnapshot: Snapshot | undefined;
private disposePromise: Promise<void> | undefined;

Expand Down Expand Up @@ -1906,6 +1907,20 @@ export class Program<Id extends ProjectId = ProjectId> implements FormatDiagnost
return retained as unknown as SourceFile;
}

// @sync-skip-block-start
let pending = this.pendingSourceFiles.get(path);
if (!pending) {
pending = this.fetchSourceFile(file).finally(() => this.pendingSourceFiles.delete(path));
this.pendingSourceFiles.set(path, pending);
}
return pending;
// @sync-skip-block-end
// @sync-only-start
// return this.fetchSourceFile(file);
// @sync-only-end
}

private async fetchSourceFile(file: DocumentIdentifier): Promise<SourceFile | undefined> {
// Fetch from server
const binaryData = await this.client.apiRequestBinary("getSourceFile", {
snapshot: this.snapshotId,
Expand Down
31 changes: 24 additions & 7 deletions packages/typescript/src/api/sync/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3246,6 +3246,29 @@ export class Program<Id extends ProjectId = ProjectId> implements FormatDiagnost
return retained as unknown as SourceFile;
}

return owner.fetchSourceFile(file);
},
function* (file: DocumentIdentifier, path: PathKey): Generator<ProtocolRequest, SourceFile | undefined, ProtocolResponse["result"]> {
// Check if we already have a retained cache entry for this (snapshot, project) pair
const retained = owner.sourceFileCache.getRetained(path, owner.snapshotId, owner.project.id);
if (retained) {
return retained as unknown as SourceFile;
}

return yield* owner.fetchSourceFile.gen(file);
},
);
}

private get fetchSourceFile(): {
(file: DocumentIdentifier): SourceFile | undefined;
gen(file: DocumentIdentifier): Generator<ProtocolRequest, SourceFile | undefined, ProtocolResponse["result"]>;
} {
const owner = this;
return cacheGeneratorMethod(
owner,
"fetchSourceFile",
function (file: DocumentIdentifier): SourceFile | undefined {
// Fetch from server
const binaryData = owner.client.apiRequestBinary("getSourceFile", {
snapshot: owner.snapshotId,
Expand All @@ -3260,13 +3283,7 @@ export class Program<Id extends ProjectId = ProjectId> implements FormatDiagnost
const decoded = new RemoteSourceFile(binaryData, owner.decoder, owner.client.getTimingCollector(), owner.project.api);
return owner.sourceFileCache.set(decoded, owner.snapshotId, owner.project.id) as unknown as SourceFile;
},
function* (file: DocumentIdentifier, path: PathKey): Generator<ProtocolRequest, SourceFile | undefined, ProtocolResponse["result"]> {
// Check if we already have a retained cache entry for this (snapshot, project) pair
const retained = owner.sourceFileCache.getRetained(path, owner.snapshotId, owner.project.id);
if (retained) {
return retained as unknown as SourceFile;
}

function* (file: DocumentIdentifier): Generator<ProtocolRequest, SourceFile | undefined, ProtocolResponse["result"]> {
// Fetch from server
const binaryData = sourceFileResponseToUint8Array(
yield* apiRequest("getSourceFile", {
Expand Down
28 changes: 28 additions & 0 deletions packages/typescript/test/async/api.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1987,6 +1987,34 @@ declare module "augmentation" {}`,

// @sync-skip-block-start
describe("API - automatic batching", { concurrency }, () => {
test("shares concurrent Program source file requests and retries failures", async context => {
const file = "/src/main.ts";
const text = "export const value = 1;";
await using api = spawnAPI({ [file]: text });
await using program = await api.createProgram([file], { noLib: true });
// @ts-ignore -- Internal client requests are exercised by source tests.
const client = api["client"];
const request = context.mock.method(client, "apiRequestBinary");
const error = new Error("Source file request failed");
request.mock.mockImplementationOnce(() => Promise.reject(error));

await Promise.all([
assert.rejects(program.getSourceFile(file), error),
assert.rejects(program.getSourceFile("/src/./main.ts"), error),
]);
assert.equal(request.mock.callCount(), 1);

const [sourceFile, repeated] = await Promise.all([
program.getSourceFile(file),
program.getSourceFile("/src/./main.ts"),
]);
assert.equal(request.mock.callCount(), 2);
assert.equal(sourceFile?.text, text);
assert.strictEqual(repeated, sourceFile);
assert.strictEqual(await program.getSourceFile(file), sourceFile);
assert.equal(request.mock.callCount(), 2);
});

test("fetches Program statistics for every request", async context => {
await using api = spawnAPI({ "/src/main.ts": "export const value = 1;" });
await using program = await api.createProgram(["/src/main.ts"], { noLib: true });
Expand Down
Loading