This commit is contained in:
adueck 2024-08-14 14:51:15 -04:00
parent e95116e1b2
commit d0e1a71827
2 changed files with 12 additions and 4 deletions

View File

@ -17,7 +17,7 @@ export class DictionaryDb {
private ready = false; private ready = false;
// eslint-disable-next-line // eslint-disable-next-line
public collection: Collection<any>; public collection: Collection<any> | undefined;
constructor(options: { constructor(options: {
url: string; url: string;
@ -82,6 +82,9 @@ export class DictionaryDb {
private async addDictionaryToLoki(dictionary: T.Dictionary): Promise<"done"> { private async addDictionaryToLoki(dictionary: T.Dictionary): Promise<"done"> {
return await new Promise((resolve: (value: "done") => void, reject) => { return await new Promise((resolve: (value: "done") => void, reject) => {
if (!this.collection) {
reject("dictionary not initialized");
}
// Add it to Lokijs // Add it to Lokijs
this.collection = this.lokidb.addCollection( this.collection = this.lokidb.addCollection(
this.dictionaryCollectionName, this.dictionaryCollectionName,
@ -201,8 +204,10 @@ export class DictionaryDb {
notifyUpdateComing(); notifyUpdateComing();
this.ready = false; this.ready = false;
localStorage.removeItem(this.dictionaryInfoLocalStorageKey); localStorage.removeItem(this.dictionaryInfoLocalStorageKey);
this.collection.clear(); if (this.collection) {
this.lokidb.removeCollection(this.dictionaryCollectionName); this.collection.clear();
this.lokidb.removeCollection(this.dictionaryCollectionName);
}
await (async () => { await (async () => {
return new Promise((resolve: (value: "done") => void) => { return new Promise((resolve: (value: "done") => void) => {
this.lokidb.saveDatabase(() => { this.lokidb.saveDatabase(() => {
@ -225,7 +230,7 @@ export class DictionaryDb {
*/ */
// TODO: not working in app usage now now with new 'this' issues // TODO: not working in app usage now now with new 'this' issues
public findOneByTs(ts: number): T.DictionaryEntry | undefined { public findOneByTs(ts: number): T.DictionaryEntry | undefined {
if (!this.ready) { if (!this.ready || !this.collection) {
return undefined; return undefined;
} }
const res = this.collection.by("ts", ts); const res = this.collection.by("ts", ts);

View File

@ -14,6 +14,9 @@ const dictDb = new DictionaryDb({
}); });
function queryP(p: string): T.DictionaryEntry[] { function queryP(p: string): T.DictionaryEntry[] {
if (!dictDb.collection) {
return [];
}
return dictDb.collection.find({ p }); return dictDb.collection.find({ p });
} }
const memoizedQueryP = queryP; const memoizedQueryP = queryP;