Skip to content
This repository was archived by the owner on Jan 11, 2023. It is now read-only.

Commit 051f4ad

Browse files
committed
Fix permanent loading state (#5645)
1 parent e217d6a commit 051f4ad

File tree

4 files changed

+49
-17
lines changed

4 files changed

+49
-17
lines changed

src/actions/sources/loadSourceText.js

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { isOriginalId } from "devtools-source-map";
88
import { PROMISE } from "../utils/middleware/promise";
99
import { getSource, getGeneratedSource } from "../../selectors";
1010
import * as parser from "../../workers/parser";
11-
import { isLoading, isLoaded } from "../../utils/source";
11+
import { isLoaded } from "../../utils/source";
1212

1313
import defer from "../../utils/defer";
1414
import type { ThunkArgs } from "../types";
@@ -41,20 +41,21 @@ async function loadSource(source: SourceRecord, { sourceMaps, client }) {
4141
*/
4242
export function loadSourceText(source: SourceRecord) {
4343
return async ({ dispatch, getState, client, sourceMaps }: ThunkArgs) => {
44-
const telemetryStart = performance.now();
45-
const deferred = defer();
44+
const id = source.get("id");
4645

4746
// Fetch the source text only once.
48-
if (isLoaded(source)) {
49-
return Promise.resolve(source);
47+
if (requests.has(id)) {
48+
return requests.get(id);
5049
}
5150

52-
const id = source.get("id");
53-
if (isLoading(source) || requests.has(id)) {
54-
return requests.get(id);
51+
if (isLoaded(source)) {
52+
return Promise.resolve();
5553
}
5654

55+
const telemetryStart = performance.now();
56+
const deferred = defer();
5757
requests.set(id, deferred.promise);
58+
5859
try {
5960
await dispatch({
6061
type: "LOAD_SOURCE_TEXT",

src/actions/sources/newSources.js

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import {
2222
getPendingBreakpointsForSource
2323
} from "../../selectors";
2424

25-
import type { Source } from "../../types";
25+
import type { Source, SourceId } from "../../types";
2626
import type { ThunkArgs } from "../types";
2727

2828
function createOriginalSource(
@@ -44,15 +44,20 @@ function createOriginalSource(
4444
* @memberof actions/sources
4545
* @static
4646
*/
47-
export function loadSourceMap(generatedSource: Source) {
47+
function loadSourceMap(generatedSourceId: SourceId) {
4848
return async function({ dispatch, getState, sourceMaps }: ThunkArgs) {
49-
let urls;
49+
const generatedSource = getSource(getState(), generatedSourceId).toJS();
50+
if (!generatedSource.sourceMapURL) {
51+
return;
52+
}
53+
54+
let urls = null;
5055
try {
5156
urls = await sourceMaps.getOriginalURLs(generatedSource);
5257
} catch (e) {
5358
console.error(e);
54-
urls = null;
5559
}
60+
5661
if (!urls) {
5762
// If this source doesn't have a sourcemap, enable it for pretty printing
5863
dispatch({
@@ -66,10 +71,6 @@ export function loadSourceMap(generatedSource: Source) {
6671
createOriginalSource(url, generatedSource, sourceMaps)
6772
);
6873

69-
// TODO: check if this line is really needed, it introduces
70-
// a lot of lag to the application.
71-
const generatedSourceRecord = getSource(getState(), generatedSource.id);
72-
await dispatch(loadSourceText(generatedSourceRecord));
7374
dispatch(newSources(originalSources));
7475
};
7576
}
@@ -171,7 +172,7 @@ export function newSources(sources: Source[]) {
171172
}
172173

173174
await Promise.all(
174-
filteredSources.map(source => dispatch(loadSourceMap(source)))
175+
filteredSources.map(source => dispatch(loadSourceMap(source.id)))
175176
);
176177
// We would like to restore the blackboxed state
177178
// after loading all states to make sure the correctness.

src/actions/sources/tests/newSources.spec.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,4 +40,31 @@ describe("sources - new sources", () => {
4040
await dispatch(actions.newSource(baseSource));
4141
expect(getSelectedSource(getState()).get("url")).toBe(baseSource.url);
4242
});
43+
44+
it("should add original sources", async () => {
45+
const { dispatch, getState } = createStore(
46+
threadClient,
47+
{},
48+
{
49+
getOriginalURLs: () => Promise.resolve(["magic.js"]),
50+
generatedToOriginalId: (a, b) => `${a}/${b}`
51+
}
52+
);
53+
54+
await dispatch(
55+
actions.newSource(makeSource("base.js", { sourceMapURL: "base.js.map" }))
56+
);
57+
58+
const magic = getSource(getState(), "base.js/magic.js");
59+
expect(magic.get("url")).toEqual("magic.js");
60+
});
61+
62+
// eslint-disable-next-line
63+
it("should no attempt to fetch original sources if it's missing a source map url", async () => {
64+
const getOriginalURLs = jest.fn();
65+
const { dispatch } = createStore(threadClient, {}, { getOriginalURLs });
66+
67+
await dispatch(actions.newSource(makeSource("base.js")));
68+
expect(getOriginalURLs).not.toHaveBeenCalled();
69+
});
4370
});

src/reducers/sources.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,7 @@ function getTextPropsFromAction(action: any) {
170170
} else if (action.status === "error") {
171171
return { id: sourceId, error: action.error, loadedState: "loaded" };
172172
}
173+
173174
return {
174175
text: value.text,
175176
id: sourceId,
@@ -191,12 +192,14 @@ function updateSource(state: Record<SourcesState>, source: Source | Object) {
191192
if (!source.id) {
192193
return state;
193194
}
195+
194196
const existingSource = state.getIn(["sources", source.id]);
195197

196198
if (existingSource) {
197199
const updatedSource = existingSource.merge(source);
198200
return state.setIn(["sources", source.id], updatedSource);
199201
}
202+
200203
return state.setIn(["sources", source.id], new SourceRecordClass(source));
201204
}
202205

0 commit comments

Comments
 (0)