From adb90dc0b61030e937326bb20c2118bc3110e7f5 Mon Sep 17 00:00:00 2001 From: Primiano Tucci Date: Thu, 24 Oct 2024 17:21:17 +0100 Subject: [PATCH] ui: Fix trace off-by-one in UiMain Before this CL, this.trace was set only in oncreate. But that is too late, because by that point, mithril already called view() once. Creating view(), in turn causes the instantiation of the sub-components. As a result we ended up instantiating components with an empty trace attrs on the first call (after the second call, the non-null trace got passed correctly). This creates surprising behaviour if the sub-component end up binding the trace object passed oncreate. It doesn't happen today but I hit this while writing a CL. This CL fixes it by moving most of the logic in the constructor. Change-Id: I7d45318ca4790d889dd815faadb8307a38e10b57 --- ui/src/frontend/ui_main.ts | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/ui/src/frontend/ui_main.ts b/ui/src/frontend/ui_main.ts index c1d105077c..69ab3b8992 100644 --- a/ui/src/frontend/ui_main.ts +++ b/ui/src/frontend/ui_main.ts @@ -16,7 +16,7 @@ import m from 'mithril'; import {copyToClipboard} from '../base/clipboard'; import {findRef} from '../base/dom_utils'; import {FuzzyFinder} from '../base/fuzzy'; -import {assertExists, assertTrue, assertUnreachable} from '../base/logging'; +import {assertExists, assertUnreachable} from '../base/logging'; import {undoCommonChatAppReplacements} from '../base/string_utils'; import {Actions} from '../common/actions'; import { @@ -98,9 +98,9 @@ export class UiMainPerTrace implements m.ClassComponent { private trace?: TraceImpl; // This function is invoked once per trace. - async oncreate(vnode: m.VnodeDOM) { - this.updateOmniboxInputRef(vnode.dom); - this.maybeFocusOmnibar(); + constructor() { + const trace = AppImpl.instance.trace; + this.trace = trace; // Register global commands (commands that are useful even without a trace // loaded). @@ -125,10 +125,7 @@ export class UiMainPerTrace implements m.ClassComponent { // When the UI loads there is no trace. There is no point registering // commands or anything in this state as they will be useless. - const trace = AppImpl.instance.trace as TraceImpl; if (trace === undefined) return; - assertTrue(trace instanceof TraceImpl); - this.trace = trace; document.title = `${trace.traceInfo.traceTitle || 'Trace'} - Perfetto UI`; this.maybeShowJsonWarning(); @@ -657,6 +654,11 @@ export class UiMainPerTrace implements m.ClassComponent { return m('.stepthrough', children); } + oncreate(vnode: m.VnodeDOM) { + this.updateOmniboxInputRef(vnode.dom); + this.maybeFocusOmnibar(); + } + view({children}: m.Vnode): m.Children { const cmdMgr = AppImpl.instance.commands; const hotkeys: HotkeyConfig[] = [];