reactea

package module
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Sep 25, 2022 License: MIT Imports: 2 Imported by: 17

README

reactea

Go Reference Code Climate maintainability

Rather simple Bubbletea companion for handling hierarchy and support for lifting state up.
It Reactifies Bubbletea philosophy and makes it especially easy to work with in bigger projects.

For me, personally - It's a must in project with multiple pages and component communication

Check our example code right here!

Installation

go get -u github.com/londek/reactea

General info

Always return reactea.Destroy instead of tea.Quit in order to follow our convention
The goal is to create components which are

  • dimensions-aware (especially unify all setSize conventions)
  • propful
  • easy to lift the state up
  • able to communicate with parent without importing it (I spent too many hours solving import cycles hehe)
  • easier to code
  • all of that without code duplication

The point of library is not to make 150% use of Go performance, because either way Bubbletea
refresh rate is only 60hz and 50 allocations in entire runtime won't really hurt anyone.
Most info is currently in source code so I suggest checking it out

Component lifecycle

Component lifecycle image

reactea takes pointer approach for components making state modifiable in any lifecycle method
There are also 2 additional lifecycle methods: AfterUpdate() and UpdateProps()

AfterUpdate()

AfterUpdate is only lifecycle method that does not depend on parent. It's called right after root component finishes Update(). Components that want it executed should queue itself by reactea.AfterUpdate(component)

UpdateProps()

UpdateProps is lifecycle method that derives state from props, It can happen anytime during lifecycle. Usually called by Init()

Notes

Update IS NOT guaranteed to be called on first-run, Init() for most part is, and critical logic should be there

Lifecycle is fully controlled by parent component making graph above fully theoritical and possibly invalid for third-party components

Example code

Check our example code right here!

Stateless components

Stateless components are represented by following function types

Renderer ProplessRenderer DumbRenderer
Properties
Dimensions
Type func(TProps, int, int) string func(int, int) string func() string

There are many utility functions for transforming stateless into stateful components or for rendering any component without knowing its type (reactea.RenderAny, reactea.RenderPropless)

Reactea Routes API

Routes API allows developers for easy creation of multi-page apps. Routes are kind of substitute for window.Location inside bubbletea

reactea.CurrentRoute() Route

Returns copy of current route

reactea.LastRoute() Route

Returns copy of last route

reactea.WasRouteChanged() bool

returns LastRoute() != CurrentRoute()

Router Component

Router Component is basic implementation of how routing could look in your application. It doesn't support wildcards yet or relative pathing. All data is provided from within props

router.Props

router.Props is a map of route initializers keyed by routes serialized to strings following format r1/r2/r3...etc

What is RouteInitializer?

RouteInitializer is function that initializes the current route component

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AfterUpdate

func AfterUpdate(afterUpdater AfterUpdater)

Queue component for AfterUpdate event

func Destroy

func Destroy() tea.Msg

Destroys app before quiting

func New

func New(root Component[NoProps]) model

func RenderAny

func RenderAny[TRenderer AnyComponent[TProps], TProps any](renderer TRenderer, props TProps, width, height int) string

I'm sorry gophers, I did it for compile-time safety Hopefully nobody is ever gonna get headache because of this

func RenderPropless

func RenderPropless[TRenderer AnyProplessComponent](renderer TRenderer, width, height int) string

I'm sorry gophers, I did it for compile-time safety Hopefully nobody is ever gonna get headache because of this

func SetCurrentRoute

func SetCurrentRoute(r Route)

func WasRouteChanged

func WasRouteChanged() bool

Types

type AfterUpdater

type AfterUpdater interface {
	AfterUpdate() tea.Cmd
}

type AnyComponent

type AnyComponent[TProps any] interface {
	Renderer[TProps] | AnyProplessComponent
}

I decided to give it a name "Component" and not "Renderer" Because Component.Render is itself ProplessRenderer (or the other way around ProplessRenderer = Component.Render) So the naming is infact valid for any type of components in reactea

type AnyProplessComponent

type AnyProplessComponent interface {
	ProplessRenderer | DumbRenderer
}

