Browse Source

fix #1984: auto-fetch icon cache if missing

tophf 1 year ago
parent
commit
328a7af988

+ 1 - 1
src/background/utils/icon.js

@@ -241,7 +241,7 @@ async function loadIcon(url) {
   const isOwn = url.startsWith(ICON_PREFIX);
   img.src = isOwn ? url.slice(extensionOrigin.length) // must be a relative path in Firefox Android
     : url.startsWith('data:') ? url
-      : makeDataUri(url[0] === 'i' ? url : await storage.cache.getOne(url));
+      : makeDataUri(url[0] === 'i' ? url : await storage.cache.getOne(url, true));
   await new Promise((resolve) => {
     img.onload = resolve;
     img.onerror = resolve;

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

@@ -43,6 +43,9 @@ function cacheOrFetch(handlers = {}) {
       if (res) {
         const result = transform ? await transform(res, ...args) : res.data;
         await this.setOne(url, result);
+        if (options === true) {
+          return result;
+        }
       }
     } finally {
       delete requests[url];

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

@@ -24,10 +24,17 @@ class StorageArea {
       : '';
   }
 
-  async getOne(id) {
+  /**
+   * @param {string|number} id
+   * @param {boolean} [fetchMissing]
+   * @return {Promise<?>}
+   */
+  async getOne(id, fetchMissing) {
     const key = this.toKey(id);
-    const data = await api.get([key]);
-    return data[key];
+    const {
+      [key]: val = fetchMissing ? await this.fetch(id, true).catch(console.warn) : undefined,
+    } = await api.get([key]);
+    return val;
   }
 
   /**

+ 1 - 1
src/common/index.js

@@ -297,8 +297,8 @@ export function tryUrl(str) {
 }
 
 /**
- * @param {string} url
  * @param {string} raw - raw value in storage.cache
+ * @param {string} [url]
  * @returns {?string}
  */
 export function makeDataUri(raw, url) {

+ 3 - 2
src/types.d.ts

@@ -308,7 +308,7 @@ declare interface VMRealmData {
 declare namespace VMReq {
   interface Options extends RequestInit {
     /** @implements XMLHttpRequestResponseType */
-    responseType: '' | 'arraybuffer' | 'blob' | 'json' | 'text';
+    responseType?: '' | 'arraybuffer' | 'blob' | 'json' | 'text';
   }
   interface Response {
     url: string;
@@ -328,7 +328,8 @@ declare type VMSearchOptions = {
 /** Throws on error */
 declare type VMStorageFetch = (
   url: string,
-  options?: VMReq.Options,
+  /** Resolves with the result if true, `undefined` otherwise */
+  options?: VMReq.Options | true,
   check?: (...args) => void // throws on error
 ) => Promise<void>