Skip to content

Commit

Permalink
perf: check for and return promise instead of awaiting (#2586)
Browse files Browse the repository at this point in the history
There are some checks in `SvelteDocument` where we do the work once in case it hasn't started yet. But we're potentially doing the work more often than necessary, because we're awaiting the result before assigning it to the "chache" and returning it. That way, if another request would come in while the promise isn't resolve yet, we would kick off another needless compile. This fixes that by assigning the promise to the "cache" instead.
  • Loading branch information
dummdidumm authored Nov 14, 2024
1 parent 77160ff commit 1b205c2
Showing 1 changed file with 5 additions and 5 deletions.
10 changes: 5 additions & 5 deletions packages/language-server/src/plugins/svelte/SvelteDocument.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ type PositionMapper = Pick<DocumentMapper, 'getGeneratedPosition' | 'getOriginal
* Represents a text document that contains a svelte component.
*/
export class SvelteDocument {
private transpiledDoc: ITranspiledSvelteDocument | undefined;
private compileResult: SvelteCompileResult | undefined;
private transpiledDoc: Promise<ITranspiledSvelteDocument> | undefined;
private compileResult: Promise<SvelteCompileResult> | undefined;
private svelteVersion: [number, number] | undefined;

public script: TagInformation | null;
Expand Down Expand Up @@ -76,12 +76,12 @@ export class SvelteDocument {
const [major, minor] = this.getSvelteVersion();

if (major > 3 || (major === 3 && minor >= 32)) {
this.transpiledDoc = await TranspiledSvelteDocument.create(
this.transpiledDoc = TranspiledSvelteDocument.create(
this.parent,
await this.config
);
} else {
this.transpiledDoc = await FallbackTranspiledSvelteDocument.create(
this.transpiledDoc = FallbackTranspiledSvelteDocument.create(
this.parent,
(await this.config)?.preprocess
);
Expand All @@ -92,7 +92,7 @@ export class SvelteDocument {

async getCompiled(): Promise<SvelteCompileResult> {
if (!this.compileResult) {
this.compileResult = await this.getCompiledWith((await this.config)?.compilerOptions);
this.compileResult = this.getCompiledWith((await this.config)?.compilerOptions);
}

return this.compileResult;
Expand Down

0 comments on commit 1b205c2

Please sign in to comment.