Skip to content

Commit

Permalink
skip reading element for imported data
Browse files Browse the repository at this point in the history
  • Loading branch information
bl00mber committed May 13, 2020
1 parent 6514e4a commit a5e6d0d
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ describe('profiling utils', () => {

it('should throw if importing older/unsupported data', () => {
expect(() =>
utils.prepareProfilingDataFrontendFromExport(
utils.prepareProfilingDataFrontendFromImport(
({
version: 0,
dataForRoots: [],
Expand Down
10 changes: 5 additions & 5 deletions packages/react-devtools-shared/src/__tests__/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ export function requireTestRenderer(): ReactTestRenderer {
export function exportImportHelper(bridge: FrontendBridge, store: Store): void {
const {
prepareProfilingDataExport,
prepareProfilingDataFrontendFromExport,
prepareProfilingDataFrontendFromImport,
} = require('react-devtools-shared/src/devtools/views/Profiler/utils');

const {profilerStore} = store;
Expand All @@ -194,18 +194,18 @@ export function exportImportHelper(bridge: FrontendBridge, store: Store): void {
);
const parsedProfilingDataExport = JSON.parse(serializedProfilingDataExport);

const profilingDataFrontend = prepareProfilingDataFrontendFromExport(
const profilingDataFrontendImport = prepareProfilingDataFrontendFromImport(
(parsedProfilingDataExport: any),
);

// Sanity check that profiling snapshots are serialized correctly.
expect(profilingDataFrontendInitial).toEqual(profilingDataFrontend);
expect(profilingDataFrontendInitial.dataForRoots).toEqual(profilingDataFrontendImport.dataForRoots);

// Snapshot the JSON-parsed object, rather than the raw string, because Jest formats the diff nicer.
expect(parsedProfilingDataExport).toMatchSnapshot('imported data');

act(() => {
// Apply the new exported-then-reimported data so tests can re-run assertions.
profilerStore.profilingData = profilingDataFrontend;
// Apply the new exported-then-imported data so tests can re-run assertions.
profilerStore.profilingData = profilingDataFrontendImport;
});
}
5 changes: 5 additions & 0 deletions packages/react-devtools-shared/src/devtools/store.js
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,11 @@ export default class Store extends EventEmitter<{|
}

getElementByID(id: number): Element | null {
if (
this._profilerStore._dataFrontend &&
this._profilerStore._dataFrontend.imported === true
)
return null;
const element = this._idToElement.get(id);
if (element == null) {
console.warn(`No element found with id "${id}"`);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import ButtonIcon from '../ButtonIcon';
import {StoreContext} from '../context';
import {
prepareProfilingDataExport,
prepareProfilingDataFrontendFromExport,
prepareProfilingDataFrontendFromImport,
} from './utils';
import {downloadFile} from '../utils';

Expand Down Expand Up @@ -77,11 +77,11 @@ export default function ProfilingImportExportButtons() {
fileReader.addEventListener('load', () => {
try {
const raw = ((fileReader.result: any): string);
const profilingDataExport = ((JSON.parse(
const profilingDataImport = ((JSON.parse(
raw,
): any): ProfilingDataExport);
profilerStore.profilingData = prepareProfilingDataFrontendFromExport(
profilingDataExport,
profilerStore.profilingData = prepareProfilingDataFrontendFromImport(
profilingDataImport,
);
} catch (error) {
modalDialogDispatch({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ export type ProfilingDataForRootFrontend = {|
export type ProfilingDataFrontend = {|
// Profiling data per root.
dataForRoots: Map<number, ProfilingDataForRootFrontend>,
imported: boolean,
|};

export type CommitDataExport = {|
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,21 +99,21 @@ export function prepareProfilingDataFrontendFromBackendAndStore(
);
});

return {dataForRoots};
return {dataForRoots, imported: false};
}

// Converts a Profiling data export into the format required by the Store.
export function prepareProfilingDataFrontendFromExport(
profilingDataExport: ProfilingDataExport,
export function prepareProfilingDataFrontendFromImport(
profilingDataImport: ProfilingDataExport,
): ProfilingDataFrontend {
const {version} = profilingDataExport;
const {version} = profilingDataImport;

if (version !== PROFILER_EXPORT_VERSION) {
throw Error(`Unsupported profiler export version "${version}"`);
}

const dataForRoots: Map<number, ProfilingDataForRootFrontend> = new Map();
profilingDataExport.dataForRoots.forEach(
profilingDataImport.dataForRoots.forEach(
({
commitData,
displayName,
Expand Down Expand Up @@ -156,7 +156,7 @@ export function prepareProfilingDataFrontendFromExport(
},
);

return {dataForRoots};
return {dataForRoots, imported: true};
}

// Converts a Store Profiling data into a format that can be safely (JSON) serialized for export.
Expand Down

0 comments on commit a5e6d0d

Please sign in to comment.