Bram Kragten 2021-05-26 10:22:38 +02:00 committed by GitHub
parent 3cc4628d03
commit c4a8899780
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 27 additions and 28 deletions

View File

@ -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",

View File

@ -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<Icons>;
}
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);
}
};

View File

@ -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"