Documentation ¶
Overview ¶
Package mvc adds utilities on top of the component package that allow one to model a user interface that uses a type of MVC pattern.
The design pattern is based on models with notifications and hierarchical action reducers.
Index ¶
- Variables
- func BindProperty[T any](scope co.Scope, prop *Property[T])
- func Dispatch(scope co.Scope, action Action)
- func EventListener(delegate co.Component) co.Component
- func IsChange(change, target Change) bool
- func UseBinding(scope co.Scope, obs Observable, fltrs ...ChangeFilter)
- func UseReducer(scope co.Scope, reducer Reducer) co.Scope
- func Wrap(delegate co.Component) co.Component
- type Action
- type Callback
- type CallbackFunc
- type Change
- type ChangeFilter
- type Event
- type EventBus
- type MultiChange
- type Observable
- type Property
- type Reducer
- type ReducerFunc
- type Subscription
Constants ¶
This section is empty.
Variables ¶
var PropertyChange = NewChange("property")
Functions ¶
func BindProperty ¶ added in v0.11.0
BindProperty is a hook that binds the current Component to the specified Property. Whenever a Change is reported by the Property the Component is invalidated and eventually re-rendered.
func Dispatch ¶
Dispatch propagates the specified Action through the chain of Reducers registered on the specified Scope.
func IsChange ¶
IsChange checks whether the specified Change or any of its parents is equal to the target Change.
func UseBinding ¶
func UseBinding(scope co.Scope, obs Observable, fltrs ...ChangeFilter)
UseBinding is a hook that binds the current Component to the specified Observable. Whenever a Change is reported by the Observable and is accepted by all of the specified ChangeFilters, the Component is invalidated and eventually re-rendered.
func UseReducer ¶
UseReducer extends the specified Scope with the specified Reducer. Dispatch calls to the returned scope will first be processed by the specified Reducer, followed by any parent Reducer registrations in case the current one does not recognize the action.
NOTE: Make sure to attach the returned Scope to any child components in the hierarchy, otherwise they would not have access to the Reducer.
Types ¶
type Action ¶
type Action interface{}
Action represents an arbitrary operation that can be performed by a Reducer. In most cases, though not necessarily, an Action would result in the modification of a model.
type Callback ¶
type Callback func(change Change)
Callback is a function to be called when a Change is received by the Subscription.
type CallbackFunc ¶ added in v0.12.0
type CallbackFunc func(Event)
CallbackFunc is a mechanism to be notified of an Event.
type Change ¶
type Change interface { // Description returns a string description of what has changed. Description() string }
Change represents a notification that something has changed.
type ChangeFilter ¶
ChangeFilter is a filter for Change objects.
type EventBus ¶ added in v0.12.0
type EventBus struct {
// contains filtered or unexported fields
}
EventBus is a mechanism to listen for global events from within your components.
The general pattern is to have on such EventBus and inject it in the root Scope to be accessible by all components.
Then components that need to have special invalidation logic would subscribe and depending on the event would call Invalidate on themselves. If the event is too generic as a type, then its fields need to narrow down the receiver as much as possible, otherwise there is a risk that too many components would be invalidated without need.
func NewEventBus ¶ added in v0.12.0
func NewEventBus() *EventBus
NewEventBus creates a new EventBus instance.
func (*EventBus) Notify ¶ added in v0.12.0
Notify sends the specified event to all subscribed listeners.
func (*EventBus) Subscribe ¶ added in v0.12.0
func (b *EventBus) Subscribe(callback CallbackFunc) Subscription
Subscribe adds the specified callback to be invoked whenever an event occurs.
The returned Subscription can be used to unregister the callback.
func (*EventBus) Unsubscribe ¶ added in v0.12.0
func (b *EventBus) Unsubscribe(subscription Subscription)
Unsubscribe disables the specified subscription and future events would not be sent to it.
type MultiChange ¶
type MultiChange struct { // Changes is a slice of Changes that make up this MultiChange. Changes []Change }
MultiChange represents a Change that groups multiple other Changes.
func (*MultiChange) Description ¶
func (c *MultiChange) Description() string
func (*MultiChange) Is ¶
func (c *MultiChange) Is(target Change) bool
type Observable ¶
type Observable interface { // Subscribe registers the specified callback function to this Observable. // It will only be called if the inbound Change passes the specified filters. // The returned Subscription can be used to unsubscribe from this Target. Subscribe(callback Callback, filters ...ChangeFilter) Subscription // SignalChange notifies all Subscribers of the specified Change. SignalChange(change Change) // AccumulateChanges calls the specified closure function and any calls // to SignalChange during that invocation will not be sent but instead will // be recorded. Once the closure is complete then all recorded changes will // be sent through a single MultiChange change, as long as the closure // does not return an error. AccumulateChanges(fn func() error) error }
Observable represents something that can be observed for changes.
func NewObservable ¶
func NewObservable() Observable
NewObservable creates a new Observable instance.
type Property ¶ added in v0.11.0
type Property[T any] struct { // contains filtered or unexported fields }
func NewProperty ¶ added in v0.11.0
func (*Property[T]) Subscribe ¶ added in v0.11.0
func (p *Property[T]) Subscribe(callback Callback) Subscription
type Reducer ¶
type Reducer interface { // Reduce attempts to process the specified action. If the Reducer does not // recognize the action, then it should return false, in which case a // parent reducer will be invoked to try and handle it. Reduce(action Action) bool }
Reducer represents an algorithm that can process actions.
type ReducerFunc ¶
ReducerFunc is a helper func wrapper that implements the Reducer interface.
func (ReducerFunc) Reduce ¶
func (f ReducerFunc) Reduce(action Action) bool
type Subscription ¶
type Subscription interface { // Delete removes this Subscription from the associated Observable and the // Callback of this Subscription will no longer be called. Delete() }
Subscription represents an association of a Callback function to an Observable.