github.com/philippseith/signalr@v0.6.3/streamer.go (about)

     1  package signalr
     2  
     3  import (
     4  	"reflect"
     5  	"sync"
     6  )
     7  
     8  type streamer struct {
     9  	cancels sync.Map
    10  	conn    hubConnection
    11  }
    12  
    13  func (s *streamer) Start(invocationID string, reflectedChannel reflect.Value) {
    14  	go func() {
    15  	loop:
    16  		for {
    17  			// Waits for channel, so might hang
    18  			if chanResult, ok := reflectedChannel.Recv(); ok {
    19  				if _, ok := s.cancels.Load(invocationID); ok {
    20  					s.cancels.Delete(invocationID)
    21  					_ = s.conn.Completion(invocationID, nil, "")
    22  					break loop
    23  				}
    24  				if s.conn.Context().Err() != nil {
    25  					break loop
    26  				}
    27  				_ = s.conn.StreamItem(invocationID, chanResult.Interface())
    28  			} else {
    29  				if s.conn.Context().Err() == nil {
    30  					_ = s.conn.Completion(invocationID, nil, "")
    31  				}
    32  				break loop
    33  			}
    34  		}
    35  	}()
    36  }
    37  
    38  func (s *streamer) Stop(invocationID string) {
    39  	s.cancels.Store(invocationID, struct{}{})
    40  }