mirror of
https://github.com/vrtmrz/obsidian-livesync.git
synced 2025-12-28 00:51:33 +00:00
- Unexpected massive palallel running of file checking in boot sequence is solved. - Batch file change is not missing changes now. - Ignore changes caused by the plug-ins themselves. - Garbage collection is completely disabled. - Fixed sometimes fails initial replication after dropping local DB. Improved: - a bit more understandable messages - Save the file into the big chunk on initial scan. - Use history is always enabled. - Boot sequence got faster.
53 lines
1.6 KiB
TypeScript
53 lines
1.6 KiB
TypeScript
import { deleteDB, IDBPDatabase, openDB } from "idb";
|
|
export interface KeyValueDatabase {
|
|
get<T>(key: string): Promise<T>;
|
|
set<T>(key: string, value: T): Promise<IDBValidKey>;
|
|
del(key: string): Promise<void>;
|
|
clear(): Promise<void>;
|
|
keys(query?: IDBValidKey | IDBKeyRange, count?: number): Promise<IDBValidKey[]>;
|
|
close(): void;
|
|
destroy(): void;
|
|
}
|
|
const databaseCache: { [key: string]: IDBPDatabase<any> } = {};
|
|
export const OpenKeyValueDatabase = async (dbKey: string): Promise<KeyValueDatabase> => {
|
|
if (dbKey in databaseCache) {
|
|
databaseCache[dbKey].close();
|
|
delete databaseCache[dbKey];
|
|
}
|
|
const storeKey = dbKey;
|
|
const dbPromise = openDB(dbKey, 1, {
|
|
upgrade(db) {
|
|
db.createObjectStore(storeKey);
|
|
},
|
|
});
|
|
let db: IDBPDatabase<any> = null;
|
|
db = await dbPromise;
|
|
databaseCache[dbKey] = db;
|
|
return {
|
|
get<T>(key: string): Promise<T> {
|
|
return db.get(storeKey, key);
|
|
},
|
|
set<T>(key: string, value: T) {
|
|
return db.put(storeKey, value, key);
|
|
},
|
|
del(key: string) {
|
|
return db.delete(storeKey, key);
|
|
},
|
|
clear() {
|
|
return db.clear(storeKey);
|
|
},
|
|
keys(query?: IDBValidKey | IDBKeyRange, count?: number) {
|
|
return db.getAllKeys(storeKey, query, count);
|
|
},
|
|
close() {
|
|
delete databaseCache[dbKey];
|
|
return db.close();
|
|
},
|
|
async destroy() {
|
|
delete databaseCache[dbKey];
|
|
db.close();
|
|
await deleteDB(dbKey);
|
|
},
|
|
};
|
|
};
|