forked from firefox-devtools/debugger
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbreakpoints.js
More file actions
227 lines (181 loc) · 5.77 KB
/
Copy pathbreakpoints.js
File metadata and controls
227 lines (181 loc) · 5.77 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
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
/* 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
/**
* Breakpoints reducer
* @module reducers/breakpoints
*/
import * as I from "immutable";
import makeRecord from "../utils/makeRecord";
import { isGeneratedId } from "devtools-source-map";
import { makeLocationId } from "../utils/breakpoint";
import type { Breakpoint, Location } from "../types";
import type { Action, DonePromiseAction } from "../actions/types";
import type { Record } from "../utils/makeRecord";
export type BreakpointsMap = I.Map<string, Breakpoint>;
export type BreakpointsState = {
breakpoints: BreakpointsMap
};
export function initialBreakpointsState(): Record<BreakpointsState> {
return makeRecord(
({
breakpoints: I.Map(),
breakpointsDisabled: false
}: BreakpointsState)
)();
}
function update(
state: Record<BreakpointsState> = initialBreakpointsState(),
action: Action
) {
switch (action.type) {
case "ADD_BREAKPOINT": {
return addBreakpoint(state, action);
}
case "SYNC_BREAKPOINT": {
return syncBreakpoint(state, action);
}
case "ENABLE_BREAKPOINT": {
return addBreakpoint(state, action);
}
case "DISABLE_BREAKPOINT": {
return updateBreakpoint(state, action);
}
case "DISABLE_ALL_BREAKPOINTS": {
return updateAllBreakpoints(state, action);
}
case "ENABLE_ALL_BREAKPOINTS": {
return updateAllBreakpoints(state, action);
}
case "SET_BREAKPOINT_CONDITION": {
return updateBreakpoint(state, action);
}
case "REMOVE_BREAKPOINT": {
return removeBreakpoint(state, action);
}
case "REMAP_BREAKPOINTS": {
return remapBreakpoints(state, action);
}
case "NAVIGATE": {
return initialBreakpointsState();
}
}
return state;
}
function addBreakpoint(state, action) {
if (action.status === "start" && action.breakpoint) {
const { breakpoint } = action;
const locationId = makeLocationId(breakpoint.location);
return state.setIn(["breakpoints", locationId], breakpoint);
}
// when the action completes, we can commit the breakpoint
if (action.status === "done") {
const { value } = ((action: any): DonePromiseAction);
return syncBreakpoint(state, value);
}
// Remove the optimistic update
if (action.status === "error" && action.breakpoint) {
const locationId = makeLocationId(action.breakpoint.location);
return state.deleteIn(["breakpoints", locationId]);
}
return state;
}
function syncBreakpoint(state, data) {
const { breakpoint, previousLocation } = data;
if (previousLocation) {
state = state.deleteIn(["breakpoints", makeLocationId(previousLocation)]);
}
if (!breakpoint) {
return state;
}
const locationId = makeLocationId(breakpoint.location);
return state.setIn(["breakpoints", locationId], breakpoint);
}
function updateBreakpoint(state, action) {
const { breakpoint } = action;
const locationId = makeLocationId(breakpoint.location);
return state.setIn(["breakpoints", locationId], breakpoint);
}
function updateAllBreakpoints(state, action) {
const { breakpoints } = action;
breakpoints.forEach(breakpoint => {
const locationId = makeLocationId(breakpoint.location);
state = state.setIn(["breakpoints", locationId], breakpoint);
});
return state;
}
function remapBreakpoints(state, action) {
const breakpoints = action.breakpoints.reduce(
(updatedBreakpoints, breakpoint) => {
const locationId = makeLocationId(breakpoint.location);
return { ...updatedBreakpoints, [locationId]: breakpoint };
},
{}
);
return state.set("breakpoints", I.Map(breakpoints));
}
function removeBreakpoint(state, action) {
const { breakpoint } = action;
const id = makeLocationId(breakpoint.location);
return state.deleteIn(["breakpoints", id]);
}
// Selectors
// TODO: these functions should be moved out of the reducer
type OuterState = { breakpoints: Record<BreakpointsState> };
export function getBreakpoints(state: OuterState) {
return state.breakpoints.breakpoints;
}
export function getBreakpoint(state: OuterState, location: Location) {
const breakpoints = getBreakpoints(state);
return breakpoints.get(makeLocationId(location));
}
export function getBreakpointsDisabled(state: OuterState): boolean {
return state.breakpoints.breakpoints.every(x => x.disabled);
}
export function getBreakpointsLoading(state: OuterState) {
const breakpoints = getBreakpoints(state);
const isLoading = !!breakpoints
.valueSeq()
.filter(bp => bp.loading)
.first();
return breakpoints.size > 0 && isLoading;
}
export function getBreakpointsForSource(state: OuterState, sourceId: string) {
if (!sourceId) {
return I.Map();
}
const isGeneratedSource = isGeneratedId(sourceId);
const breakpoints = getBreakpoints(state);
return breakpoints.filter(bp => {
const location = isGeneratedSource
? bp.generatedLocation || bp.location
: bp.location;
return location.sourceId === sourceId;
});
}
export function getBreakpointForLine(
state: OuterState,
sourceId: string,
line: number | null
): ?Breakpoint {
if (!sourceId) {
return I.Map();
}
const breakpoints = getBreakpointsForSource(state, sourceId);
return breakpoints.find(breakpoint => breakpoint.location.line === line);
}
export function getHiddenBreakpoint(state: OuterState) {
return getBreakpoints(state)
.valueSeq()
.filter(breakpoint => breakpoint.hidden)
.first();
}
export function getHiddenBreakpointLocation(state: OuterState) {
const hiddenBreakpoint = getHiddenBreakpoint(state);
if (!hiddenBreakpoint) {
return null;
}
return hiddenBreakpoint.location;
}
export default update;