parent
fa8ae33d15
commit
ba6ef75747
@ -0,0 +1,91 @@ |
||||
/* |
||||
Copyright 2019 New Vector Ltd |
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License"); |
||||
you may not use this file except in compliance with the License. |
||||
You may obtain a copy of the License at |
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software |
||||
distributed under the License is distributed on an "AS IS" BASIS, |
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
See the License for the specific language governing permissions and |
||||
limitations under the License. |
||||
*/ |
||||
|
||||
// just *accessing* indexedDB throws an exception in firefox if disabled.
|
||||
let indexedDB; |
||||
try { |
||||
indexedDB = window.indexedDB; |
||||
} catch (e) {} |
||||
|
||||
let idb = null; |
||||
|
||||
async function idbInit() { |
||||
if (!indexedDB) { |
||||
throw new Error("IndexedDB not available"); |
||||
} |
||||
idb = await new Promise((resolve, reject) => { |
||||
const request = indexedDB.open("matrix-react-sdk", 1); |
||||
request.onerror = reject; |
||||
request.onsuccess = event => { |
||||
resolve(request.result); |
||||
}; |
||||
request.onupgradeneeded = event => { |
||||
const db = request.result; |
||||
db.createObjectStore("account"); |
||||
}; |
||||
}); |
||||
} |
||||
|
||||
export async function idbLoad(table, key) { |
||||
if (!idb) { |
||||
await idbInit(); |
||||
} |
||||
return new Promise((resolve, reject) => { |
||||
const txn = idb.transaction([table], "readonly"); |
||||
txn.onerror = reject; |
||||
|
||||
const objectStore = txn.objectStore(table); |
||||
const request = objectStore.get(key); |
||||
request.onerror = reject; |
||||
request.onsuccess = event => { |
||||
resolve(request.result); |
||||
}; |
||||
}); |
||||
} |
||||
|
||||
export async function idbSave(table: string, key: string | string[], data: any): Promise<void> { |
||||
if (!idb) { |
||||
await idbInit(); |
||||
} |
||||
return new Promise((resolve, reject) => { |
||||
const txn = idb.transaction([table], "readwrite"); |
||||
txn.onerror = reject; |
||||
|
||||
const objectStore = txn.objectStore(table); |
||||
const request = objectStore.put(data, key); |
||||
request.onerror = reject; |
||||
request.onsuccess = event => { |
||||
resolve(); |
||||
}; |
||||
}); |
||||
} |
||||
|
||||
export async function idbDelete(table: string, key: string | string[]): Promise<void> { |
||||
if (!idb) { |
||||
await idbInit(); |
||||
} |
||||
return new Promise((resolve, reject) => { |
||||
const txn = idb.transaction([table], "readwrite"); |
||||
txn.onerror = reject; |
||||
|
||||
const objectStore = txn.objectStore(table); |
||||
const request = objectStore.delete(key); |
||||
request.onerror = reject; |
||||
request.onsuccess = event => { |
||||
resolve(); |
||||
}; |
||||
}); |
||||
} |
Loading…
Reference in new issue