reactor

package module
v0.0.0-...-340b932 Latest Latest
Warning

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

Go to latest
Published: Sep 27, 2017 License: BSD-2-Clause-Views Imports: 15 Imported by: 2

README

go-reactor

Framework for writing completely reactive web applications.

Hello-World

Simple one page hello world program that counts number of clicks:

package main

import (
	"fmt"
	"sync"

	"github.com/draganm/go-reactor"
)

func main() {
	r := reactor.New()
	r.AddScreen("/", indexScreenFactory)
	r.Serve(":8080")
}

func indexScreenFactory(ctx reactor.ScreenContext) reactor.Screen {
	return &indexScreen{
		ctx: ctx,
	}
}

var indexScreenTemplate = reactor.MustParseDisplayModel(`
<div className="well">
	You've clicked <mark id="hw" reportEvents="click">hello world</mark>: <span id="count"/> times
</div>
`)

type indexScreen struct {
	sync.Mutex
	ctx          reactor.ScreenContext
	clickCounter int
}

func (i *indexScreen) Mount() {
	i.clickCounter = 0
	i.render()
}

func (i *indexScreen) render() {
	ui := indexScreenTemplate.DeepCopy()
	ui.SetElementText("count", fmt.Sprintf("%d", i.clickCounter))
	i.ctx.UpdateScreen(&reactor.DisplayUpdate{
		Model: ui,
	})
}

func (i *indexScreen) OnUserEvent(evt *reactor.UserEvent) {
	i.Lock()
	defer i.Unlock()

	if evt.ElementID == "hw" {
		i.clickCounter++
		i.render()
	}
}

func (i *indexScreen) Unmount() {}

Updating dependencies

npm install
gulp
cd public
go generate
cd ..

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewrateLimitedScreenUpdater

func NewrateLimitedScreenUpdater(minDuration time.Duration, parent func(*DisplayUpdate)) *rateLimitedScreenUpdater

Types

type DefaultNotFoundScreen

type DefaultNotFoundScreen struct {
	// contains filtered or unexported fields
}

func (*DefaultNotFoundScreen) Mount

func (d *DefaultNotFoundScreen) Mount()

func (*DefaultNotFoundScreen) OnUserEvent

func (d *DefaultNotFoundScreen) OnUserEvent(*UserEvent)

func (*DefaultNotFoundScreen) Unmount

func (d *DefaultNotFoundScreen) Unmount()

type DisplayModel

type DisplayModel struct {
	ID           string                 `json:"id,omitempty"`
	Element      string                 `json:"el,omitempty"`
	Text         string                 `json:"te,omitempty"`
	Children     []*DisplayModel        `json:"ch,omitempty"`
	Attributes   map[string]interface{} `json:"at,omitempty"`
	ReportEvents []ReportEvent          `json:"ev,omitempty"`
}

func MustParseDisplayModel

func MustParseDisplayModel(src string) *DisplayModel

func ParseDisplayModel

func ParseDisplayModel(src string) (*DisplayModel, error)

func (*DisplayModel) AppendChild

func (m *DisplayModel) AppendChild(id string, child *DisplayModel)

func (*DisplayModel) DeepCopy

func (m *DisplayModel) DeepCopy() *DisplayModel

func (*DisplayModel) DeepEqual

func (m *DisplayModel) DeepEqual(other *DisplayModel) bool

func (*DisplayModel) DeleteChild

func (m *DisplayModel) DeleteChild(id string)

func (*DisplayModel) FindElementByID

func (m *DisplayModel) FindElementByID(id string) *DisplayModel

func (*DisplayModel) FindElementPathByID

func (m *DisplayModel) FindElementPathByID(id string) *[]int

func (*DisplayModel) ReplaceChild

func (m *DisplayModel) ReplaceChild(id string, replacement *DisplayModel) *DisplayModel

func (*DisplayModel) ReplaceElementWithPath

func (m *DisplayModel) ReplaceElementWithPath(path []int, replacement *DisplayModel)

func (*DisplayModel) SetElementAttribute

func (m *DisplayModel) SetElementAttribute(id, name string, value interface{})

func (*DisplayModel) SetElementText

func (m *DisplayModel) SetElementText(id, text string) *DisplayModel

type DisplayUpdate

type DisplayUpdate struct {
	Model    *DisplayModel `json:"model,omitempty"`
	Eval     string        `json:"eval,omitempty"`
	Title    string        `json:"title,omitempty"`
	Location string        `json:"location,omitempty"`
}

func (*DisplayUpdate) DeepEqual

func (d *DisplayUpdate) DeepEqual(other *DisplayUpdate) bool

type Reactor

type Reactor struct {
	sync.RWMutex
	// contains filtered or unexported fields
}

func New

func New(handlers ...negroni.Handler) *Reactor

func (*Reactor) AddScreen

func (r *Reactor) AddScreen(pathPattern string, factory ScreenFactory) error

func (*Reactor) RemoveScreens

func (r *Reactor) RemoveScreens()

func (*Reactor) Serve

func (r *Reactor) Serve(bind string)

func (*Reactor) ServeHTTP

func (re *Reactor) ServeHTTP(rw http.ResponseWriter, r *http.Request, next http.HandlerFunc)

type ReportEvent

type ReportEvent struct {
	Name            string   `json:"name,omitempty"`
	StopPropagation bool     `json:"sp,omitempty"`
	PreventDefault  bool     `json:"pd,omitempty"`
	ExtraValues     []string `json:"xv,omitempty"`
}

type Screen

type Screen interface {
	Mount()
	OnUserEvent(*UserEvent)
	Unmount()
}

func DefaultNotFoundScreenFactory

func DefaultNotFoundScreenFactory(ctx ScreenContext) Screen

type ScreenContext

type ScreenContext struct {
	Path         string
	ConnectionID string
	Params       map[string]string
	UpdateScreen func(*DisplayUpdate)
}

type ScreenFactory

type ScreenFactory func(ScreenContext) Screen

type UserEvent

type UserEvent struct {
	ElementID   string                 `json:"id,omitempty"`
	Type        string                 `json:"type,omitempty"`
	Value       string                 `json:"value,omitempty"`
	Data        string                 `json:"data,omitempty"`
	ExtraValues map[string]interface{} `json:"xv,omitempty"`
}

UserEvent is an event triggered by the client. Such as click, or key events.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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