1
1
package sync
2
2
3
- type Selector interface {
4
- AddFuture (f Future , handler func (ctx Context , f Future )) Selector
3
+ // type Selector interface {
4
+ // AddFuture(f Future, handler func(ctx Context, f Future)) Selector
5
5
6
- AddChannelReceive (c Channel , handler func (ctx Context , c Channel )) Selector
6
+ // AddChannelReceive(c Channel, handler func(ctx Context, c Channel)) Selector
7
7
8
- AddDefault (handler func ()) Selector
8
+ // AddDefault(handler func()) Selector
9
9
10
- Select (ctx Context )
11
- }
12
-
13
- func NewSelector () Selector {
14
- return & selector {
15
- cases : make ([]selectorCase , 0 ),
16
- }
17
- }
10
+ // Select(ctx Context)
11
+ // }
18
12
19
- type selector struct {
20
- cases []selectorCase
21
-
22
- defaultFunc func ()
13
+ type SelectCase interface {
14
+ Ready () bool
15
+ Handle (ctx Context )
23
16
}
24
17
25
- func ( s * selector ) AddFuture ( f Future , handler func (ctx Context , f Future )) Selector {
26
- s . cases = append ( s . cases , & futureCase {
18
+ func Await ( f Future , handler func (Context , Future )) SelectCase {
19
+ return & futureCase {
27
20
f : f .(* futureImpl ),
28
21
fn : handler ,
29
- })
30
-
31
- return s
22
+ }
32
23
}
33
24
34
- func ( s * selector ) AddChannelReceive ( c Channel , handler func (ctx Context , c Channel )) Selector {
25
+ func ReceiveChan ( c Channel , handler func (Context , Channel )) SelectCase {
35
26
channel := c .(* channel )
36
27
37
- s . cases = append ( s . cases , & channelCase {
28
+ return & channelCase {
38
29
c : channel ,
39
30
fn : handler ,
40
- })
41
-
42
- return s
31
+ }
43
32
}
44
33
45
- func ( s * selector ) AddDefault ( handler func ()) Selector {
46
- s . defaultFunc = handler
47
-
48
- return s
34
+ func Default ( handler func (Context )) SelectCase {
35
+ return & defaultCase {
36
+ fn : handler ,
37
+ }
49
38
}
50
39
51
- func ( s * selector ) Select (ctx Context ) {
40
+ func Select (ctx Context , cases ... SelectCase ) {
52
41
cs := getCoState (ctx )
53
42
54
43
for {
55
44
// Is any case ready?
56
- for i , c := range s . cases {
45
+ for _ , c := range cases {
57
46
if c .Ready () {
58
47
c .Handle (ctx )
59
-
60
- // Remove handled case
61
- s .cases = append (s .cases [:i ], s .cases [i + 1 :]... )
62
48
return
63
49
}
64
50
}
65
51
66
- if s .defaultFunc != nil {
67
- s .defaultFunc ()
68
- return
69
- }
70
-
71
52
// else, yield and wait for result
72
53
cs .Yield ()
73
54
}
74
55
}
75
56
76
- type selectorCase interface {
77
- Ready () bool
78
- Handle (ctx Context )
79
- }
80
-
81
- var _ = selectorCase (& futureCase {})
57
+ var _ = SelectCase (& futureCase {})
82
58
83
59
type futureCase struct {
84
60
f * futureImpl
@@ -93,6 +69,8 @@ func (fc *futureCase) Handle(ctx Context) {
93
69
fc .fn (ctx , fc .f )
94
70
}
95
71
72
+ var _ = SelectCase (& channelCase {})
73
+
96
74
type channelCase struct {
97
75
c * channel
98
76
fn func (Context , Channel )
@@ -105,3 +83,17 @@ func (cc *channelCase) Ready() bool {
105
83
func (cc * channelCase ) Handle (ctx Context ) {
106
84
cc .fn (ctx , cc .c )
107
85
}
86
+
87
+ var _ = SelectCase (& defaultCase {})
88
+
89
+ type defaultCase struct {
90
+ fn func (Context )
91
+ }
92
+
93
+ func (dc * defaultCase ) Ready () bool {
94
+ return true
95
+ }
96
+
97
+ func (dc * defaultCase ) Handle (ctx Context ) {
98
+ dc .fn (ctx )
99
+ }
0 commit comments