import { SubscriptionContent , SubscriptionContentIdentifier , SubscriptionContentJSON , SubscriptionContentSerializer } from "./SubscriptionContent" ; import { splice } from "@immutable-array/prototype" ; import { Serializer } from "ddd-base" ; import { TimeStamp } from "../TimeStamp" ; export const SubscriptionContentsSerializer = { fromJSON ( json ) { return new SubscriptionContents ( { contents : json . contents . map ( ( content ) => SubscriptionContentSerializer . fromJSON ( content ) ) , lastUpdatedTimestamp : TimeStamp . createTimeStampFromMillisecond ( json . lastUpdatedTimestampMs ) } ) ; } , toJSON ( entity ) { return { contents : entity . contents . map ( ( content ) => SubscriptionContentSerializer . toJSON ( content ) ) , lastUpdatedTimestampMs : entity . lastUpdatedTimestamp . millSecond } ; } } ; export interface SubscriptionContentsJSON { contents : SubscriptionContentJSON [ ] ; lastUpdatedTimestampMs : number ; } export interface SubscriptionContentsArgs { contents : SubscriptionContent [ ] ; lastUpdatedTimestamp : TimeStamp ; continuationKey ? : string ; } export class SubscriptionContents { contents : SubscriptionContent [ ] ; lastUpdatedTimestamp : TimeStamp ; continuationKey ? : string ; constructor ( args ) { this . contents = args . contents ; this . continuationKey = args . continuationKey ; this . lastUpdatedTimestamp = args . lastUpdatedTimestamp ; } get hasContent ( ) : boolean { return this . contents . length > 0 ; } get debounceDelayTimeMs ( ) { return 120 * 1000 ; } isNeededToUpdate ( currentTimeStampMs ) { if ( ! this . hasContent ) { return true ; } return currentTimeStampMs > this . lastUpdatedTimestamp . millSecond + this . debounceDelayTimeMs ; } updateContents ( contents ) { return new SubscriptionContents ( { ... ( this as SubscriptionContentsArgs ) , contents } ) ; } getContentList ( ) { return this . contents ; } param { TimeStamp } timeStamp param { number } minCount returns { SubscriptionContents } getContentsNewerThanTheTime ( timeStamp , minCount ) { let lastContentIndex = - 1 ; for ( let i = this . contents . length - 1 ; i >= 0 ; i -- ) { const content = this . contents [ i ] ; if ( content . updatedDate . compare ( ">=" , timeStamp ) ) { lastContentIndex = i + 1 ; break ; } } if ( lastContentIndex !== - 1 ) { const newerContents = this . contents . slice ( 0 , lastContentIndex ) ; return new SubscriptionContents ( { ... ( this as SubscriptionContentsArgs ) , contents : newerContents } ) ; } if ( minCount !== undefined ) { return new SubscriptionContents ( { ... ( this as SubscriptionContentsArgs ) , contents : this . contents . slice ( 0 , minCount ) } ) ; } return new SubscriptionContents ( { ... ( this as SubscriptionContentsArgs ) , contents : [ ] } ) ; } getContentsWithPredicate ( predicate ) { const filteredContents = this . contents . filter ( predicate ) ; return new SubscriptionContents ( { ... ( this as SubscriptionContentsArgs ) , contents : filteredContents } ) ; } findContentById ( id ) { return this . contents . find ( ( content ) => content . id . equals ( id ) ) ; } prevContentOf ( aContent ) { const index = this . contents . findIndex ( ( content ) => content . equals ( aContent ) ) ; if ( index === - 1 ) { return ; } return this . contents [ index - 1 ] ; } nextContentOf ( aContent ) { const index = this . contents . findIndex ( ( content ) => content . equals ( aContent ) ) ; if ( index === - 1 ) { return ; } return this . contents [ index + 1 ] ; } concat ( subscriptionContents ) { return new SubscriptionContents ( { ... ( this as SubscriptionContentsArgs ) , ... ( subscriptionContents as SubscriptionContentsArgs ) , contents : this . contents . concat ( subscriptionContents . contents ) } ) ; } add ( aContent ) { return this . updateContents ( this . contents . concat ( aContent ) ) ; } remove ( aContent ) { const index = this . contents . findIndex ( ( content ) => content . id . equals ( aContent . id ) ) ; if ( index === - 1 ) { return ; } return this . updateContents ( splice ( this . contents , index , 1 ) ) ; } }