Skip to content

Commit

Permalink
Stop monitoring events when no longer required (#15528)
Browse files Browse the repository at this point in the history
* Stop monitoring events when no longer required

* Dispose handler in another spot

* Simpler

* Misc
  • Loading branch information
DonJayamanne authored Apr 10, 2024
1 parent f432cdb commit 34660ae
Showing 1 changed file with 11 additions and 4 deletions.
15 changes: 11 additions & 4 deletions src/standalone/survey/dataScienceSurveyBanner.node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

import { inject, injectable } from 'inversify';
import {
Disposable,
NotebookCellExecutionState,
NotebookCellExecutionStateChangeEvent,
UIKind,
Expand All @@ -25,6 +26,7 @@ import { isJupyterNotebook } from '../../platform/common/utils';
import { noop } from '../../platform/common/utils/misc';
import { openInBrowser } from '../../platform/common/net/browser';
import { getVSCodeChannel } from '../../platform/common/application/applicationEnvironment';
import type { IDisposable } from '@c4312/evt';

export const ISurveyBanner = Symbol('ISurveyBanner');
export interface ISurveyBanner extends IExtensionSyncActivationService, IJupyterExtensionBanner {}
Expand Down Expand Up @@ -101,7 +103,7 @@ export class DataScienceSurveyBanner implements IJupyterExtensionBanner, IExtens
private readonly showBannerState = new Map<BannerType, IPersistentState<ShowBannerWithExpiryTime>>();
private static surveyDelay = false;
private readonly NotebookExecutionThreshold = 250; // Cell executions before showing survey

private onDidChangeNotebookCellExecutionStateHandler?: IDisposable;
constructor(
@inject(IPersistentStateFactory) private persistentState: IPersistentStateFactory,
@inject(IDisposableRegistry) private disposables: IDisposableRegistry
Expand All @@ -110,16 +112,17 @@ export class DataScienceSurveyBanner implements IJupyterExtensionBanner, IExtens
this.setPersistentState(BannerType.ExperimentNotebookSurvey, ExperimentNotebookSurveyStateKeys.ShowBanner);

// Change the surveyDelay flag after 10 minutes
setTimeout(
const timer = setTimeout(
() => {
DataScienceSurveyBanner.surveyDelay = true;
},
10 * 60 * 1000
);
this.disposables.push(new Disposable(() => clearTimeout(timer)));
}

public activate() {
notebooks.onDidChangeNotebookCellExecutionState(
this.onDidChangeNotebookCellExecutionStateHandler = notebooks.onDidChangeNotebookCellExecutionState(
this.onDidChangeNotebookCellExecutionState,
this,
this.disposables
Expand All @@ -128,12 +131,12 @@ export class DataScienceSurveyBanner implements IJupyterExtensionBanner, IExtens

public async showBanner(type: BannerType): Promise<void> {
const show = this.shouldShowBanner(type);
this.onDidChangeNotebookCellExecutionStateHandler?.dispose();
if (!show) {
return;
}
// Disable for the current session.
this.disabledInCurrentSession = true;

const response = await window.showInformationMessage(this.getBannerMessage(type), ...this.bannerLabels);
switch (response) {
case this.bannerLabels[DSSurveyLabelIndex.Yes]: {
Expand Down Expand Up @@ -226,6 +229,10 @@ export class DataScienceSurveyBanner implements IJupyterExtensionBanner, IExtens
}

private async updateStateAndShowBanner(val: string, banner: BannerType) {
if (!this.shouldShowBanner(banner)) {
this.onDidChangeNotebookCellExecutionStateHandler?.dispose();
return;
}
const state = this.persistentState.createGlobalPersistentState<number>(val, 0);
await state.updateValue(state.value + 1);
this.showBanner(banner).catch(noop);
Expand Down

0 comments on commit 34660ae

Please sign in to comment.