/
receiver.go
149 lines (120 loc) · 3.64 KB
/
receiver.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package core
import (
"errors"
"fmt"
"sync"
"time"
"github.com/inoxlang/inox/internal/core/symbolic"
)
var (
_ = []MessageReceiver{(*Object)(nil)}
ErrMutableMessageData = errors.New("impossible to create a Message with mutable data")
MSG_PROPNAMES = []string{"data"}
)
func init() {
RegisterSymbolicGoFunction(SendVal, func(ctx *symbolic.Context, v symbolic.Value, r symbolic.MessageReceiver, s symbolic.Value) *symbolic.Error {
return nil
})
}
type MessageReceiver interface {
SystemGraphNodeValue
ReceiveMessage(ctx *Context, msg Message) error
}
func SendVal(ctx *Context, value Value, r MessageReceiver, sender Value) error {
r.AddSystemGraphEvent(ctx, "reception of a message")
return r.ReceiveMessage(ctx, Message{data: value, sender: sender})
}
// A Message is an immutable package around an immutable piece of data sent by a sender to a MessageReceiver, Message implements Value.
type Message struct {
data Value // immutable value
sender Value
sendindgDate DateTime
}
func (m Message) Data() Value {
return m.data
}
func NewMessage(data Value, sender Value) Message {
if data.IsMutable() {
panic(ErrMutableMessageData)
}
return Message{data: data, sender: sender, sendindgDate: DateTime(time.Now())}
}
func (m Message) Prop(ctx *Context, name string) Value {
switch name {
case "data":
return m.data
}
panic(FormatErrPropertyDoesNotExist(name, m))
}
func (Message) SetProp(ctx *Context, name string, value Value) error {
return ErrCannotSetProp
}
func (Message) PropertyNames(ctx *Context) []string {
return MSG_PROPNAMES
}
type SynchronousMessageHandler struct {
pattern Pattern
handler *InoxFunction
mutationFieldsLock sync.Mutex // exclusive access for initializing .watchers & .mutationCallbacks
watchers *ValueWatchers
mutationCallbacks *MutationCallbacks
watchingDepth WatchingDepth
}
func NewSynchronousMessageHandler(ctx *Context, fn *InoxFunction, pattern Pattern) *SynchronousMessageHandler {
if ok, expl := fn.IsSharable(fn.originState); !ok {
panic(fmt.Errorf("only sharable functions are allowed: %s", expl))
}
fn.Share(ctx.GetClosestState())
return &SynchronousMessageHandler{
handler: fn,
pattern: pattern,
}
}
func (h *SynchronousMessageHandler) Pattern() Pattern {
return h.pattern
}
func (h *SynchronousMessageHandler) Prop(ctx *Context, name string) Value {
switch name {
}
panic(FormatErrPropertyDoesNotExist(name, h))
}
func (*SynchronousMessageHandler) SetProp(ctx *Context, name string, value Value) error {
return ErrCannotSetProp
}
func (*SynchronousMessageHandler) PropertyNames(ctx *Context) []string {
return nil
}
type SynchronousMessageHandlers struct {
list []*SynchronousMessageHandler
}
func NewSynchronousMessageHandlers(handlers ...*SynchronousMessageHandler) *SynchronousMessageHandlers {
return &SynchronousMessageHandlers{list: handlers}
}
func (handlers *SynchronousMessageHandlers) CallHandlers(ctx *Context, msg Message, self Value) error {
if handlers == nil {
return nil
}
for _, h := range handlers.list {
if h.Pattern().Test(ctx, msg.Data()) {
_, err := h.handler.Call(ctx.GetClosestState(), self, []Value{msg.Data()}, nil)
if err != nil {
return fmt.Errorf("one of the message handler returned an error: %w", err)
}
}
}
return nil
}
// receivers
func (obj *Object) ReceiveMessage(ctx *Context, msg Message) error {
state := ctx.GetClosestState()
obj._lock(state)
defer obj._unlock(state)
if !obj.hasAdditionalFields() {
return nil
}
if err := obj.messageHandlers.CallHandlers(ctx, msg, obj); err != nil {
return err
}
obj.watchers.InformAboutAsync(ctx, msg, ShallowWatching, false)
return nil
}