initial commit

This commit is contained in:
2025-08-17 11:45:23 +02:00
commit 9425aaae0f
10 changed files with 707 additions and 0 deletions

37
cursor.go Normal file
View File

@ -0,0 +1,37 @@
package nuapi
import (
"context"
)
type Cursor[T any] struct {
ctx context.Context
cancelFunc context.CancelFunc
Chan <-chan *T
}
func (c *Cursor[T]) First() *T {
defer c.cancelFunc()
return <-c.Chan
}
func (c *Cursor[T]) Close() {
c.cancelFunc()
}
func (c *Cursor[T]) Next() (*T, bool) {
if c.ctx.Err() != nil {
return nil, false
}
value, ok := <-c.Chan
return value, ok
}
func (c *Cursor[T]) Slice() []T {
s := make([]T, 0)
for value, ok := c.Next(); ok; value, ok = c.Next() {
s = append(s, *value)
}
return s
}