Browse Source

refactor: revert objectSet's support of arrays

tophf 5 years ago
parent
commit
d5fc99f9b4
2 changed files with 5 additions and 9 deletions
  1. 3 3
      src/background/utils/values.js
  2. 2 6
      src/common/object.js

+ 3 - 3
src/background/utils/values.js

@@ -5,7 +5,7 @@ import {
 import { getValueStoresByIds, dumpValueStores, dumpValueStore } from './db';
 import { commands } from './message';
 
-const openers = {}; // { scriptId: { tabId: [frameId, ... ], ... } }
+const openers = {}; // { scriptId: { tabId: { frameId: 1, ... }, ... } }
 let cache; // { scriptId: { key: [{ value, src }, ... ], ... } }
 let updateScheduled;
 
@@ -44,7 +44,7 @@ export function resetValueOpener(tabId) {
 
 export function addValueOpener(tabId, frameId, scriptIds) {
   scriptIds.forEach((id) => {
-    objectSet(openers, [id, [tabId]], frameId);
+    objectSet(openers, [id, tabId, frameId], 1);
   });
 }
 
@@ -84,7 +84,7 @@ function broadcastUpdates(updates, oldCache = {}) {
   const toSend = {};
   updates::forEachEntry(([id, data]) => {
     openers[id]::forEachEntry(([tabId, frames]) => {
-      frames.forEach(frameId => {
+      frames::forEachKey(frameId => {
         objectSet(toSend, [tabId, frameId, id],
           avoidInitiator(data, oldCache[id], tabId, frameId));
       });

+ 2 - 6
src/common/object.js

@@ -23,15 +23,11 @@ export function objectSet(obj, rawKey, val) {
   if (!keys.length) return val;
   const root = obj || {};
   let sub = root;
-  let lastKey = keys.pop();
+  const lastKey = keys.pop();
   keys.forEach((key) => {
     sub = sub[key] || (sub[key] = {});
   });
-  if (Array.isArray(lastKey)) {
-    lastKey = lastKey[0];
-    sub = sub[lastKey] || (sub[lastKey] = []);
-    sub.push(val);
-  } else if (typeof val === 'undefined') {
+  if (typeof val === 'undefined') {
     delete sub[lastKey];
   } else {
     sub[lastKey] = val;