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", "google-timezones-json": "^1.0.2",
"hls.js": "^1.0.3", "hls.js": "^1.0.3",
"home-assistant-js-websocket": "^5.10.0", "home-assistant-js-websocket": "^5.10.0",
"idb-keyval": "^3.2.0", "idb-keyval": "^5.0.5",
"intl-messageformat": "^9.6.13", "intl-messageformat": "^9.6.13",
"js-yaml": "^4.1.0", "js-yaml": "^4.1.0",
"leaflet": "^1.7.1", "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 { iconMetadata } from "../resources/icon-metadata";
import { IconMeta } from "../types"; import { IconMeta } from "../types";
@ -10,11 +10,11 @@ export interface Chunks {
[key: string]: Promise<Icons>; [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"]; 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 // Queue up as many icon fetches in 1 transaction
export const getIcon = (iconName: string) => export const getIcon = (iconName: string) =>
@ -25,14 +25,12 @@ export const getIcon = (iconName: string) =>
return; return;
} }
const results: Array<[(string) => void, IDBRequest]> = []; const results: Array<[(iconPath: string) => void, IDBRequest]> = [];
iconStore iconStore("readonly", (store) => {
._withIDBStore("readonly", (store) => {
for (const [iconName_, resolve_] of toRead) { for (const [iconName_, resolve_] of toRead) {
results.push([resolve_, store.get(iconName_)]); results.push([resolve_, store.get(iconName_)]);
} }
toRead = [];
}) })
.then(() => { .then(() => {
for (const [resolve_, request] of results) { for (const [resolve_, request] of results) {
@ -44,11 +42,13 @@ export const getIcon = (iconName: string) =>
for (const [, , reject_] of toRead) { for (const [, , reject_] of toRead) {
reject_(); reject_();
} }
})
.finally(() => {
toRead = []; toRead = [];
}); });
}); });
export const findIconChunk = (icon): string => { export const findIconChunk = (icon: string): string => {
let lastChunk: IconMeta; let lastChunk: IconMeta;
for (const chunk of iconMetadata.parts) { for (const chunk of iconMetadata.parts) {
if (chunk.start !== undefined && icon < chunk.start) { if (chunk.start !== undefined && icon < chunk.start) {
@ -63,7 +63,7 @@ export const writeCache = async (chunks: Chunks) => {
const keys = Object.keys(chunks); const keys = Object.keys(chunks);
const iconsSets: Icons[] = await Promise.all(Object.values(chunks)); const iconsSets: Icons[] = await Promise.all(Object.values(chunks));
// We do a batch opening the store just once, for (considerable) performance // We do a batch opening the store just once, for (considerable) performance
iconStore._withIDBStore("readwrite", (store) => { iconStore("readwrite", (store) => {
iconsSets.forEach((icons, idx) => { iconsSets.forEach((icons, idx) => {
Object.entries(icons).forEach(([name, path]) => { Object.entries(icons).forEach(([name, path]) => {
store.put(path, name); store.put(path, name);
@ -73,14 +73,13 @@ export const writeCache = async (chunks: Chunks) => {
}); });
}; };
export const checkCacheVersion = () => { export const checkCacheVersion = async () => {
get("_version", iconStore).then((version) => { const version = await get("_version", iconStore);
if (!version) { if (!version) {
set("_version", iconMetadata.version, iconStore); set("_version", iconMetadata.version, iconStore);
} else if (version !== iconMetadata.version) { } else if (version !== iconMetadata.version) {
clear(iconStore).then(() => await clear(iconStore);
set("_version", iconMetadata.version, 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: dependencies:
safer-buffer ">= 2.1.2 < 3" safer-buffer ">= 2.1.2 < 3"
idb-keyval@^3.2.0: idb-keyval@^5.0.5:
version "3.2.0" version "5.0.5"
resolved "https://registry.yarnpkg.com/idb-keyval/-/idb-keyval-3.2.0.tgz#cbbf354deb5684b6cdc84376294fc05932845bd6" resolved "https://registry.yarnpkg.com/idb-keyval/-/idb-keyval-5.0.5.tgz#1b02984dcf42ad4aa290bf9f10935b485419df34"
integrity sha512-slx8Q6oywCCSfKgPgL0sEsXtPVnSbTLWpyiDcu6msHOyKOLari1TD1qocXVCft80umnkk3/Qqh3lwoFt8T/BPQ== integrity sha512-cqi65rrjhgPExI9vmSU7VcYEbHCUfIBY+9YUWxyr0PyGizptFgGFnvZQ0w+tqOXk1lUcGCZGVLfabf7QnR2S0g==
ieee754@^1.1.4: ieee754@^1.1.4:
version "1.1.13" version "1.1.13"