Skip to content

Commit 060f99c

Browse files
committed
current text in indexedDb
1 parent 3446716 commit 060f99c

File tree

2 files changed

+27
-14
lines changed

2 files changed

+27
-14
lines changed

frontend/src/ts/ready.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import Konami from "konami";
88
import * as ServerConfiguration from "./ape/server-configuration";
99
import { getActiveFunboxesWithFunction } from "./test/funbox/list";
1010
import { loadPromise } from "./config";
11+
import { initCurrentText } from "./test/custom-text";
1112

1213
$(async (): Promise<void> => {
1314
await loadPromise;
@@ -67,4 +68,6 @@ $(async (): Promise<void> => {
6768
});
6869
}
6970
}
71+
72+
await initCurrentText();
7073
});

frontend/src/ts/test/custom-text.ts

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,14 @@ const defaultCustomTextSettings: CustomTextSettings = {
2525
pipeDelimiter: false,
2626
};
2727

28+
let cachedCurrentText = defaultCustomTextSettings.text;
29+
export async function initCurrentText(): Promise<void> {
30+
const db = await getDB();
31+
cachedCurrentText =
32+
(await db.get("currentText", "_currentText_")) ??
33+
defaultCustomTextSettings.text;
34+
}
35+
2836
const customTextSettings = new LocalStorageWithSchema({
2937
key: "customTextSettings",
3038
schema: CustomTextSettingsSchema,
@@ -59,6 +67,7 @@ type CustomTextDB = DBSchema & {
5967
progress: number;
6068
};
6169
};
70+
currentText: { key: "_currentText_"; value: string[] };
6271
};
6372

6473
export async function getDB(): Promise<IDBPDatabase<CustomTextDB>> {
@@ -89,11 +98,11 @@ export async function getDB(): Promise<IDBPDatabase<CustomTextDB>> {
8998
//create objectStores
9099
await db.createObjectStore("customTexts");
91100
await db.createObjectStore("customLongTexts");
92-
//await db.createObjectStore("currentSettings");
101+
await db.createObjectStore("currentText");
93102

94103
const ctStore = tx.objectStore("customTexts");
95104
const longCtStore = tx.objectStore("customLongTexts");
96-
//const currentSettingsStore = tx.objectStore("currentSettings");
105+
const currentTextStore = tx.objectStore("currentText");
97106

98107
//copy from old localStorage
99108
await Promise.all([
@@ -103,10 +112,7 @@ export async function getDB(): Promise<IDBPDatabase<CustomTextDB>> {
103112
...Object.entries(customTextLongLS.get()).map(async ([key, value]) =>
104113
longCtStore.add({ text: value.text, progress: value.progress }, key)
105114
),
106-
/* currentSettingsStore.put(
107-
customTextSettings.get(),
108-
"_currentSettings_"
109-
),*/
115+
currentTextStore.put(customTextSettings.get().text, "_currentText_"),
110116
tx.done,
111117
]);
112118

@@ -115,6 +121,10 @@ export async function getDB(): Promise<IDBPDatabase<CustomTextDB>> {
115121
//customTextLS.destroy();
116122
//customTextLongLS.destroy();
117123
//customTextSettings.destroy();
124+
customTextSettings.set({
125+
...customTextSettings.get(),
126+
text: ["outdated"],
127+
});
118128
}
119129
},
120130
});
@@ -124,19 +134,19 @@ window.globalThis["db"] = {
124134
get: getDB,
125135
getText: getCustomText,
126136
setText: setCustomText,
137+
getData: getData,
127138
};
128139

129140
export function getText(): string[] {
130-
return customTextSettings.get().text;
141+
return cachedCurrentText;
131142
}
132143

133144
export function setText(txt: string[]): void {
134-
const currentSettings = customTextSettings.get();
135-
customTextSettings.set({
136-
...currentSettings,
137-
text: txt,
138-
limit: { value: txt.length, mode: currentSettings.limit.mode },
139-
});
145+
cachedCurrentText = txt;
146+
setTimeout(async () => {
147+
const db = await getDB();
148+
await db.put("currentText", cachedCurrentText, "_currentText_");
149+
}, 0);
140150
}
141151

142152
export function getMode(): CustomTextMode {
@@ -197,7 +207,7 @@ export function setPipeDelimiter(val: boolean): void {
197207
}
198208

199209
export function getData(): CustomTextSettings {
200-
return customTextSettings.get();
210+
return { ...customTextSettings.get(), text: cachedCurrentText };
201211
}
202212

203213
export async function getCustomText(

0 commit comments

Comments
 (0)