summaryrefslogtreecommitdiffstats
path: root/devtools/client/debugger/src/reducers/pause.js
blob: 8cb5eefe98bbe32f4bff2bc7521f53247fa3590d (plain)
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
/* 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/>. */

/* eslint complexity: ["error", 36]*/

/**
 * Pause reducer
 * @module reducers/pause
 */

import { prefs } from "../utils/prefs";

// Pause state associated with an individual thread.

// Pause state describing all threads.

export function initialPauseState(thread = "UnknownThread") {
  return {
    cx: {
      navigateCounter: 0,
    },
    // This `threadcx` is the `cx` variable we pass around in components and actions.
    // This is pulled via getThreadContext().
    // This stores information about the currently selected thread and its paused state.
    threadcx: {
      navigateCounter: 0,
      thread,
      pauseCounter: 0,
    },
    threads: {},
    skipPausing: prefs.skipPausing,
    mapScopes: prefs.mapScopes,
    shouldPauseOnDebuggerStatement: prefs.pauseOnDebuggerStatement,
    shouldPauseOnExceptions: prefs.pauseOnExceptions,
    shouldPauseOnCaughtExceptions: prefs.pauseOnCaughtExceptions,
  };
}

const resumedPauseState = {
  isPaused: false,
  frames: null,
  framesLoading: false,
  frameScopes: {
    generated: {},
    original: {},
    mappings: {},
  },
  selectedFrameId: null,
  why: null,
  inlinePreview: {},
};

const createInitialPauseState = () => ({
  ...resumedPauseState,
  isWaitingOnBreak: false,
  command: null,
  previousLocation: null,
  expandedScopes: new Set(),
  lastExpandedScopes: [],
});

export function getThreadPauseState(state, thread) {
  // Thread state is lazily initialized so that we don't have to keep track of
  // the current set of worker threads.
  return state.threads[thread] || createInitialPauseState();
}

