hub

package
v0.0.0-...-bbc744d Latest Latest
Warning

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

Go to latest
Published: Apr 6, 2016 License: MIT, Apache-2.0 Imports: 1 Imported by: 0

README

hub

GoDoc Build Status

Documentation

Overview

Package hub provides a simple event dispatcher for publish/subscribe pattern.

Example
package main

import "fmt"

// Different event kinds
const (
	happenedA = iota
	happenedB
	happenedC
)

// Our custom event type
type EventA struct {
	arg1, arg2 int
}

// Implement hub.Event interface
func (e EventA) Kind() int { return happenedA }

func main() {
	Subscribe(happenedA, func(e Event) {
		a := e.(EventA) // Cast to concrete type
		fmt.Println(a.arg1 + a.arg2)
	})

	Publish(EventA{2, 3})
}
Output:

5

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Publish

func Publish(e Event)

Publish an event to the subscribers in DefaultHub.

func Subscribe

func Subscribe(kind int, f func(Event)) (cancel func())

Subscribe registers f for the event of a specific kind in the DefaultHub.

Types

type Event

type Event interface {
	Kind() int
}

Event is an interface for published events.

type Hub

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

Hub is an event dispatcher, publishes events to the subscribers which are subscribed for a specific event type. Optimized for publish calls. The handlers may be called in order different than they are registered.

var DefaultHub Hub

DefaultHub is the default Hub used by Publish and Subscribe.

func (*Hub) Publish

func (h *Hub) Publish(e Event)

Publish an event to the subscribers.

func (*Hub) Subscribe

func (h *Hub) Subscribe(kind int, f func(Event)) (cancel func())

Subscribe registers f for the event of a specific kind.

Jump to

Keyboard shortcuts

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