paginate is a generic helper function to provide a paginated iterator.
(ctx context.Context, params P, listFunc func(context.Context, P) (R, error), items func(R) []*T)
| 1161 | |
| 1162 | // paginate is a generic helper function to provide a paginated iterator. |
| 1163 | func paginate[P listParams, R listResult[T], T any](ctx context.Context, params P, listFunc func(context.Context, P) (R, error), items func(R) []*T) iter.Seq2[*T, error] { |
| 1164 | return func(yield func(*T, error) bool) { |
| 1165 | for { |
| 1166 | res, err := listFunc(ctx, params) |
| 1167 | if err != nil { |
| 1168 | yield(nil, err) |
| 1169 | return |
| 1170 | } |
| 1171 | for _, r := range items(res) { |
| 1172 | if !yield(r, nil) { |
| 1173 | return |
| 1174 | } |
| 1175 | } |
| 1176 | nextCursorVal := res.nextCursorPtr() |
| 1177 | if nextCursorVal == nil || *nextCursorVal == "" { |
| 1178 | return |
| 1179 | } |
| 1180 | *params.cursorPtr() = *nextCursorVal |
| 1181 | } |
| 1182 | } |
| 1183 | } |
searching dependent graphs…