Skip to content

Commit

Permalink
fix(global): missing global key
Browse files Browse the repository at this point in the history
  • Loading branch information
Heargo committed Jul 21, 2024
1 parent b6968f1 commit 71b8ff1
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 6 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "perseform",
"version": "1.0.0",
"version": "1.0.1",
"description": "Allow to keep track of form states and inputs dependencies",
"main": "dist/index.js",
"module": "dist/index.mjs",
Expand Down
2 changes: 1 addition & 1 deletion src/db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ export async function getGlobalValueFromDb(
const store = transaction.objectStore("globalValue");
const getRequest = store.get(id);
getRequest.onsuccess = () => {
resolve(getRequest.result.value);
resolve(getRequest.result?.value);
};
getRequest.onerror = (event) => {
reject(event);
Expand Down
11 changes: 7 additions & 4 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,11 @@ export async function saveFormConfig(form: FormConfig): Promise<IDBValidKey> {
//create global state if needed
for (const key in form.inputsConfig) {
const input = form.inputsConfig[key]!;
const globalValue = getGlobalValueFromDb(input.globalKey!);
if (input.globalKey && !globalValue) {
await saveGlobalValue(input.globalKey, input.value);
if (input.globalKey) {
const globalValue = getGlobalValueFromDb(input.globalKey);
if (!globalValue) {
await saveGlobalValue(input.globalKey, input.value);
}
}
}
return save(form, "Config");
Expand Down Expand Up @@ -130,7 +132,8 @@ export async function getInputValue(
* @returns A promise that resolves to the retrieved value.
* @template T - The type of the value to retrieve.
*/
export async function getGlobalValue<T>(id: string): Promise<T> {
export async function getGlobalValue<T>(id: string): Promise<T | undefined> {
if (!id) return undefined;
return (await getGlobalValueFromDb(id)) as T;
}

Expand Down

0 comments on commit 71b8ff1

Please sign in to comment.