commands

package
v1.0.7 Latest Latest
Warning

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

Go to latest
Published: Jul 16, 2025 License: GPL-3.0 Imports: 6 Imported by: 0

README

commands

That helper to implement some kind of event system, when we have some functions, which we should call by specific event, but we are not able to directly call such functions. We run them in few gorutines, and all data pass through context.

For example:

// We subscribe on event "test", and call `Set` function, which we take by closure
GetEventManager().Subscribe("test", commands.EventHandlerFunc(func(ctx context.Context) {
    scene, ok := ctx.Value(ctxSceneName).(Scene)
    if !ok {
        return
    }
    err := m.Set(scene)
    if err != nil {
        fastlog.WLogger().Errorw("Error setting new scene", "err", err)
    }
}))

// Then somewhere in code, we can call, and ctx should have expected `ctxSceneName`
GetEventManager().Call(ctx, "test")

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type EventHandler

type EventHandler interface {
	Handle(context.Context)
}

EventHandler describe event handler

type EventHandlerFunc

type EventHandlerFunc func(ctx context.Context)

EventHandlerFunc event handler like function

func (EventHandlerFunc) Handle

func (f EventHandlerFunc) Handle(ctx context.Context)

Handle for event handler function

type EventManager

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

EventManager event manager

func GetEventManager

func GetEventManager() *EventManager

GetEventManager return default event manager

func NewEventManager

func NewEventManager(workers int) *EventManager

NewEventManager return new event manager

func (*EventManager) Call

func (e *EventManager) Call(ctx context.Context, event string)

Call call event

func (*EventManager) GetHandlers

func (e *EventManager) GetHandlers(event string) []EventHandler

GetHandlers return all handlers for event

func (*EventManager) NextID

func (e *EventManager) NextID() uint64

NextID return next id

func (*EventManager) Subscribe

func (e *EventManager) Subscribe(event string, handler EventHandler) uint64

Subscribe subscribe on event

func (*EventManager) Unsubscribe

func (e *EventManager) Unsubscribe(event string, id uint64)

Unsubscribe unsubscribe from event

Jump to

Keyboard shortcuts

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