Skip to content

Commit

Permalink
Adds timestamp to state models for webviews
Browse files Browse the repository at this point in the history
  • Loading branch information
eamodio committed May 17, 2023
1 parent fdec17c commit 2ff1890
Show file tree
Hide file tree
Showing 18 changed files with 47 additions and 10 deletions.
3 changes: 3 additions & 0 deletions src/plus/webviews/focus/focusWebview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,7 @@ export class FocusWebviewProvider implements WebviewProvider<State> {
const { subscription, isPlus } = await this.getSubscription();
if (!isPlus) {
return {
timestamp: Date.now(),
isPlus: isPlus,
subscription: subscription,
};
Expand All @@ -273,6 +274,7 @@ export class FocusWebviewProvider implements WebviewProvider<State> {

if (deferState || !hasConnectedRepos) {
return {
timestamp: Date.now(),
isPlus: isPlus,
subscription: subscription,
repos: (hasConnectedRepos ? connectedRepos : githubRepos).map(r => serializeRepoWithRichRemote(r)),
Expand All @@ -296,6 +298,7 @@ export class FocusWebviewProvider implements WebviewProvider<State> {
}));

return {
timestamp: Date.now(),
isPlus: isPlus,
subscription: subscription,
pullRequests: serializedPrs,
Expand Down
2 changes: 2 additions & 0 deletions src/plus/webviews/focus/protocol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import type { Subscription } from '../../../subscription';
import { IpcCommandType, IpcNotificationType } from '../../../webviews/protocol';

export type State = {
timestamp: number;

isPlus: boolean;
subscription: Subscription;
pullRequests?: PullRequestResult[];
Expand Down
5 changes: 3 additions & 2 deletions src/plus/webviews/graph/graphWebview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1712,7 +1712,7 @@ export class GraphWebviewProvider implements WebviewProvider<State> {

private async getState(deferRows?: boolean): Promise<State> {
if (this.container.git.repositoryCount === 0) {
return { debugging: this.container.debugging, allowed: true, repositories: [] };
return { timestamp: Date.now(), debugging: this.container.debugging, allowed: true, repositories: [] };
}

if (this.trialBanner == null) {
Expand All @@ -1725,7 +1725,7 @@ export class GraphWebviewProvider implements WebviewProvider<State> {
if (this.repository == null) {
this.repository = this.container.git.getBestRepositoryOrFirst();
if (this.repository == null) {
return { debugging: this.container.debugging, allowed: true, repositories: [] };
return { timestamp: Date.now(), debugging: this.container.debugging, allowed: true, repositories: [] };
}
}

Expand Down Expand Up @@ -1802,6 +1802,7 @@ export class GraphWebviewProvider implements WebviewProvider<State> {
}

return {
timestamp: Date.now(),
windowFocused: this.isWindowFocused,
trialBanner: this.trialBanner,
repositories: formatRepositories(this.container.git.openRepositories),
Expand Down
2 changes: 2 additions & 0 deletions src/plus/webviews/graph/protocol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ export const enum GraphMinimapMarkerTypes {
export const supportedRefMetadataTypes: GraphRefMetadataType[] = Object.values(GraphRefMetadataTypes);

export interface State {
timestamp: number;

windowFocused?: boolean;
repositories?: GraphRepository[];
selectedRepository?: string;
Expand Down
2 changes: 2 additions & 0 deletions src/plus/webviews/timeline/protocol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import type { FeatureAccess } from '../../../features';
import { IpcCommandType, IpcNotificationType } from '../../../webviews/protocol';

export interface State {
timestamp: number;

dataset?: Commit[];
emptyMessage?: string;
period: Period;
Expand Down
4 changes: 4 additions & 0 deletions src/plus/webviews/timeline/timelineWebview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,7 @@ export class TimelineWebviewProvider implements WebviewProvider<State> {
if (current.uri == null) {
const access = await this.container.git.access(PlusFeatures.Timeline);
return {
timestamp: Date.now(),
emptyMessage: 'There are no editors open that can provide file history information',
period: period,
title: '',
Expand All @@ -268,6 +269,7 @@ export class TimelineWebviewProvider implements WebviewProvider<State> {
if (access.allowed === false) {
const dataset = generateRandomTimelineDataset();
return {
timestamp: Date.now(),
dataset: dataset.sort((a, b) => b.sort - a.sort),
period: period,
title: 'src/app/index.ts',
Expand Down Expand Up @@ -296,6 +298,7 @@ export class TimelineWebviewProvider implements WebviewProvider<State> {

if (log == null) {
return {
timestamp: Date.now(),
dataset: [],
emptyMessage: 'No commits found for the specified time period',
period: period,
Expand Down Expand Up @@ -354,6 +357,7 @@ export class TimelineWebviewProvider implements WebviewProvider<State> {
dataset.sort((a, b) => b.sort - a.sort);

return {
timestamp: Date.now(),
dataset: dataset,
period: period,
title: title,
Expand Down
16 changes: 13 additions & 3 deletions src/webviews/apps/shared/appBase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ function nextIpcId() {
return `webview:${ipcSequence}`;
}

export abstract class App<State = undefined> {
export abstract class App<State extends { timestamp: number } = { timestamp: number }> {
private readonly _api: VsCodeApi;
protected state: State;

Expand All @@ -65,6 +65,16 @@ export abstract class App<State = undefined> {
// this.log(`ctor(${this.state ? JSON.stringify(this.state) : ''})`);

this._api = acquireVsCodeApi();
if (this.state != null) {
const state = this.getState();
if (state != null) {
if (this.state.timestamp > (state.timestamp ?? 0)) {
this._api.setState(this.state);
} else {
this.state = state;
}
}
}

const disposables: Disposable[] = [];

Expand Down Expand Up @@ -151,8 +161,8 @@ export abstract class App<State = undefined> {
Logger.log(message, ...optionalParams);
}

protected getState(): State {
return this._api.getState() as State;
protected getState(): State | undefined {
return this._api.getState() as State | undefined;
}

protected sendCommand<TCommand extends IpcCommandType<any>>(
Expand Down
2 changes: 2 additions & 0 deletions src/webviews/apps/shared/appWithConfigBase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ const date = new Date(
);

interface AppStateWithConfig {
timestamp: number;

config: Config;
customSettings?: Record<string, boolean>;
}
Expand Down
1 change: 1 addition & 0 deletions src/webviews/commitDetails/commitDetailsWebview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -447,6 +447,7 @@ export class CommitDetailsWebviewProvider implements WebviewProvider<State, Seri
// const commitChoices = await Promise.all(this.commits.map(async commit => summaryModel(commit)));

const state = serialize<State>({
timestamp: Date.now(),
pinned: current.pinned,
includeRichContent: current.richStateLoaded,
// commits: commitChoices,
Expand Down
2 changes: 2 additions & 0 deletions src/webviews/commitDetails/protocol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ export type Preferences = {
};

export type State = {
timestamp: number;

pinned: boolean;
preferences?: Preferences;
// commits?: CommitSummary[];
Expand Down
1 change: 1 addition & 0 deletions src/webviews/home/homeWebview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,7 @@ export class HomeWebviewProvider implements WebviewProvider<State> {
const dismissedBanners = this.container.storage.get('home:banners:dismissed', []);

return {
timestamp: Date.now(),
repositories: this.getRepositoriesState(),
webroot: this.host.getWebRoot(),
subscription: sub.subscription,
Expand Down
8 changes: 3 additions & 5 deletions src/webviews/home/protocol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,9 @@ export const enum CompletedActions {
}

export interface State {
repositories: {
count: number;
openCount: number;
hasUnsafe: boolean;
};
timestamp: number;

repositories: DidChangeRepositoriesParams;
webroot?: string;
subscription: Subscription;
completedActions: CompletedActions[];
Expand Down
2 changes: 2 additions & 0 deletions src/webviews/rebase/protocol.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { IpcCommandType, IpcNotificationType } from '../protocol';

export interface State {
timestamp: number;

branch: string;
onto: { sha: string; commit?: Commit } | undefined;

Expand Down
1 change: 1 addition & 0 deletions src/webviews/rebase/rebaseEditor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -691,6 +691,7 @@ async function parseRebaseTodo(
}

return {
timestamp: Date.now(),
branch: context.branchName ?? '',
onto: onto
? {
Expand Down
2 changes: 2 additions & 0 deletions src/webviews/settings/protocol.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import type { Config } from '../../config';

export interface State {
timestamp: number;

config: Config;
customSettings?: Record<string, boolean>;
scope: 'user' | 'workspace';
Expand Down
1 change: 1 addition & 0 deletions src/webviews/settings/settingsWebview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ export class SettingsWebviewProvider extends WebviewProviderWithConfigBase<State
}

return {
timestamp: Date.now(),
// Make sure to get the raw config, not from the container which has the modes mixed in
config: configuration.getAll(true),
customSettings: this.getCustomSettings(),
Expand Down
2 changes: 2 additions & 0 deletions src/webviews/welcome/protocol.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import type { Config } from '../../config';

export interface State {
timestamp: number;

config: Config;
customSettings?: Record<string, boolean>;
}
1 change: 1 addition & 0 deletions src/webviews/welcome/welcomeWebview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import type { State } from './protocol';
export class WelcomeWebviewProvider extends WebviewProviderWithConfigBase<State> implements WebviewProvider<State> {
includeBootstrap(): State {
return {
timestamp: Date.now(),
// Make sure to get the raw config, not from the container which has the modes mixed in
config: configuration.getAll(true),
};
Expand Down

0 comments on commit 2ff1890

Please sign in to comment.