urlparam

package
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Feb 24, 2026 License: MIT Imports: 11 Imported by: 0

Documentation

Overview

Package urlparam provides enhanced URL parameter synchronization for Vango.

URLParam 2.0 supports:

  • Push/Replace history modes
  • Debouncing for search inputs
  • Multiple encoding options (Flat, JSON, Comma)
  • Complex type support (structs, arrays)

Example:

// Search input - replaces history, debounced
searchQuery := setup.URLParam(&s, "q", "", urlparam.Replace, urlparam.Debounce(300*time.Millisecond))

// Filter struct - flat encoding (empty key is valid for flat struct mode)
filters := setup.URLParam(&s, "", Filters{}, urlparam.WithEncoding(urlparam.EncodingFlat))

// Tag array - comma encoding (requires a non-empty key)
tags := setup.URLParam(&s, "tags", []string{}, urlparam.WithEncoding(urlparam.EncodingComma))

Index

Constants

This section is empty.

Variables

View Source
var InitialParamsKey = &struct{ name string }{"InitialURLParams"}

InitialParamsKey is the context key for initial URL params. The session sets this from the client handshake payload.

View Source
var NavigatorKey = &struct{ name string }{"URLNavigator"}

NavigatorKey is the context key for the URL navigator. The session sets this on the root owner so URLParam can queue patches.

Functions

This section is empty.

Types

type Encoding

type Encoding int

Encoding specifies how complex types are serialized to URLs.

const (
	// EncodingFlat serializes structs as flat params: ?cat=tech&sort=asc
	EncodingFlat Encoding = iota

	// EncodingJSON serializes as base64-encoded JSON: ?filter=eyJjYXQiOiJ0ZWNoIn0
	EncodingJSON

	// EncodingComma serializes arrays as comma-separated: ?tags=go,web,api
	EncodingComma
)

type InitialURLState

type InitialURLState struct {
	// contains filtered or unexported fields
}

InitialURLState holds the current URL state for URLParam hydration. The session keeps this updated from handshake, route navigation, and URL delta patches so late-mounted URLParams hydrate from current state.

func NewInitialURLState

func NewInitialURLState(path string, params map[string]string) *InitialURLState

NewInitialURLState creates a URL state snapshot.

func (*InitialURLState) ApplyDelta

func (s *InitialURLState) ApplyDelta(delta map[string]string)

ApplyDelta applies URL delta semantics to the current params. Empty-string values delete keys; omitted keys remain unchanged.

func (*InitialURLState) Set

func (s *InitialURLState) Set(path string, params map[string]string)

Set replaces the URL path and params snapshot.

func (*InitialURLState) Snapshot

func (s *InitialURLState) Snapshot() (path string, params map[string]string)

Snapshot returns a copy of the current path and params.

type Navigator struct {
	// contains filtered or unexported fields
}

Navigator handles URL updates for URLParam. It queues URL patches that are sent to the client along with DOM patches.

func NewNavigator

func NewNavigator(queuePatch func(protocol.Patch)) *Navigator

NewNavigator creates a navigator that queues patches via the provided function. The session passes in a closure that appends to its pending patch buffer.

func (n *Navigator) Navigate(params map[string]string, mode URLMode)

Navigate queues a URL update patch. The patch will be sent to the client with other DOM patches in the same tick.

func (n *Navigator) QueuePatch(patch protocol.Patch)

QueuePatch queues an arbitrary patch to be sent with the next render frame. This is intended for experimental features that need client-side integration without direct access to the session patch buffer.

type URLMode

type URLMode int

URLMode determines how URL updates are handled.

const (
	// ModePush adds a new history entry (default behavior).
	ModePush URLMode = iota

	// ModeReplace replaces the current history entry (no back button spam).
	ModeReplace
)

type URLParam

type URLParam[T any] struct {
	// contains filtered or unexported fields
}

URLParam represents a reactive value synchronized with URL parameters.

func SetupParam

func SetupParam[P any, T any](s *vango.SetupCtx[P], key string, defaultValue T, opts ...URLParamOption) *URLParam[T]

SetupParam creates a new URL parameter during Setup. It allocates state using setup helpers and does not rely on hook slots.

Key rules:

  • key must be non-empty for scalar/default, JSON, comma, and flat non-struct values.
  • key may be empty only for flat struct bindings (field tags/names become query keys).

func (*URLParam[T]) Get

func (u *URLParam[T]) Get() T

Get returns the current value. In a tracking context, this will subscribe the listener to changes.

func (*URLParam[T]) Peek

func (u *URLParam[T]) Peek() T

Peek returns the current value without subscribing.

func (*URLParam[T]) Reset

func (u *URLParam[T]) Reset()

Reset resets the value to the default.

func (*URLParam[T]) Set

func (u *URLParam[T]) Set(value T)

Set updates the value and synchronizes with the URL.

func (*URLParam[T]) SetFromURL

func (u *URLParam[T]) SetFromURL(params map[string]string) error

SetFromURL updates the value from URL parameters. This is called during initialization to sync with the current URL.

func (*URLParam[T]) SetNavigator

func (u *URLParam[T]) SetNavigator(fn func(params map[string]string, mode URLMode))

SetNavigator sets the navigation function for URL updates. This is called during component initialization.

func (*URLParam[T]) Update

func (u *URLParam[T]) Update(fn func(T) T)

Update atomically reads and updates the value.

type URLParamOption

type URLParamOption interface {
	// contains filtered or unexported methods
}

URLParamOption is a functional option for configuring URL parameters.

var (
	// Push creates a new history entry (default behavior).
	Push URLParamOption = modeOption{/* contains filtered or unexported fields */}

	// Replace updates URL without creating history entry (use for filters, search).
	Replace URLParamOption = modeOption{/* contains filtered or unexported fields */}
)

Mode options as values (not functions) to avoid collision with navigation methods.

func Debounce

func Debounce(d time.Duration) URLParamOption

Debounce delays URL updates by the specified duration. Use this for search inputs to avoid spamming the history.

Example:

searchQuery := setup.URLParam(&s, "q", "", urlparam.Replace, urlparam.Debounce(300*time.Millisecond))

func WithEncoding

func WithEncoding(e Encoding) URLParamOption

WithEncoding sets the encoding for complex types. Spec shows: vango.Encoding(vango.URLEncodingFlat) Due to import cycles, the actual call is: urlparam.WithEncoding(urlparam.EncodingFlat)

Example:

filters := setup.URLParam(&s, "", Filters{}, urlparam.WithEncoding(urlparam.EncodingFlat))

Jump to

Keyboard shortcuts

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