observable

package module
v0.6.0 Latest Latest
Warning

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

Go to latest
Published: Apr 28, 2019 License: Apache-2.0 Imports: 3 Imported by: 0

README

Go-Observable

Highly reduced go observable system based on the spirit of ReactiveX.

Code snippets
// Create a new observer
o := observable.New()

// Register a listener method.
sub := o.Subscribe(func(e interface{}) {
  log.Println("Hello " e) // Emits 'Hello Tom'
})

// Unsubscribe
sub.Unsubscribe()

// Register a listener method for the whol event.
o.SubscribeEvent(func(evt Event) {
  log.Print("Data", evt.Data())
  evt.Unsubscribe()
})

// Emit an event.
o.Next("Tom")

// Emit an event.
o.Next(ClientDisconnectedEvent {
  IP: "10.2.0.2",
})

// Close the observable
o.Complete()

Description

Implements the minimal observer pattern. Aims to mimic the reactive pattern in the future.

Roadmap

  • Subscriptions
  • Middlewares
  • More RX-like support
  • Full test coverage

Install

go get -u github.com/joernlenoch/go-observable

or

import "github.com/joernlenoch/go-observable"

Examples

Emit events

Example of event listener and emitter.

// Open an observer and start running
o := observable.New()
defer o.Close()

// Add a listener that logs events
o.Subscribe(func(e interface{}) {
  log.Printf("Received: %s.\n", e.(string))
})

// This event will be picked by the listener
o.Next("Hello")
Middleware
// Use middlewares
o.Pipe(
  // Only allow value 10
  Filter(func(i interface{}) bool {
    return i == 10
  }),

  // Map the value on 2*x
  Map(func(i interface{}) interface{} {
    num := i.(int)
    return num * 2
  }),

  // Unsubscribe after the first event
  First(),
).SubscribeEvent(func(i interface{}) {
  evt := i.(int)
  log.Print("Recieved", evt)
})

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Event

type Event interface {
	Subscription
	Data() interface{}
	// contains filtered or unexported methods
}

type EventListener

type EventListener func(Event)

type FilterFunc

type FilterFunc func(data interface{}) bool

type FilterMiddleware

type FilterMiddleware struct {
	FilterFunc FilterFunc
}

func (*FilterMiddleware) Pipe

type FirstMiddleware

type FirstMiddleware struct {
}

func (*FirstMiddleware) Pipe

func (f *FirstMiddleware) Pipe(evt Event) MiddlewareResult

type Listener

type Listener func(interface{})

type MapFunc

type MapFunc func(data interface{}) interface{}

type MapMiddleware

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

func (*MapMiddleware) Pipe

func (f *MapMiddleware) Pipe(evt Event) MiddlewareResult

type Middleware

type Middleware interface {
	Pipe(evt Event) MiddlewareResult
}

func Filter

func Filter(fn FilterFunc) Middleware

func First

func First() Middleware

func Map

func Map(fn MapFunc) Middleware

func TakeUntil added in v0.6.0

func TakeUntil(trigger *Observable) Middleware

type MiddlewareResult

type MiddlewareResult struct {
	Event       Event
	Abort       bool
	Unsubscribe bool
}

type Observable

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

func (*Observable) Complete

func (o *Observable) Complete()

func (*Observable) Next

func (o *Observable) Next(evt interface{})

func (*Observable) Pipe

func (o *Observable) Pipe(middlewares ...Middleware) *Observable

func (*Observable) Subscribe

func (o *Observable) Subscribe(l Listener) Subscription

func (*Observable) SubscribeEvent

func (o *Observable) SubscribeEvent(l EventListener) Subscription

type Subscription

type Subscription interface {
	Unsubscribe()
	// contains filtered or unexported methods
}

type TakeUntilFunc added in v0.6.0

type TakeUntilFunc func(data interface{}) bool

type TakeUntilMiddleware added in v0.6.0

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

func (*TakeUntilMiddleware) Pipe added in v0.6.0

Jump to

Keyboard shortcuts

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