voyeur

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Oct 14, 2017 License: GPL-3.0 Imports: 2 Imported by: 2

README

voyeur

voyeur is a tiny middleware that implements the observable pattern in Go.

Built for consentual observation only.

Godoc

Documentation

Overview

voyeur helps you build applications using the Observer pattern. If you want to know more about it, check out https://en.wikipedia.org/wiki/Observer_pattern.

Example
/*
   This file is part of voyeur.

   voyeur is free software: you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation, either version 3 of the License, or
   (at your option) any later version.

   voyeur is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with voyeur.  If not, see <http://www.gnu.org/licenses/>.
*/

package main

import (
	"context"
	"fmt"
	"time"
)

// printObserver simply prints all received events to stdout
type printObserver struct{}

func (o printObserver) OnEvent(ctx context.Context, e Event) {
	fmt.Println(e)
}

// stringEvent is a very simple event type
type stringEvent string

func (e stringEvent) EventType() string {
	return "string"
}

func (e stringEvent) String() string {
	return string(e)
}

func main() {
	// build a context we can cancel
	ctx := context.Background()
	ctx, cancel := context.WithCancel(ctx)
	em, o := Pair()

	// create and register a very simple observer. It just prints events to stdout.
	printer := printObserver{}
	o.Register(ctx, printer)

	// emit some events.
	em.Emit(ctx, stringEvent("test"))
	em.Emit(ctx, stringEvent("foo"))

	// cancel the observation. This only affects observers that use the context returned by context.WithCancel(ctx)
	cancel()
	time.Sleep(time.Millisecond) // kick scheduler, otherwise the observer will not be removed before we emit the next event

	// this event is not seen by the observer anymore, because we cancelled the observation before.
	em.Emit(ctx, stringEvent("bar"))

	// let's reregister, this time without being able to cancel
	ctx = context.Background()
	o.Register(ctx, printer)

	em.End(ctx)

}
Output:

test
foo
End

Index

Examples

Constants

This section is empty.

Variables

View Source
var End = simpleEvent{"End"}

End is like EOF or a channel close. Nothing to see here anymore.

Functions

func Pair

func Pair() (Emitter, Observable)

Pair returns an Emitter and corresponding Observable. Events emitted on one can be observed on the other.

Types

type Emitter

type Emitter interface {
	Emit(context.Context, Event)
	End(context.Context)
}

Emitter lets you send events

type Event

type Event interface {
	// EventType returns a short descriptor for the event
	EventType() string
}

Event describes an event.

type Observable

type Observable interface {
	Register(context.Context, Observer)
}

Observable emits events

type Observer

type Observer interface {
	OnEvent(context.Context, Event)
}

Observer consumes events

Jump to

Keyboard shortcuts

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