github.com/MontFerret/ferret@v0.18.0/pkg/drivers/cdp/events/listener.go (about)

     1  package events
     2  
     3  import "context"
     4  
     5  type (
     6  	// Handler represents a function that is called when a particular event occurs
     7  	// Returned boolean value indicates whether the handler needs to be called again
     8  	// False value indicated that it needs to be removed and never called again
     9  	Handler func(ctx context.Context, message interface{}) bool
    10  
    11  	// ListenerID is an internal listener ID that can be used to unsubscribe from a particular event
    12  	ListenerID int
    13  
    14  	// Listener is an internal listener representation
    15  	Listener struct {
    16  		ID      ListenerID
    17  		EventID ID
    18  		Handler Handler
    19  	}
    20  )
    21  
    22  // Always returns a handler wrapper that always gets executed by an event loop
    23  func Always(fn func(ctx context.Context, message interface{})) Handler {
    24  	return func(ctx context.Context, message interface{}) bool {
    25  		fn(ctx, message)
    26  
    27  		return true
    28  	}
    29  }
    30  
    31  // Once returns a handler wrapper that gets executed only once by an event loop
    32  func Once(fn func(ctx context.Context, message interface{})) Handler {
    33  	return func(ctx context.Context, message interface{}) bool {
    34  		fn(ctx, message)
    35  
    36  		return false
    37  	}
    38  }