Bug 1273584 - Stop growing device list forever. r=gl
MozReview-Commit-ID: L6jPEWeFDcr
--- a/devtools/client/responsive.html/devices.js
+++ b/devtools/client/responsive.html/devices.js
@@ -24,58 +24,59 @@ let initDevices = Task.async(function* (
for (let type of devices.TYPES) {
dispatch(addDeviceType(type));
for (let device of devices[type]) {
if (device.os == "fxos") {
continue;
}
let newDevice = Object.assign({}, device, {
- displayed: deviceList.includes(device.name) ?
+ displayed: deviceList.has(device.name) ?
true :
!!device.featured,
});
if (newDevice.displayed) {
- deviceList.push(newDevice.name);
+ deviceList.add(newDevice.name);
}
dispatch(addDevice(newDevice, type));
}
}
updateDeviceList(deviceList);
});
/**
- * Returns an array containing the user preference of displayed devices.
+ * Returns a set containing the user preference of displayed devices.
*
- * @return {Array} containing the device names that are to be displayed in the
+ * @return {Set} containing the device names that are to be displayed in the
* device catalog.
*/
function loadDeviceList() {
- let deviceList = [];
+ let deviceList = new Set();
if (Services.prefs.prefHasUserValue(DISPLAYED_DEVICES_PREF)) {
try {
- deviceList = JSON.parse(Services.prefs.getCharPref(
- DISPLAYED_DEVICES_PREF));
+ let savedList = Services.prefs.getCharPref(DISPLAYED_DEVICES_PREF);
+ deviceList = new Set(JSON.parse(savedList));
} catch (e) {
console.error(e);
}
}
return deviceList;
}
/**
* Update the displayed device list preference with the given device list.
*
- * @param {Array} devices
- * Array of device names that are displayed in the device catalog.
+ * @param {Set} devices
+ * Set of device names that are displayed in the device catalog.
*/
function updateDeviceList(devices) {
- Services.prefs.setCharPref(DISPLAYED_DEVICES_PREF, JSON.stringify(devices));
+ let listToSave = JSON.stringify(Array.from(devices));
+ Services.prefs.setCharPref(DISPLAYED_DEVICES_PREF, listToSave);
}
exports.initDevices = initDevices;
exports.loadDeviceList = loadDeviceList;
exports.updateDeviceList = updateDeviceList;
--- a/devtools/client/responsive.html/test/browser/browser_device_modal_exit.js
+++ b/devtools/client/responsive.html/test/browser/browser_device_modal_exit.js
@@ -26,13 +26,13 @@ addRDMTask(TEST_URL, function* ({ ui })
uncheckedCb.click();
closeButton.click();
ok(modal.classList.contains("hidden"),
"The device modal is hidden on exit.");
info("Check that the device list remains unchanged after exitting.");
let deviceListAfter = loadDeviceList();
- is(deviceListBefore.length, deviceListAfter.length,
+ is(deviceListBefore.size, deviceListAfter.size,
"Got expected number of displayed devices.");
- ok(!deviceListAfter.includes(value),
+ ok(!deviceListAfter.has(value),
value + " was not added to displayed device list.");
});
--- a/devtools/client/responsive.html/test/browser/browser_device_modal_submit.js
+++ b/devtools/client/responsive.html/test/browser/browser_device_modal_submit.js
@@ -18,40 +18,40 @@ addRDMTask(TEST_URL, function* ({ ui })
openDeviceModal(ui);
info("Checking displayed device checkboxes are checked in the device modal.");
let checkedCbs = [...document.querySelectorAll(".device-input-checkbox")]
.filter(cb => cb.checked);
let deviceList = loadDeviceList();
- is(deviceList.length, checkedCbs.length,
+ is(deviceList.size, checkedCbs.length,
"Got expected number of displayed devices.");
for (let cb of checkedCbs) {
- ok(deviceList.includes(cb.value), cb.value + " is correctly checked.");
+ ok(deviceList.has(cb.value), cb.value + " is correctly checked.");
}
info("Check the first unchecked device and submit new device list.");
let uncheckedCb = [...document.querySelectorAll(".device-input-checkbox")]
.filter(cb => !cb.checked)[0];
let value = uncheckedCb.value;
uncheckedCb.click();
submitButton.click();
ok(modal.classList.contains("hidden"),
"The device modal is hidden on submit.");
info("Checking new device is added to the displayed device list.");
deviceList = loadDeviceList();
- ok(deviceList.includes(value), value + " added to displayed device list.");
+ ok(deviceList.has(value), value + " added to displayed device list.");
info("Checking new device is added to the device selector.");
let options = [...select.options];
- is(options.length - 2, deviceList.length,
+ is(options.length - 2, deviceList.size,
"Got expected number of devices in device selector.");
ok(options.filter(o => o.value === value)[0],
value + " added to the device selector.");
info("Reopen device modal and check new device is correctly checked");
openDeviceModal(ui);
ok([...document.querySelectorAll(".device-input-checkbox")]
.filter(cb => cb.checked && cb.value === value)[0],