-
Notifications
You must be signed in to change notification settings - Fork 0
/
sched.go
57 lines (46 loc) · 830 Bytes
/
sched.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
//go:build !co
// Code generated by github.com/goghcrow/go-co DO NOT EDIT.
package sched
import (
ʂɘʠ "github.com/goghcrow/go-co/seq"
"sync"
)
type (
Continuation func(v any, err error)
Async func(Continuation)
Sched struct {
val any
err error
}
)
func Co(co func(s *Sched) ʂɘʠ.Iterator[Async]) {
sched := &Sched{}
sched.run(co)
}
var wg = &sync.WaitGroup{}
func DeferMain() {
wg.Wait()
}
func (s *Sched) run(co func(s *Sched) ʂɘʠ.Iterator[Async]) {
it := co(s)
var recur func()
recur = func() {
if it.MoveNext() {
it.Current()(func(v any, err error) {
s.Send(v, err)
recur()
})
return
}
wg.Done()
}
wg.Add(1)
recur()
}
func (s *Sched) Send(v any, err error) {
s.val = v
s.err = err
}
func (s *Sched) GetReceive() (any, error) {
return s.val, s.err
}