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

Detect changes instead of querying every time #15529

Merged
merged 4 commits into from
Apr 10, 2024
Merged
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
18 changes: 14 additions & 4 deletions src/notebooks/outputs/jupyterCellOutputMimeTypeTracker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,12 @@ import { JupyterNotebookView } from '../../platform/common/constants';
import { dispose } from '../../platform/common/utils/lifecycle';
import { IDisposable, IDisposableRegistry } from '../../platform/common/types';
import { isJupyterNotebook } from '../../platform/common/utils';
import { ResourceTypeTelemetryProperty, sendTelemetryEvent, Telemetry } from '../../telemetry';
import {
onDidChangeTelemetryEnablement,
ResourceTypeTelemetryProperty,
sendTelemetryEvent,
Telemetry
} from '../../telemetry';
import { isTelemetryDisabled } from '../../telemetry';

/**
Expand All @@ -25,14 +30,13 @@ import { isTelemetryDisabled } from '../../telemetry';
export class CellOutputMimeTypeTracker implements IExtensionSyncActivationService, IDisposable {
private sentMimeTypes: Set<string> = new Set<string>();
private readonly disposables: IDisposable[] = [];
private get isTelemetryDisabled() {
return isTelemetryDisabled();
}
private isTelemetryDisabled: boolean;

constructor(@inject(IDisposableRegistry) disposables: IDisposableRegistry) {
disposables.push(this);
}
public activate() {
this.isTelemetryDisabled = isTelemetryDisabled();
workspace.onDidOpenNotebookDocument(this.onDidOpenCloseDocument, this, this.disposables);
workspace.onDidCloseNotebookDocument(this.onDidOpenCloseDocument, this, this.disposables);
workspace.onDidSaveNotebookDocument(this.onDidOpenCloseDocument, this, this.disposables);
Expand All @@ -41,6 +45,12 @@ export class CellOutputMimeTypeTracker implements IExtensionSyncActivationServic
this,
this.disposables
);
this.disposables.push(
onDidChangeTelemetryEnablement((enabled) => {
this.isTelemetryDisabled = enabled;
}),
this
);
}

public dispose() {
Expand Down
4 changes: 4 additions & 0 deletions src/platform/common/utils/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

import type { Event } from 'vscode';
import { IDisposable } from '../types';
import { EmptyDisposable } from './lifecycle';

/**
* Given an event, returns another event which only fires once.
Expand Down Expand Up @@ -43,3 +44,6 @@ export function once<T>(event: Event<T>): Event<T> {
export function toPromise<T>(event: Event<T>, thisArgs: any = null, disposables?: IDisposable[]): Promise<T> {
return new Promise((resolve) => once(event)(resolve, thisArgs, disposables));
}

// eslint-disable-next-line @typescript-eslint/no-explicit-any
export const EmptyEvent: Event<any> = () => EmptyDisposable;
4 changes: 2 additions & 2 deletions src/platform/common/utils/lifecycle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ import { once } from './functional';
import { Iterable } from './iterable';

let disposableTracker: IDisposable[] | undefined = undefined;
export const EmptyDisposable = Object.freeze({
export const EmptyDisposable = {
dispose: () => {
/** */
}
});
};

export function setDisposableTracker(tracker: IDisposable[] | undefined): void {
disposableTracker = tracker;
Expand Down
13 changes: 12 additions & 1 deletion src/platform/telemetry/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { StopWatch } from '../common/utils/stopWatch';
import { ExcludeType, noop, PickType, UnionToIntersection } from '../common/utils/misc';
import { populateTelemetryWithErrorInfo } from '../errors';
import { TelemetryEventInfo, IEventNamePropertyMapping } from '../../telemetry';
import { workspace } from 'vscode';
import { workspace, type Disposable } from 'vscode';

/**
* TODO@rebornix
Expand Down Expand Up @@ -45,6 +45,17 @@ export function isTelemetryDisabled(): boolean {
return settings.globalValue === false ? true : false;
}

export function onDidChangeTelemetryEnablement(handler: (enabled: boolean) => void): Disposable {
return workspace.onDidChangeConfiguration((e) => {
if (!e.affectsConfiguration('telemetry')) {
return;
}
const settings = workspace.getConfiguration('telemetry').inspect<boolean>('enableTelemetry')!;
const enabled = settings.globalValue === false ? true : false;
handler(enabled);
});
}

const sharedProperties: Partial<SharedPropertyMapping> = {};
/**
* Set shared properties for all telemetry events.
Expand Down
13 changes: 9 additions & 4 deletions src/standalone/import-export/importTracker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
notebooks,
workspace
} from 'vscode';
import { ResourceTypeTelemetryProperty, sendTelemetryEvent } from '../../telemetry';
import { ResourceTypeTelemetryProperty, onDidChangeTelemetryEnablement, sendTelemetryEvent } from '../../telemetry';
import { IExtensionSyncActivationService } from '../../platform/activation/types';
import { isCI, isTestExecution, JupyterNotebookView, PYTHON_LANGUAGE } from '../../platform/common/constants';
import { dispose } from '../../platform/common/utils/lifecycle';
Expand Down Expand Up @@ -64,11 +64,10 @@ export class ImportTracker implements IExtensionSyncActivationService, IDisposab
private pendingChecks = new ResourceMap<NodeJS.Timer | number>();
private disposables: IDisposable[] = [];
private sentMatches = new Set<string>();
private get isTelemetryDisabled() {
return isTelemetryDisabled();
}
private isTelemetryDisabled: boolean;
constructor(@inject(IDisposableRegistry) disposables: IDisposableRegistry) {
disposables.push(this);
this.isTelemetryDisabled = isTelemetryDisabled();
workspace.onDidOpenNotebookDocument(
(t) => this.onOpenedOrClosedNotebookDocument(t, 'onOpenCloseOrSave'),
this.disposables
Expand All @@ -90,6 +89,12 @@ export class ImportTracker implements IExtensionSyncActivationService, IDisposab
this,
disposables
);
this.disposables.push(
onDidChangeTelemetryEnablement((enabled) => {
this.isTelemetryDisabled = enabled;
}),
this
);
}

public dispose() {
Expand Down
4 changes: 3 additions & 1 deletion src/standalone/import-export/importTracker.unit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import { ResourceTypeTelemetryProperty, getTelemetryReporter } from '../../telem
import { waitForCondition } from '../../test/common';
import { createMockedNotebookDocument } from '../../test/datascience/editor-integration/helpers';
import { mockedVSCodeNamespaces } from '../../test/vscode-mock';
import { EmptyEvent } from '../../platform/common/utils/events';

[true, false].forEach((useCustomMetadata) => {
suite(`Import Tracker (${useCustomMetadata ? 'with custom metadata' : 'without custom metadata'})`, async () => {
Expand Down Expand Up @@ -140,6 +141,7 @@ import { mockedVSCodeNamespaces } from '../../test/vscode-mock';
onDidChangeNotebookCellExecutionState.event
);
when(mockedVSCodeNamespaces.workspace.notebookDocuments).thenReturn([]);
when(mockedVSCodeNamespaces.workspace.onDidChangeConfiguration).thenReturn(EmptyEvent);
when(mockedVSCodeNamespaces.workspace.getConfiguration('telemetry')).thenReturn({
inspect: () => {
return {
Expand All @@ -148,7 +150,7 @@ import { mockedVSCodeNamespaces } from '../../test/vscode-mock';
};
}
} as any);
importTracker = new ImportTracker(disposables);
importTracker = new ImportTracker([]);
});
teardown(() => {
sinon.restore();
Expand Down
Loading