Skip to content

Commit 8f084bf

Browse files
committed
ui: Remove TraceController
Turn TraceController in an async function. Change-Id: I2791e351bece49e3e5006c76a3ab69dc119fc3eb
1 parent 8d13b68 commit 8f084bf

File tree

16 files changed

+120
-286
lines changed

16 files changed

+120
-286
lines changed

ui/src/common/actions.ts

Lines changed: 0 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -22,20 +22,11 @@ import {
2222
RecordingTarget,
2323
State,
2424
} from './state';
25-
import {SerializedAppState} from '../public/state_serialization_schema';
26-
import {PostedTrace} from '../public/trace_source';
2725

2826
type StateDraft = Draft<State>;
2927

30-
function generateNextId(draft: StateDraft): string {
31-
const nextId = String(Number(draft.nextId) + 1);
32-
draft.nextId = nextId;
33-
return nextId;
34-
}
35-
3628
export const StateActions = {
3729
clearState(state: StateDraft, _args: {}) {
38-
const nextId = state.nextId;
3930
const recordConfig = state.recordConfig;
4031
const recordingTarget = state.recordingTarget;
4132
const fetchChromeCategories = state.fetchChromeCategories;
@@ -44,7 +35,6 @@ export const StateActions = {
4435
const chromeCategories = state.chromeCategories;
4536

4637
Object.assign(state, createEmptyState());
47-
state.nextId = nextId;
4838
state.recordConfig = recordConfig;
4939
state.recordingTarget = recordingTarget;
5040
state.fetchChromeCategories = fetchChromeCategories;
@@ -53,49 +43,6 @@ export const StateActions = {
5343
state.chromeCategories = chromeCategories;
5444
},
5545

56-
openTraceFromFile(state: StateDraft, args: {file: File}): void {
57-
this.clearState(state, {});
58-
const id = generateNextId(state);
59-
state.engine = {
60-
id,
61-
source: {type: 'FILE', file: args.file},
62-
};
63-
},
64-
65-
openTraceFromBuffer(state: StateDraft, args: PostedTrace): void {
66-
this.clearState(state, {});
67-
const id = generateNextId(state);
68-
state.engine = {
69-
id,
70-
source: {type: 'ARRAY_BUFFER', ...args},
71-
};
72-
},
73-
74-
openTraceFromUrl(
75-
state: StateDraft,
76-
args: {url: string; serializedAppState?: SerializedAppState},
77-
): void {
78-
this.clearState(state, {});
79-
const id = generateNextId(state);
80-
state.engine = {
81-
id,
82-
source: {
83-
type: 'URL',
84-
url: args.url,
85-
serializedAppState: args.serializedAppState,
86-
},
87-
};
88-
},
89-
90-
openTraceFromHttpRpc(state: StateDraft, _args: {}): void {
91-
this.clearState(state, {});
92-
const id = generateNextId(state);
93-
state.engine = {
94-
id,
95-
source: {type: 'HTTP_RPC'},
96-
};
97-
},
98-
9946
requestTrackReload(state: StateDraft, _: {}) {
10047
// eslint-disable-next-line @typescript-eslint/strict-boolean-expressions
10148
if (state.lastTrackReloadRequest) {

ui/src/common/actions_unittest.ts

Lines changed: 0 additions & 53 deletions
This file was deleted.

ui/src/common/empty_state.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@ export function keyedMap<T>(
4646
export function createEmptyState(): State {
4747
return {
4848
version: STATE_VERSION,
49-
nextId: '-1',
5049

5150
recordConfig: AUTOLOAD_STARTED_CONFIG_FLAG.get()
5251
? autosaveConfigStore.get()

ui/src/common/recordingV2/recording_page_controller.ts

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
import {assertExists, assertTrue} from '../../base/logging';
1616
import {currentDateHourAndMinute} from '../../base/time';
17+
import {AppImpl} from '../../core/app_impl';
1718
import {raf} from '../../core/raf_scheduler';
1819
import {globals} from '../../frontend/globals';
1920
import {autosaveConfigStore} from '../../frontend/record_config';
@@ -306,13 +307,11 @@ export class RecordingPageController {
306307
if (this.tracingSessionWrapper !== tracingSessionWrapper) {
307308
return;
308309
}
309-
globals.dispatch(
310-
Actions.openTraceFromBuffer({
311-
title: 'Recorded trace',
312-
buffer: trace.buffer,
313-
fileName: `trace_${currentDateHourAndMinute()}${TRACE_SUFFIX}`,
314-
}),
315-
);
310+
AppImpl.instance.openTraceFromBuffer({
311+
title: 'Recorded trace',
312+
buffer: trace.buffer,
313+
fileName: `trace_${currentDateHourAndMinute()}${TRACE_SUFFIX}`,
314+
});
316315
this.clearRecordingState();
317316
}
318317

ui/src/common/state.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,6 @@ export interface PendingDeeplinkState {
158158

159159
export interface State {
160160
version: number;
161-
nextId: string;
162161

163162
/**
164163
* State of the ConfigEditor.

ui/src/controller/app_controller.ts

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,8 @@
1313
// limitations under the License.
1414

1515
import {RECORDING_V2_FLAG} from '../core/feature_flags';
16-
import {globals} from '../frontend/globals';
1716
import {Child, Controller, ControllerInitializerAny} from './controller';
1817
import {RecordController} from './record_controller';
19-
import {TraceController} from './trace_controller';
2018

2119
// The root controller for the entire app. It handles the lifetime of all
2220
// the other controllers (e.g., track and query controllers) according to the
@@ -44,10 +42,6 @@ export class AppController extends Controller<'main'> {
4442
Child('record', RecordController, {extensionPort: this.extensionPort}),
4543
);
4644
}
47-
if (globals.state.engine !== undefined) {
48-
const engineCfg = globals.state.engine;
49-
childControllers.push(Child(engineCfg.id, TraceController, engineCfg));
50-
}
5145
return childControllers;
5246
}
5347
}

ui/src/controller/record_controller.ts

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ import {
4545
import {Controller} from './controller';
4646
import {RecordConfig} from './record_config_types';
4747
import {Consumer, RpcConsumerPort} from './record_controller_interfaces';
48+
import {AppImpl} from '../core/app_impl';
4849

4950
type RPCImplMethod = Method | rpc.ServiceMethod<Message<{}>, Message<{}>>;
5051

@@ -328,13 +329,11 @@ export class RecordController extends Controller<'main'> implements Consumer {
328329
return;
329330
}
330331
const trace = this.generateTrace();
331-
globals.dispatch(
332-
Actions.openTraceFromBuffer({
333-
title: 'Recorded trace',
334-
buffer: trace.buffer,
335-
fileName: `recorded_trace${this.recordedTraceSuffix}`,
336-
}),
337-
);
332+
AppImpl.instance.openTraceFromBuffer({
333+
title: 'Recorded trace',
334+
buffer: trace.buffer,
335+
fileName: `recorded_trace${this.recordedTraceSuffix}`,
336+
});
338337
this.traceBuffer = [];
339338
}
340339

ui/src/core/app_impl.ts

Lines changed: 48 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,10 @@ import {PluginManager} from './plugin_manager';
2323
import {NewEngineMode} from '../trace_processor/engine';
2424
import {RouteArgs} from '../public/route_schema';
2525
import {SqlPackage} from '../public/extra_sql_packages';
26-
27-
// The pseudo plugin id used for the core instance of AppImpl.
28-
29-
export const CORE_PLUGIN_ID = '__core__';
26+
import {SerializedAppState} from '../public/state_serialization_schema';
27+
import {PostedTrace, TraceSource} from '../public/trace_source';
28+
import {loadTrace} from './load_trace';
29+
import {CORE_PLUGIN_ID} from './plugin_manager';
3030

3131
// The args that frontend/index.ts passes when calling AppImpl.initialize().
3232
// This is to deal with injections that would otherwise cause circular deps.
@@ -160,6 +160,50 @@ export class AppImpl implements App {
160160
return this.appCtx.initialRouteArgs;
161161
}
162162

163+
openTraceFromFile(file: File): void {
164+
this.openTrace({type: 'FILE', file});
165+
}
166+
167+
openTraceFromUrl(url: string, serializedAppState?: SerializedAppState) {
168+
this.openTrace({type: 'URL', url, serializedAppState});
169+
}
170+
171+
openTraceFromBuffer(postMessageArgs: PostedTrace): void {
172+
this.openTrace({type: 'ARRAY_BUFFER', ...postMessageArgs});
173+
}
174+
175+
openTraceFromHttpRpc(): void {
176+
this.openTrace({type: 'HTTP_RPC'});
177+
}
178+
179+
private async openTrace(src: TraceSource) {
180+
assertTrue(this.pluginId === CORE_PLUGIN_ID);
181+
this.closeCurrentTrace();
182+
this.appCtx.isLoadingTrace = true;
183+
try {
184+
// loadTrace() in trace_loader.ts will do the following:
185+
// - Create a new engine.
186+
// - Pump the data from the TraceSource into the engine.
187+
// - Do the initial queries to build the TraceImpl object
188+
// - Call AppImpl.setActiveTrace(TraceImpl)
189+
// - Continue with the trace loading logic (track decider, plugins, etc)
190+
// - Resolve the promise when everything is done.
191+
await loadTrace(this, src);
192+
this.omnibox.reset(/* focus= */ false);
193+
// loadTrace() internally will call setActiveTrace() and change our
194+
// _currentTrace in the middle of its ececution. We cannot wait for
195+
// loadTrace to be finished before setting it because some internal
196+
// implementation details of loadTrace() rely on that trace to be current
197+
// to work properly (mainly the router hash uuid).
198+
} catch (err) {
199+
this.omnibox.showStatusMessage(`${err}`);
200+
throw err;
201+
} finally {
202+
this.appCtx.isLoadingTrace = false;
203+
raf.scheduleFullRedraw();
204+
}
205+
}
206+
163207
closeCurrentTrace() {
164208
// This method should be called only on the core instance, plugins don't
165209
// have access to openTrace*() methods.
@@ -190,13 +234,6 @@ export class AppImpl implements App {
190234
return this.appCtx.isLoadingTrace;
191235
}
192236

193-
// TODO(primiano): this is very temporary and will go away as soon as
194-
// TraceController is turned into an async function.
195-
setIsLoadingTrace(loading: boolean) {
196-
this.appCtx.isLoadingTrace = loading;
197-
raf.scheduleFullRedraw();
198-
}
199-
200237
get rootUrl() {
201238
return this.appCtx.initArgs.rootUrl;
202239
}

0 commit comments

Comments
 (0)