Skip to content
This repository has been archived by the owner on May 16, 2019. It is now read-only.

Commit

Permalink
Trim the in-memory timeline to 150 events
Browse files Browse the repository at this point in the history
Part of #66
  • Loading branch information
turt2live committed Mar 8, 2018
1 parent b25a616 commit 9594c5f
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions src/app/models/matrix/dto/room.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<RoomUpdatedEvent> = new ReplaySubject<RoomUpdatedEvent>();

public backfillToken: string;
Expand Down Expand Up @@ -158,6 +164,7 @@ export class MatrixRoom {

public addTimelineEvent(event: RoomEvent): void {
this.timeline.push(event);
this.checkTimelineLength();
this.publishUpdate("timeline");
}

Expand All @@ -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");
Expand Down

0 comments on commit 9594c5f

Please sign in to comment.