summaryrefslogtreecommitdiffstats
path: root/devtools/client/dom/content/reducers/grips.js
blob: c7d589434d4d462f9785b55b6c9d6fcad6c31c8e (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
/* -*- indent-tabs-mode: nil; js-indent-level: 2 -*- */
/* vim: set ft=javascript ts=2 et sw=2 tw=80: */
/* 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 constants = require("../constants");

/**
 * Initial state definition
 */
function getInitialState() {
  return new Map();
}

/**
 * Maintain a cache of received grip responses from the backend.
 */
function grips(state = getInitialState(), action) {
  // This reducer supports only one action, fetching actor properties
  // from the backend so, bail out if we are dealing with any other
  // action.
  if (action.type != constants.FETCH_PROPERTIES) {
    return state;
  }

  switch (action.status) {
    case "start":
      return onRequestProperties(state, action);
    case "end":
      return onReceiveProperties(state, action);
  }

  return state;
}

/**
 * Handle requestProperties action
 */
function onRequestProperties(state, action) {
  return state;
}

/**
 * Handle receiveProperties action
 */
function onReceiveProperties(cache, action) {
  let response = action.response;
  let from = response.from;
  let className = action.grip.class;

  // Properly deal with getters.
  mergeProperties(response);

  // Compute list of requested children.
  let previewProps = response.preview ? response.preview.ownProperties : null;
  let ownProps = response.ownProperties || previewProps || [];

  let props = Object.keys(ownProps).map(key => {
    // Array indexes as a special case. We convert any keys that are string
    // representations of integers to integers.
    if (className === "Array" && isInteger(key)) {
      key = parseInt(key, 10);
    }
    return new Property(key, ownProps[key], key);
  });

  props.sort(sortName);

  // Return new state/map.
  let newCache = new Map(cache);
  newCache.set(from, props);

  return newCache;
}

// Helpers

function mergeProperties(response) {
  let { ownProperties } = response;

  // 'safeGetterValues' is new and isn't necessary defined on old grips.
  let safeGetterValues = response.safeGetterValues || {};

  // Merge the safe getter values into one object such that we can use it
  // in variablesView.
  for (let name of Object.keys(safeGetterValues)) {
    if (name in ownProperties) {
      let { getterValue, getterPrototypeLevel } = safeGetterValues[name];
      ownProperties[name].getterValue = getterValue;
      ownProperties[name].getterPrototypeLevel = getterPrototypeLevel;
    } else {
      ownProperties[name] = safeGetterValues[name];
    }
  }
}

function sortName(a, b) {
  // Display non-enumerable properties at the end.
  if (!a.value.enumerable && b.value.enumerable) {
    return 1;
  }
  if (a.value.enumerable && !b.value.enumerable) {
    return -1;
  }
  return a.name > b.name ? 1 : -1;
}

function isInteger(n) {
  // We use parseInt(n, 10) == n to disregard scientific notation e.g. "3e24"
  return isFinite(n) && parseInt(n, 10) == n;
}

function Property(name, value, key) {
  this.name = name;
  this.value = value;
  this.key = key;
}

// Exports from this module
exports.grips = grips;
exports.Property = Property;