import StompX , { StompXError , StompXPage } from '../stompx' ; import { ChatKittyError } from './error' ; export class ChatKittyPaginator < I > { static async createInstance < I > ( request ) { const page = await new Promise < StompXPage > ( ( resolve , reject ) => { request . stompX . relayResource < StompXPage > ( { destination : request . relay , parameters : request . parameters , onSuccess : ( resource ) => resolve ( resource ) , onError : ( error ) => reject ( error ) , } ) ; } ) ; let items = [ ] ; if ( page . _embedded ) { items = page . _embedded [ request . contentName ] as I [ ] ; } const mapper = request . mapper ; const asyncMapper = request . asyncMapper ; if ( mapper ) { items = items . map ( ( item ) => mapper ( item ) ) ; } else if ( asyncMapper ) { const mappedItems = [ ] ; for ( const item of items ) { mappedItems . concat ( await asyncMapper ( item ) ) ; } items = mappedItems ; } return new ChatKittyPaginator < I > ( items , request . stompX , request . contentName , page . _relays . prev , page . _relays . next , request . parameters , mapper , asyncMapper ) ; } private constructor ( public items , private stompX , private contentName , private prevRelay ? , private nextRelay ? , private parameters ? , private mapper ? , private asyncMapper ? ) { } get hasPrevPage ( ) : boolean { return ! ! this . prevRelay ; } get hasNextPage ( ) : boolean { return ! ! this . nextRelay ; } async prevPage ( ) { return this . getPage ( this . prevRelay ) ; } async nextPage ( ) { return this . getPage ( this . nextRelay ) ; } private async getPage ( relay ? ) { const page = await new Promise < StompXPage > ( ( resolve , reject ) => { if ( relay ) { this . stompX . relayResource < StompXPage > ( { destination : relay , parameters : this . parameters , onSuccess : ( resource ) => resolve ( resource ) , onError : ( error ) => reject ( error ) , } ) ; } else { reject ( new PageOutOfBoundsError ( ) ) ; } } ) ; let items = [ ] ; if ( page . _embedded ) { items = page . _embedded [ this . contentName ] as I [ ] ; } const mapper = this . mapper ; const asyncMapper = this . asyncMapper ; if ( mapper ) { items = items . map ( ( item ) => mapper ( item ) ) ; } else if ( asyncMapper ) { const mappedItems = [ ] ; for ( const item of items ) { mappedItems . concat ( await asyncMapper ( item ) ) ; } items = mappedItems ; } return new ChatKittyPaginator < I > ( items , this . stompX , this . contentName , page . _relays . prev , page . _relays . next , this . parameters , this . mapper , this . asyncMapper ) ; } } export declare class CreatePaginatorRequest < I > { stompX : StompX ; relay : string ; contentName : string ; parameters ? : Record < string , unknown > ; mapper ? : ( item : I ) => I ; asyncMapper ? : ( item : I ) => Promise < I > ; onError ? : ( error : StompXError ) => void ; } export class PageOutOfBoundsError extends ChatKittyError { constructor ( ) { super ( 'PageOutOfBoundsError' , "You've requested a page that doesn't exists." ) ; } }