ea

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Oct 4, 2022 License: MIT Imports: 2 Imported by: 0

README

Ea

Go Reference Go Report Card

Ea is a Bubble Tea-inspired Go implementation of the Elm Architecture. It is designed primarily for use by other libraries that want to expose an API based on the Elm Architecture.

Example

package boba

import (
	"context"
	"fmt"

	"deedles.dev/ea"
)

type Model interface {
	ea.Model[Model]
	View() string
}

type Program struct {
	loop *ea.Loop
}

func New(initialModel Model) *Program {
	p := &Program{
		loop: ea.New(initialModel),
	}
	p.loop.PostUpdate = p.view

	return p
}

func (p *Program) view(model Model) {
	fmt.Println(model.View())
}

func (p *Program) Run() {
	p.loop.Run(context.Background())
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Cmd

type Cmd func() Msg

A Cmd is a function that is run concurrently. The Msg returned is passed to Model.Update to produce a new Model.

func Batch

func Batch(cmds ...Cmd) Cmd

Batch returns a Cmd that runs multiple other Cmds.

type Loop

type Loop[M Model[M]] struct {
	// If not nil, PostUpdate is called synchronously with the new Model
	// after every update.
	PostUpdate func(M)
	// contains filtered or unexported fields
}

Loop runs a update loop.

func New

func New[M Model[M]](model M) *Loop[M]

New returns a Loop with an initial Model.

func (*Loop[M]) Run

func (loop *Loop[M]) Run(ctx context.Context, cmd Cmd) M

Run runs the Loop with an optional initial command. It blocks until the loop exits, returning the final Model.

Behavior is undefined if two calls to Run happen concorrently.

func (*Loop[M]) Send

func (loop *Loop[M]) Send() chan<- Msg

Send returns a channel which can be used to send Msgs to a running Loop. Doing so will trigger an update.

type Model

type Model[N any] interface {
	Update(Msg) (N, Cmd)
}

Model is a state that is capable of producing a new state from itself and a Msg.

type Msg

type Msg any

A Msg is an arbitrary piece of data passed to a Model's Update method to produce a new Model.

func Quit

func Quit() Msg

Quit is a command that causes the main loop to exit.

Jump to

Keyboard shortcuts

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