eventstream

package
v1.0.4 Latest Latest
Warning

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

Go to latest
Published: Jul 22, 2024 License: Apache-2.0 Imports: 2 Imported by: 0

Documentation

Overview

Package eventstream implements a publisher / subscriber.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type EventStream

type EventStream struct {
	sync.RWMutex
	// contains filtered or unexported fields
}

func NewEventStream

func NewEventStream() *EventStream

Create a new EventStream value and returns it back.

func (*EventStream) Length

func (es *EventStream) Length() int32

Returns an integer that represents the current number of subscribers to the stream

func (*EventStream) Publish

func (es *EventStream) Publish(evt interface{})

Publishes the given event to all the subscribers in the stream

func (*EventStream) Subscribe

func (es *EventStream) Subscribe(handler Handler) *Subscription

Subscribe the given handler to the EventStream

Example

Subscribe subscribes to events

package main

import (
	"fmt"

	"github.com/bwgame666/protoactor-go/eventstream"
)

func main() {
	es := eventstream.NewEventStream()
	handler := func(event interface{}) {
		fmt.Println(event)
	}

	// only allow strings
	predicate := func(event interface{}) bool {
		_, ok := event.(string)
		return ok
	}

	sub := es.SubscribeWithPredicate(handler, predicate)

	es.Publish("Hello World")
	es.Publish(1)

	es.Unsubscribe(sub)

}
Output:

Hello World

func (*EventStream) SubscribeWithPredicate

func (es *EventStream) SubscribeWithPredicate(handler Handler, p Predicate) *Subscription

SubscribeWithPredicate creates a new Subscription value and sets a predicate to filter messages passed to the subscriber, it returns a pointer to the Subscription value

func (*EventStream) Unsubscribe

func (es *EventStream) Unsubscribe(sub *Subscription)

Unsubscribes the given subscription from the EventStream

type Handler

type Handler func(interface{})

Handler defines a callback function that must be pass when subscribing.

type Predicate

type Predicate func(evt interface{}) bool

Predicate is a function used to filter messages before being forwarded to a subscriber

type Subscription

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

Subscription is returned from the Subscribe function.

This value and can be passed to Unsubscribe when the observer is no longer interested in receiving messages

func (*Subscription) Activate

func (s *Subscription) Activate() bool

Activates the Subscription setting its active flag as 1, if the subscription was already active it returns false, true otherwise

func (*Subscription) Deactivate

func (s *Subscription) Deactivate() bool

Deactivates the Subscription setting its active flag as 0, if the subscription was already inactive it returns false, true otherwise

func (*Subscription) IsActive

func (s *Subscription) IsActive() bool

Returns true if the active flag of the Subscription is set as 1 otherwise it returns false

Jump to

Keyboard shortcuts

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