From c4a88997803cd6424c997f6d329b96cb00a08c72 Mon Sep 17 00:00:00 2001 From: Bram Kragten Date: Wed, 26 May 2021 10:22:38 +0200 Subject: [PATCH] Bump idb-keyval (#9248) https://github.com/jakearchibald/idb-keyval#updating-from-3x --- package.json | 2 +- src/data/iconsets.ts | 45 ++++++++++++++++++++++---------------------- yarn.lock | 8 ++++---- 3 files changed, 27 insertions(+), 28 deletions(-) diff --git a/package.json b/package.json index 269e4e052e..2213f328e4 100644 --- a/package.json +++ b/package.json @@ -111,7 +111,7 @@ "google-timezones-json": "^1.0.2", "hls.js": "^1.0.3", "home-assistant-js-websocket": "^5.10.0", - "idb-keyval": "^3.2.0", + "idb-keyval": "^5.0.5", "intl-messageformat": "^9.6.13", "js-yaml": "^4.1.0", "leaflet": "^1.7.1", diff --git a/src/data/iconsets.ts b/src/data/iconsets.ts index 2e6df41914..8b92ddbfa3 100644 --- a/src/data/iconsets.ts +++ b/src/data/iconsets.ts @@ -1,4 +1,4 @@ -import { clear, get, set, Store } from "idb-keyval"; +import { clear, get, set, createStore } from "idb-keyval"; import { iconMetadata } from "../resources/icon-metadata"; import { IconMeta } from "../types"; @@ -10,11 +10,11 @@ export interface Chunks { [key: string]: Promise; } -export const iconStore = new Store("hass-icon-db", "mdi-icon-store"); +export const iconStore = createStore("hass-icon-db", "mdi-icon-store"); export const MDI_PREFIXES = ["mdi", "hass", "hassio", "hademo"]; -let toRead: Array<[string, (string) => void, () => void]> = []; +let toRead: Array<[string, (iconPath: string) => void, () => void]> = []; // Queue up as many icon fetches in 1 transaction export const getIcon = (iconName: string) => @@ -25,15 +25,13 @@ export const getIcon = (iconName: string) => return; } - const results: Array<[(string) => void, IDBRequest]> = []; + const results: Array<[(iconPath: string) => void, IDBRequest]> = []; - iconStore - ._withIDBStore("readonly", (store) => { - for (const [iconName_, resolve_] of toRead) { - results.push([resolve_, store.get(iconName_)]); - } - toRead = []; - }) + iconStore("readonly", (store) => { + for (const [iconName_, resolve_] of toRead) { + results.push([resolve_, store.get(iconName_)]); + } + }) .then(() => { for (const [resolve_, request] of results) { resolve_(request.result); @@ -44,11 +42,13 @@ export const getIcon = (iconName: string) => for (const [, , reject_] of toRead) { reject_(); } + }) + .finally(() => { toRead = []; }); }); -export const findIconChunk = (icon): string => { +export const findIconChunk = (icon: string): string => { let lastChunk: IconMeta; for (const chunk of iconMetadata.parts) { if (chunk.start !== undefined && icon < chunk.start) { @@ -63,7 +63,7 @@ export const writeCache = async (chunks: Chunks) => { const keys = Object.keys(chunks); const iconsSets: Icons[] = await Promise.all(Object.values(chunks)); // We do a batch opening the store just once, for (considerable) performance - iconStore._withIDBStore("readwrite", (store) => { + iconStore("readwrite", (store) => { iconsSets.forEach((icons, idx) => { Object.entries(icons).forEach(([name, path]) => { store.put(path, name); @@ -73,14 +73,13 @@ export const writeCache = async (chunks: Chunks) => { }); }; -export const checkCacheVersion = () => { - get("_version", iconStore).then((version) => { - if (!version) { - set("_version", iconMetadata.version, iconStore); - } else if (version !== iconMetadata.version) { - clear(iconStore).then(() => - set("_version", iconMetadata.version, iconStore) - ); - } - }); +export const checkCacheVersion = async () => { + const version = await get("_version", iconStore); + + if (!version) { + set("_version", iconMetadata.version, iconStore); + } else if (version !== iconMetadata.version) { + await clear(iconStore); + set("_version", iconMetadata.version, iconStore); + } }; diff --git a/yarn.lock b/yarn.lock index adc60c3e65..c0dfa50c46 100644 --- a/yarn.lock +++ b/yarn.lock @@ -7664,10 +7664,10 @@ iconv-lite@0.4.24, iconv-lite@^0.4.24, iconv-lite@^0.4.4: dependencies: safer-buffer ">= 2.1.2 < 3" -idb-keyval@^3.2.0: - version "3.2.0" - resolved "https://registry.yarnpkg.com/idb-keyval/-/idb-keyval-3.2.0.tgz#cbbf354deb5684b6cdc84376294fc05932845bd6" - integrity sha512-slx8Q6oywCCSfKgPgL0sEsXtPVnSbTLWpyiDcu6msHOyKOLari1TD1qocXVCft80umnkk3/Qqh3lwoFt8T/BPQ== +idb-keyval@^5.0.5: + version "5.0.5" + resolved "https://registry.yarnpkg.com/idb-keyval/-/idb-keyval-5.0.5.tgz#1b02984dcf42ad4aa290bf9f10935b485419df34" + integrity sha512-cqi65rrjhgPExI9vmSU7VcYEbHCUfIBY+9YUWxyr0PyGizptFgGFnvZQ0w+tqOXk1lUcGCZGVLfabf7QnR2S0g== ieee754@^1.1.4: version "1.1.13"