2016-02-10 01:23:29 +03:00
|
|
|
/* -*- 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) {
|
2018-06-01 13:36:09 +03:00
|
|
|
const response = action.response;
|
|
|
|
const from = response.from;
|
|
|
|
const className = action.grip.class;
|
2016-02-10 01:23:29 +03:00
|
|
|
|
|
|
|
// Properly deal with getters.
|
|
|
|
mergeProperties(response);
|
|
|
|
|
|
|
|
// Compute list of requested children.
|
2018-06-01 13:36:09 +03:00
|
|
|
const previewProps = response.preview ? response.preview.ownProperties : null;
|
|
|
|
const ownProps = response.ownProperties || previewProps || [];
|
2016-07-12 11:33:07 +03:00
|
|
|
|
2018-06-01 13:36:09 +03:00
|
|
|
const props = Object.keys(ownProps).map(key => {
|
2016-07-12 11:33:07 +03:00
|
|
|
// 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);
|
|
|
|
}
|
2016-02-10 01:23:29 +03:00
|
|
|
return new Property(key, ownProps[key], key);
|
|
|
|
});
|
|
|
|
|
|
|
|
props.sort(sortName);
|
|
|
|
|
|
|
|
// Return new state/map.
|
2018-06-01 13:36:09 +03:00
|
|
|
const newCache = new Map(cache);
|
2016-02-10 01:23:29 +03:00
|
|
|
newCache.set(from, props);
|
|
|
|
|
|
|
|
return newCache;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Helpers
|
|
|
|
|
|
|
|
function mergeProperties(response) {
|
2018-06-01 13:36:09 +03:00
|
|
|
const { ownProperties } = response;
|
2016-02-10 01:23:29 +03:00
|
|
|
|
|
|
|
// 'safeGetterValues' is new and isn't necessary defined on old grips.
|
2018-06-01 13:36:09 +03:00
|
|
|
const safeGetterValues = response.safeGetterValues || {};
|
2016-02-10 01:23:29 +03:00
|
|
|
|
|
|
|
// Merge the safe getter values into one object such that we can use it
|
|
|
|
// in variablesView.
|
2018-06-01 13:36:09 +03:00
|
|
|
for (const name of Object.keys(safeGetterValues)) {
|
2016-02-10 01:23:29 +03:00
|
|
|
if (name in ownProperties) {
|
2018-06-01 13:36:09 +03:00
|
|
|
const { getterValue, getterPrototypeLevel } = safeGetterValues[name];
|
2016-02-10 01:23:29 +03:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2016-07-12 11:33:07 +03:00
|
|
|
function isInteger(n) {
|
|
|
|
// We use parseInt(n, 10) == n to disregard scientific notation e.g. "3e24"
|
|
|
|
return isFinite(n) && parseInt(n, 10) == n;
|
|
|
|
}
|
|
|
|
|
2016-02-10 01:23:29 +03:00
|
|
|
function Property(name, value, key) {
|
|
|
|
this.name = name;
|
|
|
|
this.value = value;
|
|
|
|
this.key = key;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Exports from this module
|
|
|
|
exports.grips = grips;
|
|
|
|
exports.Property = Property;
|