Added signals to add and remove requirements from a lockable node

gql_cataclysm
noah metz 2023-08-15 19:17:13 -06:00
parent 98c0b7e807
commit 98f05d57f9
2 changed files with 36 additions and 2 deletions

@ -185,6 +185,37 @@ func (ext *LockableExt) HandleErrorSignal(log Logger, node *Node, source NodeID,
return msgs return msgs
} }
func (ext *LockableExt) HandleLinkSignal(log Logger, node *Node, source NodeID, signal *IDStringSignal) Messages {
id := signal.NodeID
action := signal.Str
msgs := Messages {}
if ext.State == Unlocked {
switch action {
case "add":
_, exists := ext.Requirements[id]
if exists == true {
msgs = msgs.Add(node.ID, node.Key, NewErrorSignal(signal.ID(), "already_requirement"), source)
} else {
ext.Requirements[id] = Unlocked
msgs = msgs.Add(node.ID, node.Key, NewErrorSignal(signal.ID(), "req_added"), source)
}
case "remove":
_, exists := ext.Requirements[id]
if exists == false {
msgs = msgs.Add(node.ID, node.Key, NewErrorSignal(signal.ID(), "not_requirement"), source)
} else {
delete(ext.Requirements, id)
msgs = msgs.Add(node.ID, node.Key, NewErrorSignal(signal.ID(), "req_removed"), source)
}
default:
msgs = msgs.Add(node.ID, node.Key, NewErrorSignal(signal.ID(), "unknown_action"), source)
}
} else {
msgs = msgs.Add(node.ID, node.Key, NewErrorSignal(signal.ID(), "not_unlocked"), source)
}
return msgs
}
// Handle a LockSignal and update the extensions owner/requirement states // Handle a LockSignal and update the extensions owner/requirement states
func (ext *LockableExt) HandleLockSignal(log Logger, node *Node, source NodeID, signal *StringSignal) Messages { func (ext *LockableExt) HandleLockSignal(log Logger, node *Node, source NodeID, signal *StringSignal) Messages {
state := signal.Str state := signal.Str
@ -321,6 +352,8 @@ func (ext *LockableExt) Process(ctx *Context, node *Node, source NodeID, signal
} }
case Direct: case Direct:
switch signal.Type() { switch signal.Type() {
case LinkSignalType:
messages = ext.HandleLinkSignal(ctx.Log, node, source, signal.(*IDStringSignal))
case LockSignalType: case LockSignalType:
messages = ext.HandleLockSignal(ctx.Log, node, source, signal.(*StringSignal)) messages = ext.HandleLockSignal(ctx.Log, node, source, signal.(*StringSignal))
case ErrorSignalType: case ErrorSignalType:

@ -232,9 +232,10 @@ func NewStatusSignal(status string, source NodeID) Signal {
} }
} }
func NewLinkSignal(state string) Signal { func NewLinkSignal(state string, id NodeID) Signal {
return &StringSignal{ return &IDStringSignal{
BaseSignal: NewBaseSignal(LinkSignalType, Direct), BaseSignal: NewBaseSignal(LinkSignalType, Direct),
NodeID: id,
Str: state, Str: state,
} }
} }