Skip to content

Commit

Permalink
Merge pull request #5 from theplant/genx
Browse files Browse the repository at this point in the history
chain middlewares
  • Loading branch information
molon authored Dec 3, 2024
2 parents c0ceb83 + 646f6f2 commit 011e921
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 9 deletions.
18 changes: 18 additions & 0 deletions middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,3 +83,21 @@ func AppendCursorMiddleware[T any](cursorMiddlewares ...CursorMiddleware[T]) Pag
})
}
}

func chainCursorMiddlewares[T any](mws []CursorMiddleware[T]) CursorMiddleware[T] {
return func(next ApplyCursorsFunc[T]) ApplyCursorsFunc[T] {
for i := len(mws); i > 0; i-- {
next = mws[i-1](next)
}
return next
}
}

func chainPaginationMiddlewares[T any](mws []PaginationMiddleware[T]) PaginationMiddleware[T] {
return func(next Pagination[T]) Pagination[T] {
for i := len(mws); i > 0; i-- {
next = mws[i-1](next)
}
return next
}
}
11 changes: 2 additions & 9 deletions paginaition.go → pagination.go
Original file line number Diff line number Diff line change
Expand Up @@ -220,15 +220,8 @@ func New[T any](applyCursorsFunc ApplyCursorsFunc[T], middlewares ...PaginationM
}

var p Pagination[T] = PaginationFunc[T](func(ctx context.Context, req *PaginateRequest[T]) (*Connection[T], error) {
applyCursorsFunc := applyCursorsFunc
cursorMiddlewares := CursorMiddlewaresFromContext[T](ctx)
for _, cursorMiddleware := range cursorMiddlewares {
applyCursorsFunc = cursorMiddleware(applyCursorsFunc)
}
return paginate(ctx, req, applyCursorsFunc)
return paginate(ctx, req, chainCursorMiddlewares(cursorMiddlewares)(applyCursorsFunc))
})
for _, middleware := range middlewares {
p = middleware(p)
}
return p
return chainPaginationMiddlewares(middlewares)(p)
}

0 comments on commit 011e921

Please sign in to comment.