Skip to content

Commit

Permalink
fix: Fix Paginator#setDirection to return a new instance
Browse files Browse the repository at this point in the history
  • Loading branch information
neet committed Jul 27, 2023
1 parent 5ee8d09 commit b004e95
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 8 deletions.
14 changes: 9 additions & 5 deletions src/adapters/action/paginator-http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,12 @@ import { type mastodon } from "../../mastodon";
export class PaginatorHttp<Entity, Params = undefined>
implements mastodon.Paginator<Entity, Params>
{
private direction: mastodon.Direction = "next";

constructor(
private readonly http: Http,
private nextPath?: string,
private nextParams?: Params | string,
private readonly meta?: HttpMetaParams,
private readonly direction: mastodon.Direction = "next",
) {}

async next(): Promise<IteratorResult<Entity, undefined>> {
Expand Down Expand Up @@ -76,9 +75,13 @@ export class PaginatorHttp<Entity, Params = undefined>
}

setDirection(direction: mastodon.Direction): PaginatorHttp<Entity, Params> {
const that = this.clone();
that.direction = direction;
return that;
return new PaginatorHttp(
this.http,
this.nextPath,
this.nextParams,
this.meta,
direction,
);
}

[Symbol.asyncIterator](): AsyncIterator<
Expand Down Expand Up @@ -117,6 +120,7 @@ export class PaginatorHttp<Entity, Params = undefined>
this.nextPath,
this.nextParams,
this.meta,
this.direction,
);
}
}
18 changes: 15 additions & 3 deletions src/mastodon/paginator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,29 @@ export type Direction = "next" | "prev";

// eslint-disable-next-line prettier/prettier
export interface Paginator<Entity, Params = undefined> extends PromiseLike<Entity> {
/**
* Get the current direction of the paginator.
* @returns The current direction of the paginator.
*/
getDirection(): Direction;
setDirection(direction: Direction): void;

/**
* Creates a new paginator with the given direction.
* @param direction New direction of the paginator.
* @returns A new paginator with the given direction.
*/
setDirection(direction: Direction): Paginator<Entity, Params>;

/**
* Clones the paginator.
* @returns A new paginator with the same direction and parameters.
*/
clone(): Paginator<Entity, Params>;

next(params?: Params | string): Promise<IteratorResult<Entity, undefined>>;

return(
value: undefined | PromiseLike<undefined>,
): Promise<IteratorResult<Entity, undefined>>;

throw(e?: unknown): Promise<IteratorResult<Entity, undefined>>;

values(): AsyncIterableIterator<Entity>;
Expand Down

0 comments on commit b004e95

Please sign in to comment.