gobserve

package module
v0.0.0-...-5444a50 Latest Latest
Warning

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

Go to latest
Published: Jun 12, 2022 License: MIT Imports: 3 Imported by: 0

README

Gobserve

A simple event-dispatcher library.

Go Reference

Install

to add the library to your project simply run :

go get github.com/decima/gobserve

example usage :

package main

import (
	"github.com/decima/gobserve"
	"log"
)

type helloWorld int

func (s helloWorld) Name() string {
	return "sayHello"
}

func main() {
	gobserve.Subscribe("sayHello", func(h helloWorld) error {
		log.Println("hello from 1")
		return nil
	})
	gobserve.Subscribe("sayHello", func(h helloWorld) error {
		log.Println("hello from 2")
		return nil
	})

	gobserve.DispatchConcurrent(helloWorld(1))

}

By default you can use the Subscribe and Dispatch methods in order to use a globally defined event Subscriber. But you can create your own dispatcher for more precise and independant control.

Documentation

Index

Constants

View Source
const (
	HighPriority = 100
	//NormalPriority default priority
	NormalPriority = 0
	LowPriority    = -100

	//Sequential runs every action sequentially.
	Sequential ProcessingStrategy = iota
	//Concurrent runs every action at once, regardless priority
	Concurrent
	//PerPriorityConcurrent runs every actions concurrently regarding every priority. The priority processing is sequential.
	PerPriorityConcurrent
)

Variables

This section is empty.

Functions

func Dispatch

func Dispatch[T EventInterface](event T, strategy ProcessingStrategy) []error

Dispatch sends an event to every same name events. Processing Strategies are defined for concurrency/sequential behaviors.

func DispatchConcurrent

func DispatchConcurrent[T EventInterface](event T) []error

func DispatchConcurrentPerPriority

func DispatchConcurrentPerPriority[T EventInterface](event T) []error

DispatchConcurrentPerPriority processes every priority sequentially, but with concurrent working in the tasks.

func DispatchSequential

func DispatchSequential[T EventInterface](event T) []error

DispatchSequential sends an event through every action in a sequential mode.

func Subscribe

func Subscribe[T EventInterface](name string, action func(T) error) interface{}

Subscribe attach an action to an event name given. Internally, this function, will attach the action to a normalPriority (0)

func SubscribeWithPriority

func SubscribeWithPriority[T EventInterface](name string, action func(T) error, priority int) interface{}

SubscribeWithPriority attach an action to an event name given and weights the priority The higher the priority the first will the action be trigger.

func Unsubscribe

func Unsubscribe(sub interface{})

Unsubscribe removes the subscription previously made.

Types

type EventDispatcher

type EventDispatcher[T EventInterface] struct {
	// contains filtered or unexported fields
}

This structure contains a map of every events registered in the event Dispatcher

func NewDispatcher

func NewDispatcher() EventDispatcher[EventInterface]

NewDispatcher creates a new eventDispatcher for EventInterfaces

func (*EventDispatcher[T]) Dispatch

func (e *EventDispatcher[T]) Dispatch(
	event T,
	processingStrategy ProcessingStrategy,
) []error

Dispatch sends an event through every concerned event subscribers

func (*EventDispatcher[T]) Subscribe

func (e *EventDispatcher[T]) Subscribe(name string, action func(T) error) Subscriber[T]

Subscribe attach an action to an event name given. Internally, this function, will attach the action to a normalPriority (0)

func (*EventDispatcher[T]) SubscribeWithPriority

func (e *EventDispatcher[T]) SubscribeWithPriority(name string, action func(T) error, priority int) interface{}

SubscribeWithPriority attach an action to an event name given and weights the priority The higher the priority the first will the action be trigger.

func (*EventDispatcher[T]) Unsubscribe

func (e *EventDispatcher[T]) Unsubscribe(rawSub interface{})

Unsubscribe removes the subscription previously made.

type EventInterface

type EventInterface interface {
	//Name the name of the event
	Name() string
}

EventInterface This interface is the "event enveloppe" for dispatching elements through all subscribes

type ProcessingStrategy

type ProcessingStrategy int

ProcessingStrategy the type of the

type Subscriber

type Subscriber[T EventInterface] struct {
	// contains filtered or unexported fields
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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