This commit is contained in:
Manav Rathi 2024-06-27 20:04:02 +05:30
parent 640fd48e70
commit d33ba285a6
No known key found for this signature in database
7 changed files with 20 additions and 34 deletions

View File

@ -172,9 +172,7 @@ For more details, see [translations.md](translations.md).
layer on top of web workers to make them more easier to use.
- [idb](https://github.com/jakearchibald/idb) provides a promise API over the
browser-native IndexedDB APIs, and is used as our primary tabular database.
[idb-keyval](https://github.com/jakearchibald/idb-keyval) is its sibling
library that we use for ad-hoc key value storage.
browser-native IndexedDB APIs.
> For more details about IDB and its role, see [storage.md](storage.md).

View File

@ -28,7 +28,8 @@ IndexedDB is a transactional NoSQL store provided by browsers. It has quite
large storage limits, and data is stored per origin (and remains persistent
across tab restarts).
Unlike local storage, IndexedDB is also accessible from web workers.
Unlike local storage, IndexedDB is also accessible from web workers and so we
also use IndexedDB for storing ad-hoc key value pairs.
Older code used the LocalForage library for storing things in Indexed DB. This
library falls back to localStorage in case Indexed DB storage is not available.
@ -41,13 +42,6 @@ For more details, see:
- https://web.dev/articles/indexeddb
- https://github.com/jakearchibald/idb
## IndexedDB KV
We earlier used local storage for ad-hoc key value storage, but local storage is
not accessible from web workers which we use quite a bit. So now we use _idb_'s
sibling libary, idb-keyval, for storing key value pairs that need to be accessed
from both the main thread and web workers.
## OPFS
OPFS is used for caching entire files when we're running under Electron (the Web

View File

@ -1,11 +1,11 @@
import { clearBlobCaches } from "@/next/blob-cache";
import { clearHTTPState } from "@/next/http";
import { clearKVDB } from "@/next/kv";
import log from "@/next/log";
import InMemoryStore from "@ente/shared/storage/InMemoryStore";
import localForage from "@ente/shared/storage/localForage";
import { clearData } from "@ente/shared/storage/localStorage";
import { clearKeys } from "@ente/shared/storage/sessionStorage";
import { clear as clearKV } from "idb-keyval";
import { logout as remoteLogout } from "../api/user";
/**
@ -25,41 +25,41 @@ export const accountLogout = async () => {
try {
await remoteLogout();
} catch (e) {
ignoreError("remote", e);
ignoreError("Remote", e);
}
try {
InMemoryStore.clear();
} catch (e) {
ignoreError("in-memory store", e);
ignoreError("In-memory store", e);
}
try {
clearKeys();
} catch (e) {
ignoreError("session store", e);
ignoreError("Session storage", e);
}
try {
clearData();
} catch (e) {
ignoreError("local storage", e);
ignoreError("Local storage", e);
}
try {
await localForage.clear();
} catch (e) {
ignoreError("local forage", e);
ignoreError("Local forage", e);
}
try {
await clearBlobCaches();
} catch (e) {
ignoreError("cache", e);
ignoreError("Blob cache", e);
}
try {
clearHTTPState();
} catch (e) {
ignoreError("http", e);
ignoreError("HTTP", e);
}
try {
await clearKV();
await clearKVDB();
} catch (e) {
ignoreError("kv", e);
ignoreError("KV DB", e);
}
};

View File

@ -1,3 +1,4 @@
import { getKV, removeKV, setKV } from "@/next/kv";
import log from "@/next/log";
import InfoOutlinedIcon from "@mui/icons-material/InfoOutlined";
import {
@ -14,7 +15,6 @@ import {
} from "@mui/material";
import { useFormik } from "formik";
import { t } from "i18next";
import { del, get, set } from "idb-keyval";
import React, { useEffect, useState } from "react";
import { z } from "zod";
import { FocusVisibleButton } from "./FocusVisibleButton";
@ -68,7 +68,7 @@ const Contents: React.FC<ContentsProps> = (props) => {
useEffect(
() =>
void get<string>("apiOrigin").then((o) =>
void getKV("apiOrigin").then((o) =>
setInitialAPIOrigin(
// TODO: Migration of apiOrigin from local storage to indexed DB
// Remove me after a bit (27 June 2024).
@ -213,7 +213,7 @@ const Form: React.FC<FormProps> = ({ initialAPIOrigin, onClose }) => {
*/
const updateAPIOrigin = async (origin: string) => {
if (!origin) {
await del("apiOrigin");
await removeKV("apiOrigin");
// TODO: Migration of apiOrigin from local storage to indexed DB
// Remove me after a bit (27 June 2024).
localStorage.removeItem("apiOrigin");
@ -230,7 +230,7 @@ const updateAPIOrigin = async (origin: string) => {
throw new Error("Invalid response");
}
await set("apiOrigin", origin);
await setKV("apiOrigin", origin);
};
const PingResponse = z.object({

View File

@ -1,4 +1,4 @@
import { get, set } from "idb-keyval";
import { getKV, setKV } from "@/next/kv";
/**
* Return the origin (scheme, host, port triple) that should be used for making
@ -35,14 +35,14 @@ export const apiURL = async (path: string) => (await apiOrigin()) + path;
* Otherwise return undefined.
*/
export const customAPIOrigin = async () => {
let origin = await get<string>("apiOrigin");
let origin = await getKV("apiOrigin");
if (!origin) {
// TODO: Migration of apiOrigin from local storage to indexed DB
// Remove me after a bit (27 June 2024).
const legacyOrigin = localStorage.getItem("apiOrigin");
if (legacyOrigin !== null) {
origin = legacyOrigin;
if (origin) await set("apiOrigin", origin);
if (origin) await setKV("apiOrigin", origin);
localStorage.removeItem("apiOrigin");
}
}

View File

@ -11,7 +11,6 @@
"get-user-locale": "^2.3",
"i18next": "^23.10",
"i18next-resources-to-backend": "^1.2.0",
"idb-keyval": "^6",
"is-electron": "^2.2",
"next": "^14.1",
"react": "^18",

View File

@ -2922,11 +2922,6 @@ i18next@^23.10:
dependencies:
"@babel/runtime" "^7.23.2"
idb-keyval@^6:
version "6.2.1"
resolved "https://registry.yarnpkg.com/idb-keyval/-/idb-keyval-6.2.1.tgz#94516d625346d16f56f3b33855da11bfded2db33"
integrity sha512-8Sb3veuYCyrZL+VBt9LJfZjLUPWVvqn8tG28VqYNFCo43KHcKuq+b4EiXGeuaLAQWL2YmyDgMp2aSpH9JHsEQg==
idb@^8:
version "8.0.0"
resolved "https://registry.yarnpkg.com/idb/-/idb-8.0.0.tgz#33d7ed894ed36e23bcb542fb701ad579bfaad41f"