Documentation
¶
Overview ¶
Package pref provides user preference management with sync capabilities.
Preferences are persisted values that:
- Work for anonymous users (stored in LocalStorage via client)
- Sync when user logs in (merge with database)
- Stay consistent across tabs and devices
Example:
// Simple theme preference
theme := pref.New("theme", "light")
// With merge strategy
settings := pref.New("settings", Settings{}, pref.MergeWith(pref.DBWins))
// Read/write
current := theme.Get()
theme.Set("dark")
Index ¶
- type MergeStrategy
- type Pref
- func (p *Pref[T]) Get() T
- func (p *Pref[T]) Key() string
- func (p *Pref[T]) MarshalJSON() ([]byte, error)
- func (p *Pref[T]) Reset()
- func (p *Pref[T]) Set(value T)
- func (p *Pref[T]) SetFromRemote(value T, remoteUpdatedAt time.Time)
- func (p *Pref[T]) SetPersistHandlers(local func(key string, value any, updatedAt time.Time), ...)
- func (p *Pref[T]) UnmarshalJSON(data []byte) error
- func (p *Pref[T]) UpdatedAt() time.Time
- type PrefOption
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type MergeStrategy ¶
type MergeStrategy int
MergeStrategy determines how conflicts are resolved when local and remote values differ.
const ( // DBWins uses the server/database value, discards local. // Best for settings that should be authoritative from server. DBWins MergeStrategy = iota // LocalWins uses the local value, updates server. // Best for preferences that the user just modified. LocalWins // Prompt notifies the user to choose. // Best for important settings where the user should decide. Prompt // LWW uses last-write-wins with timestamps. // Best for frequently changing preferences. LWW )
type Pref ¶
type Pref[T any] struct { // contains filtered or unexported fields }
Pref represents a user preference with sync capabilities.
func New ¶
func New[T any](key string, defaultValue T, opts ...PrefOption) *Pref[T]
New creates a new preference with the given key and default value.
func (*Pref[T]) MarshalJSON ¶
MarshalJSON implements json.Marshaler.
func (*Pref[T]) Set ¶
func (p *Pref[T]) Set(value T)
Set updates the preference value and triggers sync.
func (*Pref[T]) SetFromRemote ¶
SetFromRemote updates the value from a remote source (another tab or server). Uses the configured merge strategy to resolve conflicts.
func (*Pref[T]) SetPersistHandlers ¶
func (p *Pref[T]) SetPersistHandlers( local func(key string, value any, updatedAt time.Time), db func(key string, value any, updatedAt time.Time), broadcast func(key string, value any, updatedAt time.Time), )
SetPersistHandlers sets the persistence handlers. Called during component initialization.
func (*Pref[T]) UnmarshalJSON ¶
UnmarshalJSON implements json.Unmarshaler.
type PrefOption ¶
type PrefOption func(*prefConfig)
PrefOption is a functional option for configuring preferences.
func LocalOnly ¶
func LocalOnly() PrefOption
LocalOnly prevents syncing this preference to the server. Useful for device-specific settings like volume.
func MergeWith ¶
func MergeWith(strategy MergeStrategy) PrefOption
MergeWith sets the merge strategy for conflict resolution.
func OnConflict ¶
func OnConflict(handler func(local, remote any) any) PrefOption
OnConflict sets a custom conflict handler. The handler receives local and remote values and returns the resolved value.