react

package module
v1.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: 4 Imported by: 0

README

go-react

Go Reference Github status GitHub release codecov License

Not React.js for Golang!

go-react is a library for data binding.

See here for Golang1.18+.

Install

go get github.com/Nomango/go-react

Usage

ch := make(chan int)

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

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

// A binding can be canceled
defer cancel()

// Set action on change
vInt.OnChange(func(i interface{}) {
    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, func(v interface{}) interface{} {
    return v.(int) + 1 // Processing raw value
}))

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

// Convert a int value to a string value
asyncBinding := react.NewAsyncBinding(vInt, func(v interface{}) interface{} {
    return fmt.Sprint(v.(int) + 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(b Binding) (Value, CancelFunc)

NewBindingValue ...

Types

type Binding

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

Binding ...

func NewAsyncBinding

func NewAsyncBinding(from Binding, t Transform) Binding

NewAsyncBinding creates a binding that runs in a separate goroutine

func NewBinding

func NewBinding(from Binding, t Transform) Binding

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 interface {
	Binding

	// Change updates the Source
	Change(v interface{})
}

Source ...

func NewChanSource

func NewChanSource(ch interface{}) Source

NewChanSource ...

func NewSource

func NewSource() Source

NewSource ...

func NewTickSource

func NewTickSource(interval time.Duration) Source

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

type Transform

type Transform func(interface{}) interface{}

Transform converts interface{} to interface{}

type Value

type Value interface {
	Binding

	// Load ...
	Load() interface{}

	// Store ...
	Store(v interface{})

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

Value ...

func NewValue

func NewValue() Value

NewValue ...

func NewValueFrom

func NewValueFrom(vv interface{}) Value

NewValueFrom ...

Jump to

Keyboard shortcuts

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