This repository has been archived by the owner on Jan 11, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 759
/
breakpoints.js
147 lines (129 loc) · 4.48 KB
/
breakpoints.js
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/* 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/. */
"use strict";
const promise = require("ff-devtools-libs/sham/promise");
const constants = require("../constants");
const { PROMISE } = require("ff-devtools-libs/client/shared/redux/middleware/promise");
const {
getBreakpoint, getBreakpoints
} = require("../selectors");
const { Task } = require("ff-devtools-libs/sham/task");
const fromJS = require("../util/fromJS");
function enableBreakpoint(location) {
// Enabling is exactly the same as adding. It will use the existing
// breakpoint that still stored.
return addBreakpoint(location);
}
function _breakpointExists(state, location) {
const currentBp = getBreakpoint(state, location);
return currentBp && !currentBp.get("disabled");
}
function _getOrCreateBreakpoint(state, location, condition) {
return getBreakpoint(state, location) || fromJS({ location, condition });
}
function addBreakpoint(location, { condition, getTextForLine }) {
return ({ dispatch, getState, client }) => {
if (_breakpointExists(getState(), location)) {
return promise.resolve();
}
const bp = _getOrCreateBreakpoint(getState(), location, condition);
return dispatch({
type: constants.ADD_BREAKPOINT,
breakpoint: bp.toJS(),
condition: condition,
[PROMISE]: Task.spawn(function* () {
const { id, actualLocation } = yield client.setBreakpoint(
bp.get("location").toJS(),
bp.get("condition")
);
return {
id: id,
actualLocation: actualLocation,
text: getTextForLine ? getTextForLine(actualLocation.line) : ""
};
})
});
};
}
function disableBreakpoint(location) {
return _removeOrDisableBreakpoint(location, true);
}
function removeBreakpoint(location) {
return _removeOrDisableBreakpoint(location);
}
function _removeOrDisableBreakpoint(location, isDisabled) {
return ({ dispatch, getState, client }) => {
let bp = getBreakpoint(getState(), location);
if (!bp) {
throw new Error("attempt to remove breakpoint that does not exist");
}
if (bp.get("loading")) {
// TODO(jwl): make this wait until the breakpoint is saved if it
// is still loading
throw new Error("attempt to remove unsaved breakpoint");
}
const action = {
type: constants.REMOVE_BREAKPOINT,
breakpoint: bp.toJS(),
disabled: isDisabled
};
// If the breakpoint is already disabled, we don't need to remove
// it from the server. We just need to dispatch an action
// simulating a successful server request to remove it, and it
// will be removed completely from the state.
if (!bp.disabled) {
return dispatch(Object.assign({}, action, {
[PROMISE]: client.removeBreakpoint(bp.get("id"))
}));
}
return dispatch(Object.assign({}, action, { status: "done" }));
};
}
function removeAllBreakpoints() {
return ({ dispatch, getState }) => {
const breakpoints = getBreakpoints(getState());
const activeBreakpoints = breakpoints.filter(bp => !bp.disabled);
activeBreakpoints.forEach(bp => removeBreakpoint(bp.location));
};
}
/**
* Update the condition of a breakpoint.
*
* @param object aLocation
* @see DebuggerController.Breakpoints.addBreakpoint
* @param string aClients
* The condition to set on the breakpoint
* @return object
* A promise that will be resolved with the breakpoint client
*/
function setBreakpointCondition(location, condition) {
throw new Error("not implemented");
// return ({ dispatch, getState, client }) => {
// const bp = getBreakpoint(getState(), location);
// if (!bp) {
// throw new Error("Breakpoint does not exist at the specified location");
// }
// if (bp.get("loading")) {
// // TODO(jwl): when this function is called, make sure the action
// // creator waits for the breakpoint to exist
// throw new Error("breakpoint must be saved");
// }
// return dispatch({
// type: constants.SET_BREAKPOINT_CONDITION,
// breakpoint: bp,
// condition: condition,
// [PROMISE]: Task.spawn(function* () {
// yield client.setBreakpointCondition(bp.get("id"), condition);
// })
// });
// };
}
module.exports = {
enableBreakpoint,
addBreakpoint,
disableBreakpoint,
removeBreakpoint,
removeAllBreakpoints,
setBreakpointCondition
};