Skip to content

Commit 3abc119

Browse files
committed
examples
1 parent 10bafae commit 3abc119

File tree

5 files changed

+76
-12
lines changed

5 files changed

+76
-12
lines changed

cover.out

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
11
mode: set
2+
github.com/WolvenSpirit/node-graph-flow/circular.go:17.54,18.23 1 1
3+
github.com/WolvenSpirit/node-graph-flow/circular.go:18.23,19.24 1 1
4+
github.com/WolvenSpirit/node-graph-flow/circular.go:22.3,22.24 1 1
5+
github.com/WolvenSpirit/node-graph-flow/circular.go:25.3,26.52 2 1
6+
github.com/WolvenSpirit/node-graph-flow/circular.go:19.24,21.4 1 1
7+
github.com/WolvenSpirit/node-graph-flow/circular.go:22.24,24.4 1 1
28
github.com/WolvenSpirit/node-graph-flow/main.go:15.38,17.2 1 1
39
github.com/WolvenSpirit/node-graph-flow/main.go:25.30,27.2 1 1
410
github.com/WolvenSpirit/node-graph-flow/main.go:29.50,30.36 1 1
@@ -25,9 +31,3 @@ github.com/WolvenSpirit/node-graph-flow/main.go:105.35,107.3 1 1
2531
github.com/WolvenSpirit/node-graph-flow/main.go:108.40,110.3 1 1
2632
github.com/WolvenSpirit/node-graph-flow/main.go:111.16,114.3 2 1
2733
github.com/WolvenSpirit/node-graph-flow/main.go:118.43,120.2 1 1
28-
github.com/WolvenSpirit/node-graph-flow/circular.go:17.54,18.23 1 1
29-
github.com/WolvenSpirit/node-graph-flow/circular.go:18.23,19.24 1 1
30-
github.com/WolvenSpirit/node-graph-flow/circular.go:22.3,22.24 1 1
31-
github.com/WolvenSpirit/node-graph-flow/circular.go:25.3,26.52 2 1
32-
github.com/WolvenSpirit/node-graph-flow/circular.go:19.24,21.4 1 1
33-
github.com/WolvenSpirit/node-graph-flow/circular.go:22.24,24.4 1 1

examples/basic/basic.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package main
2+
3+
import (
4+
"context"
5+
6+
flow "github.com/WolvenSpirit/node-graph-flow"
7+
)
8+
9+
func main() {
10+
node1 := flow.Node{Task: func(fc *flow.FlowContext, i flow.Input) (flow.Output, error) {
11+
return "node1 Output\n", nil
12+
}, Name: "node1"}
13+
node2 := flow.Node{Task: func(fc *flow.FlowContext, i flow.Input) (flow.Output, error) {
14+
return i.(string) + "node2 Output\n", nil
15+
}, Name: "node2"}
16+
node3 := flow.Node{Task: func(fc *flow.FlowContext, i flow.Input) (flow.Output, error) {
17+
return i.(string) + "node3 Output\n", nil
18+
}, Name: "node3"}
19+
flow.BindNodes(&node1, &node2)
20+
flow.BindNodes(&node2, &node3)
21+
ctx, cancel := context.WithCancel(context.Background())
22+
fctx := flow.FlowContext{Ctx: ctx, Cancel: cancel}
23+
flow.StartFlow(&fctx, &node1)
24+
println(node3.Output.(string))
25+
}

examples/circular/circular.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package main
2+
3+
import (
4+
"context"
5+
"time"
6+
7+
flow "github.com/WolvenSpirit/node-graph-flow"
8+
)
9+
10+
func main() {
11+
stopChain := make(chan int, 1)
12+
13+
node1 := flow.Node{Task: func(fc *flow.FlowContext, i flow.Input) (flow.Output, error) {
14+
println("node1 executing")
15+
return nil, nil
16+
}, Name: "node1"}
17+
node2 := flow.Node{Task: func(fc *flow.FlowContext, i flow.Input) (flow.Output, error) {
18+
println("node2 executing")
19+
return nil, nil
20+
}, Name: "node2"}
21+
node3 := flow.Node{Task: func(fc *flow.FlowContext, i flow.Input) (flow.Output, error) {
22+
println("node3 executing")
23+
return nil, nil
24+
}, Name: "node3"}
25+
26+
flow.BuildChain(&stopChain, &node1, &node2, &node3)
27+
28+
ctx, cancel := context.WithCancel(context.Background())
29+
fctx := flow.FlowContext{Ctx: ctx, Cancel: cancel}
30+
31+
go func() {
32+
println("Node chain loop start")
33+
flow.StartFlow(&fctx, &node1)
34+
println("Chain loop stop")
35+
}()
36+
37+
time.Sleep(time.Second * 3)
38+
stopChain <- 1
39+
}

main.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,16 @@ func (err AbortError) Error() string {
1818

1919
// Initialized context and cancel func need to be populated
2020
type FlowContext struct {
21-
ctx context.Context
22-
cancel context.CancelFunc
21+
Ctx context.Context
22+
Cancel context.CancelFunc
2323
}
2424

2525
func (c *FlowContext) Init() {
26-
c.ctx, c.cancel = context.WithCancel(context.Background())
26+
c.Ctx, c.Cancel = context.WithCancel(context.Background())
2727
}
2828

2929
func (c *FlowContext) IsCanceled() (bool, error) {
30-
if err := c.ctx.Err(); err != nil {
30+
if err := c.Ctx.Err(); err != nil {
3131
return true, err
3232
}
3333
return false, nil
@@ -103,7 +103,7 @@ func Flow(ctx *FlowContext, n *Node, i Input, SubNodeIndex int, LateralNodeIndex
103103
n.FlowTrail = append(n.ParentNode.FlowTrail, n.FlowTrail...)
104104
}
105105
if _, ok := err.(AbortError); ok {
106-
ctx.cancel() // Unrecoverable error or inability to further process downstream with currently available inputs or no lateral nodes left
106+
ctx.Cancel() // Unrecoverable error or inability to further process downstream with currently available inputs or no lateral nodes left
107107
}
108108
if len(n.SubNodes) != 0 && err == nil {
109109
Flow(ctx, n.SubNodes[SubNodeIndex], o, SubNodeIndex, LateralNodeIndex, err)

main_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ func TestStartFlow(t *testing.T) {
151151
args args
152152
}{
153153
{name: "Flow test",
154-
args: args{n: &n1, ctx: &FlowContext{ctx: ctx, cancel: cancel}}},
154+
args: args{n: &n1, ctx: &FlowContext{Ctx: ctx, Cancel: cancel}}},
155155
}
156156
for _, tt := range tests {
157157
t.Run(tt.name, func(t *testing.T) {

0 commit comments

Comments
 (0)