-
Notifications
You must be signed in to change notification settings - Fork 460
Expand file tree
/
Copy pathapp.ts
More file actions
422 lines (385 loc) · 13 KB
/
app.ts
File metadata and controls
422 lines (385 loc) · 13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
import { oneLine } from 'common-tags';
import {
getSelectedTab,
getDataSource,
getLocalTrackOrderByPid,
} from 'firefox-profiler/selectors/url-state';
import {
getTrackThreadHeights,
getIsEventDelayTracksEnabled,
getIsExperimentalCPUGraphsEnabled,
getIsExperimentalProcessCPUTracksEnabled,
} from 'firefox-profiler/selectors/app';
import {
getLocalTracksByPid,
getThreads,
getCounters,
getProfile,
} from 'firefox-profiler/selectors/profile';
import { sendAnalytics } from 'firefox-profiler/utils/analytics';
import {
stateFromLocation,
withHistoryReplaceStateSync,
} from 'firefox-profiler/app-logic/url-handling';
import {
finalizeProfileView,
type ProfileAndProfileUpgradeInfo,
} from './receive-profile';
import { fatalError } from './errors';
import {
addEventDelayTracksForThreads,
initializeLocalTrackOrderByPid,
addProcessCPUTracksForProcess,
} from 'firefox-profiler/profile-logic/tracks';
import { selectedThreadSelectors } from 'firefox-profiler/selectors/per-thread';
import {
getIsCPUUtilizationProvided,
getAreThereAnyProcessCPUCounters,
} from 'firefox-profiler/selectors/cpu';
import type {
ThreadsKey,
CssPixels,
Action,
ThunkAction,
UrlState,
UploadedProfileInformation,
IndexIntoCategoryList,
} from 'firefox-profiler/types';
import type { TabSlug } from 'firefox-profiler/app-logic/tabs-handling';
import type {
BrowserConnection,
BrowserConnectionStatus,
} from 'firefox-profiler/app-logic/browser-connection';
export function changeSelectedTab(selectedTab: TabSlug): ThunkAction<void> {
return (dispatch, getState) => {
const previousTab = getSelectedTab(getState());
if (previousTab !== selectedTab) {
sendAnalytics({
hitType: 'pageview',
page: selectedTab,
});
dispatch({
type: 'CHANGE_SELECTED_TAB',
selectedTab,
});
}
};
}
export function changeProfilesToCompare(profiles: string[]): Action {
return {
type: 'CHANGE_PROFILES_TO_COMPARE',
profiles,
};
}
export function startFetchingProfiles(): Action {
return { type: 'START_FETCHING_PROFILES' };
}
export function urlSetupDone(): ThunkAction<void> {
return (dispatch, getState) => {
dispatch({ type: 'URL_SETUP_DONE' });
// After the url setup is done, we can successfully query our state about its
// initial page.
const dataSource = getDataSource(getState());
sendAnalytics({
hitType: 'pageview',
page: dataSource === 'none' ? 'home' : getSelectedTab(getState()),
});
sendAnalytics({
hitType: 'event',
eventCategory: 'datasource',
eventAction: dataSource,
});
};
}
export function show404(url: string): Action {
return { type: 'ROUTE_NOT_FOUND', url };
}
export function changeSidebarOpenState(tab: TabSlug, isOpen: boolean): Action {
return { type: 'CHANGE_SIDEBAR_OPEN_STATE', tab, isOpen };
}
export function invalidatePanelLayout(): Action {
return { type: 'INCREMENT_PANEL_LAYOUT_GENERATION' as const };
}
/**
* The viewport component provides a hint to use shift to zoom scroll. The first
* time a user does this, the hint goes away.
*/
export function setHasZoomedViaMousewheel() {
return { type: 'HAS_ZOOMED_VIA_MOUSEWHEEL' as const };
}
/**
* This function is called when we start setting up the initial url state.
* It takes the location and profile data, converts the location into url
* state and then dispatches relevant actions to finalize the view.
* `profile` parameter can be null when the data source can't provide the profile
* and the url upgrader step is not needed (e.g. 'from-browser').
*/
export function setupInitialUrlState(
location: Location,
profileAndUpgradeInfo: ProfileAndProfileUpgradeInfo | null,
browserConnection: BrowserConnection | null
): ThunkAction<void> {
return (dispatch) => {
let urlState;
try {
urlState = stateFromLocation(location, profileAndUpgradeInfo);
} catch (e) {
if (e.name === 'UrlUpgradeError') {
// The error is an URL upgrade error, let's fire a fatal error.
// If there's a service worker update, the class `ServiceWorkerManager`
// will automatically reload in case the new code knows how to handle
// this URL version.
dispatch(fatalError(e));
return;
}
// The location could not be parsed, show a 404 instead.
console.error(e);
dispatch(show404(location.pathname + location.search));
return;
}
// Validate the initial URL state. We can't refresh on from-file or
// unpublished URLs.
if (
urlState.dataSource === 'from-file' ||
urlState.dataSource === 'unpublished'
) {
urlState = null;
}
// Normally having multiple dispatches is an anti pattern, but here it's
// necessary because we are doing different things inside those actions and
// they can't be merged because we are also calling those seperately on
// other parts of the code.
// The first dispatch here updates the url state, then changes state as the url
// setup is done, and lastly finalizes the profile view since everything is set up now.
// All of this is done while the history is replaced, as this is part of the initial
// load process.
withHistoryReplaceStateSync(() => {
dispatch(updateUrlState(urlState));
// finalizeProfileView is intentionally not awaited: it kicks off
// long-running async work like symbolication that should run in the
// background without blocking this action. The .catch() ensures that any
// synchronous errors thrown before the first await (e.g. from selectors)
// are still routed to the FATAL_ERROR state rather than escaping as
// unhandled promise rejections.
dispatch(finalizeProfileView(browserConnection)).catch((error) => {
dispatch(fatalError(error));
});
dispatch(urlSetupDone());
});
};
}
/**
* This function is called when a browser navigation event happens. A new UrlState
* is generated when the window.location is serialized, or the state is pulled out of
* the history API.
*/
export function updateUrlState(newUrlState: UrlState | null): Action {
return { type: 'UPDATE_URL_STATE', newUrlState };
}
export function reportTrackThreadHeight(
threadsKey: ThreadsKey,
height: CssPixels
): ThunkAction<void> {
return (dispatch, getState) => {
const trackThreadHeights = getTrackThreadHeights(getState());
const previousHeight = trackThreadHeights[threadsKey];
if (previousHeight !== height) {
// Guard against unnecessary dispatches. This could happen frequently.
dispatch({
type: 'UPDATE_TRACK_THREAD_HEIGHT',
height,
threadsKey,
});
}
};
}
/**
* This action dismisses the newly published state. This happens when a user first
* uploads a profile. We only want to remember this when we fist open the profile.
*/
export function dismissNewlyPublished(): Action {
return { type: 'DISMISS_NEWLY_PUBLISHED' as const };
}
/**
* Called when a user has started dragging a file. Used for loading
* profiles with the drag and drop component.
*/
export function startDragging(): Action {
return { type: 'START_DRAGGING' as const };
}
/**
* Called when a user has stopped dragging a file.
*/
export function stopDragging(): Action {
return { type: 'STOP_DRAGGING' as const };
}
/**
* Called when a custom drag and drop overlay is mounted. This lets
* the app know that we shouldn't create a default overlay.
*/
export function registerDragAndDropOverlay(): Action {
return { type: 'REGISTER_DRAG_AND_DROP_OVERLAY' as const };
}
/**
* Called when a custom drag and drop overlay is unmounted.
*/
export function unregisterDragAndDropOverlay(): Action {
return { type: 'UNREGISTER_DRAG_AND_DROP_OVERLAY' as const };
}
/*
* This action enables the event delay tracks. They are hidden by default because
* they are usually for power users and not so meaningful for average users.
* There is no UI that triggers this action in the profiler interface. Instead,
* users have to enable this from the developer console by writing this line:
* `experimental.enableEventDelayTracks()`
*/
export function enableEventDelayTracks(): ThunkAction<boolean> {
return (dispatch, getState) => {
if (getIsEventDelayTracksEnabled(getState())) {
console.error(
'Tried to enable the event delay tracks, but they are already enabled.'
);
return false;
}
if (
selectedThreadSelectors.getSamplesTable(getState()).eventDelay ===
undefined
) {
// Return early if the profile doesn't have eventDelay values.
console.error(oneLine`
Tried to enable the event delay tracks, but this profile does
not have eventDelay values. It is likely an older profile.
`);
return false;
}
const oldLocalTracks = getLocalTracksByPid(getState());
const localTracksByPid = addEventDelayTracksForThreads(
getThreads(getState()),
oldLocalTracks
);
const localTrackOrderByPid = initializeLocalTrackOrderByPid(
getLocalTrackOrderByPid(getState()),
localTracksByPid,
null,
getProfile(getState())
);
dispatch({
type: 'ENABLE_EVENT_DELAY_TRACKS',
localTracksByPid,
localTrackOrderByPid,
});
return true;
};
}
/*
* This action enables the CPU graph tracks. They are hidden by default because
* they are usually for power users and not so meaningful for average users.
* There is no UI that triggers this action in the profiler interface. Instead,
* users have to enable this from the developer console by writing this line:
* `experimental.enableCPUGraphs()`
*/
export function enableExperimentalCPUGraphs(): ThunkAction<boolean> {
return (dispatch, getState) => {
if (getIsExperimentalCPUGraphsEnabled(getState())) {
console.error(
'Tried to enable the CPU graph tracks, but they are already enabled.'
);
return false;
}
if (!getIsCPUUtilizationProvided(getState())) {
// Return early if the profile doesn't have threadCPUDelta values.
console.error(oneLine`
Tried to enable the CPU graph tracks, but this profile does
not have threadCPUDelta values. It is likely an older profile.
`);
return false;
}
dispatch({
type: 'ENABLE_EXPERIMENTAL_CPU_GRAPHS',
});
return true;
};
}
/*
* This action enables the process CPU tracks. They are hidden by default because
* the front-end work is not done for this data yet.
* There is no UI that triggers this action in the profiler interface. Instead,
* users have to enable this from the developer console by writing this line:
* `experimental.enableExperimentalProcessCPUTracks()`
*/
export function enableExperimentalProcessCPUTracks(): ThunkAction<boolean> {
return (dispatch, getState) => {
if (getIsExperimentalProcessCPUTracksEnabled(getState())) {
console.error(
'Tried to enable the process CPU tracks, but they are already enabled.'
);
return false;
}
if (
!getIsCPUUtilizationProvided(getState()) &&
getAreThereAnyProcessCPUCounters(getState())
) {
// Return early if the profile doesn't have threadCPUDelta values or
// doesn't have any experimental process CPU counters.
console.error(oneLine`
Tried to enable the process CPU tracks, but this profile does
not have threadCPUDelta values or process CPU threads.
`);
return false;
}
const oldLocalTracks = getLocalTracksByPid(getState());
const localTracksByPid = addProcessCPUTracksForProcess(
getCounters(getState()),
oldLocalTracks
);
const localTrackOrderByPid = initializeLocalTrackOrderByPid(
getLocalTrackOrderByPid(getState()),
localTracksByPid,
null,
getProfile(getState())
);
dispatch({
type: 'ENABLE_EXPERIMENTAL_PROCESS_CPU_TRACKS',
localTracksByPid,
localTrackOrderByPid,
});
return true;
};
}
/**
* This caches the profile data in the local state for synchronous access.
*/
export function setCurrentProfileUploadedInformation(
uploadedProfileInformation: UploadedProfileInformation | null
): Action {
return {
type: 'SET_CURRENT_PROFILE_UPLOADED_INFORMATION',
uploadedProfileInformation,
};
}
export function profileRemotelyDeleted(): Action {
// Ideally we should store the current profile data in a local indexeddb, and
// set the URL to /local/<indexeddb-key>.
return { type: 'PROFILE_REMOTELY_DELETED' as const };
}
export function updateBrowserConnectionStatus(
browserConnectionStatus: BrowserConnectionStatus
): Action {
return {
type: 'UPDATE_BROWSER_CONNECTION_STATUS',
browserConnectionStatus,
};
}
export function toggleOpenCategoryInSidebar(
kind: string,
category: IndexIntoCategoryList
): Action {
return {
type: 'TOGGLE_SIDEBAR_OPEN_CATEGORY',
kind,
category,
};
}