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

     1  package actor
     2  
     3  type PropsOption func(props *Props)
     4  
     5  func WithOnInit(init ...func(ctx Context)) PropsOption {
     6  	return func(props *Props) {
     7  		props.onInit = append(props.onInit, init...)
     8  	}
     9  }
    10  
    11  func WithProducer(p Producer) PropsOption {
    12  	return func(props *Props) {
    13  		props.producer = func(*ActorSystem) Actor { return p() }
    14  	}
    15  }
    16  
    17  func WithDispatcher(dispatcher Dispatcher) PropsOption {
    18  	return func(props *Props) {
    19  		props.dispatcher = dispatcher
    20  	}
    21  }
    22  
    23  func WithMailbox(mailbox MailboxProducer) PropsOption {
    24  	return func(props *Props) {
    25  		props.mailboxProducer = mailbox
    26  	}
    27  }
    28  
    29  func WithContextDecorator(contextDecorator ...ContextDecorator) PropsOption {
    30  	return func(props *Props) {
    31  		props.contextDecorator = append(props.contextDecorator, contextDecorator...)
    32  
    33  		props.contextDecoratorChain = makeContextDecoratorChain(props.contextDecorator, func(ctx Context) Context {
    34  			return ctx
    35  		})
    36  	}
    37  }
    38  
    39  func WithGuardian(guardian SupervisorStrategy) PropsOption {
    40  	return func(props *Props) {
    41  		props.guardianStrategy = guardian
    42  	}
    43  }
    44  
    45  func WithSupervisor(supervisor SupervisorStrategy) PropsOption {
    46  	return func(props *Props) {
    47  		props.supervisionStrategy = supervisor
    48  	}
    49  }
    50  
    51  func WithReceiverMiddleware(middleware ...ReceiverMiddleware) PropsOption {
    52  	return func(props *Props) {
    53  		props.receiverMiddleware = append(props.receiverMiddleware, middleware...)
    54  
    55  		// Construct the receiver middleware chain with the final receiver at the end
    56  		props.receiverMiddlewareChain = makeReceiverMiddlewareChain(props.receiverMiddleware, func(ctx ReceiverContext, envelope *MessageEnvelope) {
    57  			ctx.Receive(envelope)
    58  		})
    59  	}
    60  }
    61  
    62  func WithSenderMiddleware(middleware ...SenderMiddleware) PropsOption {
    63  	return func(props *Props) {
    64  		props.senderMiddleware = append(props.senderMiddleware, middleware...)
    65  
    66  		// Construct the sender middleware chain with the final sender at the end
    67  		props.senderMiddlewareChain = makeSenderMiddlewareChain(props.senderMiddleware, func(sender SenderContext, target *PID, envelope *MessageEnvelope) {
    68  			target.sendUserMessage(sender.ActorSystem(), envelope)
    69  		})
    70  	}
    71  }
    72  
    73  func WithSpawnFunc(spawn SpawnFunc) PropsOption {
    74  	return func(props *Props) {
    75  		props.spawner = spawn
    76  	}
    77  }
    78  
    79  func WithFunc(f ReceiveFunc) PropsOption {
    80  	return func(props *Props) {
    81  		props.producer = func(system *ActorSystem) Actor { return f }
    82  	}
    83  }
    84  
    85  func WithSpawnMiddleware(middleware ...SpawnMiddleware) PropsOption {
    86  	return func(props *Props) {
    87  		props.spawnMiddleware = append(props.spawnMiddleware, middleware...)
    88  
    89  		// Construct the spawner middleware chain with the final spawner at the end
    90  		props.spawnMiddlewareChain = makeSpawnMiddlewareChain(props.spawnMiddleware, func(actorSystem *ActorSystem, id string, props *Props, parentContext SpawnerContext) (pid *PID, e error) {
    91  			if props.spawner == nil {
    92  				return defaultSpawner(actorSystem, id, props, parentContext)
    93  			}
    94  
    95  			return props.spawner(actorSystem, id, props, parentContext)
    96  		})
    97  	}
    98  }
    99  
   100  // PropsFromProducer creates a props with the given actor producer assigned.
   101  func PropsFromProducer(producer Producer, opts ...PropsOption) *Props {
   102  	p := &Props{
   103  		producer:         func(*ActorSystem) Actor { return producer() },
   104  		contextDecorator: make([]ContextDecorator, 0),
   105  	}
   106  	p.Configure(opts...)
   107  
   108  	return p
   109  }
   110  
   111  // PropsFromProducer creates a props with the given actor producer assigned.
   112  func PropsFromProducerWithActorSystem(producer ProducerWithActorSystem, opts ...PropsOption) *Props {
   113  	p := &Props{
   114  		producer:         producer,
   115  		contextDecorator: make([]ContextDecorator, 0),
   116  	}
   117  	p.Configure(opts...)
   118  
   119  	return p
   120  }
   121  
   122  // PropsFromFunc creates a props with the given receive func assigned as the actor producer.
   123  func PropsFromFunc(f ReceiveFunc, opts ...PropsOption) *Props {
   124  	p := PropsFromProducer(func() Actor { return f }, opts...)
   125  
   126  	return p
   127  }
   128  
   129  func (props *Props) Clone(opts ...PropsOption) *Props {
   130  	cp := PropsFromProducerWithActorSystem(props.producer,
   131  		WithDispatcher(props.dispatcher),
   132  		WithMailbox(props.mailboxProducer),
   133  		WithContextDecorator(props.contextDecorator...),
   134  		WithGuardian(props.guardianStrategy),
   135  		WithSupervisor(props.supervisionStrategy),
   136  		WithReceiverMiddleware(props.receiverMiddleware...),
   137  		WithSenderMiddleware(props.senderMiddleware...),
   138  		WithSpawnFunc(props.spawner),
   139  		WithSpawnMiddleware(props.spawnMiddleware...),
   140  		WithOnInit(props.onInit...),
   141  	)
   142  
   143  	cp.Configure(opts...)
   144  
   145  	return cp
   146  }