Skip to content

Versioned Preference System #1255

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

Draft
wants to merge 1 commit into
base: dev
Choose a base branch
from
Draft
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion fission/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "synthesis-fission",
"private": false,
"version": "0.0.1",
"version": "7.2.0",
"type": "module",
"scripts": {
"init": "(bun run assetpack && bun run playwright:install) || (npm run assetpack && npm run playwright:install)",
Expand Down
27 changes: 27 additions & 0 deletions fission/src/systems/preferences/PreferenceMigrations.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@

import { Preferences } from "./PreferenceTypes"

type Migration = (prefs: any) => any

// Migrations are written to be forward-compatible
const migrations: { [key: string]: Migration } = {
"7.2.0": prefs => {
prefs.version = "7.2.0"
return prefs
},
}

export function migratePreferences(prefs: Partial<Preferences>): Partial<Preferences> {
const version = prefs.version ?? "0.0.0"

const versionsToMigrate = Object.keys(migrations).sort()

let migratedPrefs = { ...prefs }
for (const v of versionsToMigrate) {
if (version < v) {
migratedPrefs = migrations[v](migratedPrefs)
}
}

return migratedPrefs
}
1 change: 1 addition & 0 deletions fission/src/systems/preferences/PreferenceTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export type GlobalPreferences = {
export type GlobalPreference = keyof GlobalPreferences

export type Preferences = GlobalPreferences & {
version: string
[ROBOT_PREFERENCE_KEY]: Record<string, RobotPreferences>
[FIELD_PREFERENCE_KEY]: Record<string, FieldPreferences>
[MOTOR_PREFERENCES_KEY]: Record<string, MotorPreferences>
Expand Down
10 changes: 7 additions & 3 deletions fission/src/systems/preferences/PreferencesSystem.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { migratePreferences } from "./PreferenceMigrations"
import { version } from "../../../package.json"
import {
defaultFieldPreferences,
defaultGlobalPreferences,
Expand Down Expand Up @@ -210,15 +212,17 @@ class PreferencesSystem {
const loadedPrefs = window.localStorage.getItem(this._localStorageKey)

if (loadedPrefs == undefined) {
this._preferences = {}
this._preferences = { version: version }
return
}

try {
this._preferences = JSON.parse(loadedPrefs)
const parsed = JSON.parse(loadedPrefs)
this._preferences = migratePreferences(parsed)
this._preferences.version = version
} catch (e) {
console.error(e)
this._preferences = {}
this._preferences = { version: version }
}
}

Expand Down
Loading