goob

package module
v0.0.3 Latest Latest
Warning

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

Go to latest
Published: Jun 8, 2020 License: MIT Imports: 5 Imported by: 20

README

Overview

A lightweight observable lib.

  • one publisher to multiple subscribers
  • thread-safe
  • subscribers never block each other
  • publishing order is the same as listening order

The trade-off is the subscribers may delay sometime (between 1ns to 1ms) when new events come.

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Each

func Each(s chan Event, iteratee interface{})

Each event, if iteratee returns true, the iteration will stop

Types

type Event

type Event interface{}

Event interface

type Observable

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

Observable hub

func New

func New() *Observable

New observable instance

Example
package main

import (
	"context"
	"fmt"

	"github.com/ysmood/goob"
)

func main() {
	ob := goob.New()

	ctx, unsubscribe := context.WithCancel(context.Background())
	defer unsubscribe()
	s := ob.Subscribe(ctx)

	go func() {
		ob.Publish(1)
		ob.Publish(2)
		ob.Publish(3)
	}()

	for e := range s {
		fmt.Println(e)

		if e.(int) == 3 {
			break
		}
	}

}
Output:
1
2
3

func (*Observable) Filter

func (ob *Observable) Filter(ctx context.Context, iteratee interface{}) *Observable

Filter events

func (*Observable) Map

func (ob *Observable) Map(ctx context.Context, iteratee interface{}) *Observable

Map events

func (*Observable) Publish

func (ob *Observable) Publish(e Event)

Publish message to the queue

func (*Observable) Subscribe

func (ob *Observable) Subscribe(ctx context.Context) chan Event

Subscribe message, the ctx is used to cancel the subscription

Jump to

Keyboard shortcuts

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