fix: singleton IndexedDB connection to prevent cache check failures
This commit is contained in:
1 parent
fdbfcd4911
commit
9fb4d182e2
1 file changed
+23
-18
+23
-18
@@ -26,19 +26,24 @@ interface CacheEntry {
|
|||||||
ttl: number;
|
ttl: number;
|
||||||
}
|
}
|
||||||
|
|
||||||
function openDB(): Promise<IDBDatabase> {
|
let dbPromise: Promise<IDBDatabase> | null = null;
|
||||||
return new Promise((resolve, reject) => {
|
|
||||||
const request = indexedDB.open(DB_NAME, DB_VERSION);
|
function getDB(): Promise<IDBDatabase> {
|
||||||
request.onupgradeneeded = () => {
|
if (!dbPromise) {
|
||||||
const db = request.result;
|
dbPromise = new Promise((resolve, reject) => {
|
||||||
if (!db.objectStoreNames.contains(STORE_NAME)) {
|
const request = indexedDB.open(DB_NAME, DB_VERSION);
|
||||||
const store = db.createObjectStore(STORE_NAME, { keyPath: 'key' });
|
request.onupgradeneeded = () => {
|
||||||
store.createIndex('storedAt', 'storedAt', { unique: false });
|
const db = request.result;
|
||||||
}
|
if (!db.objectStoreNames.contains(STORE_NAME)) {
|
||||||
};
|
const store = db.createObjectStore(STORE_NAME, { keyPath: 'key' });
|
||||||
request.onsuccess = () => resolve(request.result);
|
store.createIndex('storedAt', 'storedAt', { unique: false });
|
||||||
request.onerror = () => reject(request.error);
|
}
|
||||||
});
|
};
|
||||||
|
request.onsuccess = () => resolve(request.result);
|
||||||
|
request.onerror = () => reject(request.error);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
return dbPromise;
|
||||||
}
|
}
|
||||||
|
|
||||||
export class DataCache {
|
export class DataCache {
|
||||||
@@ -48,7 +53,7 @@ export class DataCache {
|
|||||||
*/
|
*/
|
||||||
static async get(key: string): Promise<unknown | null> {
|
static async get(key: string): Promise<unknown | null> {
|
||||||
try {
|
try {
|
||||||
const db = await openDB();
|
const db = await getDB();
|
||||||
return new Promise<unknown | null>((resolve) => {
|
return new Promise<unknown | null>((resolve) => {
|
||||||
const tx = db.transaction(STORE_NAME, 'readonly');
|
const tx = db.transaction(STORE_NAME, 'readonly');
|
||||||
const store = tx.objectStore(STORE_NAME);
|
const store = tx.objectStore(STORE_NAME);
|
||||||
@@ -82,7 +87,7 @@ export class DataCache {
|
|||||||
*/
|
*/
|
||||||
static async set(key: string, data: unknown, ttl: number = DEFAULT_TTL_MS): Promise<void> {
|
static async set(key: string, data: unknown, ttl: number = DEFAULT_TTL_MS): Promise<void> {
|
||||||
try {
|
try {
|
||||||
const db = await openDB();
|
const db = await getDB();
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
const tx = db.transaction(STORE_NAME, 'readwrite');
|
const tx = db.transaction(STORE_NAME, 'readwrite');
|
||||||
const store = tx.objectStore(STORE_NAME);
|
const store = tx.objectStore(STORE_NAME);
|
||||||
@@ -107,7 +112,7 @@ export class DataCache {
|
|||||||
*/
|
*/
|
||||||
static async remove(key: string): Promise<void> {
|
static async remove(key: string): Promise<void> {
|
||||||
try {
|
try {
|
||||||
const db = await openDB();
|
const db = await getDB();
|
||||||
return new Promise((resolve) => {
|
return new Promise((resolve) => {
|
||||||
db.transaction(STORE_NAME, 'readwrite')
|
db.transaction(STORE_NAME, 'readwrite')
|
||||||
.objectStore(STORE_NAME)
|
.objectStore(STORE_NAME)
|
||||||
@@ -124,7 +129,7 @@ export class DataCache {
|
|||||||
*/
|
*/
|
||||||
static async clear(): Promise<void> {
|
static async clear(): Promise<void> {
|
||||||
try {
|
try {
|
||||||
const db = await openDB();
|
const db = await getDB();
|
||||||
return new Promise((resolve) => {
|
return new Promise((resolve) => {
|
||||||
db.transaction(STORE_NAME, 'readwrite')
|
db.transaction(STORE_NAME, 'readwrite')
|
||||||
.objectStore(STORE_NAME)
|
.objectStore(STORE_NAME)
|
||||||
@@ -157,7 +162,7 @@ export class DataCache {
|
|||||||
*/
|
*/
|
||||||
static async count(): Promise<number> {
|
static async count(): Promise<number> {
|
||||||
try {
|
try {
|
||||||
const db = await openDB();
|
const db = await getDB();
|
||||||
return new Promise((resolve) => {
|
return new Promise((resolve) => {
|
||||||
const tx = db.transaction(STORE_NAME, 'readonly');
|
const tx = db.transaction(STORE_NAME, 'readonly');
|
||||||
const store = tx.objectStore(STORE_NAME);
|
const store = tx.objectStore(STORE_NAME);
|
||||||
|
|||||||
Reference in new issue
Block a user