summaryrefslogtreecommitdiffstats
path: root/devtools/client/shared/components/object-inspector/reducer.js
blob: aa8af2b52994c68b0b6a8b0eb31c7d0af2d48141 (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
/* 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/>. */

function initialOIState(overrides) {
  return {
    expandedPaths: new Set(),
    loadedProperties: new Map(),
    evaluations: new Map(),
    watchpoints: new Map(),
    ...overrides,
  };
}

function reducer(state = initialOIState(), action = {}) {
  const { type, data } = action;

  const cloneState = overrides => ({ ...state, ...overrides });

  if (type === "NODE_EXPAND") {
    return cloneState({
      expandedPaths: new Set(state.expandedPaths).add(data.node.path),
    });
  }

  if (type === "NODE_COLLAPSE") {
    const expandedPaths = new Set(state.expandedPaths);
    expandedPaths.delete(data.node.path);
    return cloneState({ expandedPaths });
  }

  if (type == "SET_WATCHPOINT") {
    const { watchpoint, property, path } = data;
    const obj = state.loadedProperties.get(path);

    return cloneState({
      loadedProperties: new Map(state.loadedProperties).set(
        path,
        updateObject(obj, property, watchpoint)
      ),
      watchpoints: new Map(state.watchpoints).set(data.actor, data.watchpoint),
    });
  }

  if (type === "REMOVE_WATCHPOINT") {
    const { path, property, actor } = data;
    const obj = state.loadedProperties.get(path);
    const watchpoints = new Map(state.watchpoints);
    watchpoints.delete(actor);

    return cloneState({
      loadedProperties: new Map(state.loadedProperties).set(
        path,
        updateObject(obj, property, null)
      ),
      watchpoints: watchpoints,
    });
  }

  if (type === "NODE_PROPERTIES_LOADED") {
    return cloneState({
      loadedProperties: new Map(state.loadedProperties).set(
        data.node.path,
        action.data.properties
      ),
    });
  }

  if (type === "ROOTS_CHANGED") {
    return cloneState();
  }

  if (type === "GETTER_INVOKED") {
    return cloneState({
      evaluations: new Map(state.evaluations).set(data.node.path, {
        getterValue:
          data.result &&
          data.result.value &&
          (data.result.value.throw || data.result.value.return),
      }),
    });
  }

  // NOTE: we clear the state on resume because otherwise the scopes pane
  // would be out of date. Bug 1514760
  if (type === "RESUME" || type == "NAVIGATE") {
    return initialOIState({ watchpoints: state.watchpoints });
  }

  return state;
}

function updateObject(obj, property, watchpoint) {
  return {
    ...obj,
    ownProperties: {
      ...obj.ownProperties,
      [property]: {
        ...obj.ownProperties[property],
        watchpoint,
      },
    },
  };
}

function getObjectInspectorState(state) {
  return state.objectInspector || state;
}

function getExpandedPaths(state) {
  return getObjectInspectorState(state).expandedPaths;
}

function getExpandedPathKeys(state) {
  return [...getExpandedPaths(state).keys()];
}

function getWatchpoints(state) {
  return getObjectInspectorState(state).watchpoints;
}

function getLoadedProperties(state) {
  return getObjectInspectorState(state).loadedProperties;
}

function getLoadedPropertyKeys(state) {
  return [...getLoadedProperties(state).keys()];
}

function getEvaluations(state) {
  return getObjectInspectorState(state).evaluations;
}

const selectors = {
  getWatchpoints,
  getEvaluations,
  getExpandedPathKeys,
  getExpandedPaths,
  getLoadedProperties,
  getLoadedPropertyKeys,
};

Object.defineProperty(module.exports, "__esModule", {
  value: true,
});
module.exports = { ...selectors, initialOIState };
module.exports.default = reducer;