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

     1  package persistence
     2  
     3  import (
     4  	"log"
     5  	"reflect"
     6  
     7  	"github.com/asynkron/protoactor-go/actor"
     8  )
     9  
    10  func Using(provider Provider) func(next actor.ReceiverFunc) actor.ReceiverFunc {
    11  	return func(next actor.ReceiverFunc) actor.ReceiverFunc {
    12  		fn := func(ctx actor.ReceiverContext, env *actor.MessageEnvelope) {
    13  			switch env.Message.(type) {
    14  
    15  			// intercept the started event, handle it and then apply the persistence init logic
    16  			case *actor.Started:
    17  				next(ctx, env)
    18  
    19  				// check if the actor is persistent
    20  				if p, ok := ctx.Actor().(persistent); ok {
    21  					// initialize it
    22  					p.init(provider, ctx.(actor.Context))
    23  				} else {
    24  					// not an persistent actor, bail out
    25  					log.Fatalf("Actor type %v is not persistent", reflect.TypeOf(ctx.Actor()))
    26  				}
    27  			default:
    28  				next(ctx, env)
    29  			}
    30  		}
    31  		return fn
    32  	}
    33  }