import { isUndefined } from 'lodash' ; import { ConversationId } from '../entities/conversation.entity' ; import { MessageEvent } from '../entities/message-event.entity' ; import { CreatedBeforeScroller } from './created-before-scroller' ; type FetchMessages = ( { conversationId , createdBefore , } : { conversationId : ConversationId ; createdBefore ? : Date ; } ) => Promise < MessageEvent [ ] > ; export class MessageScroller extends CreatedBeforeScroller < MessageEvent > { private fetchMessages : FetchMessages ; private conversationId : ConversationId ; fieldName : 'createdAt' = 'createdAt' ; constructor ( { fetchMessages , conversationId , createdBefore , } ) { super ( { createdBefore } ) ; this . fetchMessages = fetchMessages ; this . conversationId = conversationId ; } async fetch ( ) { return this . fetchMessages ( { conversationId : this . conversationId , ... ( isUndefined ( this . createdBefore ) ? { } : { createdBefore : new Date ( this . createdBefore ) } ) , } ) ; } }