Просмотр исходного кода

feat(confirm): cache deps on local file change

close violentmonkey/violentmonkey#93
Gerald 8 лет назад
Родитель
Сommit
c90fa9dd50
3 измененных файлов с 56 добавлено и 28 удалено
  1. 4 19
      src/background/utils/cache.js
  2. 30 0
      src/common/cache.js
  3. 22 9
      src/options/views/confirm.vue

+ 4 - 19
src/background/utils/cache.js

@@ -1,21 +1,6 @@
-const cache = {};
+import initCache from 'src/common/cache';
 
-export function getCache(key) {
-  const obj = cache[key];
-  return obj && obj.value;
-}
+const cache = initCache();
 
-export function setCache(key, value) {
-  if (value) {
-    let obj = cache[key];
-    if (!obj) {
-      obj = { key };
-      cache[key] = obj;
-    }
-    obj.value = value;
-    if (obj.timer) clearTimeout(obj.timer);
-    obj.timer = setTimeout(setCache, 3000, key);
-  } else {
-    delete cache[key];
-  }
-}
+export const getCache = cache.get;
+export const setCache = cache.set;

+ 30 - 0
src/common/cache.js

@@ -0,0 +1,30 @@
+export default function initCache(options) {
+  const cache = {};
+  const { lifetime: defaultLifetime } = options || {
+    lifetime: 3000,
+  };
+  return { get, put, del, has };
+  function get(key, def) {
+    const item = cache[key];
+    return item ? item.value : def;
+  }
+  function put(key, value, lifetime = defaultLifetime) {
+    del(key);
+    if (value) {
+      cache[key] = {
+        value,
+        timer: lifetime > 0 && setTimeout(del, lifetime, key),
+      };
+    }
+  }
+  function del(key) {
+    const item = cache[key];
+    if (item) {
+      if (item.timer) clearTimeout(item.timer);
+      delete cache[key];
+    }
+  }
+  function has(key) {
+    return !!cache[key];
+  }
+}

+ 22 - 9
src/options/views/confirm.vue

@@ -32,9 +32,12 @@
 <script>
 import { sendMessage, zfill, request } from 'src/common';
 import options from 'src/common/options';
+import initCache from 'src/common/cache';
 import VmCode from './code';
 import { store } from '../utils';
 
+const cache = initCache({});
+
 const settings = {
   closeAfterInstall: options.get('closeAfterInstall'),
 };
@@ -57,8 +60,6 @@ export default {
       dependencyOK: false,
       message: '',
       code: '',
-      require: {},
-      resources: {},
       commands: {
         cancel: this.close,
       },
@@ -102,13 +103,17 @@ export default {
           this.message = this.i18n('msgLoadingDependency', [finished, length]);
         };
         updateStatus();
-        let promises = script.require.map(url => this.getFile(url).then((res) => {
-          this.require[url] = res;
-        }))
-        .concat(urls.map(url => this.getFile(url, true).then((res) => {
+        this.require = {};
+        this.resources = {};
+        const promises = script.require.map(url => (
+          this.getFile(url, { useCache: true }).then(res => {
+            this.require[url] = res;
+          })
+        ))
+        .concat(urls.map(url => this.getFile(url, { isBlob: true, useCache: true }).then((res) => {
           this.resources[url] = res;
-        })));
-        promises = promises.map(promise => promise.then(() => {
+        })))
+        .map(promise => promise.then(() => {
           finished += 1;
           updateStatus();
         }, url => {
@@ -130,7 +135,11 @@ export default {
     close() {
       window.close();
     },
-    getFile(url, isBlob) {
+    getFile(url, { isBlob, useCache }) {
+      const cacheKey = isBlob ? `blob+${url}` : `text+${url}`;
+      if (useCache && cache.has(cacheKey)) {
+        return Promise.resolve(cache.get(cacheKey));
+      }
       return request(url, {
         responseType: isBlob ? 'blob' : null,
       })
@@ -143,6 +152,10 @@ export default {
           };
           reader.readAsBinaryString(data);
         });
+      })
+      .then(data => {
+        if (useCache) cache.put(cacheKey, data);
+        return data;
       });
     },
     getScript(url) {