瀏覽代碼

fix: set -> setOne

+ use the class name for type inference
tophf 3 年之前
父節點
當前提交
39c4ee5729
共有 3 個文件被更改,包括 12 次插入14 次删除
  1. 3 3
      src/background/utils/storage-fetch.js
  2. 0 1
      src/background/utils/values.js
  3. 9 10
      src/common/storage.js

+ 3 - 3
src/background/utils/storage-fetch.js

@@ -25,20 +25,20 @@ storage.require.fetch = cacheOrFetch({
 function cacheOrFetch(handlers = {}) {
   const requests = {};
   const { init, transform } = handlers;
-  /** @this VMStorageBase */
+  /** @this StorageArea */
   return function cacheOrFetchHandler(...args) {
     const [url] = args;
     const promise = requests[url] || (requests[url] = this::doFetch(...args));
     return promise;
   };
-  /** @this VMStorageBase */
+  /** @this StorageArea */
   async function doFetch(...args) {
     const [url, options] = args;
     try {
       const res = await requestNewer(url, init ? init(options) : options);
       if (res) {
         const result = transform ? await transform(res, ...args) : res.data;
-        await this.set(url, result);
+        await this.setOne(url, result);
       }
     } finally {
       delete requests[url];

+ 0 - 1
src/background/utils/values.js

@@ -69,7 +69,6 @@ export function addValueOpener(tabId, frameId, injectedScripts) {
   });
 }
 
-/** Caution: may delete keys in `data` */
 function commit(data) {
   storage.value.set(data);
   chain = chain.catch(console.warn).then(broadcast);

+ 9 - 10
src/common/storage.js

@@ -3,8 +3,7 @@ import { ensureArray } from './util';
 
 let api = browser.storage.local;
 
-/** @namespace VMStorageBase */
-class Area {
+class StorageArea {
   constructor(prefix) {
     this.name = '';
     this.prefix = prefix;
@@ -67,17 +66,17 @@ class Area {
 const storage = {
   get api() { return api; },
   set api(val) { api = val; },
-  base: new Area(''),
-  cache: new Area('cac:'),
-  code: new Area('code:'),
+  base: new StorageArea(''),
+  cache: new StorageArea('cac:'),
+  code: new StorageArea('code:'),
   /** last-modified HTTP header value per URL */
-  mod: new Area('mod:'),
-  require: new Area('req:'),
-  script: new Area('scr:'),
-  value: new Area('val:'),
+  mod: new StorageArea('mod:'),
+  require: new StorageArea('req:'),
+  script: new StorageArea('scr:'),
+  value: new StorageArea('val:'),
 };
 storage::mapEntry((val, name) => {
-  if (val instanceof Area) {
+  if (val instanceof StorageArea) {
     val.name = name;
   }
 });