forked from cwtch.im/cwtch
32 lines
828 B
Go
32 lines
828 B
Go
|
package event
|
||
|
|
||
|
// Queue is a wrapper around a channel for handling Events in a consistent way across subsystems.
|
||
|
// The expectation is that each subsystem in Cwtch will manage a given an event.Queue fed from
|
||
|
// the event.Manager.
|
||
|
type Queue struct {
|
||
|
EventChannel chan Event
|
||
|
}
|
||
|
|
||
|
// NewEventQueue initializes an event.Queue of the given buffer size.
|
||
|
func NewEventQueue(buffer int) *Queue {
|
||
|
queue := new(Queue)
|
||
|
queue.EventChannel = make(chan Event, buffer)
|
||
|
return queue
|
||
|
}
|
||
|
|
||
|
// Backlog returns the length of the queue backlog
|
||
|
func (eq *Queue) Backlog() int {
|
||
|
return len(eq.EventChannel)
|
||
|
}
|
||
|
|
||
|
// Next returns the next available event from the front of the queue
|
||
|
func (eq *Queue) Next() (event Event) {
|
||
|
event = <-eq.EventChannel
|
||
|
return
|
||
|
}
|
||
|
|
||
|
// Shutdown closes our EventChannel
|
||
|
func (eq *Queue) Shutdown() {
|
||
|
close(eq.EventChannel)
|
||
|
}
|