mvc

package
v0.9.0 Latest Latest
Warning

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

Go to latest
Published: Sep 25, 2022 License: MIT Imports: 4 Imported by: 0

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

Constants

This section is empty.

Variables

This section is empty.

Functions

func Dispatch

func Dispatch(scope co.Scope, action Action)

Dispatch propagates the specified Action through the chain of Reducers registered on the specified Scope.

func IsChange

func IsChange(change, target Change) bool

IsChange checks whether the specified Change or any of its parents is equal to the target Change.

func UseBinding

func UseBinding(obs Observable, fltrs ...ChangeFilter)

UseBinding is a render 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

func UseReducer(scope co.Scope, reducer Reducer) co.Scope

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 Change

type Change interface {

	// Description returns a string description of what has changed.
	Description() string
}

Change represents a notification that something has changed.

func NewChange

func NewChange(description string) Change

NewChange creates a new Change instance with the specified description.

func SubChange

func SubChange(parent Change, description string) Change

SubChange creates a new Change that extends an existing Change and adds the specified description.

type ChangeFilter

type ChangeFilter = filter.Func[Change]

ChangeFilter is a filter for Change objects.

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 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

type ReducerFunc func(action Action) bool

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.

Jump to

Keyboard shortcuts

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