Skip to content

Commit b2d4bd2

Browse files
committed
preserve data when webviews are moved
1 parent e40250f commit b2d4bd2

File tree

4 files changed

+31
-45
lines changed

4 files changed

+31
-45
lines changed

.vscode/launch.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
"debugWebviews": true,
1414
"cwd": "${workspaceFolder}/packages/vscode-js-profile-flame",
1515
"rendererDebugOptions": {
16-
"webRoot": "${workspaceFolder}/packages/vscode-js-profile-table"
16+
"webRoot": "${workspaceFolder}/packages/vscode-js-profile-flame"
1717
},
1818
"args": [
1919
"--extensionDevelopmentPath=${workspaceFolder}/packages/vscode-js-profile-table",

packages/vscode-js-profile-flame/src/extension.ts

-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ export function activate(context: vscode.ExtensionContext) {
3434

3535
vscode.workspace.onDidChangeConfiguration(evt => {
3636
if (allConfig.some(c => evt.affectsConfiguration(c))) {
37-
realtime.updateSettings();
3837
realtimeTracker.updateSettings();
3938
}
4039
}),

packages/vscode-js-profile-flame/src/realtimeSessionTracker.ts

+28-17
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@ export class RealtimeSessionTracker {
4242
private webviews = new Set<vscode.WebviewView>();
4343
private sessionData = new Map<vscode.DebugSession, ISessionData>();
4444
private displayedSession?: vscode.DebugSession;
45-
private hasVisibleSession = false;
4645

4746
/**
4847
* Returns any realtime metric webviews that are currently visible.
@@ -56,16 +55,18 @@ export class RealtimeSessionTracker {
5655
*/
5756
public trackWebview(webview: vscode.WebviewView) {
5857
this.webviews.add(webview);
58+
5959
webview.onDidChangeVisibility(() => {
60-
this.recalcVisibility();
60+
if (webview.visible) {
61+
this.hydrate(webview.webview);
62+
}
6163
});
6264

6365
webview.onDidDispose(() => {
6466
this.webviews.delete(webview);
65-
this.recalcVisibility();
6667
});
6768

68-
this.recalcVisibility();
69+
this.hydrate(webview.webview);
6970
}
7071

7172
/**
@@ -98,7 +99,7 @@ export class RealtimeSessionTracker {
9899
}
99100

100101
this.displayedSession = session;
101-
this.postMessage({ type: MessageType.ApplyData, data: data.metrics.map(m => m.metrics) });
102+
this.broadcast({ type: MessageType.ApplyData, data: data.metrics.map(m => m.metrics) });
102103
}
103104

104105
/**
@@ -129,6 +130,8 @@ export class RealtimeSessionTracker {
129130
metric.reset(steps + 3); // no-ops if the steps are already the same
130131
}
131132
}
133+
134+
this.broadcast({ type: MessageType.UpdateSettings, settings: this.settings });
132135
}
133136

134137
private collectFromSession(data: ISessionData) {
@@ -163,26 +166,34 @@ export class RealtimeSessionTracker {
163166
}
164167

165168
if (session === this.displayedSession) {
166-
this.postMessage({ type: MessageType.AddData, data: v });
169+
this.broadcast({ type: MessageType.AddData, data: v });
167170
}
168171
}
169172

170-
private postMessage(message: ToWebViewMessage) {
173+
private broadcast(message: ToWebViewMessage) {
171174
for (const webview of this.visibleWebviews) {
172175
webview.webview.postMessage(message);
173176
}
174177
}
175178

176-
private recalcVisibility() {
177-
const wasVisible = this.hasVisibleSession;
178-
const visible = [...this.webviews].some(w => w.visible);
179-
this.hasVisibleSession = visible;
180-
if (visible && !wasVisible) {
181-
this.onDidChangeActiveSession(vscode.debug.activeDebugSession);
182-
} else if (!visible) {
183-
for (const session of this.sessionData.keys()) {
184-
this.onSessionDidEnd(session);
185-
}
179+
private hydrate(webview: vscode.Webview) {
180+
webview.postMessage(this.getSettingsUpdate());
181+
182+
const data = this.displayedSession && this.sessionData.get(this.displayedSession);
183+
if (!data) {
184+
return;
186185
}
186+
187+
const metrics: ToWebViewMessage = {
188+
type: MessageType.ApplyData,
189+
data: data.metrics.map(m => m.metrics),
190+
};
191+
webview.postMessage(metrics);
192+
}
193+
194+
private getSettingsUpdate() {
195+
const settings = readRealtimeSettings();
196+
const message: ToWebViewMessage = { type: MessageType.UpdateSettings, settings };
197+
return message;
187198
}
188199
}

packages/vscode-js-profile-flame/src/realtimeWebviewProvider.ts

+2-26
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44

55
import { randomBytes } from 'crypto';
66
import * as vscode from 'vscode';
7-
import { FromWebViewMessage, MessageType, ToWebViewMessage } from './realtime/protocol';
8-
import { readRealtimeSettings, RealtimeSessionTracker } from './realtimeSessionTracker';
7+
import { FromWebViewMessage, MessageType } from './realtime/protocol';
8+
import { RealtimeSessionTracker } from './realtimeSessionTracker';
99

1010
const enabledMetricsKey = 'enabledMetrics';
1111

@@ -18,16 +18,6 @@ export class RealtimeWebviewProvider implements vscode.WebviewViewProvider {
1818
private readonly tracker: RealtimeSessionTracker,
1919
) {}
2020

21-
/**
22-
* Pushes a settings up to all active webviews.
23-
*/
24-
public updateSettings() {
25-
const message = this.getSettingsUpdate();
26-
for (const view of this.tracker.visibleWebviews) {
27-
view.webview.postMessage(message);
28-
}
29-
}
30-
3121
/**
3222
* @inheritdoc
3323
*/
@@ -39,14 +29,6 @@ export class RealtimeWebviewProvider implements vscode.WebviewViewProvider {
3929
webviewView.webview.html = this.getHtmlForWebview(webviewView.webview);
4030
this.tracker.trackWebview(webviewView);
4131

42-
webviewView.onDidChangeVisibility(() => {
43-
if (webviewView.visible) {
44-
webviewView.webview.postMessage(this.getSettingsUpdate());
45-
}
46-
});
47-
48-
webviewView.webview.postMessage(this.getSettingsUpdate());
49-
5032
webviewView.webview.onDidReceiveMessage((evt: FromWebViewMessage) => {
5133
switch (evt.type) {
5234
case MessageType.SetEnabledMetrics:
@@ -58,12 +40,6 @@ export class RealtimeWebviewProvider implements vscode.WebviewViewProvider {
5840
});
5941
}
6042

61-
private getSettingsUpdate() {
62-
const settings = readRealtimeSettings();
63-
const message: ToWebViewMessage = { type: MessageType.UpdateSettings, settings };
64-
return message;
65-
}
66-
6743
private getHtmlForWebview(webview: vscode.Webview) {
6844
const scriptUri = webview.asWebviewUri(
6945
vscode.Uri.joinPath(this.extensionUri, 'out', 'realtime.bundle.js'),

0 commit comments

Comments
 (0)