Bug 1273584 - Stop growing device list forever. r=gl draft
authorJ. Ryan Stinnett <jryans@gmail.com>
Mon, 16 May 2016 13:53:25 -0500
changeset 368442 85e203f3e2719bb64b9820463d4e43455432bcf9
parent 368441 437c48d2fb82216f288b3292baefbbfe2a5e09a3
child 521271 d2520737143e55e6099a42a0eb9e0f7babb5c094
push id18533
push userbmo:jryans@gmail.com
push dateWed, 18 May 2016 18:42:24 +0000
reviewersgl
bugs1273584
milestone49.0a1
Bug 1273584 - Stop growing device list forever. r=gl MozReview-Commit-ID: L6jPEWeFDcr
devtools/client/responsive.html/devices.js
devtools/client/responsive.html/test/browser/browser_device_modal_exit.js
devtools/client/responsive.html/test/browser/browser_device_modal_submit.js
--- 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],