Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove the currentState property from room and instead use a getter. #4153

Draft
wants to merge 1 commit into
base: develop
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
97 changes: 23 additions & 74 deletions src/models/room.ts
Original file line number Diff line number Diff line change
Expand Up @@ -383,20 +383,7 @@ export class Room extends ReadReceipt<RoomEmittedEvents, RoomEventHandlerMap> {
* The room summary.
*/
public summary: RoomSummary | null = null;
/**
* oldState The state of the room at the time of the oldest event in the live timeline.
*
* @deprecated Present for backwards compatibility.
* Use getLiveTimeline().getState(EventTimeline.BACKWARDS) instead
*/
public oldState!: RoomState;
/**
* currentState The state of the room at the time of the newest event in the timeline.
*
* @deprecated Present for backwards compatibility.
* Use getLiveTimeline().getState(EventTimeline.FORWARDS) instead.
*/
public currentState!: RoomState;

public readonly relations = new RelationsContainer(this.client, this);

/**
Expand Down Expand Up @@ -484,8 +471,6 @@ export class Room extends ReadReceipt<RoomEmittedEvents, RoomEventHandlerMap> {
this.timelineSets = [new EventTimelineSet(this, opts)];
this.reEmitter.reEmit(this.getUnfilteredTimelineSet(), [RoomEvent.Timeline, RoomEvent.TimelineReset]);

this.fixUpLegacyTimelineFields();

if (this.opts.pendingEventOrdering === PendingEventOrdering.Detached) {
this.pendingEventList = [];
this.client.store.getPendingEvents(this.roomId).then((events) => {
Expand Down Expand Up @@ -802,6 +787,28 @@ export class Room extends ReadReceipt<RoomEmittedEvents, RoomEventHandlerMap> {
return this.getLiveTimeline().getEvents();
}

/**
* The state of the room at the time of the oldest event in the live timeline.
*/
public get oldState(): RoomState {
const roomState = this.getLiveTimeline().getState(EventTimeline.BACKWARDS);
if (!roomState) {
throw new Error("No oldState");
}
return roomState;
}

/**
* The state of the room at the time of the newest event in the timeline.
*/
public get currentState(): RoomState {
const roomState = this.getLiveTimeline().getState(EventTimeline.FORWARDS);
if (!roomState) {
throw new Error("No currentState");
}
return roomState;
}

/**
* Get the timestamp of the last message in the room
*
Expand Down Expand Up @@ -1217,9 +1224,6 @@ export class Room extends ReadReceipt<RoomEmittedEvents, RoomEventHandlerMap> {
// Set our new fresh timeline as the live timeline to continue syncing
// forwards and back paginating from.
timelineSet.setLiveTimeline(newTimeline!);
// Fixup `this.oldstate` so that `scrollback` has the pagination tokens
// available
this.fixUpLegacyTimelineFields();
Comment on lines -1220 to -1222
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The comment is indeed stale but I think we still need to update the re-emitter whenever the live timeline changes

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes this is definitly required. (Tests without updating the reemitter breaks a bunch of things)

} else {
logger.log(
`[refreshLiveTimeline for ${this.roomId}] \`/sync\` or some other request beat us to creating a new ` +
Expand Down Expand Up @@ -1254,61 +1258,6 @@ export class Room extends ReadReceipt<RoomEmittedEvents, RoomEventHandlerMap> {
for (const thread of this.threads.values()) {
thread.resetLiveTimeline(backPaginationToken, forwardPaginationToken);
}

this.fixUpLegacyTimelineFields();
}

/**
* Fix up this.timeline, this.oldState and this.currentState
*
* @internal
*/
private fixUpLegacyTimelineFields(): void {
const previousOldState = this.oldState;
const previousCurrentState = this.currentState;

// maintain this.oldState and this.currentState as references to the
// state at the start and end of that timeline. These are more
// for backwards-compatibility than anything else.
this.oldState = this.getLiveTimeline().getState(EventTimeline.BACKWARDS)!;
this.currentState = this.getLiveTimeline().getState(EventTimeline.FORWARDS)!;

// Let people know to register new listeners for the new state
// references. The reference won't necessarily change every time so only
// emit when we see a change.
if (previousOldState !== this.oldState) {
this.emit(RoomEvent.OldStateUpdated, this, previousOldState, this.oldState);
}

if (previousCurrentState !== this.currentState) {
this.emit(RoomEvent.CurrentStateUpdated, this, previousCurrentState, this.currentState);

// Re-emit various events on the current room state
// TODO: If currentState really only exists for backwards
// compatibility, shouldn't we be doing this some other way?
this.reEmitter.stopReEmitting(previousCurrentState, [
RoomStateEvent.Events,
RoomStateEvent.Members,
RoomStateEvent.NewMember,
RoomStateEvent.Update,
RoomStateEvent.Marker,
BeaconEvent.New,
BeaconEvent.Update,
BeaconEvent.Destroy,
BeaconEvent.LivenessChange,
]);
this.reEmitter.reEmit(this.currentState, [
RoomStateEvent.Events,
RoomStateEvent.Members,
RoomStateEvent.NewMember,
RoomStateEvent.Update,
RoomStateEvent.Marker,
BeaconEvent.New,
BeaconEvent.Update,
BeaconEvent.Destroy,
BeaconEvent.LivenessChange,
]);
}
}

private onReceipt(event: MatrixEvent): void {
Expand Down
Loading