Browse Source

fix vacuum callback

Gerald 8 years ago
parent
commit
124c071a90
2 changed files with 36 additions and 34 deletions
  1. 2 1
      src/background/app.js
  2. 34 33
      src/background/utils/db.js

+ 2 - 1
src/background/app.js

@@ -228,7 +228,8 @@ initialize()
         });
       }
     }
-    return res;
+    // undefined will be ignored
+    return res || null;
   });
   setTimeout(autoUpdate, 2e4);
   sync.initialize();

+ 34 - 33
src/background/utils/db.js

@@ -552,7 +552,8 @@ export function vacuum() {
     [storage.require, requireKeys],
     [storage.code, codeKeys],
   ];
-  browser.storage.get().then(data => {
+  return browser.storage.local.get()
+  .then(data => {
     Object.keys(data).forEach(key => {
       mappings.some(([substore, map]) => {
         const { prefix } = substore;
@@ -564,39 +565,39 @@ export function vacuum() {
         return false;
       });
     });
-  });
-  const touch = (obj, key) => {
-    if (obj[key] < 0) obj[key] = 1;
-    else if (!obj[key]) obj[key] = 2;
-  };
-  store.scripts.forEach(script => {
-    const { id } = script.props;
-    touch(codeKeys, id);
-    touch(valueKeys, id);
-    if (!script.custom.pathMap) buildPathMap(script);
-    const { pathMap } = script.custom;
-    script.meta.require.forEach(url => {
-      touch(requireKeys, pathMap[url] || url);
-    });
-    Object.values(script.meta.resources).forEach(url => {
-      touch(cacheKeys, pathMap[url] || url);
-    });
-    const { icon } = script.meta;
-    if (isRemote(icon)) {
-      const fullUrl = pathMap[icon] || icon;
-      touch(cacheKeys, fullUrl);
-    }
-  });
-  mappings.forEach(([substore, map]) => {
-    Object.keys(map).forEach(key => {
-      const value = map[key];
-      if (value < 0) {
-        // redundant value
-        substore.remove(key);
-      } else if (value === 2 && substore.fetch) {
-        // missing resource
-        substore.fetch(key);
+    const touch = (obj, key) => {
+      if (obj[key] < 0) obj[key] = 1;
+      else if (!obj[key]) obj[key] = 2;
+    };
+    store.scripts.forEach(script => {
+      const { id } = script.props;
+      touch(codeKeys, id);
+      touch(valueKeys, id);
+      if (!script.custom.pathMap) buildPathMap(script);
+      const { pathMap } = script.custom;
+      script.meta.require.forEach(url => {
+        touch(requireKeys, pathMap[url] || url);
+      });
+      Object.values(script.meta.resources).forEach(url => {
+        touch(cacheKeys, pathMap[url] || url);
+      });
+      const { icon } = script.meta;
+      if (isRemote(icon)) {
+        const fullUrl = pathMap[icon] || icon;
+        touch(cacheKeys, fullUrl);
       }
     });
+    mappings.forEach(([substore, map]) => {
+      Object.keys(map).forEach(key => {
+        const value = map[key];
+        if (value < 0) {
+          // redundant value
+          substore.remove(key);
+        } else if (value === 2 && substore.fetch) {
+          // missing resource
+          substore.fetch(key);
+        }
+      });
+    });
   });
 }