github.com/quantosnetwork/Quantos@v0.0.0-20220306172517-e20b28c5a29a/events/events.go (about)

     1  package events
     2  
     3  import (
     4  	"github.com/google/uuid"
     5  	"github.com/minio/blake2b-simd"
     6  	"go.uber.org/zap"
     7  	"lukechampine.com/frand"
     8  )
     9  
    10  type Event struct {
    11  	EventID      string `json:"event_id"`
    12  	Name         string `json:"event_name"`
    13  	Handler      EventHandler
    14  	subscribable bool
    15  	observable   bool
    16  	once         bool
    17  	hasState     bool
    18  	subscribers  map[string]bool
    19  }
    20  
    21  func (e Event) Observe(event *Event) {
    22  	//TODO implement me
    23  	panic("implement me")
    24  }
    25  
    26  func (e Event) Dispatch() {
    27  	//TODO implement me
    28  	panic("implement me")
    29  }
    30  
    31  func (e Event) Subscribe(subscriber string) {
    32  	//TODO implement me
    33  	panic("implement me")
    34  }
    35  
    36  func (e Event) Unsubscribe(subscriber string) {
    37  	//TODO implement me
    38  	panic("implement me")
    39  }
    40  
    41  func (e Event) Log() *zap.Logger {
    42  	z := zap.L()
    43  	return z
    44  }
    45  
    46  func (e Event) Observable() bool {
    47  	return e.observable
    48  }
    49  
    50  func (e Event) Subscribable() bool {
    51  	return e.subscribable
    52  }
    53  
    54  func (e Event) Once() bool {
    55  	return e.once
    56  }
    57  
    58  func (e Event) HasState() bool {
    59  	return e.hasState
    60  }
    61  
    62  type EventHandler = func(eventName string) bool
    63  
    64  type Events struct {
    65  	List map[*Event]bool
    66  }
    67  
    68  type EventInterface interface {
    69  	Create(name string, handler EventHandler, canSubscribe, canObserve, onlyOnce, hasState bool) *Event
    70  	Observe(*Event)
    71  	Dispatch()
    72  	Subscribe(subscriber string)
    73  	Unsubscribe(subscriber string)
    74  	Log() *zap.Logger
    75  	Observable() bool
    76  	Subscribable() bool
    77  	Once() bool
    78  	HasState() bool
    79  }
    80  
    81  var EventsList Events
    82  
    83  func New(name string, handler EventHandler, canSubscribe, canObserve, onlyOnce, hasState bool) EventInterface {
    84  	var newEvent Event
    85  	e := newEvent.Create(name, handler, canSubscribe, canObserve, onlyOnce, hasState)
    86  	EventsList.List[e] = true
    87  	return e
    88  
    89  }
    90  
    91  func (e Event) Create(name string, handler EventHandler, canSubscribe, canObserve, onlyOnce, hasState bool) *Event {
    92  	event := &Event{}
    93  	buf := make([]byte, 32)
    94  	frand.Read(buf)
    95  	h := blake2b.New256()
    96  	space, _ := uuid.NewUUID()
    97  	data := []byte("events_" + name)
    98  	version := 4
    99  
   100  	id := uuid.NewHash(h, space, data, version)
   101  	event.EventID = id.String()
   102  	event.Name = name
   103  	event.Handler = handler
   104  	event.subscribable = canSubscribe
   105  	event.observable = canObserve
   106  	event.once = onlyOnce
   107  	event.hasState = hasState
   108  	return event
   109  }