pref

package
v0.2.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 23, 2026 License: MIT Imports: 3 Imported by: 0

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

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]) Get

func (p *Pref[T]) Get() T

Get returns the current preference value.

func (*Pref[T]) Key

func (p *Pref[T]) Key() string

Key returns the preference key.

func (*Pref[T]) MarshalJSON

func (p *Pref[T]) MarshalJSON() ([]byte, error)

MarshalJSON implements json.Marshaler.

func (*Pref[T]) Reset

func (p *Pref[T]) Reset()

Reset resets the preference to its default value.

func (*Pref[T]) Set

func (p *Pref[T]) Set(value T)

Set updates the preference value and triggers sync.

func (*Pref[T]) SetFromRemote

func (p *Pref[T]) SetFromRemote(value T, remoteUpdatedAt time.Time)

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

func (p *Pref[T]) UnmarshalJSON(data []byte) error

UnmarshalJSON implements json.Unmarshaler.

func (*Pref[T]) UpdatedAt

func (p *Pref[T]) UpdatedAt() time.Time

UpdatedAt returns when the preference was last updated.

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL