@@ -44,6 +44,27 @@ async function idbInit(): Promise<void> {
44
44
} ) ;
45
45
}
46
46
47
+ async function idbTransaction (
48
+ table : string ,
49
+ mode : IDBTransactionMode ,
50
+ fn : ( objectStore : IDBObjectStore ) => IDBRequest < any > ,
51
+ ) : Promise < any > {
52
+ if ( ! idb ) {
53
+ await idbInit ( ) ;
54
+ }
55
+ return new Promise ( ( resolve , reject ) => {
56
+ const txn = idb ! . transaction ( [ table ] , mode ) ;
57
+ txn . onerror = reject ;
58
+
59
+ const objectStore = txn . objectStore ( table ) ;
60
+ const request = fn ( objectStore ) ;
61
+ request . onerror = reject ;
62
+ request . onsuccess = ( ) : void => {
63
+ resolve ( request . result ) ;
64
+ } ;
65
+ } ) ;
66
+ }
67
+
47
68
/**
48
69
* Loads an item from an IndexedDB table within the underlying `matrix-react-sdk` database.
49
70
*
@@ -57,17 +78,7 @@ export async function idbLoad(table: string, key: string | string[]): Promise<an
57
78
if ( ! idb ) {
58
79
await idbInit ( ) ;
59
80
}
60
- return new Promise ( ( resolve , reject ) => {
61
- const txn = idb ! . transaction ( [ table ] , "readonly" ) ;
62
- txn . onerror = reject ;
63
-
64
- const objectStore = txn . objectStore ( table ) ;
65
- const request = objectStore . get ( key ) ;
66
- request . onerror = reject ;
67
- request . onsuccess = ( event ) : void => {
68
- resolve ( request . result ) ;
69
- } ;
70
- } ) ;
81
+ return idbTransaction ( table , "readonly" , ( objectStore ) => objectStore . get ( key ) ) ;
71
82
}
72
83
73
84
/**
@@ -84,17 +95,7 @@ export async function idbSave(table: string, key: string | string[], data: any):
84
95
if ( ! idb ) {
85
96
await idbInit ( ) ;
86
97
}
87
- return new Promise ( ( resolve , reject ) => {
88
- const txn = idb ! . transaction ( [ table ] , "readwrite" ) ;
89
- txn . onerror = reject ;
90
-
91
- const objectStore = txn . objectStore ( table ) ;
92
- const request = objectStore . put ( data , key ) ;
93
- request . onerror = reject ;
94
- request . onsuccess = ( event ) : void => {
95
- resolve ( ) ;
96
- } ;
97
- } ) ;
98
+ return idbTransaction ( table , "readwrite" , ( objectStore ) => objectStore . put ( data , key ) ) ;
98
99
}
99
100
100
101
/**
@@ -110,15 +111,20 @@ export async function idbDelete(table: string, key: string | string[]): Promise<
110
111
if ( ! idb ) {
111
112
await idbInit ( ) ;
112
113
}
113
- return new Promise ( ( resolve , reject ) => {
114
- const txn = idb ! . transaction ( [ table ] , "readwrite" ) ;
115
- txn . onerror = reject ;
114
+ return idbTransaction ( table , "readwrite" , ( objectStore ) => objectStore . delete ( key ) ) ;
115
+ }
116
116
117
- const objectStore = txn . objectStore ( table ) ;
118
- const request = objectStore . delete ( key ) ;
119
- request . onerror = reject ;
120
- request . onsuccess = ( ) : void => {
121
- resolve ( ) ;
122
- } ;
123
- } ) ;
117
+ /**
118
+ * Clears all records from an IndexedDB table within the underlying `matrix-react-sdk` database.
119
+ *
120
+ * If IndexedDB access is not supported in the environment, an error is thrown.
121
+ *
122
+ * @param {string } table The name of the object store where the records are stored.
123
+ * @returns {Promise<void> } A Promise that resolves when the record(s) have been successfully deleted.
124
+ */
125
+ export async function idbClear ( table : string ) : Promise < void > {
126
+ if ( ! idb ) {
127
+ await idbInit ( ) ;
128
+ }
129
+ return idbTransaction ( table , "readwrite" , ( objectStore ) => objectStore . clear ( ) ) ;
124
130
}
0 commit comments