Skip to content

Commit 36abe63

Browse files
authored
fix(workspace): ensure catalog promise always resolves and dedupe change events (#79)
1 parent f11a40d commit 36abe63

2 files changed

Lines changed: 12 additions & 5 deletions

File tree

src/composables/workspace-context.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,14 @@ export function useWorkspaceContext() {
3030
}
3131

3232
useDisposable(workspace.onDidChangeTextDocument(({ document }) => {
33-
if (document !== window.activeTextEditor?.document)
33+
const activeEditor = window.activeTextEditor
34+
if (
35+
!activeEditor
36+
|| document !== activeEditor.document
37+
|| document.version === activeEditor.document.version
38+
) {
3439
return
40+
}
3541

3642
deleteCacheByUri(document.uri, false)
3743
}))

src/core/workspace.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,20 +54,21 @@ class WorkspaceContext {
5454
}
5555

5656
async loadWorkspace() {
57-
this.#catalogs = undefined
57+
this.#catalogs = Promise.withResolvers()
5858
this.packageManager = await getPackageManager(this.folder.uri)
5959

6060
logger.info(`[workspace-context] detect package manager: ${this.packageManager}`)
6161

6262
if (this.packageManager !== 'npm') {
63-
this.#catalogs = Promise.withResolvers()
6463
const workspaceFilename = workspaceFileMapping[this.packageManager]
6564
this.workspaceFileUri = Uri.joinPath(this.folder.uri, workspaceFilename)
6665
this.#catalogs.resolve(
6766
await accessOk(this.workspaceFileUri)
6867
? (await this.loadWorkspaceCatalogInfo(this.workspaceFileUri))?.catalogs
6968
: undefined,
7069
)
70+
} else {
71+
this.#catalogs.resolve(undefined)
7172
}
7273
}
7374

@@ -85,7 +86,7 @@ class WorkspaceContext {
8586
}
8687

8788
async getCatalogs(): Promise<CatalogsInfo | undefined> {
88-
return this.#catalogs?.promise
89+
return this.#catalogs!.promise
8990
}
9091

9192
#createResolvedDependencyInfo(dependency: DependencyInfo, catalogs?: CatalogsInfo): ResolvedDependencyInfo {
@@ -131,7 +132,7 @@ class WorkspaceContext {
131132

132133
const [info, catalogs] = await Promise.all([
133134
getDocumentText(uri).then((text) => extractor.getPackageManifestInfo(text)),
134-
this.#catalogs?.promise,
135+
this.getCatalogs(),
135136
])
136137

137138
if (!info)

0 commit comments

Comments
 (0)