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
90 lines (73 loc) · 2.44 KB
/
loadSourceText.js
File metadata and controls
90 lines (73 loc) · 2.44 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
/* 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 { getSource, getGeneratedSource } from "../../selectors";
import * as parser from "../../workers/parser";
import { isLoaded } from "../../utils/source";
import defer from "../../utils/defer";
import type { ThunkArgs } from "../types";
import type { SourceRecord } from "../../types";
const requests = new Map();
import { Services } from "devtools-modules";
const loadSourceHistogram = Services.telemetry.getHistogramById(
"DEVTOOLS_DEBUGGER_LOAD_SOURCE_MS"
);
async function loadSource(source: SourceRecord, { sourceMaps, client }) {
const id = source.get("id");
if (isOriginalId(id)) {
return await sourceMaps.getOriginalSourceText(source.toJS());
}
const response = await client.sourceContents(id);
return {
id,
text: response.source,
contentType: response.contentType || "text/javascript"
};
}
/**
* @memberof actions/sources
* @static
*/
export function loadSourceText(source: SourceRecord) {
return async ({ dispatch, getState, client, sourceMaps }: ThunkArgs) => {
const id = source.get("id");
// Fetch the source text only once.
if (requests.has(id)) {
return requests.get(id);
}
if (isLoaded(source)) {
return Promise.resolve();
}
const telemetryStart = performance.now();
const deferred = defer();
requests.set(id, deferred.promise);
try {
await dispatch({
type: "LOAD_SOURCE_TEXT",
sourceId: id,
[PROMISE]: loadSource(source, { sourceMaps, client })
});
} catch (e) {
deferred.resolve();
requests.delete(id);
return;
}
const newSource = getSource(getState(), source.get("id")).toJS();
if (isOriginalId(newSource.id) && !newSource.isWasm) {
const generatedSource = getGeneratedSource(getState(), source.toJS());
await dispatch(loadSourceText(generatedSource));
}
if (!newSource.isWasm) {
await parser.setSource(newSource);
}
// signal that the action is finished
deferred.resolve();
requests.delete(id);
const telemetryEnd = performance.now();
const duration = telemetryEnd - telemetryStart;
loadSourceHistogram.add(duration);
};
}