import { Long } from 'bson' ; import { Chatlog , UpdatableChatListStore } from '../../chat' ; const EMPTY_ITERATOR = { [ Symbol . asyncIterator ] ( ) { return this ; } , async next ( ) { return { done : true , value : null } ; } } ; export const EmptyChatListStore = { async get ( ) { return ; } , async last ( ) { return ; } , before ( ) { return EMPTY_ITERATOR as AsyncIterableIterator < Chatlog > ; } , since ( ) { return EMPTY_ITERATOR as AsyncIterableIterator < Chatlog > ; } , all ( ) { return EMPTY_ITERATOR as AsyncIterableIterator < Chatlog > ; } , async addChat ( ) { return ; } , async updateChat ( ) { return ; } , async removeChat ( ) { return false ; } } ; /**
 * Inmemory chat list with count limit
 */ export class TalkMemoryChatListStore implements UpdatableChatListStore { constructor ( public limit , private _chatList = [ ] ) { } private findIndex ( logId ) { return this . _chatList . findIndex ( ( value ) => logId . eq ( value . logId ) ) ; } private makeIterator ( slice ) { const iter = slice [ Symbol . iterator ] ( ) ; return { [ Symbol . asyncIterator ] ( ) { return this ; } , next : async ( ) => { return iter . next ( ) ; } } ; } async get ( logId ) { const i = this . findIndex ( logId ) ; if ( i < 0 ) return ; return this . _chatList [ i ] ; } async last ( ) { if ( this . _chatList . length < 0 ) return ; return this . _chatList [ this . _chatList . length - 1 ] ; } before ( logId , maxCount = this . _chatList . length ) { const start = this . findIndex ( logId ) ; const slice = start < 0 ? [ ] : this . _chatList . slice ( Math . max ( 0 , start - maxCount ) , start ) ; return this . makeIterator ( slice ) ; } since ( time ) { const start = this . _chatList . findIndex ( ( value ) => value . sendAt >= time ) ; const slice = start < 0 ? [ ] : this . _chatList . slice ( start ) ; return this . makeIterator ( slice ) ; } all ( ) { return this . makeIterator ( this . _chatList . slice ( ) ) ; } async addChat ( ... chat ) { if ( this . _chatList . length >= this . limit ) { this . _chatList = this . _chatList . slice ( this . limit - this . _chatList . length + 1 ) ; } this . _chatList . push ( ... chat ) ; this . _chatList . sort ( ( a , b ) => { return a . logId . comp ( b . logId ) ; } ) ; } async updateChat ( logId , chat ) { const i = this . findIndex ( logId ) ; if ( i < 0 || ! this . _chatList [ i ] ) return ; this . _chatList [ i ] = { ... this . _chatList [ i ] , ... chat } ; } async removeChat ( logId ) { const i = this . findIndex ( logId ) ; if ( i < 0 ) return false ; this . _chatList . splice ( i , 1 ) ; return true ; } }