2022-02-06 10:39:04 +01:00
|
|
|
package anilist
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2022-02-24 21:11:35 +01:00
|
|
|
func (c Cursor[T]) Close() {
|
|
|
|
defer c.cancelFunc()
|
|
|
|
}
|
|
|
|
|
2022-02-06 10:39:04 +01:00
|
|
|
func (c Cursor[T]) Next() (*T, bool) {
|
|
|
|
if c.ctx.Err() == nil {
|
|
|
|
value, ok := <-c.Chan
|
|
|
|
return value, ok
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|