Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": "^3.13.1",
"leaflet": "^1.7.1",
Expand Down
45 changes: 22 additions & 23 deletions src/data/iconsets.ts
Original file line number Diff line number Diff line change
@@ -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";

Expand All @@ -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) =>
Expand All @@ -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);
Expand All @@ -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) {
Expand All @@ -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);
Expand All @@ -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);
}
};
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -7655,10 +7655,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"
Expand Down