react

package module
v2.0.3 Latest Latest
Warning

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

Go to latest
Published: Aug 9, 2022 License: MIT Imports: 3 Imported by: 0

README

go-react

Go Reference GitHub release Github status codecov License

Not React.js for Golang!

go-react is a library for data binding.

See here for earlier version of Golang.

Install

go get github.com/Nomango/go-react/v2

Usage

ch := make(chan int)

// Create a source
s := react.NewChanSource(ch)

// Create a value and bind with the source
vInt := react.NewValue[int]()
cancel := vInt.Bind(s)

// A binding can be canceled
defer cancel()

// Set action on change
vInt.OnChange(func(i int) {
    fmt.Println(i)
})

// A source can be bound more than one time
// So the following code is valid
vInt2 := react.NewValueFrom(0)
vInt2.Bind(react.NewBinding(s.Binding(), func(v int) int {
    return v + 1 // Processing raw value
}))

// Bind another value
vInt32 := react.NewValue[int32]()
vInt32.Bind(react.NewBinding(vInt.Binding(), func(v int) int32 {
    return int32(v + 1)
}))

// Convert a int value to a string value
asyncBinding := react.NewAsyncBinding(vInt.Binding(), func(v int) string {
    return fmt.Sprint(v + 3) // Processing in a separate goroutine
})
vStr, _ := react.NewBindingValue(asyncBinding)

// Send a value to Source
ch <- 1

// Wait for the update to complete
time.Sleep(time.Millisecond * 10)

fmt.Println(vInt2.Load())
fmt.Println(vInt32.Load())
fmt.Println(vStr.Load())

// Output:
// 1
// 2
// 3
// 4

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewBindingValue

func NewBindingValue[T any](b Binding[T]) (Value[T], CancelFunc)

NewBindingValue ...

Types

type Binding

type Binding[T any] interface {
	// OnChange registers a handler f that handles value changes.
	// Note that f should not perform time-consuming operations.
	OnChange(f func(T)) CancelFunc

	// Binding is a helper function to return this
	Binding() Binding[T]
}

Binding ...

func NewAsyncBinding

func NewAsyncBinding[T, U any](from Binding[T], t Transform[T, U]) Binding[U]

NewAsyncBinding creates a binding that runs in a separate goroutine

func NewBinding

func NewBinding[T, U any](from Binding[T], t Transform[T, U]) Binding[U]

NewBinding ...

type CancelFunc

type CancelFunc func()

A CancelFunc tells an operation to abandon its work. A CancelFunc may be called by multiple goroutines simultaneously. After the first call, subsequent calls to a CancelFunc do nothing.

type Source

type Source[T any] interface {
	Binding[T]

	// Change updates the Source
	Change(v T)
}

Source ...

func NewChanSource

func NewChanSource[T any](ch <-chan T) Source[T]

NewChanSource ...

func NewSource

func NewSource[T any]() Source[T]

NewSource ...

func NewTickSource

func NewTickSource(interval time.Duration) Source[time.Time]

NewTickSource creates a Source that will send the current time after each tick

type Transform

type Transform[T, U any] func(T) U

Transform converts T to U

type Value

type Value[T any] interface {
	Binding[T]

	// Load ...
	Load() T

	// Store ...
	Store(v T)

	// Bind creates a binding to this value
	Bind(b Binding[T]) CancelFunc
}

Value ...

func NewValue

func NewValue[T any]() Value[T]

NewValue ...

func NewValueFrom

func NewValueFrom[T any](vv T) Value[T]

NewValueFrom ...

Jump to

Keyboard shortcuts

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