@samitouri / QOS-React-2 / commits / 6fd2e72b5e

Minor nits

Brian Vaughn committed May 14, 2019 at 11:01 UTC 6fd2e72b5e06358ebe6ab719edbbde2a1845ce4a
5 files changed +87 -110
src/__tests__/setupTests.js
+22 -32
@@ -31,43 +31,33 @@ env.beforeEach(() => {
31
32 installHook(global);
33
34 - function init() {
35 - const bridgeListeners = [];
36 - const bridge = new Bridge({
37 - listen(callback) {
38 - bridgeListeners.push(callback);
39 - return () => {
40 - const index = bridgeListeners.indexOf(callback);
41 - if (index >= 0) {
42 - bridgeListeners.splice(index, 1);
43 - }
44 - };
45 - },
46 - send(event: string, payload: any, transferable?: Array<any>) {
47 - bridgeListeners.forEach(callback => callback({ event, payload }));
48 - },
49 - });
34 + const bridgeListeners = [];
35 + const bridge = new Bridge({
36 + listen(callback) {
37 + bridgeListeners.push(callback);
38 + return () => {
39 + const index = bridgeListeners.indexOf(callback);
40 + if (index >= 0) {
41 + bridgeListeners.splice(index, 1);
42 + }
43 + };
44 + },
45 + send(event: string, payload: any, transferable?: Array<any>) {
46 + bridgeListeners.forEach(callback => callback({ event, payload }));
47 + },
48 + });
49
51 - const agent = new Agent(bridge);
50 + const agent = new Agent(bridge);
51
53 - const hook = global.__REACT_DEVTOOLS_GLOBAL_HOOK__;
52 + const hook = global.__REACT_DEVTOOLS_GLOBAL_HOOK__;
53
55 - const destroyBackend = initBackend(hook, agent, global);
54 + initBackend(hook, agent, global);
55
57 - const store = new Store(bridge);
56 + const store = new Store(bridge);
57
59 - global.agent = agent;
60 - global.bridge = bridge;
61 - global.store = store;
62 -
63 - // Reinit may be used to reset the store and the bridge during a test.
64 - global.reinit = () => {
65 - destroyBackend();
66 - init();
67 - };
68 - }
69 -
70 - init();
58 + global.agent = agent;
59 + global.bridge = bridge;
60 + global.store = store;
61 });
62 env.afterEach(() => {
63 delete global.__REACT_DEVTOOLS_GLOBAL_HOOK__;
src/__tests__/utils.js
+3 -3
@@ -139,7 +139,7 @@ export function exportImportHelper(
139 rendererID: number,
140 rootID: number
141 ): void {
142 - const utils = require('./utils');
142 + const { act } = require('./utils');
143 const {
144 prepareExportedProfilingSummary,
145 prepareImportedProfilingData,
@@ -153,7 +153,7 @@ export function exportImportHelper(
153 };
154 bridge.addListener('exportFile', onExportFile);
155
156 - utils.act(() => {
156 + act(() => {
157 const exportProfilingSummary = prepareExportedProfilingSummary(
158 store.profilingOperations,
159 store.profilingSnapshots,
@@ -183,7 +183,7 @@ export function exportImportHelper(
183 // Snapshot the JSON-parsed object, rather than the raw string, because Jest formats the diff nicer.
184 expect(importedProfilingData).toMatchSnapshot('imported data');
185
186 - utils.act(() => {
186 + act(() => {
187 store.importedProfilingData = importedProfilingData;
188 });
189 }
src/backend/agent.js
+2 -2
@@ -175,8 +175,8 @@ export default class Agent extends EventEmitter {
175 contents: JSON.stringify(exportedProfilingData, null, 2),
176 filename: 'profile-data.json',
177 });
178 - } catch (ex) {
179 - console.warn(`Unable to export file: ${ex.stack}`);
178 + } catch (error) {
179 + console.warn(`Unable to export file: ${error.stack}`);
180 }
181 };
182
src/backend/renderer.js
+20 -25
@@ -784,14 +784,14 @@ export function attach(
784
785 function recordMount(fiber: Fiber, parentFiber: Fiber | null) {
786 const isRoot = fiber.tag === HostRoot;
787 - const fiberID = getFiberID(getPrimaryFiber(fiber));
787 + const id = getFiberID(getPrimaryFiber(fiber));
788
789 const hasOwnerMetadata = fiber.hasOwnProperty('_debugOwner');
790 const isProfilingSupported = fiber.hasOwnProperty('treeBaseDuration');
791
792 if (isRoot) {
793 pushOperation(TREE_OPERATION_ADD);
794 - pushOperation(fiberID);
794 + pushOperation(id);
795 pushOperation(ElementTypeRoot);
796 pushOperation(isProfilingSupported ? 1 : 0);
797 pushOperation(hasOwnerMetadata ? 1 : 0);
@@ -810,7 +810,7 @@ export function attach(
810 let displayNameStringID = getStringID(displayName);
811 let keyStringID = getStringID(key);
812 pushOperation(TREE_OPERATION_ADD);
813 - pushOperation(fiberID);
813 + pushOperation(id);
814 pushOperation(elementType);
815 pushOperation(parentID);
816 pushOperation(ownerID);
@@ -819,7 +819,7 @@ export function attach(
819 }
820
821 if (isProfilingSupported) {
822 - idToRootMap.set(fiberID, currentRootID);
822 + idToRootMap.set(id, currentRootID);
823
824 recordProfilingDurations(fiber);
825 }
@@ -972,10 +972,10 @@ export function attach(
972 }
973
974 function recordProfilingDurations(fiber: Fiber) {
975 - const fiberID = getFiberID(getPrimaryFiber(fiber));
975 + const id = getFiberID(getPrimaryFiber(fiber));
976 const { actualDuration, treeBaseDuration } = fiber;
977
978 - idToTreeBaseDurationMap.set(fiberID, fiber.treeBaseDuration || 0);
978 + idToTreeBaseDurationMap.set(id, fiber.treeBaseDuration || 0);
979
980 if (isProfiling) {
981 const { alternate } = fiber;
@@ -990,7 +990,7 @@ export function attach(
990 (fiber.treeBaseDuration || 0) * 1000
991 );
992 pushOperation(TREE_OPERATION_UPDATE_TREE_BASE_DURATION);
993 - pushOperation(fiberID);
993 + pushOperation(id);
994 pushOperation(treeBaseDuration);
995 }
996
@@ -1014,7 +1014,7 @@ export function attach(
1014 // In some cases actualDuration might be 0 for fibers we worked on (particularly if we're using Date.now)
1015 // In other cases (e.g. Memo) actualDuration might be greater than 0 even if we "bailed out".
1016 const metadata = ((currentCommitProfilingMetadata: any): CommitProfilingData);
1017 - metadata.durations.push(fiberID, actualDuration, selfDuration);
1017 + metadata.durations.push(id, actualDuration, selfDuration);
1018 metadata.maxActualDuration = Math.max(
1019 metadata.maxActualDuration,
1020 actualDuration
@@ -2073,9 +2073,9 @@ export function attach(
2073 };
2074 }
2075
2076 - function getCommitDetailsForEachCommit(
2076 + function getExportedProfilingData(
2077 rootID: number
2078 - ): Array<CommitDetailsBackend> {
2078 + ): ExportedProfilingDataFromRenderer {
2079 const commitDetailsForEachCommit = [];
2080 const commitProfilingMetadata = ((rootToCommitProfilingMetadataMap: any): CommitProfilingMetadataMap).get(
2081 rootID
@@ -2085,16 +2085,11 @@ export function attach(
2085 commitDetailsForEachCommit.push(getCommitDetails(rootID, index));
2086 }
2087 }
2088 - return commitDetailsForEachCommit;
2089 - }
2088
2091 - function getExportedProfilingData(
2092 - rootID: number
2093 - ): ExportedProfilingDataFromRenderer {
2089 return {
2090 version: PROFILER_EXPORT_VERSION,
2091 profilingSummary: getProfilingSummary(rootID),
2097 - commitDetails: getCommitDetailsForEachCommit(rootID),
2092 + commitDetails: commitDetailsForEachCommit,
2093 interactions: getInteractions(rootID),
2094 };
2095 }
@@ -2119,14 +2114,14 @@ export function attach(
2114
2115 const initialTreeBaseDurations = [];
2116 if (initialTreeBaseDurationsMap != null) {
2122 - initialTreeBaseDurationsMap.forEach((treeBaseDuration, fiberID) => {
2117 + initialTreeBaseDurationsMap.forEach((treeBaseDuration, id) => {
2118 if (
2119 initialIDToRootMap != null &&
2125 - initialIDToRootMap.get(fiberID) === rootID
2120 + initialIDToRootMap.get(id) === rootID
2121 ) {
2122 // We don't need to convert milliseconds to microseconds in this case,
2123 // because the profiling summary is JSON serialized.
2129 - initialTreeBaseDurations.push(fiberID, treeBaseDuration);
2124 + initialTreeBaseDurations.push(id, treeBaseDuration);
2125 }
2126 });
2127 }
@@ -2175,11 +2170,11 @@ export function attach(
2170
2171 let forceFallbackForSuspenseIDs = new Set();
2172 function shouldSuspendFiberAccordingToSet(fiber) {
2178 - const fiberID = getFiberID(getPrimaryFiber(((fiber: any): Fiber)));
2179 - return forceFallbackForSuspenseIDs.has(fiberID);
2173 + const id = getFiberID(getPrimaryFiber(((fiber: any): Fiber)));
2174 + return forceFallbackForSuspenseIDs.has(id);
2175 }
2176
2182 - function overrideSuspense(fiberID, forceFallback) {
2177 + function overrideSuspense(id, forceFallback) {
2178 if (
2179 typeof setSuspenseHandler !== 'function' ||
2180 typeof scheduleUpdate !== 'function'
@@ -2189,19 +2184,19 @@ export function attach(
2184 );
2185 }
2186 if (forceFallback) {
2192 - forceFallbackForSuspenseIDs.add(fiberID);
2187 + forceFallbackForSuspenseIDs.add(id);
2188 if (forceFallbackForSuspenseIDs.size === 1) {
2189 // First override is added. Switch React to slower path.
2190 setSuspenseHandler(shouldSuspendFiberAccordingToSet);
2191 }
2192 } else {
2198 - forceFallbackForSuspenseIDs.delete(fiberID);
2193 + forceFallbackForSuspenseIDs.delete(id);
2194 if (forceFallbackForSuspenseIDs.size === 0) {
2195 // Last override is gone. Switch React back to fast path.
2196 setSuspenseHandler(shouldSuspendFiberAlwaysFalse);
2197 }
2198 }
2204 - const fiber = idToFiberMap.get(fiberID);
2199 + const fiber = idToFiberMap.get(id);
2200 scheduleUpdate(fiber);
2201 }
2202
src/devtools/ProfilingCache.js
+40 -48
@@ -123,8 +123,9 @@ export default class ProfilingCache {
123 return;
124 }
125
126 - // If no profiling data was recorded for this root, skip the round trip.
126 this._pendingCommitDetailsMap.delete(pendingKey);
127 +
128 + // If no profiling data was recorded for this root, skip the round trip.
129 resolve({
130 rootID,
131 commitIndex,
@@ -156,13 +157,12 @@ export default class ProfilingCache {
157 commitDurations.push(commitIndex, selfDuration);
158 }
159 });
159 - const fiberCommitsFrontend: FiberCommitsFrontend = {
160 + this._pendingFiberCommitsMap.delete(pendingKey);
161 + resolve({
162 commitDurations,
163 fiberID,
164 rootID,
163 - };
164 - this._pendingFiberCommitsMap.delete(pendingKey);
165 - resolve(fiberCommitsFrontend);
165 + });
166 return;
167 } else if (this._store.profilingOperations.has(rootID)) {
168 this._pendingFiberCommitsMap.set(pendingKey, resolve);
@@ -174,14 +174,14 @@ export default class ProfilingCache {
174 return;
175 }
176
177 - // If no profiling data was recorded for this root, skip the round trip.
177 this._pendingFiberCommitsMap.delete(pendingKey);
179 - const fiberCommitsFrontend: FiberCommitsFrontend = {
178 +
179 + // If no profiling data was recorded for this root, skip the round trip.
180 + resolve({
181 commitDurations: [],
182 fiberID,
183 rootID,
183 - };
184 - resolve(fiberCommitsFrontend);
184 + });
185 });
186 },
187 ({ fiberID, rendererID, rootID }: FiberCommitsParams) =>
@@ -214,13 +214,13 @@ export default class ProfilingCache {
214 return;
215 }
216
217 - // If no profiling data was recorded for this root, skip the round trip.
217 this._pendingInteractionsMap.delete(pendingKey);
219 - const interactionsFrontend: InteractionsFrontend = {
218 +
219 + // If no profiling data was recorded for this root, skip the round trip.
220 + resolve({
221 interactions: [],
222 rootID,
222 - };
223 - resolve(interactionsFrontend);
223 + });
224 });
225 },
226 ({ rendererID, rootID }: ProfilingSummaryParams) => rootID
@@ -249,16 +249,16 @@ export default class ProfilingCache {
249 return;
250 }
251
252 - // If no profiling data was recorded for this root, skip the round trip.
252 this._pendingProfileSummaryMap.delete(pendingKey);
254 - const profilingSummaryFrontend: ProfilingSummaryFrontend = {
253 +
254 + // If no profiling data was recorded for this root, skip the round trip.
255 + resolve({
256 rootID,
257 commitDurations: [],
258 commitTimes: [],
259 initialTreeBaseDurations: new Map(),
260 interactionCount: 0,
260 - };
261 - resolve(profilingSummaryFrontend);
261 + });
262 });
263 },
264 ({ rendererID, rootID }: ProfilingSummaryParams) => rootID
@@ -361,15 +361,13 @@ export default class ProfilingCache {
361 selfDurationsMap.set(fiberID, durations[i + 2]);
362 }
363
364 - resolve(
365 - ({
366 - actualDurations: actualDurationsMap,
367 - commitIndex,
368 - interactions,
369 - rootID,
370 - selfDurations: selfDurationsMap,
371 - }: CommitDetailsFrontend)
372 - );
364 + resolve({
365 + actualDurations: actualDurationsMap,
366 + commitIndex,
367 + interactions,
368 + rootID,
369 + selfDurations: selfDurationsMap,
370 + });
371 }
372 };
373
@@ -383,13 +381,11 @@ export default class ProfilingCache {
381 if (resolve != null) {
382 this._pendingFiberCommitsMap.delete(key);
383
386 - resolve(
387 - ({
388 - commitDurations,
389 - fiberID,
390 - rootID,
391 - }: FiberCommitsFrontend)
392 - );
384 + resolve({
385 + commitDurations,
386 + fiberID,
387 + rootID,
388 + });
389 }
390 };
391
@@ -398,12 +394,10 @@ export default class ProfilingCache {
394 if (resolve != null) {
395 this._pendingInteractionsMap.delete(rootID);
396
401 - resolve(
402 - ({
403 - interactions,
404 - rootID,
405 - }: InteractionsFrontend)
406 - );
397 + resolve({
398 + interactions,
399 + rootID,
400 + });
401 }
402 };
403
@@ -425,15 +419,13 @@ export default class ProfilingCache {
419 initialTreeBaseDurationsMap.set(fiberID, initialTreeBaseDuration);
420 }
421
428 - resolve(
429 - ({
430 - commitDurations,
431 - commitTimes,
432 - initialTreeBaseDurations: initialTreeBaseDurationsMap,
433 - interactionCount,
434 - rootID,
435 - }: ProfilingSummaryFrontend)
436 - );
422 + resolve({
423 + commitDurations,
424 + commitTimes,
425 + initialTreeBaseDurations: initialTreeBaseDurationsMap,
426 + interactionCount,
427 + rootID,
428 + });
429 }
430 };
431 }