asyncio: multi return types promise/generator
This commit is contained in:
114
x/io/io.go
114
x/io/io.go
@@ -20,8 +20,6 @@ import (
|
||||
"log"
|
||||
"sync"
|
||||
_ "unsafe"
|
||||
|
||||
"time"
|
||||
)
|
||||
|
||||
const (
|
||||
@@ -42,6 +40,7 @@ type asyncCall interface {
|
||||
}
|
||||
|
||||
type AsyncCall[OutT any] interface {
|
||||
Resume()
|
||||
}
|
||||
|
||||
type executor struct {
|
||||
@@ -63,9 +62,9 @@ func (e *executor) schedule(ac asyncCall) {
|
||||
e.cond.Signal()
|
||||
}
|
||||
|
||||
func Run[OutT any](ac AsyncCall[OutT]) (OutT, error) {
|
||||
func Run[OutT any](ac AsyncCall[OutT]) OutT {
|
||||
e := newExecutor()
|
||||
p := ac.(*PromiseImpl[OutT])
|
||||
p := ac.(*Promise[OutT])
|
||||
p.Exec = e
|
||||
var rootAc asyncCall = p
|
||||
e.schedule(rootAc)
|
||||
@@ -80,102 +79,121 @@ func Run[OutT any](ac AsyncCall[OutT]) (OutT, error) {
|
||||
e.acs = e.acs[1:]
|
||||
ac.Call()
|
||||
if ac.Done() && ac == rootAc {
|
||||
return p.value, p.err
|
||||
return p.value
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
type Promise[OutT any] func(OutT, error)
|
||||
|
||||
// llgo:link Promise.Await llgo.await
|
||||
func (p Promise[OutT]) Await(timeout ...time.Duration) (ret OutT, err error) {
|
||||
panic("should not called")
|
||||
type R1[T any] struct {
|
||||
V1 T
|
||||
}
|
||||
|
||||
func (p Promise[OutT]) Call() {
|
||||
panic("should not called")
|
||||
func (r R1[T]) Values() T {
|
||||
return r.V1
|
||||
}
|
||||
|
||||
func (p Promise[OutT]) Chan() <-chan OutT {
|
||||
panic("should not called")
|
||||
type R2[T1 any, T2 any] struct {
|
||||
V1 T1
|
||||
V2 T2
|
||||
}
|
||||
|
||||
func (p Promise[OutT]) Done() bool {
|
||||
panic("should not called")
|
||||
func (r R2[T1, T2]) Values() (T1, T2) {
|
||||
return r.V1, r.V2
|
||||
}
|
||||
|
||||
func (p Promise[OutT]) Err() error {
|
||||
panic("should not called")
|
||||
type R3[T1 any, T2 any, T3 any] struct {
|
||||
V1 T1
|
||||
V2 T2
|
||||
V3 T3
|
||||
}
|
||||
|
||||
func (p Promise[OutT]) Value() OutT {
|
||||
panic("should not called")
|
||||
func (r R3[T1, T2, T3]) Values() (T1, T2, T3) {
|
||||
return r.V1, r.V2, r.V3
|
||||
}
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
type R4[T1 any, T2 any, T3 any, T4 any] struct {
|
||||
V1 T1
|
||||
V2 T2
|
||||
V3 T3
|
||||
V4 T4
|
||||
}
|
||||
|
||||
type PromiseImpl[TOut any] struct {
|
||||
func (r R4[T1, T2, T3, T4]) Values() (T1, T2, T3, T4) {
|
||||
return r.V1, r.V2, r.V3, r.V4
|
||||
}
|
||||
|
||||
type Promise[TOut any] struct {
|
||||
Debug string
|
||||
Next int
|
||||
Exec *executor
|
||||
Parent asyncCall
|
||||
|
||||
Func func(resolve func(TOut, error))
|
||||
err error
|
||||
Func func()
|
||||
value TOut
|
||||
c chan TOut
|
||||
}
|
||||
|
||||
func (p *PromiseImpl[TOut]) parent() asyncCall {
|
||||
func NewPromise[TOut any](fn func()) *Promise[TOut] {
|
||||
return &Promise[TOut]{Func: fn}
|
||||
}
|
||||
|
||||
func (p *Promise[TOut]) parent() asyncCall {
|
||||
return p.Parent
|
||||
}
|
||||
|
||||
func (p *PromiseImpl[TOut]) Resume() {
|
||||
func (p *Promise[TOut]) Resume() {
|
||||
if debugAsync {
|
||||
log.Printf("Resume task: %+v\n", p)
|
||||
}
|
||||
p.Exec.schedule(p)
|
||||
}
|
||||
|
||||
func (p *PromiseImpl[TOut]) Done() bool {
|
||||
func (p *Promise[TOut]) Done() bool {
|
||||
return p.Next == -1
|
||||
}
|
||||
|
||||
func (p *PromiseImpl[TOut]) Call() {
|
||||
p.Func(func(v TOut, err error) {
|
||||
p.value = v
|
||||
p.err = err
|
||||
if debugAsync {
|
||||
log.Printf("Resolve task: %+v, %+v, %+v\n", p, v, err)
|
||||
}
|
||||
if p.Parent != nil {
|
||||
p.Parent.Resume()
|
||||
}
|
||||
})
|
||||
func (p *Promise[TOut]) Call() {
|
||||
p.Func()
|
||||
}
|
||||
|
||||
func (p *PromiseImpl[TOut]) Err() error {
|
||||
return p.err
|
||||
func (p *Promise[TOut]) Return(v TOut) {
|
||||
// TODO(lijie): panic if already resolved
|
||||
p.value = v
|
||||
if p.c != nil {
|
||||
p.c <- v
|
||||
}
|
||||
if debugAsync {
|
||||
log.Printf("Return task: %+v\n", p)
|
||||
}
|
||||
if p.Parent != nil {
|
||||
p.Parent.Resume()
|
||||
}
|
||||
}
|
||||
|
||||
func (p *PromiseImpl[TOut]) Value() TOut {
|
||||
func (p *Promise[TOut]) Yield(v TOut) {
|
||||
p.value = v
|
||||
if debugAsync {
|
||||
log.Printf("Yield task: %+v\n", p)
|
||||
}
|
||||
if p.Parent != nil {
|
||||
p.Parent.Resume()
|
||||
}
|
||||
}
|
||||
|
||||
func (p *Promise[TOut]) Value() TOut {
|
||||
return p.value
|
||||
}
|
||||
|
||||
func (p *PromiseImpl[TOut]) Chan() <-chan TOut {
|
||||
func (p *Promise[TOut]) Chan() <-chan TOut {
|
||||
if p.c == nil {
|
||||
p.c = make(chan TOut, 1)
|
||||
p.Func(func(v TOut, err error) {
|
||||
p.value = v
|
||||
p.err = err
|
||||
p.c <- v
|
||||
})
|
||||
p.Func()
|
||||
}
|
||||
return p.c
|
||||
}
|
||||
|
||||
func (p *PromiseImpl[TOut]) Await(timeout ...time.Duration) (ret TOut, err error) {
|
||||
func (p *Promise[TOut]) Await() (ret TOut) {
|
||||
panic("should not called")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user