Skip to content

Conversation

@alexkuhn
Copy link

This commit adds versioning to local storage entries, so that when version is more recent than current version, business code can have a strategy to drop the values.

This strategy is the one used by local storage field: older versions are most updated and upgraded fields so 19.1 is implicitly valid for 19.2 and 19.3. 20.0 is invalid as it can potentially be affected by unknown upgrade scripts of current version (19.3), thus it's dropped.

Part of Task-5003012

@robodoo
Copy link

robodoo commented Nov 10, 2025

This PR targets the un-managed branch odoo-dev/odoo:master-field-local-storage-aku, it needs to be retargeted before it can be merged.

Before this commit, having a record field saved in local storage
and synced among browser tabs was cumbersome. Roughly this was
defined as follow:

```js
class Settings extends Record {
    static new(...args) {
        const settings = super.new(...args);
        settings.onStorage = settings.onStorage.bind(settings);
        browser.addEventListener("storage", onStorage);
        return settings;
    }
    delete() {
        super.delete();
        browser.removeEventListener("storage", onStorage);
    }
    onStorage(ev) {
        if (ev.key === USE_BLUR_KEY) {
             this.useBlur = ev.newValue === "true";
        }
    }
    useBlur = fields.Attr(false, {
        compute: () => browser.localStorage.getItem(USE_BLUR_KEY) === "true",
        onUpdate: () =>
            this.useBlur
                ? browser.localStorage.seItem(USE_BLUR_KEY, "true")
                : browser.localStorage.removeItem(USE_BLUR_KEY),
    });
}
```

This commit simplifies drastically how to define fields that are
saved and synced to local storage:

```js
class Settings extends Record {
    useBlur = fields.Attr(false, { localStorage: true });
}
```

The write in local storage is made only when the field value differs
from default:

- if value is same as default value, then remove from local storage
- if value is different than default value, write current value in
local storage

This commit also introduces local storage upgrade scripts. Since
local storage keys and values are expected to change but we want to
keep user preferences, these scripts help upgrade old local storage
values to newer ones.

To define an upgrade script:
```js
import { addUpgrade } from "./upgrade_helpers";

addUpgrade({
    version: NEW_VERSION,
    key: OLD_KEY,
    upgrade: ({ value: oldValue }) => ({
        key: NEW_KEY,
        value: NEW_VALUE,
    }),
});
```

This upgrades `OLD_KEY` local storage entry to `NEW_KEY` local
storage entry with `NEW_VALUE` with `NEW_VERSION`. `NEW_VERSION`
matches the targeted odoo version, as a string, in the format
`MAJOR.MINOR`, e.g. "19.1" and "20.0".

Upgrades are executed from oldest to newest version. Range is based
on version in local storage at bottom and curent odoo version at the
top.

For example, let's say we have upgrades for 19.1, 19.3 and 20.0:
- current odoo version is 19.1 and local storage has no version,
then upgrade using 19.1 scripts.
- current odoo version is 20.0 and local storage has version 19.2,
then upgrade using 19.3 scripts then 20.0 scripts.
- current odoo version is 17.0 and local storage has version 20.0,
don't upgrade.

Part of Task-5003012
@alexkuhn alexkuhn force-pushed the master-field-local-storage-aku branch from 12aa6aa to 13c04eb Compare November 10, 2025 16:26
This commit adds versioning to local storage entries, so that when
version is more recent than current version, business code can have
a strategy to drop the values.

This strategy is the one used by local storage field: older versions
are most updated and upgraded fields so 19.1 is implicitly valid for
19.2 and 19.3. 20.0 is invalid as it can potentially be affected
by unknown upgrade scripts of current version (19.3), thus it's
dropped.

Part of Task-5003012
@alexkuhn alexkuhn force-pushed the master-local-storage-downgrade-aku branch from a950179 to 6e83eaf Compare November 10, 2025 16:30
@alexkuhn alexkuhn force-pushed the master-field-local-storage-aku branch 7 times, most recently from ea7604a to 7a87dde Compare November 13, 2025 13:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants