mirror of
https://github.com/naturalcrit/homebrewery.git
synced 2026-01-08 20:23:39 +00:00
Add IDB Proxy
This commit is contained in:
@@ -1,5 +1,40 @@
|
|||||||
import * as IDB from 'idb-keyval/dist/index.js';
|
import * as IDB from 'idb-keyval/dist/index.js';
|
||||||
|
|
||||||
|
// The Proxy and the Wrapper _should_ be equivalent
|
||||||
|
|
||||||
|
// IndexedDB Proxy
|
||||||
|
|
||||||
|
export function initIDBProxy(db, store) {
|
||||||
|
|
||||||
|
const PROP_LIST = [
|
||||||
|
'entries',
|
||||||
|
'keys',
|
||||||
|
'values',
|
||||||
|
'clear',
|
||||||
|
'get',
|
||||||
|
'getMany',
|
||||||
|
'set',
|
||||||
|
'setMany',
|
||||||
|
'update',
|
||||||
|
'del',
|
||||||
|
'delMany'
|
||||||
|
];
|
||||||
|
|
||||||
|
const IDBHandler = {
|
||||||
|
get : (target, prop)=>{
|
||||||
|
if(!PROP_LIST.includes(prop)){ return target[prop]; }
|
||||||
|
return function (...args) {
|
||||||
|
return target[prop].apply(target, [...args, target.createStore(db, store)]);
|
||||||
|
};
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
return new Proxy(IDB, IDBHandler);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// IndexedDB Wrapper
|
||||||
|
|
||||||
export function initCustomStore(db, store){
|
export function initCustomStore(db, store){
|
||||||
const createCustomStore = async ()=>{
|
const createCustomStore = async ()=>{
|
||||||
return await IDB.createStore(db, store);
|
return await IDB.createStore(db, store);
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
import { initCustomStore } from './customIDBStoreWrapper';
|
import { initIDBProxy } from './customIDBStoreWrapper';
|
||||||
|
|
||||||
export const HISTORY_PREFIX = 'HOMEBREWERY-HISTORY';
|
export const HISTORY_PREFIX = 'HOMEBREWERY-HISTORY';
|
||||||
export const HISTORY_SLOTS = 5;
|
export const HISTORY_SLOTS = 5;
|
||||||
@@ -28,7 +28,7 @@ const GARBAGE_COLLECT_DELAY = 28 * 24 * 60;
|
|||||||
const HB_DB = 'HOMEBREWERY-DB';
|
const HB_DB = 'HOMEBREWERY-DB';
|
||||||
const HB_STORE = 'HISTORY';
|
const HB_STORE = 'HISTORY';
|
||||||
|
|
||||||
const IDB = initCustomStore(HB_DB, HB_STORE);
|
const IDB = initIDBProxy(HB_DB, HB_STORE);
|
||||||
|
|
||||||
function getKeyBySlot(brew, slot){
|
function getKeyBySlot(brew, slot){
|
||||||
// Return a string representing the key for this brew and history slot
|
// Return a string representing the key for this brew and history slot
|
||||||
|
|||||||
Reference in New Issue
Block a user