Skip to content

Commit db5e96c

Browse files
DonJayamanneKartik Raj
and
Kartik Raj
authored
Cache the value of extensions.all (microsoft#20222)
Fixes microsoft#20221 @karrtikr Please feel free to make necessary changes to this PR to get this through the tests. Co-authored-by: Kartik Raj <[email protected]>
1 parent 29a72b4 commit db5e96c

File tree

3 files changed

+29
-13
lines changed

3 files changed

+29
-13
lines changed

src/client/common/application/extensions.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ import { EXTENSION_ROOT_DIR } from '../constants';
1717
*/
1818
@injectable()
1919
export class Extensions implements IExtensions {
20+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
21+
private _cachedExtensions?: readonly Extension<any>[];
22+
2023
constructor(@inject(IFileSystem) private readonly fs: IFileSystem) {}
2124

2225
// eslint-disable-next-line @typescript-eslint/no-explicit-any
@@ -32,6 +35,16 @@ export class Extensions implements IExtensions {
3235
return extensions.getExtension(extensionId);
3336
}
3437

38+
private get cachedExtensions() {
39+
if (!this._cachedExtensions) {
40+
this._cachedExtensions = this._cachedExtensions || extensions.all;
41+
extensions.onDidChange(() => {
42+
this._cachedExtensions = this._cachedExtensions || extensions.all;
43+
});
44+
}
45+
return this._cachedExtensions;
46+
}
47+
3548
/**
3649
* Code borrowed from:
3750
* https://github.com/microsoft/vscode-jupyter/blob/67fe33d072f11d6443cf232a06bed0ac5e24682c/src/platform/common/application/extensions.node.ts
@@ -51,7 +64,8 @@ export class Extensions implements IExtensions {
5164
})
5265
.filter((item) => item && !item.toLowerCase().startsWith(pythonExtRoot))
5366
.filter((item) =>
54-
this.all.some(
67+
// Use cached list of extensions as we need this to be fast.
68+
this.cachedExtensions.some(
5569
(ext) => item!.includes(ext.extensionUri.path) || item!.includes(ext.extensionUri.fsPath),
5670
),
5771
) as string[];

src/client/proposedApi.ts

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -125,16 +125,20 @@ export function buildProposedApi(
125125
const extensions = serviceContainer.get<IExtensions>(IExtensions);
126126
const envVarsProvider = serviceContainer.get<IEnvironmentVariablesProvider>(IEnvironmentVariablesProvider);
127127
function sendApiTelemetry(apiName: string, args?: unknown) {
128-
extensions
129-
.determineExtensionFromCallStack()
130-
.then((info) => {
131-
sendTelemetryEvent(EventName.PYTHON_ENVIRONMENTS_API, undefined, {
132-
apiName,
133-
extensionId: info.extensionId,
134-
});
135-
traceVerbose(`Extension ${info.extensionId} accessed ${apiName} with args: ${JSON.stringify(args)}`);
136-
})
137-
.ignoreErrors();
128+
setTimeout(() =>
129+
extensions
130+
.determineExtensionFromCallStack()
131+
.then((info) => {
132+
sendTelemetryEvent(EventName.PYTHON_ENVIRONMENTS_API, undefined, {
133+
apiName,
134+
extensionId: info.extensionId,
135+
});
136+
traceVerbose(
137+
`Extension ${info.extensionId} accessed ${apiName} with args: ${JSON.stringify(args)}`,
138+
);
139+
})
140+
.ignoreErrors(),
141+
);
138142
}
139143
disposables.push(
140144
discoveryApi.onChanged((e) => {

src/test/proposedApi.unit.test.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,8 +99,6 @@ suite('Proposed Extension API', () => {
9999
});
100100

101101
teardown(() => {
102-
// Verify each API method sends telemetry regarding who called the API.
103-
extensions.verifyAll();
104102
sinon.restore();
105103
});
106104

0 commit comments

Comments
 (0)