pubsub

package
v1.7.1 Latest Latest
Warning

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

Go to latest
Published: Jan 29, 2021 License: MIT, Apache-2.0, MIT Imports: 1 Imported by: 0

README

go-pubsub

A simple single topic thread safe event publish/subscribe interface for go

Table of Contents

Description

This module provides a generic way to for consumers of a library to subscribe to events that library publishes. Consumers can also unsubscribe to stop receiving events.

Because go lacks generics, the library should provide a function to dispatch events to subscribers when constructing a pubsub instance. The dispatch function should convert generic events to specific events and generic subscriber functions to specific callbacks. You may want to keep the pubsub instance private to your library and expose your own subscribe function to provide type safety.

Install

go get github.com/hannahhoward/go-pubsub

Usage

package mylibrary

import(
  "errors"
  "github.com/hannahhoward/go-pubsub"
)

type MyLibrary struct {
  pubSub pubsub.PubSub
}

type MyLibarySubscriber func(event int)

type internalEvent int

func dispatcher(evt pubsub.Event, subscriber pubsub.SubscriberFn) error {
  ie, ok := evt.(internalEvent)
  if !ok {
    return errors.New("wrong type of event")
  }
  myLibarySubscriber, ok := subscriber.(MyLibrarySubscriber)
  if !ok {
    return errors.new("wrong type of subscriber")
  }
  myLibraySubscriber(ie)
  return nil
}

func New() * MyLibrary {
  return &MyLibrary{
    pubSub: pubsub.New(dispatcher)
  }
}

func (ml * MyLibrary) Subscribe(subscriber MyLibrarySubscriber) pubsub.Unsubscribe {
  return ml.pubSub.Subscribe(subscriber)
}

func (ml * MyLibrary) SomeFunc(value int) {
  ml.pubSub.Publish(internalEvent(value))
}

License

Dual-licensed under MIT + Apache 2.0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Dispatcher

type Dispatcher func(Event, SubscriberFn) error

Dispatcher dispatches an event to a subscriber function. Usually, it converts the event and subscriber from generic to specific types and then calls the specific subscriber function with the specific event information

type Event

type Event interface{}

Event is a generic event that can be dispatched

type PubSub

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

PubSub is a simple emitter of data transfer events

func New

func New(dispatcher Dispatcher) *PubSub

New returns a new PubSub

func (*PubSub) Publish

func (ps *PubSub) Publish(event Event) error

Publish publishes the given event and channel state to all subscribers

func (*PubSub) Subscribe

func (ps *PubSub) Subscribe(subscriberFn SubscriberFn) Unsubscribe

Subscribe adds the given subscriber to the list of subscribers for this Pubsub

type SubscriberFn

type SubscriberFn interface{}

SubscriberFn is a function that receives events from a dispatcher

type Unsubscribe

type Unsubscribe func()

Unsubscribe is a function returned from subscribe that can be used to terminate the subscription

Jump to

Keyboard shortcuts

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