Skip to content

Commit

Permalink
Updates change events for integrations in Home
Browse files Browse the repository at this point in the history
  • Loading branch information
d13 committed Feb 3, 2025
1 parent c982edb commit 002a029
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import type { Event } from 'vscode';
import { EventEmitter } from 'vscode';
import type { IntegrationId } from '../../../constants.integrations';
import { HostingIntegrationId } from '../../../constants.integrations';
import type { StoredConfiguredIntegrationDescriptor } from '../../../constants.storage';
import type { Container } from '../../../container';
import { debounce } from '../../../system/function';
import { flatten } from '../../../system/iterable';
import { getBuiltInIntegrationSession } from '../../gk/utils/-webview/integrationAuthentication.utils';
import { isSelfHostedIntegrationId, providersMetadata } from '../providers/models';
Expand All @@ -24,7 +27,16 @@ interface StoredSession {

export type ConfiguredIntegrationType = 'cloud' | 'local';

export interface ConfiguredIntegrationChangeEvent {
ids: IntegrationId[];
}

export class ConfiguredIntegrationService {
private readonly _onDidChangeConfiguredIntegrations = new EventEmitter<ConfiguredIntegrationChangeEvent>();
get onDidChangeConfiguredIntegrations(): Event<ConfiguredIntegrationChangeEvent> {
return this._onDidChangeConfiguredIntegrations.event;
}

private _configured?: Map<IntegrationId, ConfiguredIntegrationDescriptor[]>;

constructor(private readonly container: Container) {}
Expand Down Expand Up @@ -165,6 +177,7 @@ export class ConfiguredIntegrationService {

descriptors.push(descriptor);
this.configured.set(descriptor.integrationId, descriptors);
this.queueDidChange(descriptor.integrationId);
await this.storeConfigured();
}

Expand All @@ -187,6 +200,7 @@ export class ConfiguredIntegrationService {
}

this.configured.set(id, descriptors ?? []);
this.queueDidChange(id);
await this.storeConfigured();
}

Expand Down Expand Up @@ -346,6 +360,18 @@ export class ConfiguredIntegrationService {
getSessionId(descriptor: IntegrationAuthenticationSessionDescriptor): string {
return descriptor.domain;
}

private changedIds = new Set<IntegrationId>();
private debouncedFireDidChange?: () => void;
private queueDidChange(id: IntegrationId) {
this.debouncedFireDidChange ??= debounce(() => {
this._onDidChangeConfiguredIntegrations.fire({ ids: [...this.changedIds] });
this.changedIds.clear();
}, 300);

this.changedIds.add(id);
this.debouncedFireDidChange();
}
}

function convertStoredSessionToSession(
Expand Down
9 changes: 8 additions & 1 deletion src/plus/integrations/integrationService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,10 @@ import { filterMap, flatten, join } from '../../system/iterable';
import { Logger } from '../../system/logger';
import { getLogScope } from '../../system/logger.scope';
import type { SubscriptionChangeEvent } from '../gk/subscriptionService';
import type { ConfiguredIntegrationService } from './authentication/configuredIntegrationService';
import type {
ConfiguredIntegrationChangeEvent,
ConfiguredIntegrationService,
} from './authentication/configuredIntegrationService';
import type { IntegrationAuthenticationService } from './authentication/integrationAuthenticationService';
import type { ConfiguredIntegrationDescriptor } from './authentication/models';
import {
Expand Down Expand Up @@ -69,6 +72,10 @@ export class IntegrationService implements Disposable {
return this._onDidSyncCloudIntegrations.event;
}

get onDidChangeConfiguredIntegrations(): Event<ConfiguredIntegrationChangeEvent> {
return this.configuredIntegrationService.onDidChangeConfiguredIntegrations;
}

private readonly _connectedCache = new Set<string>();
private readonly _disposable: Disposable;
private _integrations = new Map<IntegrationKey, Integration>();
Expand Down
5 changes: 3 additions & 2 deletions src/webviews/home/homeWebview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ import { showPatchesView } from '../../plus/drafts/actions';
import type { Subscription } from '../../plus/gk/models/subscription';
import type { SubscriptionChangeEvent } from '../../plus/gk/subscriptionService';
import { isSubscriptionStatePaidOrTrial } from '../../plus/gk/utils/subscription.utils';
import type { ConfiguredIntegrationChangeEvent } from '../../plus/integrations/authentication/configuredIntegrationService';
import { providersMetadata } from '../../plus/integrations/providers/models';
import type { LaunchpadCategorizedResult } from '../../plus/launchpad/launchpadProvider';
import { getLaunchpadItemGroups } from '../../plus/launchpad/launchpadProvider';
Expand Down Expand Up @@ -157,7 +158,7 @@ export class HomeWebviewProvider implements WebviewProvider<State, State, HomeWe
: emptyDisposable,
this.container.subscription.onDidChange(this.onSubscriptionChanged, this),
onDidChangeContext(this.onContextChanged, this),
this.container.integrations.onDidChangeConnectionState(this.onChangeConnectionState, this),
this.container.integrations.onDidChangeConfiguredIntegrations(this.onChangeConnectionState, this),
this.container.walkthrough.onProgressChanged(this.onWalkthroughChanged, this),
configuration.onDidChange(this.onDidChangeConfig, this),
this.container.launchpad.onDidChange(this.onDidLaunchpadChange, this),
Expand Down Expand Up @@ -217,7 +218,7 @@ export class HomeWebviewProvider implements WebviewProvider<State, State, HomeWe
this.notifyDidCompleteDiscoveringRepositories();
}

private onChangeConnectionState() {
private onChangeConnectionState(_e: ConfiguredIntegrationChangeEvent) {
void this.notifyDidChangeIntegrations();
}

Expand Down

0 comments on commit 002a029

Please sign in to comment.