Aucune description

event_buffer.go 667B

1234567891011121314151617181920212223242526272829303132333435363738
  1. package funmow
  2. func EventBuffer(inbound chan PlayerEvent) chan PlayerEvent {
  3. queue := make([]*PlayerEvent, 0)
  4. inboundBuffer := make(chan PlayerEvent)
  5. go func() {
  6. running := true
  7. bufferLoop:
  8. for running {
  9. if len(queue) == 0 {
  10. select {
  11. case newEvent, ok := <-inbound:
  12. if !ok {
  13. break bufferLoop
  14. }
  15. queue = append(queue, &newEvent)
  16. }
  17. } else {
  18. event := queue[0]
  19. select {
  20. case newEvent, ok := <-inbound:
  21. if !ok {
  22. break bufferLoop
  23. }
  24. queue = append(queue, &newEvent)
  25. case inboundBuffer <- *event:
  26. queue = queue[1:]
  27. }
  28. }
  29. }
  30. close(inboundBuffer)
  31. }()
  32. return inboundBuffer
  33. }