forked from asynkron/protoactor-go
/
props.go
109 lines (90 loc) · 2.49 KB
/
props.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package actor
//Props or properties of an actor, it defines how the actor should be created
type Props struct {
actorProducer Producer
mailboxProducer MailboxProducer
supervisionStrategy SupervisorStrategy
receivePlugins []Receive
dispatcher Dispatcher
spawner Spawner
}
func (props Props) Dispatcher() Dispatcher {
if props.dispatcher == nil {
return defaultDispatcher
}
return props.dispatcher
}
func (props Props) ProduceActor() Actor {
return props.actorProducer()
}
func (props Props) Supervisor() SupervisorStrategy {
if props.supervisionStrategy == nil {
return defaultSupervisionStrategy
}
return props.supervisionStrategy
}
func (props Props) ProduceMailbox() Mailbox {
if props.mailboxProducer == nil {
return defaultMailboxProducer()
}
return props.mailboxProducer()
}
func (props Props) spawn(id string, parent *PID) *PID {
if props.spawner != nil {
return props.spawner(id, props, parent)
}
return DefaultSpawner(id, props, parent)
}
func (props Props) WithReceivers(plugin ...Receive) Props {
//pass by value, we only modify the copy
props.receivePlugins = append(props.receivePlugins, plugin...)
return props
}
func (props Props) WithMailbox(mailbox MailboxProducer) Props {
//pass by value, we only modify the copy
props.mailboxProducer = mailbox
return props
}
func (props Props) WithSupervisor(supervisor SupervisorStrategy) Props {
//pass by value, we only modify the copy
props.supervisionStrategy = supervisor
return props
}
func (props Props) WithDispatcher(dispatcher Dispatcher) Props {
//pass by value, we only modify the copy
props.dispatcher = dispatcher
return props
}
func (props Props) WithSpawn(spawn Spawner) Props {
props.spawner = spawn
return props
}
func (props Props) WithFunc(receive Receive) Props {
props.actorProducer = makeProducerFromInstance(receive)
return props
}
func (props Props) WithInstance(a Actor) Props {
props.actorProducer = makeProducerFromInstance(a)
return props
}
func (props Props) WithProducer(p Producer) Props {
props.actorProducer = p
return props
}
func FromProducer(actorProducer Producer) Props {
return Props{actorProducer: actorProducer}
}
func FromFunc(receive Receive) Props {
return FromInstance(receive)
}
func FromSpawn(spawn Spawner) Props {
return Props{spawner: spawn}
}
func FromInstance(template Actor) Props {
return Props{actorProducer: makeProducerFromInstance(template)}
}
func makeProducerFromInstance(a Actor) Producer {
return func() Actor {
return a
}
}