gocoro

package module
v0.0.0-...-78539a5 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 28, 2024 License: Apache-2.0 Imports: 3 Imported by: 0

README

gocoro

A coroutine library for go that supports async await semantics.

Usage

return func(c *gocoro.Coroutine[func() (string, error), string, string]) (string, error) {

  // yield a function
  p := gocoro.Yield(c, func() (string, error) {
    return "foo", nil
  })

  // yield a coroutine
  c := gocoro.Spawn(c, func(c *gocoro.Coroutine[func() (string, error), string, string]) (string, error) {
    return "bar", nil
  })

  // await function
  foo, _ := gocoro.Await(c, p)

  // await coroutine
  bar, _ := gocoro.Await(c, c)

  return foo + bar, nil
}

See the example for complete usage details.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Add

func Add[T, TNext, TReturn any](s Scheduler[T, TNext], f CoroutineFunc[T, TNext, TReturn]) (promise.Promise[TReturn], bool)

func Await

func Await[T, TNext, TReturn, P any](c Coroutine[T, TNext, TReturn], p promise.Awaitable[P]) (P, error)

func Spawn

func Spawn[T, TNext, TReturn, R any](c Coroutine[T, TNext, TReturn], f CoroutineFunc[T, TNext, R]) promise.Awaitable[R]

func SpawnAndAwait

func SpawnAndAwait[T, TNext, TReturn, R any](c Coroutine[T, TNext, TReturn], f CoroutineFunc[T, TNext, R]) (R, error)

func Yield

func Yield[T, TNext, TReturn any](c Coroutine[T, TNext, TReturn], v T) promise.Awaitable[TNext]

func YieldAndAwait

func YieldAndAwait[T, TNext, TReturn any](c Coroutine[T, TNext, TReturn], v T) (TNext, error)

Types

type Coroutine

type Coroutine[T, TNext, TReturn any] interface {
	Time() int64
	Set(string, any)
	Get(string) any
	// contains filtered or unexported methods
}

type CoroutineFunc

type CoroutineFunc[T, TNext, TReturn any] func(Coroutine[T, TNext, TReturn]) (TReturn, error)

type Scheduler

type Scheduler[I, O any] interface {
	Add(scheduler.Coroutine[I, O]) bool

	RunUntilBlocked(int64)
	Shutdown()
	Size() int

	// lower level apis
	Tick(int64)
	Step(int64) bool
}

func New

func New[I, O any](io io.IO[I, O], size int) Scheduler[I, O]

Directories

Path Synopsis
pkg
io
q

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL