Skip to content

Commit

Permalink
fix: add indexeddb storage debug information
Browse files Browse the repository at this point in the history
  • Loading branch information
Ruslan Garifullin committed Mar 12, 2023
1 parent 4c8aebe commit a09d1c4
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 5 deletions.
2 changes: 1 addition & 1 deletion misc/bin/linux-sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
if [[ "$1" == *.env ]]
then
echo "Loading environment file $1..."
if [[ -f "$FILE" ]]
if [[ -f "$1" ]]
then
export $(cat $1)
bash -c "$2"
Expand Down
3 changes: 2 additions & 1 deletion packages/js/shared/web2-common/src/api/http.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
import axios, { AxiosResponse } from "axios";
import { getRightOrFail } from "../io-ts-utils";
import { ApiResponse } from "../types/api-response.type";
import { Logger } from "../utils";
import { HttpInstance } from "./http.types";
import { ApiResponse } from "../types/api-response.type";

let baseUrl: string | undefined;
let jwtToken: string | undefined;
Expand Down Expand Up @@ -42,5 +42,6 @@ export function setJwtToken(token: string) {
}

export function setBaseUrl(url: string) {
debug("Base URL set to", url);
baseUrl = url;
}
11 changes: 11 additions & 0 deletions packages/js/shared/web2-common/src/utils/format-bytes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
const units = ["bytes", "KiB", "MiB", "GiB", "TiB", "PiB", "EiB", "ZiB", "YiB"];

export function formatBytes(x: number) {
let l = 0, n = x || 0;

while (n >= 1024 && ++l) {
n = n / 1024;
}

return (n.toFixed(n < 10 && l > 0 ? 1 : 0) + " " + units[l]);
}
1 change: 1 addition & 0 deletions packages/js/shared/web2-common/src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export * from "./async-action";
export * from "./error";
export * from "./format-bytes";
export * from "./friendly-error";
export * from "./logger";
export * from "./temporal";
4 changes: 2 additions & 2 deletions packages/js/web3/frontend/src/App2.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,12 @@ export const AppRoot = observer(function App() {
dbService.on("ready", () => setIsDbLoading(false));
}, []);

if (isDbLoading || !hasWalletsListEverLoaded)
/* if (isDbLoading || !hasWalletsListEverLoaded)
return (
<Stack alignItems="center" justifyContent="center" style={{ height: "100vh" }}>
<CircularProgress />
</Stack>
);
); */

if (!hasWallets && !isWalletsListLoading)
return <SignInPage />;
Expand Down
15 changes: 14 additions & 1 deletion packages/js/web3/frontend/src/services/db.service.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Logger } from "@hdapp/shared/web2-common/utils";
import { Logger, formatBytes } from "@hdapp/shared/web2-common/utils";
import EventEmitter from "events";

const dbName = "hdapp-pwa-frontend";
Expand Down Expand Up @@ -32,7 +32,14 @@ export class DbService {
this._consumers.push(consumer);
}

private async _calculateStorage() {
const storageSize = await navigator.storage.estimate();
debug("Using", formatBytes(storageSize.usage ?? -1), "out of", formatBytes(storageSize.quota ?? -1));
}

private _requestDb() {
void this._calculateStorage();
debug("Opening IndexedDB");
const request = indexedDB.open(dbName, 3);
request.addEventListener(
"success",
Expand All @@ -42,6 +49,12 @@ export class DbService {
this._emit("ready");
}
);
request.addEventListener(
"error",
err => {
error("IndexedDB initialization failed.", err);
}
);
request.addEventListener(
"upgradeneeded",
event => {
Expand Down

0 comments on commit a09d1c4

Please sign in to comment.