Pinafore-Web-Client-Frontend/routes/_database/meta.js

58 lines
1.8 KiB
JavaScript
Raw Permalink Normal View History

2018-02-09 07:04:10 +01:00
import { dbPromise, getDatabase } from './databaseLifecycle'
import { META_STORE } from './constants'
import { metaCache, hasInCache, getInCache, setInCache } from './cache'
2018-02-09 07:29:29 +01:00
async function getMetaProperty (instanceName, key) {
2018-02-09 07:04:10 +01:00
if (hasInCache(metaCache, instanceName, key)) {
return getInCache(metaCache, instanceName, key)
}
const db = await getDatabase(instanceName)
let result = await dbPromise(db, META_STORE, 'readonly', (store, callback) => {
store.get(key).onsuccess = (e) => {
2018-03-09 08:18:18 +01:00
callback(e.target.result)
2018-02-09 07:04:10 +01:00
}
})
setInCache(metaCache, instanceName, key, result)
return result
}
2018-02-09 07:29:29 +01:00
async function setMetaProperty (instanceName, key, value) {
2018-02-09 07:04:10 +01:00
setInCache(metaCache, instanceName, key, value)
const db = await getDatabase(instanceName)
2018-02-09 07:29:29 +01:00
return dbPromise(db, META_STORE, 'readwrite', (store) => {
2018-03-09 08:18:18 +01:00
store.put(value, key)
2018-02-09 07:04:10 +01:00
})
}
2018-02-09 07:29:29 +01:00
export async function getInstanceVerifyCredentials (instanceName) {
return getMetaProperty(instanceName, 'verifyCredentials')
2018-02-09 07:04:10 +01:00
}
2018-02-09 07:29:29 +01:00
export async function setInstanceVerifyCredentials (instanceName, value) {
return setMetaProperty(instanceName, 'verifyCredentials', value)
2018-02-09 07:04:10 +01:00
}
2018-02-09 07:29:29 +01:00
export async function getInstanceInfo (instanceName) {
return getMetaProperty(instanceName, 'instance')
2018-02-09 07:04:10 +01:00
}
2018-02-09 07:29:29 +01:00
export async function setInstanceInfo (instanceName, value) {
return setMetaProperty(instanceName, 'instance', value)
2018-02-09 07:04:10 +01:00
}
2018-02-09 07:29:29 +01:00
export async function getLists (instanceName) {
return getMetaProperty(instanceName, 'lists')
2018-02-09 07:04:10 +01:00
}
2018-02-09 07:29:29 +01:00
export async function setLists (instanceName, value) {
return setMetaProperty(instanceName, 'lists', value)
}
2018-02-28 08:18:07 +01:00
export async function getCustomEmoji (instanceName) {
return getMetaProperty(instanceName, 'customEmoji')
}
export async function setCustomEmoji (instanceName, value) {
return setMetaProperty(instanceName, 'customEmoji', value)
}