Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Try to avoid corruption of the settings stored IndexedDB #921

Merged
merged 2 commits into from
Dec 23, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 30 additions & 22 deletions src/r2mm/manager/SettingsDexieStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export default class SettingsDexieStore extends Dexie {
}

public async getLatestGlobal(): Promise<ManagerSettingsInterfaceGlobal_V2> {
return this.global.toArray().then(result => {
return this.global.toArray().then(async result => {
if (result.length > 0) {
const globalEntry = result[result.length - 1];
const parsed = JSON.parse(globalEntry.settings);
Expand All @@ -52,22 +52,22 @@ export default class SettingsDexieStore extends Dexie {
} else {
// Is legacy.
const legacyToV2 = this.mapLegacyToV2(parsed, this.activeGame);
this.global.put({ settings: JSON.stringify(legacyToV2.global) });
this.gameSpecific.put({ settings: JSON.stringify(legacyToV2.gameSpecific) });
await this.global.put({ settings: JSON.stringify(legacyToV2.global) });
await this.gameSpecific.put({ settings: JSON.stringify(legacyToV2.gameSpecific) });
return legacyToV2.global;
}
} else {
ManagerSettings.NEEDS_MIGRATION = true;
const obj = this.createNewSettingsInstance();
this.global.put({ settings: JSON.stringify(obj.global) });
this.gameSpecific.put({ settings: JSON.stringify(obj.gameSpecific) });
await this.global.put({ settings: JSON.stringify(obj.global) });
await this.gameSpecific.put({ settings: JSON.stringify(obj.gameSpecific) });
return obj.global;
}
});
}

public async getLatestGameSpecific(): Promise<ManagerSettingsInterfaceGame_V2> {
return this.gameSpecific.toArray().then(result => {
return this.gameSpecific.toArray().then(async result => {
if (result.length > 0) {
const globalEntry = result[result.length - 1];
const parsed = JSON.parse(globalEntry.settings);
Expand All @@ -80,19 +80,23 @@ export default class SettingsDexieStore extends Dexie {
}
} else {
const obj = this.createNewSettingsInstance();
this.gameSpecific.put({ settings: JSON.stringify(obj.gameSpecific) });
await this.gameSpecific.put({ settings: JSON.stringify(obj.gameSpecific) });
return obj.gameSpecific;
}
});
}

public async getLatest(): Promise<ManagerSettingsInterfaceHolder> {
const latestGlobal = await this.getLatestGlobal();
const latestGameSpecific = await this.getLatestGameSpecific();
return {
global: latestGlobal,
gameSpecific: latestGameSpecific
const get = async () => {
const latestGlobal = await this.getLatestGlobal();
const latestGameSpecific = await this.getLatestGameSpecific();
return {
global: latestGlobal,
gameSpecific: latestGameSpecific
};
};

return await this.transaction("rw!", this.global, this.gameSpecific, get);
}

private createNewSettingsInstance(): ManagerSettingsInterfaceHolder {
Expand Down Expand Up @@ -125,16 +129,20 @@ export default class SettingsDexieStore extends Dexie {
}

public async save(holder: ManagerSettingsInterfaceHolder) {
await this.global.toArray().then(result => {
for (let settingsInterface of result) {
this.global.update(settingsInterface.id!, {settings: JSON.stringify(holder.global)});
}
});
await this.gameSpecific.toArray().then(result => {
for (let settingsInterface of result) {
this.gameSpecific.update(settingsInterface.id!, {settings: JSON.stringify(holder.gameSpecific)});
}
});
const update = async () => {
await this.global.toArray().then(async result => {
for (let settingsInterface of result) {
await this.global.update(settingsInterface.id!, {settings: JSON.stringify(holder.global)});
}
});
await this.gameSpecific.toArray().then(async result => {
for (let settingsInterface of result) {
await this.gameSpecific.update(settingsInterface.id!, {settings: JSON.stringify(holder.gameSpecific)});
}
});
}

await this.transaction("rw!", this.global, this.gameSpecific, update);
}

private mapLegacyToV2(itf: ManagerSettingsInterface_Legacy, game: Game): ManagerSettingsInterfaceHolder {
Expand Down