subscriptions

package
v0.8.0-rc3 Latest Latest
Warning

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

Go to latest
Published: Nov 15, 2022 License: MIT Imports: 3 Imported by: 47

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Broker

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

Broker defines a struct for managing subscriptions clients.

func NewBroker

func NewBroker() *Broker

NewBroker initializes and returns a new Broker instance.

func (*Broker) ClientById

func (b *Broker) ClientById(clientId string) (Client, error)

ClientById finds a registered client by its id.

Returns non-nil error when client with clientId is not registered.

func (*Broker) Clients

func (b *Broker) Clients() map[string]Client

Clients returns all registered clients.

func (*Broker) Register

func (b *Broker) Register(client Client)

Register adds a new client to the broker instance.

func (*Broker) Unregister

func (b *Broker) Unregister(clientId string)

Unregister removes a single client by its id.

If client with clientId doesn't exist, this method does nothing.

type Client

type Client interface {
	// Id Returns the unique id of the client.
	Id() string

	// Channel returns the client's communication channel.
	Channel() chan Message

	// Subscriptions returns all subscriptions to which the client has subscribed to.
	Subscriptions() map[string]struct{}

	// Subscribe subscribes the client to the provided subscriptions list.
	Subscribe(subs ...string)

	// Unsubscribe unsubscribes the client from the provided subscriptions list.
	Unsubscribe(subs ...string)

	// HasSubscription checks if the client is subscribed to `sub`.
	HasSubscription(sub string) bool

	// Set stores any value to the client's context.
	Set(key string, value any)

	// Get retrieves the key value from the client's context.
	Get(key string) any
}

Client is an interface for a generic subscription client.

type DefaultClient

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

DefaultClient defines a generic subscription client.

func NewDefaultClient

func NewDefaultClient() *DefaultClient

NewDefaultClient creates and returns a new DefaultClient instance.

func (*DefaultClient) Channel

func (c *DefaultClient) Channel() chan Message

Channel implements the [Client.Channel] interface method.

func (*DefaultClient) Get

func (c *DefaultClient) Get(key string) any

Get implements the [Client.Get] interface method.

func (*DefaultClient) HasSubscription

func (c *DefaultClient) HasSubscription(sub string) bool

HasSubscription implements the [Client.HasSubscription] interface method.

func (*DefaultClient) Id

func (c *DefaultClient) Id() string

Id implements the [Client.Id] interface method.

func (*DefaultClient) Set

func (c *DefaultClient) Set(key string, value any)

Set implements the [Client.Set] interface method.

func (*DefaultClient) Subscribe

func (c *DefaultClient) Subscribe(subs ...string)

Subscribe implements the [Client.Subscribe] interface method.

Empty subscriptions (aka. "") are ignored.

func (*DefaultClient) Subscriptions

func (c *DefaultClient) Subscriptions() map[string]struct{}

Subscriptions implements the [Client.Subscriptions] interface method.

func (*DefaultClient) Unsubscribe

func (c *DefaultClient) Unsubscribe(subs ...string)

Unsubscribe implements the [Client.Unsubscribe] interface method.

If subs is not set, this method removes all registered client's subscriptions.

type Message

type Message struct {
	Name string
	Data string
}

Message defines a client's channel data.

Jump to

Keyboard shortcuts

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