From 9594c5f616b70df771b02057318532f5dd207972 Mon Sep 17 00:00:00 2001 From: Travis Ralston Date: Wed, 7 Mar 2018 21:34:24 -0700 Subject: [PATCH] Trim the in-memory timeline to 150 events Part of #66 --- src/app/models/matrix/dto/room.ts | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/src/app/models/matrix/dto/room.ts b/src/app/models/matrix/dto/room.ts index ddde15b..4017361 100644 --- a/src/app/models/matrix/dto/room.ts +++ b/src/app/models/matrix/dto/room.ts @@ -10,12 +10,18 @@ import { RoomAvatarEvent } from "../events/room/state/m.room.avatar"; import { RoomTopicEvent } from "../events/room/state/m.room.topic"; import { IncompleteRoomEvent, RoomEvent } from "../events/room/room-event"; +const MAX_TIMELINE_MEMORY_LENGTH = 150; + export interface RoomUpdatedEvent { room: MatrixRoom; property: string; } export class MatrixRoom { + + // TODO: Replace this 'stream' with something else. + // This feels like a hack and just makes this entire class incredibly complicated. + // It's worse when considering the only thing we're listening to is "is there a new event?" to scroll down automatically. public static readonly UPDATED_STREAM: Observable = new ReplaySubject(); public backfillToken: string; @@ -158,6 +164,7 @@ export class MatrixRoom { public addTimelineEvent(event: RoomEvent): void { this.timeline.push(event); + this.checkTimelineLength(); this.publishUpdate("timeline"); } @@ -171,9 +178,19 @@ export class MatrixRoom { public addAllToTimeline(events: RoomEvent[]): void { for (const event of events) this.timeline.push(event); + this.checkTimelineLength(); this.publishUpdate("timeline"); } + private checkTimelineLength(): void { + if (this.timeline.length > MAX_TIMELINE_MEMORY_LENGTH) { + const excess = this.timeline.length - MAX_TIMELINE_MEMORY_LENGTH; + console.log(`Trimming ${excess} events from ${this.id}`); + + this.timeline.splice(0, excess); + } + } + public addPendingEvent(event: IncompleteRoomEvent): void { this.pendingEvents.push(event); this.publishUpdate("pendingEvents");