I decided to give it a name "Component" and not "Renderer" Because Component.Render is itself ProplessRenderer (or the other way around ProplessRenderer = Component.Render) So the naming is infact valid for any type of components in reactea

type BasicComponent

type BasicComponent struct{}

The most basic form of reactea component It implements all not required methods so you don't have to

func (*BasicComponent) AfterUpdate

func (c *BasicComponent) AfterUpdate() tea.Cmd

func (*BasicComponent) Destroy

func (c *BasicComponent) Destroy()

func (*BasicComponent) Update

func (c *BasicComponent) Update(msg tea.Msg) tea.Cmd

type BasicPropfulComponent

type BasicPropfulComponent[TProps any] struct {
	// contains filtered or unexported fields
}

Stores props in struct. If you want to derive state from UpdateProps() you're probably looking at wrong thing

func (*BasicPropfulComponent[TProps]) Init

func (c *BasicPropfulComponent[TProps]) Init(props TProps) tea.Cmd

func (*BasicPropfulComponent[TProps]) Props

func (c *BasicPropfulComponent[TProps]) Props() TProps

func (*BasicPropfulComponent[TProps]) UpdateProps

func (c *BasicPropfulComponent[TProps]) UpdateProps(props TProps)

type Component

type Component[TProps any] interface {

	// You always have to initialize component with some kind of
	// props - it can even be zero value
	// Init() Is meant to both initialize subcomponents and run
	// long IO operations through tea.Cmd
	Init(TProps) tea.Cmd

	// It's called when component is about to be destroyed
	//
	// Note: It's parent component's job to call it so
	// relying on it outside of reactea builtins is
	// not reliable
	Destroy()

	// Typical tea.Model Update(), we handle all IO events here
	Update(tea.Msg) tea.Cmd

	// Callee already knows at which size should it render at
	Render(int, int) string

	// It's an Update() but for props, the state derive stage
	// happens here
	UpdateProps(TProps)

	// AfterUpdate is stage useful for components like routers
	// to prepare content. Saying that you will probably never
	// need to use it
	AfterUpdate() tea.Cmd
}

type DumbRenderer

type DumbRenderer func() string

Doesn't have state, props, even scalling for target dimensions = DumbRenderer, or Stringer

type InvisibleComponent

type InvisibleComponent struct{}

Utility component for displaying empty string on Render()

func (*InvisibleComponent) Render

func (c *InvisibleComponent) Render(int, int) string

type NoProps

type NoProps struct{}

type ProplessRenderer

type ProplessRenderer func(int, int) string

SUPEEEEEER shorthand for components

func PropfulToLess

func PropfulToLess[TProps any](renderer Renderer[TProps], props TProps) ProplessRenderer

Wrapper function

type Renderer

type Renderer[TProps any] func(TProps, int, int) string

Ultra shorthand for components = just renderer One could say it's a stateless component Also note that it doesn't handle any IO by itself

type Route

type Route []string

func CurrentRoute

func CurrentRoute() Route

func LastRoute

func LastRoute() Route

func RouteOf

func RouteOf(route string) (dst Route)

func (Route) Copy

func (r Route) Copy() Route

func (Route) Equal

func (r1 Route) Equal(r2 Route) bool

func (Route) Pop

func (r Route) Pop() (Route, Route)

func (Route) Push

func (r Route) Push(element string) Route

func (Route) Shift

func (r Route) Shift() (Route, Route)

func (Route) String

func (r Route) String() string

type SomeComponent

type SomeComponent interface {
	Destroy()
	Update(tea.Msg) tea.Cmd
	Render(int, int) string
	AfterUpdate() tea.Cmd
}

Interface which is basically reactea.Component just without the props part

func PropfulToComponent

func PropfulToComponent[TProps any](renderer Renderer[TProps], props TProps) SomeComponent

func ProplessToComponent

func ProplessToComponent(renderer ProplessRenderer) SomeComponent

Directories

Path Synopsis
example module

Jump to

Keyboard shortcuts

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