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

View File

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