forked from firefox-devtools/profiler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
publish.js
262 lines (233 loc) · 7.92 KB
/
publish.js
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
/* 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/. */
// @flow
import { uploadBinaryProfileData } from '../profile-logic/profile-store';
import { sendAnalytics } from '../utils/analytics';
import { getProfile } from '../selectors/profile';
import {
getAbortFunction,
getUploadGeneration,
getSanitizedProfile,
getSanitizedProfileData,
getRemoveProfileInformation,
getOriginalProfile,
getOriginalUrlState,
} from '../selectors/publish';
import { getUrlState } from '../selectors/url-state';
import { viewProfile } from './receive-profile';
import { ensureExists } from '../utils/flow';
import { setHistoryReplaceState } from '../app-logic/url-handling';
import type { Action, ThunkAction } from '../types/store';
import type { CheckedSharingOptions } from '../types/actions';
import type { StartEndRange } from '../types/units';
import type { Profile, ThreadIndex } from '../types/profile';
import type { UrlState } from '../types/state';
export function toggleCheckedSharingOptions(
slug: $Keys<CheckedSharingOptions>
): Action {
return {
type: 'TOGGLE_CHECKED_SHARING_OPTION',
slug,
};
}
export function uploadCompressionStarted(): Action {
return {
type: 'UPLOAD_COMPRESSION_STARTED',
};
}
/**
* Start uploading the profile, but save an abort function to be able to cancel it.
*/
export function uploadStarted(abortFunction: () => void): Action {
return {
type: 'UPLOAD_STARTED',
abortFunction,
};
}
/**
* As the profile uploads, remember the amount that has been uploaded so that the UI
* can reflect the progress.
*/
export function updateUploadProgress(uploadProgress: number): Action {
return {
type: 'UPDATE_UPLOAD_PROGRESS',
uploadProgress,
};
}
/**
* A profile upload failed.
*/
export function uploadFailed(error: mixed): Action {
return { type: 'UPLOAD_FAILED', error };
}
/**
* This function starts the profile sharing process. Takes an optional argument that
* indicates if the share attempt is being made for the second time. We have two share
* buttons, one for sharing for the first time, and one for sharing after the initial
* share depending on the previous URL share status. People can decide to remove the
* URLs from the profile after sharing with URLs or they can decide to add the URLs after
* sharing without them. We check the current state before attempting to share depending
* on that flag.
*
* The return value is used for tests to determine if the request went all the way
* through (true) or was quit early due to the generation value being invalidated (false).
*/
export function attemptToPublish(): ThunkAction<Promise<boolean>> {
return async (dispatch, getState) => {
try {
sendAnalytics({
hitType: 'event',
eventCategory: 'profile upload',
eventAction: 'start',
});
// Get the current generation of this request. It can be aborted midway through.
// This way we can check inside this async function if we need to bail out early.
const uploadGeneration = getUploadGeneration(getState());
dispatch(uploadCompressionStarted());
const gzipData: Uint8Array = await getSanitizedProfileData(getState());
// The previous line was async, check to make sure that this request is still valid.
if (uploadGeneration !== getUploadGeneration(getState())) {
return false;
}
const { abortFunction, startUpload } = uploadBinaryProfileData();
dispatch(uploadStarted(abortFunction));
if (uploadGeneration !== getUploadGeneration(getState())) {
// The upload could have been aborted while we were compressing the data.
return false;
}
// Upload the profile, and notify it with the amount of data that has been
// uploaded.
const hash = await startUpload(gzipData, uploadProgress => {
dispatch(updateUploadProgress(uploadProgress));
});
// The previous line was async, check to make sure that this request is still valid.
if (uploadGeneration !== getUploadGeneration(getState())) {
return false;
}
const removeProfileInformation = getRemoveProfileInformation(getState());
if (removeProfileInformation) {
const {
committedRanges,
oldThreadIndexToNew,
profile,
} = getSanitizedProfile(getState());
const originalProfile = getProfile(getState());
const originalUrlState = getUrlState(getState());
// Hide the old UI gracefully.
await dispatch(hideStaleProfile());
// Update the UrlState so that we are sanitized.
dispatch(
profileSanitized(
hash,
committedRanges,
oldThreadIndexToNew,
originalProfile,
originalUrlState
)
);
// Swap out the URL state, since the view profile calculates all of the default
// settings. If we don't do this then we can go back in history to where we
// are trying to view a profile without valid view settings.
setHistoryReplaceState(true);
// Multiple dispatches are usually to be avoided, but viewProfile requires
// the next UrlState in place. It could be rewritten to have a UrlState passed
// in as a paremeter, but that doesn't seem worth it at the time of this writing.
dispatch(viewProfile(profile));
setHistoryReplaceState(false);
} else {
dispatch(profilePublished(hash));
}
sendAnalytics({
hitType: 'event',
eventCategory: 'profile upload',
eventAction: 'succeeded',
});
} catch (error) {
dispatch(uploadFailed(error));
sendAnalytics({
hitType: 'event',
eventCategory: 'profile upload',
eventAction: 'failed',
});
return false;
}
return true;
};
}
/**
* Abort the attempt to publish.
*/
export function abortUpload(): ThunkAction<Promise<void>> {
return async (dispatch, getState) => {
const abort = getAbortFunction(getState());
abort();
dispatch({ type: 'UPLOAD_ABORTED' });
sendAnalytics({
hitType: 'event',
eventCategory: 'profile upload',
eventAction: 'aborted',
});
};
}
export function resetUploadState(): Action {
return {
type: 'UPLOAD_RESET',
};
}
/**
* Report to the UrlState that the profile was sanitized. This will re-map any stored
* indexes or information that has been sanitized away.
*/
export function profileSanitized(
hash: string,
committedRanges: StartEndRange[] | null,
oldThreadIndexToNew: Map<ThreadIndex, ThreadIndex> | null,
originalProfile: Profile,
originalUrlState: UrlState
): Action {
return {
type: 'SANITIZED_PROFILE_PUBLISHED',
hash,
committedRanges,
oldThreadIndexToNew,
originalProfile,
originalUrlState,
};
}
/**
* Report that the profile was published, but not sanitized.
*/
export function profilePublished(hash: string): Action {
return {
type: 'PROFILE_PUBLISHED',
hash,
};
}
export function revertToOriginalProfile(): ThunkAction<Promise<void>> {
return async (dispatch, getState) => {
const originalProfile = ensureExists(
getOriginalProfile(getState()),
'Expected to find an original profile when reverting to it.'
);
const originalUrlState = ensureExists(
getOriginalUrlState(getState()),
'Expected to find the original url state to revert to.'
);
await dispatch(hideStaleProfile());
dispatch({
type: 'REVERT_TO_ORIGINAL_PROFILE',
originalUrlState: originalUrlState,
});
dispatch(viewProfile(originalProfile));
};
}
export function hideStaleProfile(): ThunkAction<Promise<void>> {
return dispatch => {
dispatch({ type: 'HIDE_STALE_PROFILE' });
return new Promise(resolve => {
// This timing should match .profileViewerFadeOut.
setTimeout(resolve, 300);
});
};
}