function update(state = initialPauseState(), action) {
  // All the actions updating pause state must pass an object which designate
  // the related thread.
  const getActionThread = () => {
    const thread =
      action.thread || action.selectedFrame?.thread || action.frame?.thread;
    if (!thread) {
      throw new Error(`Missing thread in action ${action.type}`);
    }
    return thread;
  };

  // `threadState` and `updateThreadState` help easily get and update
  // the pause state for a given thread.
  const threadState = () => {
    return getThreadPauseState(state, getActionThread());
  };
  const updateThreadState = newThreadState => {
    return {
      ...state,
      threads: {
        ...state.threads,
        [getActionThread()]: { ...threadState(), ...newThreadState },
      },
    };
  };

  switch (action.type) {
    case "SELECT_THREAD": {
      // Ignore the action if the related thread doesn't exist.
      if (!state.threads[action.thread]) {
        console.warn(
          `Trying to select a destroyed or non-existent thread '${action.thread}'`
        );
        return state;
      }

      return {
        ...state,
        threadcx: {
          ...state.threadcx,
          thread: action.thread,
          pauseCounter: state.threadcx.pauseCounter + 1,
        },
      };
    }

    case "INSERT_THREAD": {
      // When navigating to a new location,
      // we receive NAVIGATE early, which clear things
      // then we have REMOVE_THREAD of the previous thread.
      // INSERT_THREAD will be the very first event with the new thread actor ID.
      // Automatically select the new top level thread.
      if (action.newThread.isTopLevel) {
        return {
          ...state,
          threadcx: {
            ...state.threadcx,
            thread: action.newThread.actor,
            pauseCounter: state.threadcx.pauseCounter + 1,
          },
          threads: {
            ...state.threads,
            [action.newThread.actor]: createInitialPauseState(),
          },
        };
      }

      return {
        ...state,
        threads: {
          ...state.threads,
          [action.newThread.actor]: createInitialPauseState(),
        },
      };
    }

    case "REMOVE_THREAD": {
      if (
        action.threadActorID in state.threads ||
        action.threadActorID == state.threadcx.thread
      ) {
        // Remove the thread from the cached list
        const threads = { ...state.threads };
        delete threads[action.threadActorID];
        let threadcx = state.threadcx;

        // And also switch to another thread if this was the currently selected one.
        // As we don't store thread objects in this reducer, and only store thread actor IDs,
        // we can't try to find the top level thread. So we pick the first available thread,
        // and hope that's the top level one.
        if (state.threadcx.thread == action.threadActorID) {
          threadcx = {
            ...threadcx,
            thread: Object.keys(threads)[0],
            pauseCounter: threadcx.pauseCounter + 1,
          };
        }
        return {
          ...state,
          threadcx,
          threads,
        };
      }
      break;
    }

    case "PAUSED": {
      const { thread, topFrame, why } = action;
      state = {
        ...state,
        threadcx: {
          ...state.threadcx,
          pauseCounter: state.threadcx.pauseCounter + 1,
          thread,
        },
      };

      return updateThreadState({
        isWaitingOnBreak: false,
        selectedFrameId: topFrame.id,
        isPaused: true,
        // On pause, we only receive the top frame, all subsequent ones
        // will be asynchronously populated via `fetchFrames` action
        frames: [topFrame],
        framesLoading: true,
        frameScopes: { ...resumedPauseState.frameScopes },
        why,
        shouldBreakpointsPaneOpenOnPause: why.type === "breakpoint",
      });
    }

    case "FETCHED_FRAMES": {
      const { frames } = action;

      // We typically receive a PAUSED action before this one,
      // with only the first frame. Here, we avoid replacing it
      // with a copy of it in order to avoid triggerring selectors
      // uncessarily
      // (note that in jest, action's frames might be empty)
      // (and if we resume in between PAUSED and FETCHED_FRAMES
      //  threadState().frames might be null)
      if (threadState().frames) {
        const previousFirstFrame = threadState().frames[0];
        if (previousFirstFrame.id == frames[0]?.id) {
          frames.splice(0, 1, previousFirstFrame);
        }
      }
      return updateThreadState({ frames, framesLoading: false });
    }

    case "MAP_FRAMES": {
      const { selectedFrameId, frames } = action;
      return updateThreadState({ frames, selectedFrameId });
    }

    case "ADD_SCOPES": {
      const { status, value } = action;
      const selectedFrameId = action.selectedFrame.id;

      const generated = {
        ...threadState().frameScopes.generated,
        [selectedFrameId]: {
          pending: status !== "done",
          scope: value,
        },
      };

      return updateThreadState({
        frameScopes: {
          ...threadState().frameScopes,
          generated,
        },
      });
    }

    case "MAP_SCOPES": {
      const { status, value } = action;
      const selectedFrameId = action.selectedFrame.id;

      const original = {
        ...threadState().frameScopes.original,
        [selectedFrameId]: {
          pending: status !== "done",
          scope: value?.scope,
        },
      };

      const mappings = {
        ...threadState().frameScopes.mappings,
        [selectedFrameId]: value?.mappings,
      };

      return updateThreadState({
        frameScopes: {
          ...threadState().frameScopes,
          original,
          mappings,
        },
      });
    }

    case "BREAK_ON_NEXT":
      return updateThreadState({ isWaitingOnBreak: true });

    case "SELECT_FRAME":
      return updateThreadState({ selectedFrameId: action.frame.id });

    case "PAUSE_ON_DEBUGGER_STATEMENT": {
      const { shouldPauseOnDebuggerStatement } = action;

      prefs.pauseOnDebuggerStatement = shouldPauseOnDebuggerStatement;

      return {
        ...state,
        shouldPauseOnDebuggerStatement,
      };
    }

    case "PAUSE_ON_EXCEPTIONS": {
      const { shouldPauseOnExceptions, shouldPauseOnCaughtExceptions } = action;

      prefs.pauseOnExceptions = shouldPauseOnExceptions;
      prefs.pauseOnCaughtExceptions = shouldPauseOnCaughtExceptions;

      // Preserving for the old debugger
      prefs.ignoreCaughtExceptions = !shouldPauseOnCaughtExceptions;

      return {
        ...state,
        shouldPauseOnExceptions,
        shouldPauseOnCaughtExceptions,
      };
    }

    case "COMMAND":
      if (action.status === "start") {
        return updateThreadState({
          ...resumedPauseState,
          command: action.command,
          previousLocation: getPauseLocation(threadState(), action),
        });
      }
      return updateThreadState({ command: null });

    case "RESUME": {
      if (action.thread == state.threadcx.thread) {
        state = {
          ...state,
          threadcx: {
            ...state.threadcx,
            pauseCounter: state.threadcx.pauseCounter + 1,
          },
        };
      }

      return updateThreadState({
        ...resumedPauseState,
        expandedScopes: new Set(),
        lastExpandedScopes: [...threadState().expandedScopes],
        shouldBreakpointsPaneOpenOnPause: false,
      });
    }

    case "EVALUATE_EXPRESSION":
      return updateThreadState({
        command: action.status === "start" ? "expression" : null,
      });

    case "NAVIGATE": {
      const navigateCounter = state.cx.navigateCounter + 1;
      return {
        ...state,
        cx: {
          navigateCounter,
        },
        threadcx: {
          navigateCounter,
          thread: action.mainThread.actor,
          pauseCounter: 0,
        },
        threads: {
          ...state.threads,
          [action.mainThread.actor]: {
            ...getThreadPauseState(state, action.mainThread.actor),
            ...resumedPauseState,
          },
        },
      };
    }

    case "TOGGLE_SKIP_PAUSING": {
      const { skipPausing } = action;
      prefs.skipPausing = skipPausing;

      return { ...state, skipPausing };
    }

    case "TOGGLE_MAP_SCOPES": {
      const { mapScopes } = action;
      prefs.mapScopes = mapScopes;
      return { ...state, mapScopes };
    }

    case "SET_EXPANDED_SCOPE": {
      const { path, expanded } = action;
      const expandedScopes = new Set(threadState().expandedScopes);
      if (expanded) {
        expandedScopes.add(path);
      } else {
        expandedScopes.delete(path);
      }
      return updateThreadState({ expandedScopes });
    }

    case "ADD_INLINE_PREVIEW": {
      const { selectedFrame, previews } = action;
      const selectedFrameId = selectedFrame.id;

      return updateThreadState({
        inlinePreview: {
          ...threadState().inlinePreview,
          [selectedFrameId]: previews,
        },
      });
    }

    case "RESET_BREAKPOINTS_PANE_STATE": {
      return updateThreadState({
        ...threadState(),
        shouldBreakpointsPaneOpenOnPause: false,
      });
    }
  }

  return state;
}

function getPauseLocation(state, action) {
  const { frames, previousLocation } = state;

  // NOTE: We store the previous location so that we ensure that we
  // do not stop at the same location twice when we step over.
  if (action.command !== "stepOver") {
    return null;
  }

  const frame = frames?.[0];
  if (!frame) {
    return previousLocation;
  }

  return {
    location: frame.location,
    generatedLocation: frame.generatedLocation,
  };
}

export default update;