Skip to content

Commit

Permalink
Consistency
Browse files Browse the repository at this point in the history
  • Loading branch information
HcgRandon committed Oct 1, 2022
1 parent 225eb2d commit a8b1c60
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 26 deletions.
2 changes: 1 addition & 1 deletion lib/prompt/PaginationPrompt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ export class PaginationPrompt<T = void, U = T> extends Prompt<T> {
for (let i = start; i <= end; i++) {
const option: SelectMenuOptions = { value: i.toString(), label: (i + 1).toString(), default: i === page };
if (this.paginator.options.itemsPerPage === 1) {
const { item } = await this.paginator.getItem(i);
const [item] = await this.paginator.getItem(i);

// label
let label = item.label ?? (i + 1).toString();
Expand Down
4 changes: 2 additions & 2 deletions lib/prompt/helpers/CodeblockPaginator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ export class CodeblockPaginator<T = void> extends Paginator<CodeblockPaginatorIt
{ page: this.page + 1, total: this.pageCount }, '[Page {page}/{total}]');
}

const page = await this.getPage();
for (const { item, index } of page) {
const items = await this.getItems();
for (const { item, index } of items) {
const flare = this.options.flare ?? {};
const focused = index === this.options.focused;

Expand Down
6 changes: 3 additions & 3 deletions lib/prompt/helpers/EmbedPaginator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { BaseContext } from '../../contexts/BaseContext';
import { AgnosticMessageContent } from '../../interfaces/AgnosticMessageContent';
import { PossiblyTranslatable } from '../../interfaces/Translatable';

import { Paginator, PaginatorItem, PaginatorItems, PaginatorOptions } from './Paginator';
import { Paginator, PaginatorItem, PaginatorItems, PaginatorOptions, PaginatorPage } from './Paginator';

export interface EmbedPaginatorItem<T = void> extends Omit<PaginatorItem<T>, 'label'> {
embed: Embed;
Expand All @@ -20,8 +20,8 @@ export class EmbedPaginator<T = void> extends Paginator<EmbedPaginatorItem<T>> {
}

public async render(): Promise<AgnosticMessageContent> {
const page = await this.getPage();
const items = await this.getItems();

return { embeds: page.map(i => i.item.embed) };
return { embeds: items.map(i => i.item.embed) };
}
}
31 changes: 15 additions & 16 deletions lib/prompt/helpers/Paginator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ export interface PaginatorItem<T = void> {
/**
* Returns item at requested index
* @param index - The index
* @returns [T, number] Tupple containing the item and count of total items
* @returns [T, number] Tuple containing item, and count of total items
*/
export type PaginatorItemFunction<T> = (index: number) => Promise<{ item: T, count: number }>;
export type PaginatorItemFunction<T> = (index: number) => Promise<[T, number]>;
export type PaginatorItems<T> = Array<T> | PaginatorItemFunction<T>;

export interface PaginatorOptions {
Expand All @@ -35,6 +35,7 @@ export interface PaginatorPageItem<T> {

index: number;
}

export type PaginatorPage<T> = Array<PaginatorPageItem<T>>;

export abstract class Paginator<T = unknown> {
Expand All @@ -45,8 +46,6 @@ export abstract class Paginator<T = unknown> {
protected currentPage = 0;
public readonly options: PaginatorOptions;

protected readonly pageCache: Map<number, PaginatorPage<T>> = new Map();

public constructor(ctx: BaseContext, items: PaginatorItems<T>, options?: PaginatorOptions) {
this.ctx = ctx;
this.items = items;
Expand Down Expand Up @@ -91,37 +90,37 @@ export abstract class Paginator<T = unknown> {
return this.pageCount === 1;
}

public async getItem(index: number): Promise<{ item: T, count: number }> {
/**
* Get item at given index
* @param index Index
* @returns [T, number] Tuple of item, and total count of items
*/
public async getItem(index: number): Promise<[T, number]> {
if (Array.isArray(this.items)) {
const item = this.items[index];
return { item, count: this.items.length };
return [item, this.items.length ];
} else if (typeof this.items === 'function') {
return this.items(index);
}
}

public async getPage(num?: number, force = false): Promise<PaginatorPage<T>> {
const page = num ?? this.currentPage;
public async getItems(page?: number): Promise<Array<PaginatorPageItem<T>>> {
const num = 0 ?? this.currentPage;

// check if page is cached
if (this.pageCache.has(page) && !force) return this.pageCache.get(page);

const start = page * this.options.itemsPerPage;
const start = num * this.options.itemsPerPage;
const end = start + this.options.itemsPerPage;

const items: Array<PaginatorPageItem<T>> = [];
for (let index = start; index < end; index++) {
const { item, count } = await this.getItem(index);
const [item, count] = await this.getItem(index);
// cache count for pageCount getter
this.itemCount = count;

if (!item) break;

items.push({ item, index });
}

// cache page
this.pageCache.set(page, items);

return items;
}

Expand Down
8 changes: 4 additions & 4 deletions lib/prompt/prompts/ChoicePrompt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ export class ChoicePrompt<T> extends PaginationPrompt<T> {
// update pagination
await super.update();

const items = await this.paginator.getPage();
const items = await this.paginator.getItems();
// single option per page
if (items.length === 1) {
this.addRows([this.btnChoice]);
Expand Down Expand Up @@ -99,15 +99,15 @@ export class ChoicePrompt<T> extends PaginationPrompt<T> {

await slt.deferUpdate();

const { item } = await this.paginator.getItem(index);
const [item] = await this.paginator.getItem(index);
if (!item) return;

await this.cleanup();
this.resolve(item.value);
}

protected async handleChoiceButton(btn: ButtonContext): Promise<void> {
const items = await this.paginator.getPage();
const items = await this.paginator.getItems();
if (items.length !== 1) return;
const { item } = items[0];
if (!item) return;
Expand All @@ -122,7 +122,7 @@ export class ChoicePrompt<T> extends PaginationPrompt<T> {
response = response.toLocaleLowerCase();

// handle number select
const items = await this.paginator.getPage();
const items = await this.paginator.getItems();
for (const { item, index } of items) {
// handle text: number
const num = index + 1; // convert to 1-index
Expand Down

0 comments on commit a8b1c60

Please sign in to comment.