1234567891011121314151617181920212223242526272829303132333435363738 |
- package funmow
-
- func EventBuffer(inbound chan PlayerEvent) chan PlayerEvent {
-
- queue := make([]*PlayerEvent, 0)
- inboundBuffer := make(chan PlayerEvent)
-
- go func() {
- running := true
- bufferLoop:
- for running {
- if len(queue) == 0 {
- select {
- case newEvent, ok := <-inbound:
- if !ok {
- break bufferLoop
- }
- queue = append(queue, &newEvent)
- }
- } else {
- event := queue[0]
- select {
- case newEvent, ok := <-inbound:
- if !ok {
- break bufferLoop
- }
- queue = append(queue, &newEvent)
- case inboundBuffer <- *event:
- queue = queue[1:]
- }
- }
- }
- close(inboundBuffer)
- }()
-
- return inboundBuffer
-
- }
|