github.com/asynkron/protoactor-go@v0.0.0-20240308120642-ef91a6abee75/plugin/plugin.go (about)

     1  package plugin
     2  
     3  import (
     4  	"github.com/asynkron/protoactor-go/actor"
     5  )
     6  
     7  type plugin interface {
     8  	OnStart(actor.ReceiverContext)
     9  	OnOtherMessage(actor.ReceiverContext, *actor.MessageEnvelope)
    10  }
    11  
    12  func Use(plugin plugin) func(next actor.ReceiverFunc) actor.ReceiverFunc {
    13  	return func(next actor.ReceiverFunc) actor.ReceiverFunc {
    14  		fn := func(context actor.ReceiverContext, env *actor.MessageEnvelope) {
    15  			switch env.Message.(type) {
    16  			case *actor.Started:
    17  				plugin.OnStart(context)
    18  			default:
    19  				plugin.OnOtherMessage(context, env)
    20  			}
    21  
    22  			next(context, env)
    23  		}
    24  
    25  		return fn
    26  	}
    27  }