-
Notifications
You must be signed in to change notification settings - Fork 1
/
onmessage.jl
57 lines (43 loc) · 1.73 KB
/
onmessage.jl
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
# SPDX-License-Identifier: MPL-2.0
abstract type OnMessage <: Delivery end
mutable struct OnMessageImpl <: OnMessage
OnMessageImpl(;options...) = new()
end
@inline localdelivery(::OnMessage, scheduler, msg, targetactor) = begin
_body = body(msg)
_service = scheduler.service
for trait in traits(typeof(targetactor))
ontraitmessage(trait isa Type ? trait() : trait, targetactor, _body, _service)
end
onmessage(targetactor, _body, _service)
return false
end
"""
traits(::Type{<:Actor}) = ()
You can declare the traits of an actor by defining a method of this function.
Traits can handle messages in the name of the actor,
helping to compose the behavior of the actor (See [`ontraitmessage()`](@ref).).
E.g.: The EventSource trait handles the Subscribe and UnSubscribe messages automatically (among others).
Anything can be a trait, but we recommend to use immutable structs.
Return a tuple of traits, either instantiated or not.
Instantiated traits can hold values, while
traits given as types will be instantiated without arguments.
Important: Traits _cannot_ hold state.
If a trait needs to store state in the actor you have to add fields to the actor manually.
# Examples
struct DumpFieldTrait # Dumps a single field of the actor to stdout when the actor is dying.
fieldname::Symbol
end
CircoCore.ontraitmessage(trait::DumpFieldTrait, me, ::OnDeath, service) = begin
println("¨\$(trait.fieldname): \$(getfield(me, trait.fieldname))")
end
mutable struct MyActor <: Actor{Any}
a
b
core
MyActor() = new(rand(Int8), rand(Int8))
end
CircoCore.traits(::Type{MyActor}) = (DumpFieldTrait(:a), DumpFieldTrait(:b))
"""
traits(::Type{<:Actor}) = ()
ontraitmessage(trait, me, msg, service) = nothing