Skip to content

Commit 46d7fc8

Browse files
committed
Split Selector into its own file
1 parent e91cb7f commit 46d7fc8

File tree

2 files changed

+37
-33
lines changed

2 files changed

+37
-33
lines changed

workflow/select.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package workflow
2+
3+
import "github.com/cschleiden/go-workflows/internal/sync"
4+
5+
type SelectCase = sync.SelectCase
6+
7+
// Select is the workflow-save equivalent of the select statement.
8+
func Select(ctx Context, cases ...SelectCase) {
9+
sync.Select(ctx, cases...)
10+
}
11+
12+
// Await calls the provided handler when the given future is ready.
13+
func Await[T any](f Future[T], handler func(Context, Future[T])) SelectCase {
14+
return sync.Await[T](f, func(ctx sync.Context, f sync.Future[T]) {
15+
handler(ctx, f)
16+
})
17+
}
18+
19+
// Receive calls the provided handler if the given channel can receive a value. The handler receives
20+
// the received value, and the ok flag indicating whether the value was received or the channel was closed.
21+
func Receive[T any](c Channel[T], handler func(ctx Context, v T, ok bool)) SelectCase {
22+
return sync.Receive[T](c, handler)
23+
}
24+
25+
// Send calls the provided handler if the given value can be sent to the channel.
26+
func Send[T any](c Channel[T], value *T, handler func(ctx Context)) SelectCase {
27+
return sync.Send[T](c, value, handler)
28+
}
29+
30+
// Default calls the provided handler if none of the other cases match.
31+
func Default(handler func(Context)) SelectCase {
32+
return sync.Default(handler)
33+
}

workflow/sync.go

Lines changed: 4 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,13 @@ import (
44
"github.com/cschleiden/go-workflows/internal/sync"
55
)
66

7-
type Context = sync.Context
7+
type (
8+
Context = sync.Context
9+
WaitGroup = sync.WaitGroup
10+
)
811

912
var Canceled = sync.Canceled
1013

11-
type WaitGroup = sync.WaitGroup
12-
1314
func NewWaitGroup() WaitGroup {
1415
return sync.NewWaitGroup()
1516
}
@@ -18,33 +19,3 @@ func NewWaitGroup() WaitGroup {
1819
func Go(ctx Context, f func(ctx Context)) {
1920
sync.Go(ctx, f)
2021
}
21-
22-
type SelectCase = sync.SelectCase
23-
24-
// Select is the workflow-save equivalent of the select statement.
25-
func Select(ctx Context, cases ...SelectCase) {
26-
sync.Select(ctx, cases...)
27-
}
28-
29-
// Await calls the provided handler when the given future is ready.
30-
func Await[T any](f Future[T], handler func(Context, Future[T])) SelectCase {
31-
return sync.Await[T](f, func(ctx sync.Context, f sync.Future[T]) {
32-
handler(ctx, f)
33-
})
34-
}
35-
36-
// Receive calls the provided handler if the given channel can receive a value. The handler receives
37-
// the received value, and the ok flag indicating whether the value was received or the channel was closed.
38-
func Receive[T any](c Channel[T], handler func(ctx Context, v T, ok bool)) SelectCase {
39-
return sync.Receive[T](c, handler)
40-
}
41-
42-
// Send calls the provided handler if the given value can be sent to the channel.
43-
func Send[T any](c Channel[T], value *T, handler func(ctx Context)) SelectCase {
44-
return sync.Send[T](c, value, handler)
45-
}
46-
47-
// Default calls the given handler if none of the other cases match.
48-
func Default(handler func(Context)) SelectCase {
49-
return sync.Default(handler)
50-
}

0 commit comments

Comments
 (0)