package broker

import (
	"context"
)

type Broker interface {
	WithContext(ctx context.Context)
	Publish(e *Event)
	Dispatch(e *Event) (err error)
	DispatchCtx(ctx context.Context, e *Event) (err error)
	Subscribe(sub Subscriber) (err error)
	UnSubscribe(sub Subscriber) (err error)
	Subscribers() []Subscriber
}

func FromContext(ctx context.Context) Broker {
	if v := ctx.Value("bus"); v != nil {
		return v.(Broker)
	} else {
		return nil
	}
}

func WitchContext(ctx context.Context, b Broker) context.Context {
	return context.WithValue(ctx, "bus", b)
}
