-
Notifications
You must be signed in to change notification settings - Fork 0
persist
awekrx edited this page May 29, 2026
·
1 revision
import { persist } from '@dev-suite/decorators/persist'
property
Persist property state to storage adapter automatically.
- Manual localStorage/db sync in setters/getters
- Custom hydrate/save methods per field
class Preferences {
private _theme = 'light';
get theme() {
const saved = localStorage.getItem('theme');
return saved ?? this._theme;
}
set theme(next: string) {
this._theme = next;
localStorage.setItem('theme', next);
}
}import { persist } from '@dev-suite/decorators/persist';
class Preferences {
@persist({ adapter: localStorageAdapter, key: 'theme' })
theme = 'light';
}- Centralizes cross-cutting behavior.
- Method/class/property code stays focused on domain logic.
class CartState {
private _items: CartItem[] = [];
set items(next: CartItem[]) {
this._items = next;
db.save('cart_items', JSON.stringify(next));
}
}import { persist } from '@dev-suite/decorators/persist';
class CartState {
@persist({ adapter: dbAdapter, key: 'cart_items', serialize: JSON.stringify, deserialize: JSON.parse })
items: CartItem[] = [];
}- Second scenario reuses same policy without duplication.
- Behavior is more consistent and easier to audit.