This commit is contained in:
Quanzheng Long
2026-03-18 11:41:05 -07:00
parent 12ea4c24d5
commit c0bd2dbc07
10 changed files with 505 additions and 61 deletions
+77 -3
View File
@@ -98,13 +98,13 @@ type Context struct {
resumeEvent *WaitEvent
}
func (c *Context) WaitFor(cond AnyOfCondition) (WaitEvent, error) {
func (c *Context) WaitFor(cond AnyOfCondition) (WaitForResult, error) {
if c.resumeEvent != nil {
event := *c.resumeEvent
c.resumeEvent = nil
return event, nil
return waitForResultFromRaw(cond, event), nil
}
return WaitEvent{}, ErrWaitRequested{Condition: cond}
return WaitForResult{}, ErrWaitRequested{Condition: cond}
}
func (c *Context) PublishToChannel(channel string, value any) error {
@@ -390,3 +390,77 @@ func unwrapResumeInput(input any) (any, *WaitEvent) {
}
return rawArg, &event
}
func waitForResultFromRaw(cond AnyOfCondition, event WaitEvent) WaitForResult {
result := WaitForResult{
Conditions: make([]ConditionResult, len(cond.Conditions)),
}
if event.Condition == "timer" {
for i, raw := range cond.Conditions {
if kind, _ := raw["kind"].(string); kind == "timer" {
result.Conditions[i] = ConditionResult{Met: true}
break
}
}
return result
}
if event.Condition != "channel" {
return result
}
if event.Channel == "__any_of__" {
var matched []struct {
Channel string `json:"channel"`
Value any `json:"value"`
}
_ = json.Unmarshal(event.Value, &matched)
cursor := 0
for i, raw := range cond.Conditions {
kind, _ := raw["kind"].(string)
if kind != "channel" || cursor >= len(matched) {
continue
}
channelName, _ := raw["channel"].(string)
if channelName == matched[cursor].Channel {
result.Conditions[i] = ConditionResult{
Met: true,
ChannelName: channelName,
Values: toValues(matched[cursor].Value),
}
cursor++
}
}
return result
}
var value any
_ = json.Unmarshal(event.Value, &value)
for i, raw := range cond.Conditions {
kind, _ := raw["kind"].(string)
if kind != "channel" {
continue
}
channelName, _ := raw["channel"].(string)
if channelName == event.Channel {
result.Conditions[i] = ConditionResult{
Met: true,
ChannelName: channelName,
Values: toValues(value),
}
break
}
}
return result
}
func toValues(value any) []any {
if value == nil {
return []any{}
}
if vals, ok := value.([]any); ok {
return vals
}
return []any{value}
}
+10
View File
@@ -58,6 +58,16 @@ type WaitEvent struct {
Seconds float64 `json:"seconds,omitempty"`
}
type ConditionResult struct {
Met bool `json:"met"`
ChannelName string `json:"channel_name,omitempty"`
Values []any `json:"values,omitempty"`
}
type WaitForResult struct {
Conditions []ConditionResult `json:"conditions"`
}
type Send struct {
Node any
NodeInput any