forked from firefox-devtools/debugger
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathloadSourceText.js
More file actions
97 lines (78 loc) · 2.45 KB
/
Copy pathloadSourceText.js
File metadata and controls
97 lines (78 loc) · 2.45 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
/* 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 { isOriginalId } from "devtools-source-map";
import { PROMISE } from "../utils/middleware/promise";
import { getGeneratedSource, getSource } from "../../selectors";
import * as parser from "../../workers/parser";
import { isLoaded } from "../../utils/source";
import { Telemetry } from "devtools-modules";
import defer from "../../utils/defer";
import type { ThunkArgs } from "../types";
import type { Source } from "../../types";
const requests = new Map();
// Measures the time it takes for a source to load
const loadSourceHistogram = "DEVTOOLS_DEBUGGER_LOAD_SOURCE_MS";
const telemetry = new Telemetry();
async function loadSource(source: Source, { sourceMaps, client }) {
const id = source.id;
if (isOriginalId(id)) {
return sourceMaps.getOriginalSourceText(source);
}
const response = await client.sourceContents(id);
telemetry.finish(loadSourceHistogram, source);
return {
id,
text: response.source,
contentType: response.contentType || "text/javascript"
};
}
/**
* @memberof actions/sources
* @static
*/
export function loadSourceText(source: ?Source) {
return async ({ dispatch, getState, client, sourceMaps }: ThunkArgs) => {
if (!source) {
return;
}
const id = source.id;
// Fetch the source text only once.
if (requests.has(id)) {
return requests.get(id);
}
if (isLoaded(source)) {
return Promise.resolve();
}
const deferred = defer();
requests.set(id, deferred.promise);
telemetry.start(loadSourceHistogram, source);
try {
await dispatch({
type: "LOAD_SOURCE_TEXT",
sourceId: source.id,
[PROMISE]: loadSource(source, { sourceMaps, client })
});
} catch (e) {
deferred.resolve();
requests.delete(id);
return;
}
const newSource = getSource(getState(), source.id);
if (!newSource) {
return;
}
if (isOriginalId(newSource.id) && !newSource.isWasm) {
const generatedSource = getGeneratedSource(getState(), source);
await dispatch(loadSourceText(generatedSource));
}
if (!newSource.isWasm) {
await parser.setSource(newSource);
}
// signal that the action is finished
deferred.resolve();
requests.delete(id);
return source;
};
}